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