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