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