diff --git a/WeakAuras/GenericTrigger.lua b/WeakAuras/GenericTrigger.lua index c72cd96ac2..c99b2c4b34 100644 --- a/WeakAuras/GenericTrigger.lua +++ b/WeakAuras/GenericTrigger.lua @@ -639,9 +639,10 @@ local function RunTriggerFunc(allStates, data, id, triggernum, event, arg1, arg2 else ok, returnValue = xpcall(data.triggerFunc, errorHandler, allStates, event, arg1, arg2, ...); end - if (ok and returnValue) then + if (ok and (returnValue or (returnValue ~= false and allStates.__changed))) then updateTriggerState = true; end + allStates.__changed = nil for key, state in pairs(allStates) do if (type(state) ~= "table") then errorHandler(string.format(L["All States table contains a non table at key: '%s'."], key)) @@ -4267,7 +4268,7 @@ function GenericTrigger.GetOverlayInfo(data, triggernum) count = variables.additionalProgress; end else - local allStates = {}; + local allStates = setmetatable({}, Private.allstatesMetatable) Private.ActivateAuraEnvironment(data.id); RunTriggerFunc(allStates, events[data.id][triggernum], data.id, triggernum, "OPTIONS"); Private.ActivateAuraEnvironment(nil); diff --git a/WeakAuras/TSUHelpers.lua b/WeakAuras/TSUHelpers.lua new file mode 100644 index 0000000000..974f809cf6 --- /dev/null +++ b/WeakAuras/TSUHelpers.lua @@ -0,0 +1,103 @@ +if not WeakAuras.IsLibsOK() then return end +---@type string +local AddonName = ... +---@class Private +local Private = select(2, ...) + +---@alias key string | integer +---@alias states table + +---@type fun(state: state) +local function fixMissingFields(state) + if type(state) ~= "table" then return end + -- set show + if state.show == nil then + state.show = true + end +end + +---@type fun(states: states, key: key): boolean +local remove = function(states, key) + local changed = false + local state = states[key] + if state then + state.show = false + state.changed = true + states.__changed = true + changed = true + end + return changed +end + +---@type fun(states: states): boolean +local removeAll = function(states) + local changed = false + for _, state in pairs(states) do + state.show = false + state.changed = true + changed = true + end + if changed then + states.__changed = true + end + return changed +end + +local function recurseUpdate(t1, t2) + local changed = false + for k, v in pairs(t2) do + if type(v) == "table" and type(t1[k]) == "table" then + if recurseUpdate(t1[k], v) then + changed = true + end + else + if t1[k] ~= v then + t1[k] = v + changed = true + end + end + end + return changed +end + +---@type fun(states: states, key: key, newState: state): boolean +local update = function(states, key, newState) + local changed = false + local state = states[key] + if state then + fixMissingFields(newState) + changed = recurseUpdate(state, newState) + if changed then + state.changed = true + states.__changed = true + end + end + return changed +end + +---@type fun(states: states, key: key, newState: state): boolean +local create = function(states, key, newState) + states[key] = newState + states[key].changed = true + states.__changed = true + fixMissingFields(states[key]) + return true +end + +---@type fun(states: states, key: key?, newState: state): boolean +local createOrUpdate = function(states, key, newState) + key = key or "" + if states[key] then + return update(states, key, newState) + else + return create(states, key, newState) + end +end + +Private.allstatesMetatable = { + __index = { + Update = createOrUpdate, + Remove = remove, + RemoveAll = removeAll + } +} diff --git a/WeakAuras/WeakAuras.lua b/WeakAuras/WeakAuras.lua index 50914d6c3f..8e85ebb4ba 100644 --- a/WeakAuras/WeakAuras.lua +++ b/WeakAuras/WeakAuras.lua @@ -4404,7 +4404,9 @@ function WeakAuras.GetTriggerStateForTrigger(id, triggernum) if (triggernum == -1) then return Private.GetGlobalConditionState(); end - triggerState[id][triggernum] = triggerState[id][triggernum] or {} + if triggerState[id][triggernum] == nil then + triggerState[id][triggernum] = setmetatable({}, Private.allstatesMetatable) + end return triggerState[id][triggernum]; end diff --git a/WeakAuras/WeakAuras.toc b/WeakAuras/WeakAuras.toc index 0c845132bd..baa86da0bf 100644 --- a/WeakAuras/WeakAuras.toc +++ b/WeakAuras/WeakAuras.toc @@ -59,6 +59,7 @@ GenericTrigger.lua BossMods.lua # Helper Systems +TSUHelpers.lua AuraWarnings.lua AuraEnvironment.lua AuraEnvironmentWrappedSystems.lua diff --git a/WeakAuras/WeakAuras_Cata.toc b/WeakAuras/WeakAuras_Cata.toc index 3506b61504..bd7a4a216d 100644 --- a/WeakAuras/WeakAuras_Cata.toc +++ b/WeakAuras/WeakAuras_Cata.toc @@ -59,6 +59,7 @@ GenericTrigger.lua BossMods.lua # Helper Systems +TSUHelpers.lua AuraWarnings.lua AuraEnvironment.lua AuraEnvironmentWrappedSystems.lua diff --git a/WeakAuras/WeakAuras_Vanilla.toc b/WeakAuras/WeakAuras_Vanilla.toc index 650a80b761..3a51e30e5c 100644 --- a/WeakAuras/WeakAuras_Vanilla.toc +++ b/WeakAuras/WeakAuras_Vanilla.toc @@ -51,6 +51,7 @@ GenericTrigger.lua BossMods.lua # Helper Systems +TSUHelpers.lua AuraWarnings.lua AuraEnvironment.lua AuraEnvironmentWrappedSystems.lua diff --git a/WeakAurasOptions/OptionsFrames/TextEditor.lua b/WeakAurasOptions/OptionsFrames/TextEditor.lua index 782c271f9f..36d9d55b78 100644 --- a/WeakAurasOptions/OptionsFrames/TextEditor.lua +++ b/WeakAurasOptions/OptionsFrames/TextEditor.lua @@ -136,7 +136,7 @@ end]=] name = "Trigger State Updater", snippet = [=[ function(allstates, event, ...) - allstates[""] = { + return allstates:Update("", { show = true, changed = true, progressType = "static"||"timed", @@ -149,8 +149,7 @@ function(allstates, event, ...) icon = , stacks = , index = , - } - return true + }) end]=] }, }