-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConsole.lua
69 lines (56 loc) · 1.7 KB
/
Console.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
local _, Oken = ...
local Console = Oken:RegisterModule("Console")
local AceConfigDialog = LibStub("AceConfigDialog-3.0")
Console.commands = {}
function Console:OnEnable()
self:RegisterChatCommand("oken", "OnSlash")
end
function Console:PrintAvailableCommands()
local cmds = {}
for cmd, handler in pairs(self.commands) do
cmds[#cmds + 1] = cmd
end
self:Printf("Available commands: %s", table.concat(cmds, ", "))
end
function Console:OnSlash(cmd)
-- Extract subcmd from cmd line
local subcmd, n = self:GetArgs(cmd)
if not subcmd then
AceConfigDialog:Open("Oken Core")
--self:Print("Usage: /oken <cmd> <args>")
--self:PrintAvailableCommands()
return
end
-- Change subcmd to lowercase and fetch handler function
subcmd = subcmd:lower()
local handler = self.commands[subcmd]
-- Check that the command is defined
if not handler then
self:Printf("Undefined command '%s'.", subcmd)
self:PrintAvailableCommands()
return
end
-- Invoke the handler
handler(self:GetArgs(cmd, 10, n))
end
function Console:RegisterCommand(cmd, handler, method)
-- Default handler method
if not method then method = "OnSlash" end
-- Check if the command is not already registered
if self.commands[cmd] then
self:Printf("Unable to register chat command '%s'. This name is already taken.", cmd)
return
end
-- Check that the receiver object has the requested handler
if not handler or not handler[method] then
self:Printf("Unable to register chat command '%s'. The given command handler doesn't define the :%s() method.", cmd, method)
return
end
-- Wrapper function
self.commands[cmd] = function(...)
handler[method](handler, ...)
end
end
function Console:UnregisterCommand(cmd)
self.commands[cmd] = nil
end