Repository navigation
Expand file tree
/
Copy pathHotPathRuntime.lua
More file actions
493 lines (444 loc) · 16.8 KB
/
Copy pathHotPathRuntime.lua
File metadata and controls
493 lines (444 loc) · 16.8 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
local QuestTogether = _G.QuestTogether
local LibChev = QuestTogether.LibChev
QuestTogether.runtimeRestrictionTypes = QuestTogether.runtimeRestrictionTypes
or {
combat = true,
encounter = true,
challenge = true,
pvp = true,
map = true,
}
if not QuestTogether.GetDeferredWorkStateStore then
QuestTogether.deferredWorkState = QuestTogether.deferredWorkState or {
generations = {},
entries = {},
}
end
QuestTogether.runtimeWorkDelayByClass = QuestTogether.runtimeWorkDelayByClass
or {
quest_log_drain = 0,
task_area_refresh = 0.1,
quest_snapshot_refresh = 1,
nameplate_quest_refresh = 0,
nameplate_refresh = 0,
nameplate_tint_refresh = 0.05,
nameplate_tooltip_resolve = 0,
waypoint_mutation = 0.2,
}
local function SafeText(value, fallback)
if QuestTogether and QuestTogether.SafeToString then
return QuestTogether:SafeToString(value, fallback or "")
end
local ok, textValue = pcall(tostring, value)
if ok then
return textValue
end
return fallback or ""
end
function QuestTogether:IsRuntimeRestrictionTypeActive(restrictionType)
local restrictionTypes = Enum and Enum.AddOnRestrictionType
local restrictedActions = _G.C_RestrictedActions
if not (restrictionTypes and restrictedActions and restrictedActions.GetAddOnRestrictionState) then
return false
end
local restrictionEnum = nil
local normalizedType = type(restrictionType) == "string" and string.lower(restrictionType) or nil
if normalizedType == "combat" then
restrictionEnum = restrictionTypes.Combat
elseif normalizedType == "encounter" then
restrictionEnum = restrictionTypes.Encounter
elseif normalizedType == "challenge" then
restrictionEnum = restrictionTypes.ChallengeMode
elseif normalizedType == "pvp" then
restrictionEnum = restrictionTypes.PvPMatch
elseif normalizedType == "map" then
restrictionEnum = restrictionTypes.Map
elseif normalizedType == "chat" then
restrictionEnum = restrictionTypes.Chat
end
if restrictionEnum == nil then
return false
end
local ok, state = pcall(restrictedActions.GetAddOnRestrictionState, restrictionEnum)
local numericState = ok and self:SafeToNumber(state) or nil
-- Activating is dispatched before enforcement. Do not flush work into the
-- boundary while it is being raised, or assume an unreadable state is safe.
return numericState ~= 0
end
function QuestTogether:IsRuntimeRestricted()
if self.API and self.API.InCombatLockdown and self.API.InCombatLockdown() then
return true
end
for restrictionType in pairs(self.runtimeRestrictionTypes or {}) do
if self:IsRuntimeRestrictionTypeActive(restrictionType) then
return true
end
end
return false
end
function QuestTogether:IsMapTooltipSensitiveStateActive()
if not (self and self.API and type(self.API.IsWorldMapVisible) == "function") then
return false
end
local ok, isVisible = pcall(self.API.IsWorldMapVisible)
-- Unknown visibility is not permission to read map-sensitive tooltip data.
if not ok or not self:CanAccessValue(isVisible) or type(isVisible) ~= "boolean" then
return true
end
return isVisible
end
function QuestTogether:CreateMapWorkWakeFrame()
-- No foreign parent, hooks, callback registry, or protected template. This
-- frame observes visibility through the existing read-only API wrapper.
return CreateFrame("Frame")
end
function QuestTogether:StopMapWorkWakeup()
local frame = rawget(self, "mapWorkWakeFrame")
if frame then
frame:SetScript("OnUpdate", nil)
end
self.mapWorkWakeState = nil
end
function QuestTogether:EnsureMapWorkWakeup()
if not self.isEnabled then
return
end
local workState = self:GetDeferredWorkStateStore()
if rawget(self, "mapWorkWakeState") == workState then
return
end
local frame = rawget(self, "mapWorkWakeFrame")
if not frame then
frame = self:CreateMapWorkWakeFrame()
self.mapWorkWakeFrame = frame
end
self.mapWorkWakeState = workState
local elapsedSinceCheck = 0
frame:SetScript("OnUpdate", function(_, elapsed)
if rawget(self, "mapWorkWakeState") ~= workState then
return
end
if not self.isEnabled or self:GetDeferredWorkStateStore() ~= workState or not next(workState.entries) then
self:StopMapWorkWakeup()
return
end
elapsedSinceCheck = elapsedSinceCheck + (self:SafeToNumber(elapsed) or 0)
if elapsedSinceCheck < 0.2 then
return
end
elapsedSinceCheck = 0
if not self:IsMapTooltipSensitiveStateActive() then
self:StopMapWorkWakeup()
-- Every work class rechecks its own combat/encounter/frame policy.
self:FlushDeferredWork("WORLD_MAP_CLOSED")
end
end)
end
function QuestTogether:IsWorkBlocked(workClass)
if workClass == "announcement_presentation" then
-- Same local display path as immediate announcements. Native title reads,
-- nameplate changes and emotes keep their individual restriction guards.
return false
end
if workClass == "waypoint_mutation" then
return self:IsRuntimeRestricted()
end
if workClass == "foreign_frame_mutation" then
return self:IsRuntimeRestricted()
end
if
workClass == "nameplate_tooltip_resolve"
or workClass == "nameplate_quest_refresh"
or workClass == "nameplate_refresh"
or workClass == "nameplate_tint_refresh"
then
if self:IsMapTooltipSensitiveStateActive() then
return true
end
-- Ordinary open-world combat does not prohibit readable unit tooltip
-- data or our unprotected overlays. Keep other restriction contexts
-- blocked; each frame operation still checks its own protection state.
for restrictionType in pairs(self.runtimeRestrictionTypes or {}) do
if restrictionType ~= "combat" and self:IsRuntimeRestrictionTypeActive(restrictionType) then
return true
end
end
return false
end
if workClass == "quest_log_drain" or workClass == "task_area_refresh" or workClass == "quest_snapshot_refresh"
or workClass == "quest_share" then
if self:IsMapTooltipSensitiveStateActive() then
return true
end
return self:IsRuntimeRestricted()
end
return self:IsRuntimeRestricted()
end
local function WorkPolicy(owner, options)
options = options or {}
local delay = owner.API and owner.API.Delay
return {
getState = function()
if options.lifetime == "ui" then return owner:GetRuntimeUIWorkState() end
return owner:GetDeferredWorkStateStore()
end,
enabled = function()
return (not options.owner or options.owner.active ~= false)
and (options.lifetime == "cleanup" or options.lifetime == "ui" or owner.isEnabled == true)
end,
blocked = function(workClass)
local blocked = owner:IsWorkBlocked(workClass)
if blocked and owner:IsMapTooltipSensitiveStateActive() then
owner:EnsureMapWorkWakeup()
end
return blocked
end,
delay = type(delay) == "function" and delay or nil,
defaultDelay = function(workClass)
return owner.runtimeWorkDelayByClass[workClass] or 0
end,
invoke = function(workClass, key, reason, callback)
local ok, err
if owner.RunGuardedCallback then
ok, err = owner:RunGuardedCallback("work:" .. workClass, callback)
else
ok, err = LibChev.GuardCall(callback)
end
if not ok then
owner:Debugf(
"runtime",
"work_failed class=%s key=%s reason=%s error=%s",
workClass,
SafeText(key),
SafeText(reason),
SafeText(err)
)
end
end,
}
end
-- Support windows can remain open while the enabled feature runtime is gone.
-- Their maintenance owns a separate work lifetime and a wakeup only while
-- pending, because runtime restriction events are unregistered on disable.
function QuestTogether:GetRuntimeUIWorkState()
local state = rawget(self, "runtimeUIWorkState")
if not state then
state = LibChev.NewWorkState()
self.runtimeUIWorkState = state
end
return state
end
function QuestTogether:CreateRuntimeUIWorkFrame()
return CreateFrame("Frame")
end
function QuestTogether:StopRuntimeUIWorkWakeup()
local state = rawget(self, "runtimeUIWorkState")
if state then state.armed = nil end
local frame = rawget(self, "runtimeUIWorkFrame")
if frame and LibChev.CanMutateOwnedRegion(frame) then
frame:SetScript("OnUpdate", nil)
end
end
function QuestTogether:EnsureRuntimeUIWorkWakeup()
local state = rawget(self, "runtimeUIWorkState")
if not state or not next(state.entries) then
self:StopRuntimeUIWorkWakeup()
return
end
local frame = rawget(self, "runtimeUIWorkFrame")
if not frame then
frame = self:CreateRuntimeUIWorkFrame()
self.runtimeUIWorkFrame = frame
end
if not LibChev.CanMutateOwnedRegion(frame) then return end
if state.armed then return end
local elapsed = 0
frame:SetScript("OnUpdate", function(_, delta)
elapsed = elapsed + (self:SafeToNumber(delta) or 0)
if elapsed >= 0.25 then
elapsed = 0
self:FlushRuntimeUIWork("UI restrictions cleared")
end
end)
state.armed = true
end
-- QT owns timing/lifetime policy; LibChev owns keyed coalescing and guarded
-- dispatch. A zero debounce is immediate; nextFrame always uses the timer seam.
function QuestTogether:NewRuntimeWorkOwner()
return { active = true }
end
function QuestTogether:CancelRuntimeWorkOwner(owner)
if not owner then return end
owner.active = false
local state = self:GetDeferredWorkStateStore()
for _, store in ipairs({ state, rawget(self, "runtimeUIWorkState") }) do
for key, entry in pairs(store.entries) do
if entry.owner == owner then store.entries[key], store.generations[key] = nil, nil end
end
end
self:EnsureRuntimeUIWorkWakeup()
end
function QuestTogether:IsRuntimeWorkPending(workClass, key)
return self:GetDeferredWorkStateStore().entries[LibChev.WorkKey(workClass, key)] ~= nil
end
function QuestTogether:CancelRuntimeWork(workClass, key)
local state = self:GetDeferredWorkStateStore()
local workKey = LibChev.WorkKey(workClass, key)
state.entries[workKey], state.generations[workKey] = nil, nil
end
function QuestTogether:ScheduleRuntimeWork(workClass, key, callback, options)
options = options or {}
if type(callback) ~= "function" or (options.owner and options.owner.active == false) then return false end
local policy = WorkPolicy(self, options)
local state = policy.getState()
local workKey = LibChev.WorkKey(workClass, key)
local mode = options.mode or "debounce"
assert(mode == "immediate" or mode == "nextFrame" or mode == "bounded" or mode == "debounce", "invalid work mode")
local pending = state.entries[workKey]
if (mode == "bounded" or options.coalesce) and pending and pending.owner == options.owner then
pending.reason = options.reason or pending.reason
return true
end
local dispatch = function() return callback() end
local delay = options.delay
if mode == "nextFrame" then
-- LibChev deliberately treats zero as inline. Select its timer branch
-- while passing zero to the client timer (which runs on a later frame).
local timer = self.API and self.API.Delay
if type(timer) ~= "function" then return false end
delay = 0.000001
policy.delay = function(_, run) timer(0, run) end
elseif mode == "immediate" then
policy.allowImmediateWhenDisabled = options.allowImmediateWhenDisabled == true
local ran = LibChev.RunOrDeferWork(policy, workClass, key, dispatch, delay, options.reason)
local entry = state.entries[workKey]
if entry and entry.callback == dispatch then entry.owner, entry.policy = options.owner, policy end
if options.lifetime == "ui" then self:EnsureRuntimeUIWorkWakeup() end
return ran
end
local queued = LibChev.ScheduleWork(policy, workClass, key, dispatch, delay, options.reason)
local entry = state.entries[workKey]
if entry and entry.callback == dispatch then entry.owner, entry.policy = options.owner, policy end
if options.lifetime == "ui" then self:EnsureRuntimeUIWorkWakeup() end
return queued
end
function QuestTogether:ScheduleDeferredWork(workClass, key, callback, delaySeconds, reason)
return self:ScheduleRuntimeWork(workClass, key, callback, { delay = delaySeconds, reason = reason })
end
function QuestTogether:ScheduleBoundedRefreshWork(workClass, key, callback, delaySeconds, reason)
return self:ScheduleRuntimeWork(workClass, key, callback, { mode = "bounded", delay = delaySeconds, reason = reason })
end
function QuestTogether:RunOrDeferWork(workClass, key, callback, delaySeconds, reason)
return self:ScheduleRuntimeWork(workClass, key, callback, {
mode = "immediate", delay = delaySeconds, reason = reason,
allowImmediateWhenDisabled = workClass == "waypoint_mutation",
})
end
local function FlushWorkStore(self, getState, reason)
local state = getState(self)
local pending = {}
for key, entry in pairs(state.entries) do pending[#pending + 1] = { key, entry } end
for _, pair in ipairs(pending) do
if getState(self) ~= state then break end
local key, entry = pair[1], pair[2]
local policy = entry.policy or WorkPolicy(self)
if state.entries[key] == entry and entry.delayElapsed and policy.enabled() and not policy.blocked(entry.workClass) then
state.entries[key], state.generations[key] = nil, nil
policy.invoke(entry.workClass, entry.key, reason or entry.reason, entry.callback)
end
end
end
function QuestTogether:FlushRuntimeUIWork(reason)
if rawget(self, "runtimeUIWorkState") then
FlushWorkStore(self, self.GetRuntimeUIWorkState, reason)
self:EnsureRuntimeUIWorkWakeup()
end
end
function QuestTogether:FlushDeferredWork(reason)
FlushWorkStore(self, self.GetDeferredWorkStateStore, reason)
self:FlushRuntimeUIWork(reason)
return self.isEnabled == true
end
function QuestTogether:ADDON_RESTRICTION_STATE_CHANGED()
self:ScheduleAnnouncementChannelOrder()
self:FlushDeferredWork("ADDON_RESTRICTION_STATE_CHANGED")
end
function QuestTogether:ScheduleQuestLogTaskDrain(reason)
return self:ScheduleDeferredWork("quest_log_drain", "quest_log_drain", function()
if self.DrainQueuedQuestLogTasks then
self:DrainQueuedQuestLogTasks()
end
end, 0, reason or "quest_log_drain")
end
function QuestTogether:ScheduleTaskAreaRefreshWork(shouldAnnounce, delaySeconds, reason)
if shouldAnnounce then
self:SetRuntimeFlag("pendingScheduledTaskAreaRefreshShouldAnnounce", true)
end
return self:ScheduleDeferredWork("task_area_refresh", "task_area_refresh", function()
local announce = self:GetRuntimeFlag("pendingScheduledTaskAreaRefreshShouldAnnounce", false) and true or false
self:SetRuntimeFlag("pendingScheduledTaskAreaRefreshShouldAnnounce", false)
if self.RefreshTaskAreaStates then
self:RefreshTaskAreaStates(announce)
end
end, delaySeconds, reason or "task_area_refresh")
end
function QuestTogether:ScheduleQuestStateRefreshWork(reason, delaySeconds)
return self:ScheduleBoundedRefreshWork("quest_snapshot_refresh", "quest_snapshot_refresh", function()
self:SetRuntimeFlag("pendingDeferredNameplateQuestStateRefresh", false)
if self.RebuildQuestSnapshotStore then
local snapshot = self:RebuildQuestSnapshotStore()
if self.ReconcileQuestTracking then self:ReconcileQuestTracking(snapshot) end
end
if self.RefreshNameplatesForQuestStateChange then
self:RefreshNameplatesForQuestStateChange(reason)
end
end, delaySeconds, reason or "quest_snapshot_refresh")
end
function QuestTogether:ScheduleNameplatePresentationRefresh(reason, delaySeconds)
return self:ScheduleDeferredWork("nameplate_refresh", "visible_nameplates", function()
if self.RefreshVisibleNameplates then
self:RefreshVisibleNameplates(reason)
end
end, delaySeconds, reason or "nameplate_refresh")
end
function QuestTogether:GetNameplateWorkOwner(unitToken)
local state = self:GetNameplateStateStore()
state.workOwnersByUnitToken = state.workOwnersByUnitToken or {}
local owner = state.workOwnersByUnitToken[unitToken]
if not owner or owner.active == false then
owner = self:NewRuntimeWorkOwner()
state.workOwnersByUnitToken[unitToken] = owner
end
return owner
end
function QuestTogether:RetireNameplateWork(unitToken)
local state = self:GetNameplateStateStore()
local owners = state.workOwnersByUnitToken
if owners then self:CancelRuntimeWorkOwner(owners[unitToken]); owners[unitToken] = nil end
state.identityGenerationByUnitToken[unitToken] = (state.identityGenerationByUnitToken[unitToken] or 0) + 1
end
function QuestTogether:ScheduleNameplateTooltipResolution(unitToken, unitGuid, delaySeconds, reason)
if not self:IsNameplateUnitToken(unitToken) then
return false
end
local resolvedUnitToken = unitToken
if not self:CanAccessValue(unitGuid) or type(unitGuid) ~= "string" or unitGuid == "" then
unitGuid = nil
end
local generations = self:GetNameplateStateStore().identityGenerationByUnitToken
local generation = generations[unitToken]
local workKey = resolvedUnitToken
if unitGuid then
workKey = unitGuid
end
return self:ScheduleRuntimeWork("nameplate_tooltip_resolve", workKey, function()
if self:GetNameplateStateStore().identityGenerationByUnitToken ~= generations or generations[unitToken] ~= generation then
return
end
if not self.ResolveNameplateQuestStateForUnitToken then
return
end
self:ResolveNameplateQuestStateForUnitToken(resolvedUnitToken, unitGuid, reason)
end, { mode = (not delaySeconds or delaySeconds == 0) and "nextFrame" or "debounce",
delay = delaySeconds, owner = self:GetNameplateWorkOwner(unitToken), reason = reason or "nameplate_tooltip_resolve" })
end