-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventLocalization.lua
More file actions
285 lines (275 loc) · 10.1 KB
/
Copy pathEventLocalization.lua
File metadata and controls
285 lines (275 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
-- Receiver-side presentation only. Original text remains in every ANN packet for
-- older peers; the optional trailing field contains facts, never executable text.
local QT = _G.QuestTogether
local L = QT.Translate
local locales = {
enUS = true,
deDE = true,
frFR = true,
esES = true,
esMX = true,
ptBR = true,
ruRU = true,
itIT = true,
koKR = true,
zhCN = true,
zhTW = true,
}
local aliases = { enGB = "enUS" }
local prefixes = {
QUEST_ACCEPTED = "Quest Accepted: ",
QUEST_COMPLETED = "Quest Completed: ",
QUEST_READY_TO_TURN_IN = "Ready to Turn In: ",
QUEST_REMOVED = "Quest Removed: ",
WORLD_QUEST_ENTERED = "World Quest Entered: ",
WORLD_QUEST_LEFT = "Left World Quest: ",
WORLD_QUEST_COMPLETED = "World Quest Completed: ",
BONUS_OBJECTIVE_ENTERED = "Bonus Objective Entered: ",
BONUS_OBJECTIVE_LEFT = "Left Bonus Objective: ",
BONUS_OBJECTIVE_COMPLETED = "Bonus Objective Completed: ",
}
local progressEvents = { QUEST_PROGRESS = true, WORLD_QUEST_PROGRESS = true, BONUS_OBJECTIVE_PROGRESS = true }
local function Integer(addon, value, minimum, maximum)
local n = addon:SafeToNumber(value)
if n and n >= minimum and n <= maximum and n == math.floor(n) then
return n
end
end
function QT:GetEventLocale()
local locale = self.localizationTestLocale or self.locale or "enUS"
locale = aliases[locale] or locale
return locales[locale] and locale or "enUS"
end
function QT:GetLocalizedEventPrefix(eventType)
local prefix = prefixes[eventType]
return prefix and self.TranslateForLocale(prefix, self:GetEventLocale()) or nil
end
function QT:DecodeAnnouncementFacts(value)
if not self:CanAccessValue(value) or type(value) ~= "string" or #value > 80 then
return nil
end
local locale, kind, index, current, required = value:match("^1:([a-zA-Z]+):([qcplos]):(%d*):(%d*):(%d*)$")
if not locales[locale] then
return nil
end
local fact = { locale = locale, kind = kind }
if kind == "q" then
if index ~= "" or current ~= "" or required ~= "" then
return nil
end
elseif kind == "l" or kind == "s" then
fact.current = Integer(self, current, kind == "s" and 0 or 1, kind == "s" and 10000 or 1000)
if index ~= "" or required ~= "" or not fact.current then
return nil
end
else
fact.index = Integer(self, index, 1, 100)
fact.current = Integer(self, current, 0, kind == "p" and 100 or (kind == "o" and 1 or 1000000000))
if not fact.index or not fact.current then
return nil
end
if required ~= "" then
if kind ~= "c" then
return nil
end
fact.required = Integer(self, required, 1, 1000000000)
if not fact.required or fact.current > fact.required then
return nil
end
end
end
return fact
end
function QT:BuildAnnouncementFacts(eventType, index, objectiveType, finished, current, required)
local kind = prefixes[eventType] and "q" or nil
if eventType == "PLAYER_LEVEL_UP" or eventType == "SCAN_STATUS" then
local scan = eventType == "SCAN_STATUS"
kind, current, index, required = scan and "s" or "l", Integer(self, current, scan and 0 or 1, scan and 10000 or 1000), nil, nil
if not current then
return nil
end
elseif progressEvents[eventType] then
index = Integer(self, index, 1, 100)
if not index then
return nil
end
current = Integer(self, current, 0, 1000000000)
required = Integer(self, required, 1, 1000000000)
if self:CanAccessValue(objectiveType) and objectiveType == "progressbar" then
if not current or current > 100 then
return nil
end
kind, required = "p", nil
elseif current then
kind = "c"
if required and current > required then
required = nil
end
else
-- Text-only legacy objectives cannot safely yield numeric facts.
-- Their completion state still has a localized representation.
if not self:CanAccessValue(finished) or type(finished) ~= "boolean" then
return nil
end
kind, current, required = "o", finished and 1 or 0, nil
end
end
if not kind then
return nil
end
return table.concat({ "1", self:GetEventLocale(), kind, index or "", current or "", required or "" }, ":")
end
function QT:GetLocalizedQuestTitle(questID)
local id = Integer(self, questID, 1, 1000000000)
if not id then
return nil
end
-- These snapshots are addon-owned and already hold the client's local title.
local snapshot = self.GetQuestSnapshot and self:GetQuestSnapshot(id)
local title = snapshot and self:SafeTrimString(snapshot.title, "") or ""
if title ~= "" and not self:IsPlaceholderQuestTitle(id, title) then
return title
end
-- Enable clears saved trackers; the ready flag proves this session rebuilt
-- them. Task quests may have no normal quest-log row or native title cache.
if self:GetRuntimeFlag("questTrackerReady", false) and self.db and self.db.global then
local tracked = self:GetPlayerTracker()[id]
if tracked and tracked.titleLocale == self:GetEventLocale() then
title = self:SafeTrimString(tracked.title, "")
if title ~= "" and #title <= 512 and not self:IsPlaceholderQuestTitle(id, title) then return title end
end
end
local api = self.API or {}
if type(api.GetLocalizedQuestTitle) ~= "function" then
return nil
end
local now = self:SafeToNumber(api.GetTime and api.GetTime()) or 0
local cache = rawget(self, "localizedQuestTitles")
if not cache then
cache = { entries = {}, order = {}, requests = {} }
self.localizedQuestTitles = cache
end
local entry = cache.entries[id]
if entry and now >= entry.time and now - entry.time < (entry.title and 300 or 5) then
return entry.title
end
if self:IsWorkBlocked("quest_snapshot_refresh") then
return nil
end
local ok, rawTitle = pcall(api.GetLocalizedQuestTitle, id)
title = ok and self:SafeTrimString(rawTitle, "") or ""
if #title > 512 or self:IsPlaceholderQuestTitle(id, title) then
title = ""
end
if not entry then
if #cache.order >= 256 then
cache.entries[table.remove(cache.order, 1)] = nil
end
cache.order[#cache.order + 1] = id
entry = {}
cache.entries[id] = entry
end
entry.time, entry.title = now, title ~= "" and title or nil
if title == "" and type(api.RequestLocalizedQuestTitle) == "function" then
local session = self.partyQuestCompareSession
local inCompare = false
for _, member in ipairs(session and session.members or {}) do
if member.entries[id] then inCompare = true; break end
end
if not inCompare then session = nil end
local attempts = session and session.titleRequests and session.titleRequests[id]
-- A bounded rolling window prevents bursts at fixed-window boundaries
-- and retains per-ID throttling even if the display cache evicts an entry.
local requested = false
for i = #cache.requests, 1, -1 do
local request = cache.requests[i]
if now < request.time or now - request.time >= 30 then
table.remove(cache.requests, i)
elseif request.id == id then
requested = true
end
end
if #cache.requests < 10 and not requested and (not attempts or attempts.count < 2) then
cache.requests[#cache.requests + 1] = { id = id, time = now }
if session then
session.titleRequests = session.titleRequests or {}
session.titleRequests[id] = { count = (attempts and attempts.count or 0) + 1, time = now }
end
pcall(api.RequestLocalizedQuestTitle, id)
end
end
return entry.title
end
function QT:QUEST_DATA_LOAD_RESULT(_, questID, success)
local id = Integer(self, questID, 1, 1000000000)
local cache = rawget(self, "localizedQuestTitles")
if not id or not cache or not self:CanAccessValue(success) or success ~= true then return end
-- Ignore unrelated game loads. Retain the entry/order slot and request budget.
local entry = cache.entries[id]
if not entry or entry.title then return end
entry.time = -math.huge
local session = self.partyQuestCompareSession
for _, member in ipairs(session and session.members or {}) do
if member.entries[id] then
self:QueuePartyQuestCompareRender()
break
end
end
end
-- Presentation resolver only. Keep GetQuestTitle snapshot-only for scan/plate
-- paths; these guarded native reads belong to explicit text presentation.
function QT:GetQuestDisplayTitle(questID, fallbackTitle)
local title = self:GetLocalizedQuestTitle(questID)
if title then return title end
local fallback = self:NormalizeQuestLinkTitleText(fallbackTitle, questID)
if fallback ~= "" then return fallback end
return self:GetQuestTitle(questID)
end
function QT:GetMonitoredQuestCountText(count)
return string.format(L("Quests monitored by QuestTogether: %d"), count)
end
function QT:LocalizeAnnouncementEvent(event)
if event.eventType == "LOOKING_FOR_QUEST_PARTNERS" then
return L("Looking for questing partners") .. " :)"
end
-- Objective indexes are not stable across quest stages. Preserve the sender's
-- readable objective and counts instead of inventing a generic translation
-- or borrowing the receiver's potentially unrelated objective description.
if progressEvents[event.eventType] then
return event.text
end
local facts = self:DecodeAnnouncementFacts(event.eventFacts)
local prefix = prefixes[event.eventType]
if event.eventType == "SCAN_STATUS" and facts and facts.kind == "s" then
return self:GetMonitoredQuestCountText(facts.current)
end
if event.eventType == "PLAYER_LEVEL_UP" and facts and facts.kind == "l" then
return L("Level ") .. tostring(facts.current)
end
local id = Integer(self, event.questId, 1, 1000000000)
if not id or not prefix then
return event.text
end
if prefix and facts and facts.kind ~= "q" then
return event.text
end
if prefix then
-- Event type already identifies these labels. Optional facts can be
-- omitted by older senders or packet fitting; recognize only exact known
-- prefixes, never guess a title by splitting arbitrary translated prose.
local sourceTitle
local text = self:SafeTrimString(event.text, "")
for locale in pairs(locales) do
if not facts or facts.locale == locale then
local sourcePrefix = self.TranslateForLocale(prefix, locale)
if text:sub(1, #sourcePrefix) == sourcePrefix and #text > #sourcePrefix then
sourceTitle = text:sub(#sourcePrefix + 1)
break
end
end
end
if not facts and not sourceTitle then return event.text end
local title = self:GetLocalizedQuestTitle(id) or sourceTitle
return title and (self.TranslateForLocale(prefix, self:GetEventLocale()) .. title) or event.text
end
end