Tercio@23: --- **AceConsole-3.0** provides registration facilities for slash commands. Tercio@23: -- You can register slash commands to your custom functions and use the `GetArgs` function to parse them Tercio@23: -- to your addons individual needs. Tercio@23: -- Tercio@23: -- **AceConsole-3.0** can be embeded into your addon, either explicitly by calling AceConsole:Embed(MyAddon) or by Tercio@23: -- specifying it as an embeded library in your AceAddon. All functions will be available on your addon object Tercio@23: -- and can be accessed directly, without having to explicitly call AceConsole itself.\\ Tercio@23: -- It is recommended to embed AceConsole, otherwise you'll have to specify a custom `self` on all calls you Tercio@23: -- make into AceConsole. Tercio@23: -- @class file Tercio@23: -- @name AceConsole-3.0 Tercio@51: -- @release $Id: AceConsole-3.0.lua 1143 2016-07-11 08:52:03Z nevcairiel $ Tercio@23: local MAJOR,MINOR = "AceConsole-3.0", 7 Tercio@23: Tercio@23: local AceConsole, oldminor = LibStub:NewLibrary(MAJOR, MINOR) Tercio@23: Tercio@23: if not AceConsole then return end -- No upgrade needed Tercio@23: Tercio@23: AceConsole.embeds = AceConsole.embeds or {} -- table containing objects AceConsole is embedded in. Tercio@23: AceConsole.commands = AceConsole.commands or {} -- table containing commands registered Tercio@23: AceConsole.weakcommands = AceConsole.weakcommands or {} -- table containing self, command => func references for weak commands that don't persist through enable/disable Tercio@23: Tercio@23: -- Lua APIs Tercio@23: local tconcat, tostring, select = table.concat, tostring, select Tercio@23: local type, pairs, error = type, pairs, error Tercio@23: local format, strfind, strsub = string.format, string.find, string.sub Tercio@23: local max = math.max Tercio@23: Tercio@23: -- WoW APIs Tercio@23: local _G = _G Tercio@23: Tercio@23: -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded Tercio@23: -- List them here for Mikk's FindGlobals script Tercio@23: -- GLOBALS: DEFAULT_CHAT_FRAME, SlashCmdList, hash_SlashCmdList Tercio@23: Tercio@23: local tmp={} Tercio@23: local function Print(self,frame,...) Tercio@23: local n=0 Tercio@23: if self ~= AceConsole then Tercio@23: n=n+1 Tercio@23: tmp[n] = "|cff33ff99"..tostring( self ).."|r:" Tercio@23: end Tercio@23: for i=1, select("#", ...) do Tercio@23: n=n+1 Tercio@23: tmp[n] = tostring(select(i, ...)) Tercio@23: end Tercio@23: frame:AddMessage( tconcat(tmp," ",1,n) ) Tercio@23: end Tercio@23: Tercio@23: --- Print to DEFAULT_CHAT_FRAME or given ChatFrame (anything with an .AddMessage function) Tercio@23: -- @paramsig [chatframe ,] ... Tercio@23: -- @param chatframe Custom ChatFrame to print to (or any frame with an .AddMessage function) Tercio@23: -- @param ... List of any values to be printed Tercio@23: function AceConsole:Print(...) Tercio@23: local frame = ... Tercio@23: if type(frame) == "table" and frame.AddMessage then -- Is first argument something with an .AddMessage member? Tercio@23: return Print(self, frame, select(2,...)) Tercio@23: else Tercio@23: return Print(self, DEFAULT_CHAT_FRAME, ...) Tercio@23: end Tercio@23: end Tercio@23: Tercio@23: Tercio@23: --- Formatted (using format()) print to DEFAULT_CHAT_FRAME or given ChatFrame (anything with an .AddMessage function) Tercio@23: -- @paramsig [chatframe ,] "format"[, ...] Tercio@23: -- @param chatframe Custom ChatFrame to print to (or any frame with an .AddMessage function) Tercio@23: -- @param format Format string - same syntax as standard Lua format() Tercio@23: -- @param ... Arguments to the format string Tercio@23: function AceConsole:Printf(...) Tercio@23: local frame = ... Tercio@23: if type(frame) == "table" and frame.AddMessage then -- Is first argument something with an .AddMessage member? Tercio@23: return Print(self, frame, format(select(2,...))) Tercio@23: else Tercio@23: return Print(self, DEFAULT_CHAT_FRAME, format(...)) Tercio@23: end Tercio@23: end Tercio@23: Tercio@23: Tercio@23: Tercio@23: Tercio@23: --- Register a simple chat command Tercio@23: -- @param command Chat command to be registered WITHOUT leading "/" Tercio@23: -- @param func Function to call when the slash command is being used (funcref or methodname) Tercio@23: -- @param persist if false, the command will be soft disabled/enabled when aceconsole is used as a mixin (default: true) Tercio@23: function AceConsole:RegisterChatCommand( command, func, persist ) Tercio@23: if type(command)~="string" then error([[Usage: AceConsole:RegisterChatCommand( "command", func[, persist ]): 'command' - expected a string]], 2) end Tercio@23: Tercio@23: if persist==nil then persist=true end -- I'd rather have my addon's "/addon enable" around if the author screws up. Having some extra slash regged when it shouldnt be isn't as destructive. True is a better default. /Mikk Tercio@23: Tercio@23: local name = "ACECONSOLE_"..command:upper() Tercio@23: Tercio@23: if type( func ) == "string" then Tercio@23: SlashCmdList[name] = function(input, editBox) Tercio@23: self[func](self, input, editBox) Tercio@23: end Tercio@23: else Tercio@23: SlashCmdList[name] = func Tercio@23: end Tercio@23: _G["SLASH_"..name.."1"] = "/"..command:lower() Tercio@23: AceConsole.commands[command] = name Tercio@23: -- non-persisting commands are registered for enabling disabling Tercio@23: if not persist then Tercio@23: if not AceConsole.weakcommands[self] then AceConsole.weakcommands[self] = {} end Tercio@23: AceConsole.weakcommands[self][command] = func Tercio@23: end Tercio@23: return true Tercio@23: end Tercio@23: Tercio@23: --- Unregister a chatcommand Tercio@23: -- @param command Chat command to be unregistered WITHOUT leading "/" Tercio@23: function AceConsole:UnregisterChatCommand( command ) Tercio@23: local name = AceConsole.commands[command] Tercio@23: if name then Tercio@23: SlashCmdList[name] = nil Tercio@23: _G["SLASH_" .. name .. "1"] = nil Tercio@23: hash_SlashCmdList["/" .. command:upper()] = nil Tercio@23: AceConsole.commands[command] = nil Tercio@23: end Tercio@23: end Tercio@23: Tercio@23: --- Get an iterator over all Chat Commands registered with AceConsole Tercio@23: -- @return Iterator (pairs) over all commands Tercio@23: function AceConsole:IterateChatCommands() return pairs(AceConsole.commands) end Tercio@23: Tercio@23: Tercio@23: local function nils(n, ...) Tercio@23: if n>1 then Tercio@23: return nil, nils(n-1, ...) Tercio@23: elseif n==1 then Tercio@23: return nil, ... Tercio@23: else Tercio@23: return ... Tercio@23: end Tercio@23: end Tercio@23: Tercio@23: Tercio@23: --- Retreive one or more space-separated arguments from a string. Tercio@23: -- Treats quoted strings and itemlinks as non-spaced. Tercio@51: -- @param str The raw argument string Tercio@23: -- @param numargs How many arguments to get (default 1) Tercio@23: -- @param startpos Where in the string to start scanning (default 1) Tercio@23: -- @return Returns arg1, arg2, ..., nextposition\\ Tercio@23: -- Missing arguments will be returned as nils. 'nextposition' is returned as 1e9 at the end of the string. Tercio@23: function AceConsole:GetArgs(str, numargs, startpos) Tercio@23: numargs = numargs or 1 Tercio@23: startpos = max(startpos or 1, 1) Tercio@23: Tercio@23: local pos=startpos Tercio@23: Tercio@23: -- find start of new arg Tercio@23: pos = strfind(str, "[^ ]", pos) Tercio@23: if not pos then -- whoops, end of string Tercio@23: return nils(numargs, 1e9) Tercio@23: end Tercio@23: Tercio@23: if numargs<1 then Tercio@23: return pos Tercio@23: end Tercio@23: Tercio@23: -- quoted or space separated? find out which pattern to use Tercio@23: local delim_or_pipe Tercio@23: local ch = strsub(str, pos, pos) Tercio@23: if ch=='"' then Tercio@23: pos = pos + 1 Tercio@23: delim_or_pipe='([|"])' Tercio@23: elseif ch=="'" then Tercio@23: pos = pos + 1 Tercio@23: delim_or_pipe="([|'])" Tercio@23: else Tercio@23: delim_or_pipe="([| ])" Tercio@23: end Tercio@23: Tercio@23: startpos = pos Tercio@23: Tercio@23: while true do Tercio@23: -- find delimiter or hyperlink Tercio@23: local ch,_ Tercio@23: pos,_,ch = strfind(str, delim_or_pipe, pos) Tercio@23: Tercio@23: if not pos then break end Tercio@23: Tercio@23: if ch=="|" then Tercio@23: -- some kind of escape Tercio@23: Tercio@23: if strsub(str,pos,pos+1)=="|H" then Tercio@23: -- It's a |H....|hhyper link!|h Tercio@23: pos=strfind(str, "|h", pos+2) -- first |h Tercio@23: if not pos then break end Tercio@23: Tercio@23: pos=strfind(str, "|h", pos+2) -- second |h Tercio@23: if not pos then break end Tercio@23: elseif strsub(str,pos, pos+1) == "|T" then Tercio@23: -- It's a |T....|t texture Tercio@23: pos=strfind(str, "|t", pos+2) Tercio@23: if not pos then break end Tercio@23: end Tercio@23: Tercio@23: pos=pos+2 -- skip past this escape (last |h if it was a hyperlink) Tercio@23: Tercio@23: else Tercio@23: -- found delimiter, done with this arg Tercio@23: return strsub(str, startpos, pos-1), AceConsole:GetArgs(str, numargs-1, pos+1) Tercio@23: end Tercio@23: Tercio@23: end Tercio@23: Tercio@23: -- search aborted, we hit end of string. return it all as one argument. (yes, even if it's an unterminated quote or hyperlink) Tercio@23: return strsub(str, startpos), nils(numargs-1, 1e9) Tercio@23: end Tercio@23: Tercio@23: Tercio@23: --- embedding and embed handling Tercio@23: Tercio@23: local mixins = { Tercio@23: "Print", Tercio@23: "Printf", Tercio@23: "RegisterChatCommand", Tercio@23: "UnregisterChatCommand", Tercio@23: "GetArgs", Tercio@23: } Tercio@23: Tercio@23: -- Embeds AceConsole into the target object making the functions from the mixins list available on target:.. Tercio@23: -- @param target target object to embed AceBucket in Tercio@23: function AceConsole:Embed( target ) Tercio@23: for k, v in pairs( mixins ) do Tercio@23: target[v] = self[v] Tercio@23: end Tercio@23: self.embeds[target] = true Tercio@23: return target Tercio@23: end Tercio@23: Tercio@23: function AceConsole:OnEmbedEnable( target ) Tercio@23: if AceConsole.weakcommands[target] then Tercio@23: for command, func in pairs( AceConsole.weakcommands[target] ) do Tercio@23: target:RegisterChatCommand( command, func, false, true ) -- nonpersisting and silent registry Tercio@23: end Tercio@23: end Tercio@23: end Tercio@23: Tercio@23: function AceConsole:OnEmbedDisable( target ) Tercio@23: if AceConsole.weakcommands[target] then Tercio@23: for command, func in pairs( AceConsole.weakcommands[target] ) do Tercio@23: target:UnregisterChatCommand( command ) -- TODO: this could potentially unregister a command from another application in case of command conflicts. Do we care? Tercio@23: end Tercio@23: end Tercio@23: end Tercio@23: Tercio@23: for addon in pairs(AceConsole.embeds) do Tercio@23: AceConsole:Embed(addon) Tercio@23: end