This commit is contained in:
Bunny67
2020-11-15 23:43:10 +03:00
parent ca4a2660ec
commit 7cbc40c959
70 changed files with 7175 additions and 3055 deletions
+111 -95
View File
@@ -4,16 +4,35 @@ local AddonName, Private = ...
local L = WeakAuras.L
local timer = WeakAuras.timer
-- Dynamic Condition functions to run. keyed on event and id
-- Dynamic Condition functions to run. keyed on event and uid
local dynamicConditions = {};
-- Global Dynamic Condition Funcs, keyed on the event
local globalDynamicConditionFuncs = {};
-- Check Conditions Functions, keyed on id
-- Check Conditions Functions, keyed on uid
local checkConditions = {};
local clones = WeakAuras.clones;
local conditionChecksTimers = {};
conditionChecksTimers.recheckTime = {};
conditionChecksTimers.recheckHandle = {};
local function OnDelete(event, uid)
checkConditions[uid] = nil
conditionChecksTimers.recheckTime[uid] = nil
if (conditionChecksTimers.recheckHandle[uid]) then
for cloneId, v in pairs(conditionChecksTimers.recheckHandle[uid]) do
timer:CancelTimer(v)
end
end
conditionChecksTimers.recheckHandle[uid] = nil
for event, funcs in pairs(dynamicConditions) do
funcs[uid] = nil
end
end
Private:RegisterCallback("Delete", OnDelete)
local function formatValueForAssignment(vType, value, pathToCustomFunction, pathToFormatters)
if (value == nil) then
@@ -24,7 +43,19 @@ local function formatValueForAssignment(vType, value, pathToCustomFunction, path
elseif(vType == "number") then
return value and tostring(value) or "0";
elseif (vType == "list") then
return type(value) == "string" and string.format("%q", value) or "nil";
if type(value) == "string" then
return string.format("%q", value)
elseif type(value) == "number" then
return tostring(value)
end
return "nil"
elseif (vType == "icon") then
if type(value) == "string" then
return string.format("%q", value)
elseif type(value) == "number" then
return tostring(value)
end
return "nil"
elseif(vType == "color") then
if (value and type(value) == "table") then
return string.format("{%s, %s, %s, %s}", tostring(value[1]), tostring(value[2]), tostring(value[3]), tostring(value[4]));
@@ -78,7 +109,7 @@ local function formatValueForAssignment(vType, value, pathToCustomFunction, path
end
local function formatValueForCall(type, property)
if (type == "bool" or type == "number" or type == "list") then
if (type == "bool" or type == "number" or type == "list" or type == "icon") then
return "propertyChanges['" .. property .. "']";
elseif (type == "color") then
local pcp = "propertyChanges['" .. property .. "']";
@@ -87,33 +118,26 @@ local function formatValueForCall(type, property)
return "nil";
end
local conditionChecksTimers = {};
conditionChecksTimers.recheckTime = {};
conditionChecksTimers.recheckHandle = {};
function WeakAuras.scheduleConditionCheck(time, id, cloneId)
conditionChecksTimers.recheckTime[id] = conditionChecksTimers.recheckTime[id] or {}
conditionChecksTimers.recheckHandle[id] = conditionChecksTimers.recheckHandle[id] or {};
if (conditionChecksTimers.recheckTime[id][cloneId] and conditionChecksTimers.recheckTime[id][cloneId] > time) then
timer:CancelTimer(conditionChecksTimers.recheckHandle[id][cloneId]);
conditionChecksTimers.recheckHandle[id][cloneId] = nil;
function WeakAuras.scheduleConditionCheck(time, uid, cloneId)
conditionChecksTimers.recheckTime[uid] = conditionChecksTimers.recheckTime[uid] or {}
conditionChecksTimers.recheckHandle[uid] = conditionChecksTimers.recheckHandle[uid] or {};
if (conditionChecksTimers.recheckTime[uid][cloneId] and conditionChecksTimers.recheckTime[uid][cloneId] > time) then
timer:CancelTimer(conditionChecksTimers.recheckHandle[uid][cloneId]);
conditionChecksTimers.recheckHandle[uid][cloneId] = nil;
end
if (conditionChecksTimers.recheckHandle[id][cloneId] == nil) then
conditionChecksTimers.recheckHandle[id][cloneId] = timer:ScheduleTimer(function()
conditionChecksTimers.recheckHandle[id][cloneId] = nil;
local region;
if(cloneId and cloneId ~= "") then
region = clones[id] and clones[id][cloneId];
else
region = WeakAuras.regions[id].region;
end
if (conditionChecksTimers.recheckHandle[uid][cloneId] == nil) then
conditionChecksTimers.recheckHandle[uid][cloneId] = timer:ScheduleTimer(function()
conditionChecksTimers.recheckHandle[uid][cloneId] = nil;
local region = Private.GetRegionByUID(uid, cloneId)
if (region and region.toShow) then
checkConditions[id](region);
checkConditions[uid](region);
end
end, time - GetTime())
conditionChecksTimers.recheckTime[id][cloneId] = time;
conditionChecksTimers.recheckTime[uid][cloneId] = time;
end
end
@@ -174,7 +198,7 @@ local function CreateTestForCondition(uid, input, allConditionsTemplate, usedSta
if preamble then
WeakAuras.conditionHelpers[uid] = WeakAuras.conditionHelpers[uid] or {}
WeakAuras.conditionHelpers[uid].preambles = WeakAuras.conditionHelpers[uid].preambles or {}
tinsert(WeakAuras.conditionHelpers[uid].preambles, preamble(value));
tinsert(WeakAuras.conditionHelpers[uid].preambles, preamble(value) or "");
local preambleNumber = #WeakAuras.conditionHelpers[uid].preambles
preambleString = string.format("WeakAuras.conditionHelpers[%q].preambles[%s]", uid, preambleNumber)
end
@@ -216,6 +240,12 @@ local function CreateTestForCondition(uid, input, allConditionsTemplate, usedSta
else
check = stateCheck .. stateVariableCheck .. "state[" .. trigger .. "]" .. string.format("[%q]", variable) .. "- now" .. op .. value;
end
elseif (cType == "elapsedTimer" and value and op) then
if (op == "==") then
check = stateCheck .. stateVariableCheck .. "abs(state[" .. trigger .. "]" .. string.format("[%q]", variable) .. "- now +" .. value .. ") < 0.05";
else
check = stateCheck .. stateVariableCheck .. "now - state[" .. trigger .. "]" .. string.format("[%q]", variable) .. op .. value;
end
elseif (cType == "select" and value and op) then
if (tonumber(value)) then
check = stateCheck .. stateVariableCheck .. "state[" .. trigger .. "]" .. string.format("[%q]", variable) .. op .. tonumber(value);
@@ -234,12 +264,18 @@ local function CreateTestForCondition(uid, input, allConditionsTemplate, usedSta
check = stateCheck .. stateVariableCheck .. "state[" .. trigger .. "]" .. string.format("[%q]", variable) .. ":match([[" .. value .. "]], 1, true)";
end
end
-- If adding a new condition type, don't forget to adjust the validator in the options code
if (cType == "timer" and value) then
recheckCode = " nextTime = state[" .. trigger .. "] and state[" .. trigger .. "]" .. string.format("[%q]", variable) .. " and (state[" .. trigger .. "]" .. string.format("[%q]", variable) .. " -" .. value .. ")\n";
recheckCode = recheckCode .. " if (nextTime and (not recheckTime or nextTime < recheckTime) and nextTime >= now) then\n"
recheckCode = recheckCode .. " recheckTime = nextTime\n";
recheckCode = recheckCode .. " end\n"
elseif (cType == "elapsedTimer" and value) then
recheckCode = " nextTime = state[" .. trigger .. "] and state[" .. trigger .. "]" .. string.format("[%q]", variable) .. " and (state[" .. trigger .. "]" .. string.format("[%q]", variable) .. " +" .. value .. ")\n";
recheckCode = recheckCode .. " if (nextTime and (not recheckTime or nextTime < recheckTime) and nextTime >= now) then\n"
recheckCode = recheckCode .. " recheckTime = nextTime\n";
recheckCode = recheckCode .. " end\n"
end
end
@@ -262,13 +298,10 @@ local function CreateCheckCondition(uid, ret, condition, conditionNumber, allCon
ret = ret .. " end\n";
end
if (recheckCode) then
ret = ret .. recheckCode;
end
if (check or recheckCode) then
if (check) then
ret = ret .. "\n";
end
return ret;
return ret, recheckCode;
end
local function ParseProperty(property)
@@ -509,6 +542,7 @@ local function ConstructConditionFunction(data)
ret = ret .. "local newActiveConditions = {};\n"
ret = ret .. "local propertyChanges = {};\n"
ret = ret .. "local nextTime;\n"
ret = ret .. string.format("local uid = %q\n", data.uid)
ret = ret .. "return function(region, hideRegion)\n";
if (debug) then ret = ret .. " print('check conditions for:', region.id, region.cloneId)\n"; end
ret = ret .. " local id = region.id\n";
@@ -522,17 +556,23 @@ local function ConstructConditionFunction(data)
local normalConditionCount = data.conditions and #data.conditions;
-- First Loop gather which conditions are active
ret = ret .. " if (not hideRegion) then\n"
local recheckCode = ""
if (data.conditions) then
WeakAuras.conditionHelpers[data.uid] = nil
for conditionNumber, condition in ipairs(data.conditions) do
local nextIsLinked = data.conditions[conditionNumber + 1] and data.conditions[conditionNumber + 1].linked
ret = CreateCheckCondition(data.uid, ret, condition, conditionNumber, allConditionsTemplate, nextIsLinked, debug)
local additionalRecheckCode
ret, additionalRecheckCode = CreateCheckCondition(data.uid, ret, condition, conditionNumber, allConditionsTemplate, nextIsLinked, debug)
if additionalRecheckCode then
recheckCode = recheckCode .. "\n" .. additionalRecheckCode
end
end
end
ret = ret .. " end\n";
ret = ret .. recheckCode
ret = ret .. " if (recheckTime) then\n"
ret = ret .. " WeakAuras.scheduleConditionCheck(recheckTime, id, cloneId);\n"
ret = ret .. " WeakAuras.scheduleConditionCheck(recheckTime, uid, cloneId);\n"
ret = ret .. " end\n"
local properties = Private.GetProperties(data);
@@ -581,16 +621,28 @@ local function ConstructConditionFunction(data)
return ret;
end
local function CancelTimers(uid)
conditionChecksTimers.recheckTime[uid] = nil;
if (conditionChecksTimers.recheckHandle[uid]) then
for _, v in pairs(conditionChecksTimers.recheckHandle[uid]) do
timer:CancelTimer(v);
end
end
conditionChecksTimers.recheckHandle[uid] = nil;
end
function Private.LoadConditionFunction(data)
CancelTimers(data.uid)
local checkConditionsFuncStr = ConstructConditionFunction(data);
local checkCondtionsFunc = checkConditionsFuncStr and WeakAuras.LoadFunction(checkConditionsFuncStr, data.id, "condition checks");
checkConditions[data.id] = checkCondtionsFunc;
checkConditions[data.uid] = checkCondtionsFunc;
end
function Private.RunConditions(region, id, hideRegion)
if (checkConditions[id]) then
checkConditions[id](region, hideRegion);
function Private.RunConditions(region, uid, hideRegion)
if (checkConditions[uid]) then
checkConditions[uid](region, hideRegion);
end
end
@@ -610,12 +662,13 @@ function Private.GetGlobalConditionState()
end
local function runDynamicConditionFunctions(funcs)
for id in pairs(funcs) do
if (Private.IsAuraActive(id) and checkConditions[id]) then
local activeTriggerState = WeakAuras.GetTriggerStateForTrigger(id, Private.ActiveTrigger(id));
for cloneId, state in pairs(activeTriggerState) do
for uid in pairs(funcs) do
local id = Private.UIDtoID(uid)
if (Private.IsAuraActive(uid) and checkConditions[uid]) then
local activeStates = WeakAuras.GetActiveStates(id)
for cloneId, state in pairs(activeStates) do
local region = WeakAuras.GetRegion(id, cloneId);
checkConditions[id](region, false);
checkConditions[uid](region, false);
end
end
end
@@ -643,14 +696,14 @@ end
local registeredGlobalFunctions = {};
local function EvaluateCheckForRegisterForGlobalConditions(id, check, allConditionsTemplate, register)
local function EvaluateCheckForRegisterForGlobalConditions(uid, check, allConditionsTemplate, register)
local trigger = check and check.trigger;
local variable = check and check.variable;
if (trigger == -2) then
if (check.checks) then
for _, subcheck in ipairs(check.checks) do
EvaluateCheckForRegisterForGlobalConditions(id, subcheck, allConditionsTemplate, register);
EvaluateCheckForRegisterForGlobalConditions(uid, subcheck, allConditionsTemplate, register);
end
end
elseif trigger == -1 and variable == "customcheck" then
@@ -660,7 +713,7 @@ local function EvaluateCheckForRegisterForGlobalConditions(id, check, allConditi
register[event] = true;
dynamicConditions[event] = {};
end
dynamicConditions[event][id] = true;
dynamicConditions[event][uid] = true;
end
end
elseif (trigger and variable) then
@@ -671,7 +724,7 @@ local function EvaluateCheckForRegisterForGlobalConditions(id, check, allConditi
register[event] = true;
dynamicConditions[event] = {};
end
dynamicConditions[event][id] = true;
dynamicConditions[event][uid] = true;
end
if (conditionTemplate.globalStateUpdate and not registeredGlobalFunctions[variable]) then
@@ -686,10 +739,10 @@ local function EvaluateCheckForRegisterForGlobalConditions(id, check, allConditi
end
end
function Private.RegisterForGlobalConditions(id)
local data = WeakAuras.GetData(id);
function Private.RegisterForGlobalConditions(uid)
local data = Private.GetDataByUID(uid);
for event, conditionFunctions in pairs(dynamicConditions) do
conditionFunctions.id = nil;
conditionFunctions[uid] = nil;
end
local register = {};
@@ -698,7 +751,7 @@ function Private.RegisterForGlobalConditions(id)
allConditionsTemplate[-1] = Private.GetGlobalConditions();
for conditionNumber, condition in ipairs(data.conditions) do
EvaluateCheckForRegisterForGlobalConditions(id, condition.check, allConditionsTemplate, register);
EvaluateCheckForRegisterForGlobalConditions(uid, condition.check, allConditionsTemplate, register);
end
end
@@ -720,17 +773,17 @@ function Private.RegisterForGlobalConditions(id)
end
end
function Private.UnregisterForGlobalConditions(id)
function Private.UnregisterForGlobalConditions(uid)
for event, condFuncs in pairs(dynamicConditions) do
condFuncs[id] = nil;
condFuncs[uid] = nil;
end
end
function Private.UnloadAllConditions()
for id in pairs(conditionChecksTimers.recheckTime) do
if (conditionChecksTimers.recheckHandle[id]) then
for _, v in pairs(conditionChecksTimers.recheckHandle[id]) do
for uid in pairs(conditionChecksTimers.recheckTime) do
if (conditionChecksTimers.recheckHandle[uid]) then
for _, v in pairs(conditionChecksTimers.recheckHandle[uid]) do
timer:CancelTimer(v)
end
end
@@ -741,44 +794,7 @@ function Private.UnloadAllConditions()
dynamicConditions = {}
end
function Private.UnloadConditions(id)
conditionChecksTimers.recheckTime[id] = nil;
if (conditionChecksTimers.recheckHandle[id]) then
for _, v in pairs(conditionChecksTimers.recheckHandle[id]) do
timer:CancelTimer(v);
end
end
conditionChecksTimers.recheckHandle[id] = nil;
Private.UnregisterForGlobalConditions(id);
end
function Private.DeleteConditions(id)
checkConditions[id] = nil
conditionChecksTimers.recheckTime[id] = nil
if (conditionChecksTimers.recheckHandle[id]) then
for cloneId, v in pairs(conditionChecksTimers.recheckHandle[id]) do
timer:CancelTimer(v)
end
end
conditionChecksTimers.recheckHandle[id] = nil
for event, funcs in pairs(dynamicConditions) do
funcs[id] = nil
end
end
function Private.RenameConditions(oldid, newid)
checkConditions[newid] = checkConditions[oldid];
checkConditions[oldid] = nil;
conditionChecksTimers.recheckTime[newid] = conditionChecksTimers.recheckTime[oldid];
conditionChecksTimers.recheckTime[oldid] = nil;
conditionChecksTimers.recheckHandle[newid] = conditionChecksTimers.recheckHandle[oldid];
conditionChecksTimers.recheckHandle[oldid] = nil;
for event, funcs in pairs(dynamicConditions) do
funcs[newid] = funcs[oldid]
funcs[oldid] = nil;
end
function Private.UnloadConditions(uid)
CancelTimers(uid)
Private.UnregisterForGlobalConditions(uid);
end