Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allstates helper methods #5195

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions WeakAuras/GenericTrigger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is perhaps a tiny bit weird that code which doesn't use the utility functions can set __changed = true but on balance i think it's fine

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))
Expand Down Expand Up @@ -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);
Expand Down
103 changes: 103 additions & 0 deletions WeakAuras/TSUHelpers.lua
Original file line number Diff line number Diff line change
@@ -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<key, state>

---@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
}
}
4 changes: 3 additions & 1 deletion WeakAuras/WeakAuras.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions WeakAuras/WeakAuras.toc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ GenericTrigger.lua
BossMods.lua

# Helper Systems
TSUHelpers.lua
AuraWarnings.lua
AuraEnvironment.lua
AuraEnvironmentWrappedSystems.lua
Expand Down
1 change: 1 addition & 0 deletions WeakAuras/WeakAuras_Cata.toc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ GenericTrigger.lua
BossMods.lua

# Helper Systems
TSUHelpers.lua
AuraWarnings.lua
AuraEnvironment.lua
AuraEnvironmentWrappedSystems.lua
Expand Down
1 change: 1 addition & 0 deletions WeakAuras/WeakAuras_Vanilla.toc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ GenericTrigger.lua
BossMods.lua

# Helper Systems
TSUHelpers.lua
AuraWarnings.lua
AuraEnvironment.lua
AuraEnvironmentWrappedSystems.lua
Expand Down
5 changes: 2 additions & 3 deletions WeakAurasOptions/OptionsFrames/TextEditor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ end]=]
name = "Trigger State Updater",
snippet = [=[
function(allstates, event, ...)
allstates[""] = {
return allstates:Update("", {
show = true,
changed = true,
progressType = "static"||"timed",
Expand All @@ -149,8 +149,7 @@ function(allstates, event, ...)
icon = ,
stacks = ,
index = ,
}
return true
})
end]=]
},
}
Expand Down