Skip to content

Commit 21e6f43

Browse files
authored
Clean up console core (#3592)
* Cleanup console.lua code + small fixes * Push tokenized commands to hook * Change tokenized commands to hash table + clean up tokenization function * Switch back to sequential + small optimization * What is this symbol for? * Change text a bit
1 parent 871aeda commit 21e6f43

1 file changed

Lines changed: 41 additions & 32 deletions

File tree

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,44 @@
1-
/******************************************************************************\
2-
Console support
3-
\******************************************************************************/
1+
--
2+
-- Console support
3+
--
44

55
E2Lib.RegisterExtension("console", true, "Lets E2 chips run concommands and retrieve convars")
66

7-
local function tokenizeAndGetCommands(str)
7+
local function tokenizeAndGetCommands(concmd)
88
-- Tokenize!
99
local tokens = {}
1010
local curtoken = {}
1111
local escaped = false
12-
for i=1, #str do
13-
local char = string.sub(str, i, i)
12+
13+
for i = 1, #concmd do
14+
local char = string.sub(concmd, i, i)
15+
1416
if (escaped and char ~= "\"") or string.match(char, "[%w+-]") then
15-
curtoken[#curtoken + 1] = char
17+
table.insert(curtoken, char)
1618
else
17-
if #curtoken>0 then tokens[#tokens + 1] = table.concat(curtoken) curtoken = {} end
19+
if #curtoken > 0 then
20+
table.insert(tokens, table.concat(curtoken))
21+
curtoken = {}
22+
end
23+
1824
if char == "\"" then
1925
escaped = not escaped
2026
elseif char ~= " " then
21-
tokens[#tokens + 1] = char
27+
table.insert(tokens, char)
2228
end
2329
end
2430
end
25-
if #curtoken>0 then tokens[#tokens+1] = table.concat(curtoken) end
31+
32+
if #curtoken > 0 then
33+
table.insert(tokens, table.concat(curtoken))
34+
end
2635

2736
-- Get table of commands used
2837
local commands = {tokens[1] or ""}
29-
for i=1, #tokens do
38+
39+
for i = 1, #tokens do
3040
if tokens[i]==";" then
31-
commands[#commands + 1] = tokens[i+1] or ""
41+
table.insert(commands, tokens[i + 1])
3242
end
3343
end
3444

@@ -38,17 +48,15 @@ end
3848
---@param cvar "wire_expression2_concmd_whitelist"|"wire_expression2_convar_whitelist"
3949
---@return table whitelist # Whitelist for specific commands, if empty, disregard whitelist and allow everything
4050
local function getWhitelist(ply, cvar)
41-
local whitelist = (ply:GetInfo(cvar) or ""):Trim()
51+
local whitelist = {}
4252

43-
local whitelistTbl = {}
44-
45-
for k, v in pairs(string.Split(whitelist, ",")) do
46-
if v~="" then
47-
whitelistTbl[v] = true
53+
for _, v in ipairs(string.Split(string.Trim(ply:GetInfo(cvar)), ",")) do
54+
if v ~= "" then
55+
whitelist[v] = true
4856
end
4957
end
5058

51-
return whitelistTbl
59+
return whitelist
5260
end
5361

5462
local function checkConCmd(self, cmd)
@@ -61,17 +69,18 @@ local function checkConCmd(self, cmd)
6169
if ply:GetInfoNum("wire_expression2_concmd", 0) == 0 then return self:throw("Concmd is disabled through wire_expression2_concmd", false) end
6270
if IsConCommandBlocked(cmd) then return self:throw("This concmd is blacklisted by gmod, see https://wiki.facepunch.com/gmod/Blocked_ConCommands", false) end
6371

64-
if hook.Run( "Expression2_CanConCmd", ply, cmd ) == false then
72+
if hook.Run("Expression2_CanConCmd", ply, cmd) == false then
6573
return self:throw("Command '" .. cmd .. "' was blocked by the server. ", false)
6674
end
6775

6876
local whitelist = getWhitelist(ply, "wire_expression2_concmd_whitelist")
6977
if table.IsEmpty(whitelist) then return true end
7078

7179
local commands = tokenizeAndGetCommands(cmd)
72-
for _, command in pairs(commands) do
80+
81+
for _, command in ipairs(commands) do
7382
if not whitelist[command] then
74-
return self:throw("Command '" .. command .. "' is not whitelisted w/ wire_expression2_concmd_whitelist", false)
83+
return self:throw("Command '" .. command .. "' is not whitelisted in wire_expression2_concmd_whitelist", false)
7584
end
7685
end
7786

@@ -86,19 +95,20 @@ local function checkConVar(self, var)
8695
if ply:GetInfoNum("wire_expression2_convar", 0) == 0 then return self:throw("Convar is disabled through wire_expression2_convar", false) end
8796
var = var:match("%s*([%w_]+)%s*")
8897

98+
if hook.Run("Expression2_CanConVar", ply, var) == false then
99+
return self:throw("Convar '" .. var .. "' was blocked by the server. ", false)
100+
end
101+
89102
local whitelist = getWhitelist(ply, "wire_expression2_convar_whitelist")
90103
if table.IsEmpty(whitelist) then return true end
91104

92-
if whitelist[var] == nil then return self:throw("Convar '" .. var .. "' is not whitelisted w/ wire_expression2_convar_whitelist ", false) end
93-
94-
if hook.Run("Expression2_CanConVar", ply, var) == false then
95-
return self:throw("Convar '" .. var .. "' was blocked by the server. ", false)
96-
end
105+
if whitelist[var] == nil then
106+
return self:throw("Convar '" .. var .. "' is not whitelisted in wire_expression2_convar_whitelist ", false)
107+
end
97108

98109
return true
99110
end
100111

101-
102112
__e2setcost(5)
103113

104114
e2function number concmd(string command)
@@ -118,11 +128,10 @@ e2function number convarnum(string cvar)
118128
end
119129

120130
e2function number maxOfType(string typename)
121-
if typename == "wire_holograms" then return GetConVarNumber("wire_holograms_max") or 0 end
122-
return GetConVarNumber("sbox_max"..typename) or 0
131+
if typename == "wire_holograms" then return cvars.Number("wire_holograms_max", 0) end
132+
return cvars.Number("sbox_max" .. typename, 0)
123133
end
124134

125135
e2function number playerDamage()
126-
local ret = GetConVarNumber("sbox_playershurtplayers") or 0
127-
return ret ~= 0 and 1 or 0
136+
return cvars.Bool("sbox_playershurtplayers") and 1 or 0
128137
end

0 commit comments

Comments
 (0)