Files

350 lines
16 KiB
Lua

-- OctoTextSize: addon-local factory; Blizzard CreateFrame is unchanged.
local CreateFrame = OctoTextSize and OctoTextSize.WrapFactory(CreateFrame) or CreateFrame
-- No cast is dispatched from an event or timer. Every cast remains inside the
-- original hardware-triggered secure macro/spell action path.
local policies = WoWHybridWarriorStancePolicies or {}
local forms = { { source = 17, bit = 65536, spell = 2457 },
{ source = 18, bit = 131072, spell = 71 },
{ source = 19, bit = 262144, spell = 2458 } }
local api = {}
WoWHybridWarriorStance = api
api.status = { configured = 0, wrapped = 0, native_acceptance = false,
loaded = true, class = "unobserved", refresh = "waiting-for-event" }
local function has(mask, value)
return math.floor(mask / value) % 2 == 1
end
-- Source StancesNot wins over Stances; neither mask uses native form indices.
function api.Plan(required, excluded, owned)
if type(required) ~= "number" or type(excluded) ~= "number"
or required < 0 or excluded < 0 or required % 1 ~= 0 or excluded % 1 ~= 0
or required > 458752 or excluded > 458752 then return nil end
local knownRequired, knownExcluded = 0, 0
local allowed, denied = {}, {}
for _, form in ipairs(forms) do
if has(required, form.bit) then knownRequired = knownRequired + form.bit end
if has(excluded, form.bit) then knownExcluded = knownExcluded + form.bit end
end
if required ~= knownRequired or excluded ~= knownExcluded then return nil end
for _, form in ipairs(forms) do
local index = owned[form.spell]
if index then
if not has(excluded, form.bit) and (required == 0 or has(required, form.bit)) then
allowed[#allowed + 1] = { index = index, spell = form.spell }
elseif has(excluded, form.bit) then
denied[#denied + 1] = index
end
end
end
if #allowed == 0 then return nil end
local indices = {}
for _, row in ipairs(allowed) do indices[#indices + 1] = tostring(row.index) end
local condition = "nostance:" .. table.concat(indices, "/")
if required == 0 then
if #denied == 0 then return nil end
local parts = {}
for _, index in ipairs(denied) do parts[#parts + 1] = tostring(index) end
condition = "stance:" .. table.concat(parts, "/")
end
return condition, allowed[1].spell
end
function api.Macro(spell, condition, stance)
if type(spell) ~= "number" or spell % 1 ~= 0 or spell < 1 or spell > 2147483647
or type(stance) ~= "number" or not (stance == 2457 or stance == 71 or stance == 2458)
or type(condition) ~= "string" or not (condition:match("^nostance:%d+[%d/]*$")
or condition:match("^stance:%d+[%d/]*$")) then return nil end
-- Keep the original action slot's native rank/override and target handling.
-- A source stance ACK race may still require the next hardware press.
return "/click [" .. condition .. "] GWWarriorStanceSpell" .. stance
.. "\n/click GWWarriorStanceOriginalAction"
end
local BEFORE = [[
local source = self:GetFrameRef("gw-original-action-button") or self
if button ~= "LeftButton" or self:GetEffectiveAttribute("type", button) ~= "action"
or source:GetAttribute("type") ~= "action"
or source:GetEffectiveAttribute("type", button) ~= "action"
or source:GetAttribute("macrotext") then
return
end
local action
local id = source:GetID()
if id > 0 then
local page = source:GetEffectiveAttribute("actionpage", button) or GetActionBarPage()
if not page or page < 1 or page > 32 then
return
end
action = id + ((page - 1) * 12)
else
action = source:GetEffectiveAttribute("action", button) or 1
end
if not action or action < 1 or action > 1000 then
return
end
-- Keyboard proxies inherit the original button's attributes and ID, so
-- ineligible actions retain its native target/modifier/page dispatch.
if not control:GetAttribute("gw-warrior-enabled") or IsModifiedClick("PICKUPACTION") then return end
local form = GetShapeshiftForm()
if form ~= 0 and not control:GetAttribute("gw-owned-form-" .. form) then return end
local kind, spell = GetActionInfo(action)
if kind ~= "spell" or not spell then return end
local macro = control:GetAttribute("gw-macro-" .. spell)
if not macro then return end
-- A help/harm remap may select a different button/action type. Leave that
-- original path intact rather than guessing its second attribute lookup.
if source:GetEffectiveAttribute("helpbutton", button)
or source:GetEffectiveAttribute("harmbutton", button)
or source:GetEffectiveAttribute("downbutton", button)
or source:GetEffectiveAttribute("pressAndHoldAction", button) then return end
local forward = control:GetFrameRef("gw-action-forwarder")
if not forward then return end
forward:SetAttribute("action", action)
-- Snapshot the original modified attributes, then let the original secure
-- action handler resolve unit suffixes, vehicle/self/focus/mouseover rules.
forward:SetAttribute("unit", source:GetEffectiveAttribute("unit", button))
forward:SetAttribute("unitsuffix", source:GetEffectiveAttribute("unitsuffix", button))
forward:SetAttribute("toggleForVehicle", source:GetEffectiveAttribute("toggleForVehicle", button))
forward:SetAttribute("allowVehicleTarget", source:GetEffectiveAttribute("allowVehicleTarget", button))
forward:SetAttribute("checkselfcast", source:GetEffectiveAttribute("checkselfcast", button))
forward:SetAttribute("checkfocuscast", source:GetEffectiveAttribute("checkfocuscast", button))
forward:SetAttribute("checkmouseovercast", source:GetEffectiveAttribute("checkmouseovercast", button))
self:SetAttribute("type", "macro")
self:SetAttribute("macrotext", macro)
return nil, "gw-stance"
]]
local AFTER = [[
if message == "gw-stance" then
if self:GetFrameRef("gw-original-action-button") then
self:SetAttribute("type", nil)
else
self:SetAttribute("type", "action")
end
self:SetAttribute("macrotext", nil)
end
]]
api.Before = BEFORE
api.After = AFTER
local controller, pending, actionForwarder
local children, configured, wrapped = {}, {}, setmetatable({}, { __mode = "k" })
local keyboardButtons = setmetatable({}, { __mode = "k" })
local keyboardButtonCount = 0
local function spellButton(id)
if children[id] then return children[id] end
local name = "GWWarriorStanceSpell" .. id
if _G[name] then return nil end -- Never adopt a foreign named frame.
local button = CreateFrame("Button", name, UIParent, "SecureActionButtonTemplate")
button:RegisterForClicks("AnyUp")
button:EnableMouse(false)
button:SetSize(1, 1)
button:SetPoint("TOPLEFT", UIParent, "TOPLEFT", -10, 10)
-- /click sends an up event to these AnyUp-only children. The original
-- secure dispatcher otherwise inherits ActionButtonUseKeyDown and drops it.
button:SetAttribute("useOnKeyDown", false)
button:SetAttribute("type", "spell")
button:SetAttribute("spell", id)
children[id] = button
return button
end
-- Standard action-key bindings call TryUseActionButton directly, bypassing the
-- original frame OnClick wrapper. Transient click bindings target our secure
-- native-action proxy; persistent user bindings are never changed.
local function bindKeyboard(button)
local command = button.bindingAction
if type(command) ~= "string" or not button:GetName() then return end
local id = button:GetID()
local action
if id > 0 then
-- This is an ordinary Button; GetEffectiveAttribute belongs to the
-- restricted handles used by BEFORE, not to the public frame object.
local page = SecureButton_GetModifiedAttribute(button, "actionpage", "LeftButton") or GetActionBarPage()
if not page or page < 1 or page > 32 then return end
action = id + ((page - 1) * 12)
else
action = SecureButton_GetModifiedAttribute(button, "action", "LeftButton") or 1
end
local kind, spell = GetActionInfo(action)
if kind == "spell" and type(spell) == "number" then
api.status.action_spells[action] = spell
end
if kind ~= "spell" or not configured[spell] then return end
local proxy = keyboardButtons[button]
for _, key in ipairs({ GetBindingKey(command) }) do
-- Respect effective overrides owned by the original UI or other addons.
if type(key) == "string" and GetBindingAction(key, true) == command then
if not proxy then
keyboardButtonCount = keyboardButtonCount + 1
proxy = CreateFrame("Button", "GWWarriorStanceAction" .. keyboardButtonCount,
button, "SecureActionButtonTemplate")
proxy:RegisterForClicks("AnyDown", "AnyUp")
proxy:EnableMouse(false)
proxy:SetSize(1, 1)
proxy:SetPoint("TOPLEFT", UIParent, "BOTTOMRIGHT", 100, -100)
proxy:SetAttribute("useparent*", true)
SecureHandlerSetFrameRef(proxy, "gw-original-action-button", button)
SecureHandlerWrapScript(proxy, "OnClick", controller, BEFORE, AFTER)
keyboardButtons[button] = proxy
end
proxy:SetID(id)
SetOverrideBindingClick(controller, false, key, proxy:GetName(), "LeftButton")
api.status.keyboard = api.status.keyboard + 1
end
end
end
function api.Refresh()
if InCombatLockdown() then
pending = true
api.status.refresh = "deferred-combat"
return
end
if controller then ClearOverrideBindings(controller) end
api.status.keyboard = 0
api.status.action_spells = {}
api.status.owned_forms = {}
local _, class = UnitClass("player")
api.status.class = class or "unobserved"
api.status.refresh = "configuring"
if class ~= "WARRIOR" then
if controller then controller:SetAttribute("gw-warrior-enabled", nil) end
api.status.configured = 0
api.status.refresh = "not-warrior"
return
end
if not controller then
if _G.GWWarriorStanceControl then
api.status.refresh = "controller-name-already-owned"
return
end
controller = CreateFrame("Frame", "GWWarriorStanceControl", UIParent, "SecureHandlerBaseTemplate")
end
if not actionForwarder then
if _G.GWWarriorStanceOriginalAction then
api.status.refresh = "action-forwarder-name-already-owned"
return
end
actionForwarder = CreateFrame("Button", "GWWarriorStanceOriginalAction", UIParent,
"SecureActionButtonTemplate")
actionForwarder:RegisterForClicks("AnyUp")
actionForwarder:EnableMouse(false)
actionForwarder:SetSize(1, 1)
actionForwarder:SetPoint("TOPLEFT", UIParent, "TOPLEFT", -10, 10)
actionForwarder:SetAttribute("useOnKeyDown", false)
actionForwarder:SetAttribute("type", "action")
SecureHandlerSetFrameRef(controller, "gw-action-forwarder", actionForwarder)
end
controller:SetAttribute("gw-warrior-enabled", true)
for id in pairs(configured) do controller:SetAttribute("gw-macro-" .. id, nil) end
for index = 1, 32 do controller:SetAttribute("gw-owned-form-" .. index, nil) end
configured = {}
api.status.configured = 0
local owned, seen = {}, {}
local count = GetNumShapeshiftForms()
if type(count) ~= "number" or count < 0 or count > 32 then
api.status.refresh = "form-count-held"
return
end
for index = 1, count do
local _, _, _, spell = GetShapeshiftFormInfo(index)
for _, form in ipairs(forms) do
if spell == form.spell then
if seen[spell] then
api.status.refresh = "duplicate-form-held"
return
end
owned[spell], seen[spell] = index, true
controller:SetAttribute("gw-owned-form-" .. index, true)
end
end
end
api.status.owned_forms = owned
local configuredCount = 0
for id, policy in pairs(policies) do
-- Policy membership supplies only the source stance rule. The current
-- native GetActionInfo slot, and ultimately UseAction, own the ability
-- and rank; do not infer aliases from names or replace it with a spell.
local condition, stance = api.Plan(policy[1], policy[2], owned)
if condition and spellButton(stance) then
local macro = api.Macro(id, condition, stance)
if macro then
controller:SetAttribute("gw-macro-" .. id, macro)
configured[id] = true
configuredCount = configuredCount + 1
end
end
end
api.status.configured = configuredCount
local events = ActionBarButtonEventsFrame
if not events or type(events.ForEachFrame) ~= "function" then
api.status.refresh = "action-button-events-unavailable"
return
end
events:ForEachFrame(function(button)
if not wrapped[button] and not button.isExtra
and (button.buttonType == nil or button.buttonType == "ACTIONBUTTON")
and button:GetAttribute("type") == "action" and not button:GetAttribute("macrotext")
and button:IsProtected() and not button:IsForbidden() then
SecureHandlerWrapScript(button, "OnClick", controller, BEFORE, AFTER)
wrapped[button] = true
api.status.wrapped = api.status.wrapped + 1
end
if wrapped[button] and button:GetAttribute("type") == "action"
and not button:GetAttribute("macrotext") then bindKeyboard(button) end
end)
pending = false
api.status.refresh = "configured"
end
local events = CreateFrame("Frame")
for _, event in ipairs({ "PLAYER_LOGIN", "PLAYER_ENTERING_WORLD", "SPELLS_CHANGED",
"UPDATE_SHAPESHIFT_FORMS", "PLAYER_REGEN_ENABLED", "ADDON_LOADED",
"UPDATE_BINDINGS", "ACTIONBAR_SLOT_CHANGED", "ACTIONBAR_PAGE_CHANGED",
"UPDATE_BONUS_ACTIONBAR", "CVAR_UPDATE" }) do
events:RegisterEvent(event)
end
events:SetScript("OnEvent", function() api.Refresh() end)
-- Ordinary addon diagnostics read cached values only. No secure action, refresh,
-- live protected getter or gameplay request is dispatched by this command.
function api.StatusText()
local status = api.status
return "Warrior stance: loaded=" .. tostring(status.loaded)
.. " class=" .. tostring(status.class)
.. " configured=" .. tostring(status.configured)
.. " wrapped=" .. tostring(status.wrapped)
.. " keyboard=" .. tostring(status.keyboard or 0)
.. " reason=" .. tostring(status.refresh)
end
-- These are the values already read by Refresh, not new protected API queries.
function api.CachedMappingText()
local slots, entries = {}, {}
for slot in pairs(api.status.action_spells or {}) do slots[#slots + 1] = slot end
table.sort(slots)
for _, slot in ipairs(slots) do
if #entries == 24 then entries[#entries + 1] = "..."; break end
local spell = api.status.action_spells[slot]
entries[#entries + 1] = tostring(slot) .. ":" .. tostring(spell)
.. (configured[spell] and "*" or "?")
end
local owned = api.status.owned_forms or {}
return "Warrior stance forms: Battle=" .. tostring(owned[2457])
.. " Defensive=" .. tostring(owned[71]) .. " Berserker=" .. tostring(owned[2458]),
"Warrior action slots (slot:spell, *=configured): " .. table.concat(entries, " ")
end
SLASH_WOWHYBRIDWARRIORSTANCE1 = "/gwstance"
SlashCmdList.WOWHYBRIDWARRIORSTANCE = function(message)
local command = (message or ""):match("^%s*(.-)%s*$"):lower()
local text = command == "status" and api.StatusText() or "Usage: /gwstance status"
if DEFAULT_CHAT_FRAME then
DEFAULT_CHAT_FRAME:AddMessage(text)
if command == "status" then
local formsText, actionsText = api.CachedMappingText()
DEFAULT_CHAT_FRAME:AddMessage(formsText)
DEFAULT_CHAT_FRAME:AddMessage(actionsText)
end
end
end