mirror of
https://github.com/brues-code/SuperCleveRoidMacros.git
synced 2026-09-16 03:38:00 +00:00
increase supermacro support
This commit is contained in:
+132
-54
@@ -1,75 +1,153 @@
|
||||
-- CleveRoidMacros/Compatibility/SuperMacro.lua
|
||||
-- Make SuperMacro’s RunLine play nicely with CleveRoidMacros (incl. /castsequence)
|
||||
--[[
|
||||
SuperMacro compatibility shim for CleveRoidMacros (1.12.1-safe)
|
||||
- Installs only when BOTH SuperMacro and SuperCleveRoidMacros/CleveRoidMacros are loaded
|
||||
- Hooks SuperMacro's RunLine order-agnostically (ADDON_LOADED + PLAYER_LOGIN)
|
||||
- Intercepts CRM-owned slashcommands (including {…}, […], !, ?, ~)
|
||||
- Forwards to CRM's SlashCmdList handlers; preserves /castsequence
|
||||
- Leaves SuperMacro unmodified; safe fallback to original RunLine
|
||||
]]
|
||||
|
||||
local _G = _G or getfenv(0)
|
||||
local CRM = _G.CleveRoids or {}
|
||||
_G.CleveRoids = CRM
|
||||
do
|
||||
local _G = _G or getfenv(0)
|
||||
local CRM = _G.CleveRoids or {}
|
||||
_G.CleveRoids = CRM
|
||||
CRM.Hooks = CRM.Hooks or {}
|
||||
|
||||
local function install_supermacro_hook()
|
||||
if CRM.SM_RunLineHooked then return end
|
||||
if type(_G.RunLine) ~= "function" then return end
|
||||
|
||||
-- Commands we only intercept when they use bracket/extended syntax
|
||||
local bracketHooks = {
|
||||
cast = CRM.DoCast,
|
||||
target = CRM.DoTarget,
|
||||
use = CRM.DoUse,
|
||||
-- Commands owned by CRM (keep in sync with Console.lua registrations)
|
||||
local INTERCEPT = {
|
||||
cast=true, castsequence=true, use=true,
|
||||
startattack=true, stopattack=true, stopcasting=true,
|
||||
target=true, retarget=true, cancelaura=true, unbuff=true,
|
||||
unqueue=true, unshift=true, equip=true, equipmh=true, equipoh=true,
|
||||
runmacro=true,
|
||||
-- pet
|
||||
petattack=true, petfollow=true, petpassive=true,
|
||||
petaggressive=true, petdefensive=true, petwait=true,
|
||||
}
|
||||
|
||||
CRM.Hooks = CRM.Hooks or {}
|
||||
CRM.Hooks.RunLine = CRM.Hooks.RunLine or _G.RunLine
|
||||
-- alias map (if CRM only registers /cancelaura, but /unbuff appears)
|
||||
local ALIASES = { unbuff = "cancelaura" }
|
||||
|
||||
_G.RunLine = function(...)
|
||||
for i = 1, arg.n do
|
||||
local text = arg[i]
|
||||
local function normalize_cmd(cmd)
|
||||
if not cmd or cmd == "" then return nil end
|
||||
local _, _, s = string.find(cmd, "^(.*)$")
|
||||
s = s and string.lower(s) or nil
|
||||
if s and ALIASES[s] then s = ALIASES[s] end
|
||||
return s
|
||||
end
|
||||
|
||||
local function has_handler(cmd)
|
||||
local list = _G.SlashCmdList
|
||||
return cmd and list and type(list[string.upper(cmd)]) == "function"
|
||||
end
|
||||
|
||||
local function parse_slash(text)
|
||||
if type(text) ~= "string" then return nil, nil end
|
||||
text = (string.gsub(text, "^[%s]+", ""))
|
||||
if not string.find(text, "^/") then return nil, nil end
|
||||
local _, _, cmd, rest = string.find(text, "^/(%S+)%s*(.*)$")
|
||||
return cmd, rest
|
||||
end
|
||||
|
||||
local function install_supermacro_hook()
|
||||
if CRM.SM_RunLineHooked then return end
|
||||
if type(_G.RunLine) ~= "function" then return end
|
||||
|
||||
local orig_RunLine = _G.RunLine
|
||||
CRM.Hooks.RunLine = CRM.Hooks.RunLine or orig_RunLine
|
||||
|
||||
_G.RunLine = function(...)
|
||||
-- SuperMacro calls RunLine(line) one line at a time; handle first arg.
|
||||
local text = arg and arg[1]
|
||||
|
||||
-- respect /stopmacro guard
|
||||
if CRM.stopmacro then
|
||||
CRM.stopmacro = false
|
||||
return true
|
||||
end
|
||||
|
||||
local handled = false
|
||||
if type(text) == "string" then
|
||||
-- 1) Always handle /castsequence (with or without conditionals)
|
||||
local b1, e1, rest = string.find(text, "^%s*/castsequence%s*(.*)")
|
||||
if b1 then
|
||||
local msg = CRM.Trim(rest or "")
|
||||
local seq = CRM.GetSequence(msg)
|
||||
if seq then pcall(CRM.DoCastSequence, seq) end
|
||||
handled = true
|
||||
-- 1) special-case /castsequence → call CRM directly and RETURN TRUE
|
||||
local b, e, rest = string.find(text, "^%s*/castsequence%s*(.*)")
|
||||
if b then
|
||||
if type(CleveRoids) == "table" and type(CleveRoids.DoCastSequence) == "function" then
|
||||
pcall(CleveRoids.DoCastSequence, rest or "")
|
||||
return true
|
||||
end
|
||||
-- fallback: slash handler if defined
|
||||
local fn = _G.SlashCmdList and _G.SlashCmdList["CASTSEQUENCE"]
|
||||
if type(fn) == "function" then
|
||||
pcall(fn, rest or "")
|
||||
return true
|
||||
end
|
||||
-- nothing to do; pass through to SM
|
||||
else
|
||||
-- 2) Bracketed/advanced forms for /cast, /target, /use
|
||||
for cmd, action in bracketHooks do
|
||||
local b2, e2 = string.find(text, "^%s*/" .. cmd .. "%s+[!%[]")
|
||||
if b2 then
|
||||
local msg = string.sub(text, e2)
|
||||
msg = string.gsub(msg, "^%s+", "")
|
||||
pcall(action, msg)
|
||||
handled = true
|
||||
break
|
||||
-- 2) generic /cmd forwarding (lets CRM handlers parse [])
|
||||
local _, _, raw, msg = string.find(text, "^/(%S+)%s*(.*)$")
|
||||
if raw then
|
||||
local cmd = string.lower(raw)
|
||||
-- alias map: /unbuff → /cancelaura
|
||||
if cmd == "unbuff" then cmd = "cancelaura" end
|
||||
if INTERCEPT[cmd] and has_handler(cmd) then
|
||||
pcall(_G.SlashCmdList[string.upper(cmd)], msg or "")
|
||||
return true -- IMPORTANT: tell SM we handled it
|
||||
end
|
||||
end
|
||||
|
||||
-- 3) fast-path for extended/bracket tokens (/cast {…} [/…] ! ? ~)
|
||||
local hooks = { cast = CRM.DoCast, target = CRM.DoTarget, use = CRM.DoUse }
|
||||
for k, fn in pairs(hooks) do
|
||||
if type(fn) == "function" then
|
||||
local b2, e2 = string.find(text, "^%s*/" .. k .. "%s+[!%[%{%?~]")
|
||||
if b2 then
|
||||
local msg2 = string.gsub(string.sub(text, e2 + 1), "^%s+", "")
|
||||
pcall(fn, msg2)
|
||||
return true -- IMPORTANT
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if not handled then
|
||||
CRM.Hooks.RunLine(text) -- fall back to SuperMacro’s original logic
|
||||
end
|
||||
-- Not handled by CRM: let SuperMacro do its normal processing
|
||||
return orig_RunLine(text)
|
||||
end
|
||||
|
||||
CRM.SM_RunLineHooked = true
|
||||
end
|
||||
|
||||
-- ===== Order-agnostic loader: install only when BOTH addons are present =====
|
||||
local SM_LOADED, CRM_LOADED = false, false
|
||||
local f = CreateFrame("Frame")
|
||||
f:RegisterEvent("ADDON_LOADED")
|
||||
f:RegisterEvent("PLAYER_LOGIN")
|
||||
|
||||
local function note_loaded(name)
|
||||
if name == "SuperMacro" then
|
||||
SM_LOADED = true
|
||||
elseif name == "SuperCleveRoidMacros" or name == "CleveRoidMacros" then
|
||||
CRM_LOADED = true
|
||||
end
|
||||
end
|
||||
|
||||
CRM.SM_RunLineHooked = true
|
||||
local function both_loaded()
|
||||
if SM_LOADED and CRM_LOADED then return true end
|
||||
-- heuristic fallback in case this file loads late:
|
||||
local sm_ok = type(_G.RunLine) == "function"
|
||||
local crm_ok = _G.CleveRoids and _G.SlashCmdList and type(_G.SlashCmdList.CAST) == "function"
|
||||
return sm_ok and crm_ok
|
||||
end
|
||||
|
||||
local function try_install()
|
||||
if both_loaded() then install_supermacro_hook() end
|
||||
end
|
||||
|
||||
f:SetScript("OnEvent", function(_, evt, addon)
|
||||
if evt == "ADDON_LOADED" then
|
||||
if type(addon) == "string" then note_loaded(addon) end
|
||||
try_install()
|
||||
elseif evt == "PLAYER_LOGIN" then
|
||||
try_install()
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
-- Install after either addon is ready (order-agnostic)
|
||||
local f = CreateFrame("Frame")
|
||||
f:RegisterEvent("ADDON_LOADED")
|
||||
f:RegisterEvent("PLAYER_LOGIN")
|
||||
f:SetScript("OnEvent", function(_, event, addon)
|
||||
if event == "ADDON_LOADED" then
|
||||
if addon == "SuperMacro" or addon == "CleveRoidMacros" then
|
||||
install_supermacro_hook()
|
||||
end
|
||||
else
|
||||
install_supermacro_hook()
|
||||
end
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user