forked from Farbod678/kdm-tts
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Console.ttslua
117 lines (91 loc) · 3.24 KB
/
Console.ttslua
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
local Check = require("Kdm/Util/Check")
local EventManager = require("Kdm/Util/EventManager")
local Util = require("Kdm/Util/Util")
---------------------------------------------------------------------------------------------------
local Console = {}
---------------------------------------------------------------------------------------------------
function Console.Init()
Console.commands = {}
EventManager.AddHandler("onChat", function(message, player, previousReturnValue)
if message == "" or message:sub(1, 1) != ">" then
return (previousReturnValue == nil) or previousReturnValue
end
Console.Printf(message)
local args = Console.Tokenize(message)
local command = Console.commands[args[1]:lower()]
if not command then
Console.Printf("Unrecognized command: %s", args[1])
return false
end
command.func(args)
return false
end)
Console.AddCommand("help", function(args)
for name, command in pairs(Console.commands) do
Console.Printf("%s %s", name, command.description)
end
end, "Shows all commands")
end
---------------------------------------------------------------------------------------------------
function Console.Printf(fmt, ...)
print(Util.SafeFormat("[aaaaaa]"..fmt, ...))
end
---------------------------------------------------------------------------------------------------
function Console.AddCommand(name, commandFunc, description)
name = name:lower()
assert(Check(not Console.commands[name], "Command %s already registered", name))
Console.commands[name] = {
func = commandFunc,
description = description
}
end
---------------------------------------------------------------------------------------------------
function Console.Tokenize(message)
local tokens = {}
local dq = 34 -- ascii double quote "
local sq = 39 -- ascii single quote '
local bytes = table.pack(string.byte(message, 1, message:len()))
local i = 2 -- skip leading console char
while true do
local start = i
-- skip whitespace
while i <= #bytes and bytes[i] <= 32 do
i = i + 1
end
if i > #bytes then
break
end
local tokenStart = i
-- check for leading quote (we ignore quotes mid-token)
local quote = nil
local b = bytes[i]
if b == dq or b == sq then
quote = b
i = i + 1
tokenStart = i
end
-- find end of word
if quote then
while i <= #bytes and bytes[i] ~= quote do
i = i + 1
end
if bytes[i] != quote then
log:Errorf("String missing "..string.char(quote).." quote ")
return
end
else
while i <= #bytes and bytes[i] > 32 do
i = i + 1
end
end
table.insert(tokens, message:sub(tokenStart, i - 1):lower())
i = i + 1
end
return tokens
end
---------------------------------------------------------------------------------------------------
return {
Init = Console.Init,
Printf = Console.Printf,
AddCommand = Console.AddCommand,
}