From 0d05b893fa769588aa3d75e481406cbb3297c0a8 Mon Sep 17 00:00:00 2001 From: Astralcircle <142503363+Astralcircle@users.noreply.github.com> Date: Mon, 20 Apr 2026 19:43:19 +0300 Subject: [PATCH 1/6] Cleanup console.lua code + small fixes --- .../gmod_wire_expression2/core/console.lua | 42 +++++++++---------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/core/console.lua b/lua/entities/gmod_wire_expression2/core/console.lua index 1bb7234e17..cf4e3c1c4f 100644 --- a/lua/entities/gmod_wire_expression2/core/console.lua +++ b/lua/entities/gmod_wire_expression2/core/console.lua @@ -1,6 +1,6 @@ -/******************************************************************************\ - Console support -\******************************************************************************/ +-- +-- Console support +-- E2Lib.RegisterExtension("console", true, "Lets E2 chips run concommands and retrieve convars") @@ -38,17 +38,15 @@ end ---@param cvar "wire_expression2_concmd_whitelist"|"wire_expression2_convar_whitelist" ---@return table whitelist # Whitelist for specific commands, if empty, disregard whitelist and allow everything local function getWhitelist(ply, cvar) - local whitelist = (ply:GetInfo(cvar) or ""):Trim() + local whitelist = {} - local whitelistTbl = {} - - for k, v in pairs(string.Split(whitelist, ",")) do - if v~="" then - whitelistTbl[v] = true + for _, v in ipairs(string.Split(string.Trim(ply:GetInfo(cvar)), ",")) do + if v ~= "" then + whitelist[v] = true end end - return whitelistTbl + return whitelist end local function checkConCmd(self, cmd) @@ -61,7 +59,7 @@ local function checkConCmd(self, cmd) if ply:GetInfoNum("wire_expression2_concmd", 0) == 0 then return self:throw("Concmd is disabled through wire_expression2_concmd", false) end if IsConCommandBlocked(cmd) then return self:throw("This concmd is blacklisted by gmod, see https://wiki.facepunch.com/gmod/Blocked_ConCommands", false) end - if hook.Run( "Expression2_CanConCmd", ply, cmd ) == false then + if hook.Run("Expression2_CanConCmd", ply, cmd) == false then return self:throw("Command '" .. cmd .. "' was blocked by the server. ", false) end @@ -69,6 +67,7 @@ local function checkConCmd(self, cmd) if table.IsEmpty(whitelist) then return true end local commands = tokenizeAndGetCommands(cmd) + for _, command in pairs(commands) do if not whitelist[command] then return self:throw("Command '" .. command .. "' is not whitelisted w/ wire_expression2_concmd_whitelist", false) @@ -86,19 +85,19 @@ local function checkConVar(self, var) if ply:GetInfoNum("wire_expression2_convar", 0) == 0 then return self:throw("Convar is disabled through wire_expression2_convar", false) end var = var:match("%s*([%w_]+)%s*") - local whitelist = getWhitelist(ply, "wire_expression2_convar_whitelist") - if table.IsEmpty(whitelist) then return true end - - if whitelist[var] == nil then return self:throw("Convar '" .. var .. "' is not whitelisted w/ wire_expression2_convar_whitelist ", false) end - - if hook.Run("Expression2_CanConVar", ply, var) == false then + if hook.Run("Expression2_CanConVar", ply, var) == false then return self:throw("Convar '" .. var .. "' was blocked by the server. ", false) end + local whitelist = getWhitelist(ply, "wire_expression2_convar_whitelist") + + if whitelist[var] == nil then + return self:throw("Convar '" .. var .. "' is not whitelisted w/ wire_expression2_convar_whitelist ", false) + end + return true end - __e2setcost(5) e2function number concmd(string command) @@ -118,11 +117,10 @@ e2function number convarnum(string cvar) end e2function number maxOfType(string typename) - if typename == "wire_holograms" then return GetConVarNumber("wire_holograms_max") or 0 end - return GetConVarNumber("sbox_max"..typename) or 0 + if typename == "wire_holograms" then return cvars.Number("wire_holograms_max", 0) end + return cvars.Number("sbox_max" .. typename, 0) end e2function number playerDamage() - local ret = GetConVarNumber("sbox_playershurtplayers") or 0 - return ret ~= 0 and 1 or 0 + return cvars.Bool("sbox_playershurtplayers") and 1 or 0 end From 22de9ba7fd0eacfaac9e23205b75bd24d6399855 Mon Sep 17 00:00:00 2001 From: Astralcircle <142503363+Astralcircle@users.noreply.github.com> Date: Mon, 20 Apr 2026 19:46:17 +0300 Subject: [PATCH 2/6] Push tokenized commands to hook --- lua/entities/gmod_wire_expression2/core/console.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/core/console.lua b/lua/entities/gmod_wire_expression2/core/console.lua index cf4e3c1c4f..5b902816bf 100644 --- a/lua/entities/gmod_wire_expression2/core/console.lua +++ b/lua/entities/gmod_wire_expression2/core/console.lua @@ -59,15 +59,15 @@ local function checkConCmd(self, cmd) if ply:GetInfoNum("wire_expression2_concmd", 0) == 0 then return self:throw("Concmd is disabled through wire_expression2_concmd", false) end if IsConCommandBlocked(cmd) then return self:throw("This concmd is blacklisted by gmod, see https://wiki.facepunch.com/gmod/Blocked_ConCommands", false) end - if hook.Run("Expression2_CanConCmd", ply, cmd) == false then + local commands = tokenizeAndGetCommands(cmd) + + if hook.Run("Expression2_CanConCmd", ply, cmd, commands) == false then return self:throw("Command '" .. cmd .. "' was blocked by the server. ", false) end local whitelist = getWhitelist(ply, "wire_expression2_concmd_whitelist") if table.IsEmpty(whitelist) then return true end - local commands = tokenizeAndGetCommands(cmd) - for _, command in pairs(commands) do if not whitelist[command] then return self:throw("Command '" .. command .. "' is not whitelisted w/ wire_expression2_concmd_whitelist", false) From 1b113cc2c635a3f721c073a0e6e11a78bf410e7e Mon Sep 17 00:00:00 2001 From: Astralcircle <142503363+Astralcircle@users.noreply.github.com> Date: Mon, 20 Apr 2026 20:01:28 +0300 Subject: [PATCH 3/6] Change tokenized commands to hash table + clean up tokenization function --- .../gmod_wire_expression2/core/console.lua | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/core/console.lua b/lua/entities/gmod_wire_expression2/core/console.lua index 5b902816bf..8aae25baed 100644 --- a/lua/entities/gmod_wire_expression2/core/console.lua +++ b/lua/entities/gmod_wire_expression2/core/console.lua @@ -4,31 +4,41 @@ E2Lib.RegisterExtension("console", true, "Lets E2 chips run concommands and retrieve convars") -local function tokenizeAndGetCommands(str) +local function tokenizeAndGetCommands(concmd) -- Tokenize! local tokens = {} local curtoken = {} local escaped = false - for i=1, #str do - local char = string.sub(str, i, i) + + for i = 1, #concmd do + local char = string.sub(concmd, i, i) + if (escaped and char ~= "\"") or string.match(char, "[%w+-]") then - curtoken[#curtoken + 1] = char + table.insert(curtoken, char) else - if #curtoken>0 then tokens[#tokens + 1] = table.concat(curtoken) curtoken = {} end + if #curtoken > 0 then + table.insert(tokens, table.concat(curtoken)) + curtoken = {} + end + if char == "\"" then escaped = not escaped elseif char ~= " " then - tokens[#tokens + 1] = char + table.insert(tokens, char) end end end - if #curtoken>0 then tokens[#tokens+1] = table.concat(curtoken) end + + if #curtoken > 0 then + table.insert(tokens, table.concat(curtoken)) + end -- Get table of commands used - local commands = {tokens[1] or ""} - for i=1, #tokens do + local commands = {[tokens[1] or ""] = true} + + for i = 1, #tokens do if tokens[i]==";" then - commands[#commands + 1] = tokens[i+1] or "" + commands[tokens[i +1 ] or ""] = true end end @@ -59,6 +69,7 @@ local function checkConCmd(self, cmd) if ply:GetInfoNum("wire_expression2_concmd", 0) == 0 then return self:throw("Concmd is disabled through wire_expression2_concmd", false) end if IsConCommandBlocked(cmd) then return self:throw("This concmd is blacklisted by gmod, see https://wiki.facepunch.com/gmod/Blocked_ConCommands", false) end + -- Hash table (command = true) local commands = tokenizeAndGetCommands(cmd) if hook.Run("Expression2_CanConCmd", ply, cmd, commands) == false then @@ -68,7 +79,7 @@ local function checkConCmd(self, cmd) local whitelist = getWhitelist(ply, "wire_expression2_concmd_whitelist") if table.IsEmpty(whitelist) then return true end - for _, command in pairs(commands) do + for command in pairs(commands) do if not whitelist[command] then return self:throw("Command '" .. command .. "' is not whitelisted w/ wire_expression2_concmd_whitelist", false) end @@ -90,6 +101,7 @@ local function checkConVar(self, var) end local whitelist = getWhitelist(ply, "wire_expression2_convar_whitelist") + if table.IsEmpty(whitelist) then return true end if whitelist[var] == nil then return self:throw("Convar '" .. var .. "' is not whitelisted w/ wire_expression2_convar_whitelist ", false) From e0cfd087a4ce396cfebfde88f0a7cfd73a15f5ea Mon Sep 17 00:00:00 2001 From: Astralcircle <142503363+Astralcircle@users.noreply.github.com> Date: Mon, 20 Apr 2026 20:19:31 +0300 Subject: [PATCH 4/6] Switch back to sequential + small optimization --- lua/entities/gmod_wire_expression2/core/console.lua | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/core/console.lua b/lua/entities/gmod_wire_expression2/core/console.lua index 8aae25baed..3058b951f9 100644 --- a/lua/entities/gmod_wire_expression2/core/console.lua +++ b/lua/entities/gmod_wire_expression2/core/console.lua @@ -34,11 +34,11 @@ local function tokenizeAndGetCommands(concmd) end -- Get table of commands used - local commands = {[tokens[1] or ""] = true} + local commands = {tokens[1] or ""} for i = 1, #tokens do if tokens[i]==";" then - commands[tokens[i +1 ] or ""] = true + table.insert(commands, tokens[i + 1]) end end @@ -69,17 +69,16 @@ local function checkConCmd(self, cmd) if ply:GetInfoNum("wire_expression2_concmd", 0) == 0 then return self:throw("Concmd is disabled through wire_expression2_concmd", false) end if IsConCommandBlocked(cmd) then return self:throw("This concmd is blacklisted by gmod, see https://wiki.facepunch.com/gmod/Blocked_ConCommands", false) end - -- Hash table (command = true) - local commands = tokenizeAndGetCommands(cmd) - - if hook.Run("Expression2_CanConCmd", ply, cmd, commands) == false then + if hook.Run("Expression2_CanConCmd", ply, cmd) == false then return self:throw("Command '" .. cmd .. "' was blocked by the server. ", false) end local whitelist = getWhitelist(ply, "wire_expression2_concmd_whitelist") if table.IsEmpty(whitelist) then return true end - for command in pairs(commands) do + local commands = tokenizeAndGetCommands(cmd) + + for _, command in ipairs(commands) do if not whitelist[command] then return self:throw("Command '" .. command .. "' is not whitelisted w/ wire_expression2_concmd_whitelist", false) end From ed696e508cff0b7ec358175eaa1756d0732dabd4 Mon Sep 17 00:00:00 2001 From: Astralcircle <142503363+Astralcircle@users.noreply.github.com> Date: Mon, 20 Apr 2026 20:22:56 +0300 Subject: [PATCH 5/6] What is this symbol for? --- lua/entities/gmod_wire_expression2/core/console.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/core/console.lua b/lua/entities/gmod_wire_expression2/core/console.lua index 3058b951f9..45b1b6b39e 100644 --- a/lua/entities/gmod_wire_expression2/core/console.lua +++ b/lua/entities/gmod_wire_expression2/core/console.lua @@ -80,7 +80,7 @@ local function checkConCmd(self, cmd) for _, command in ipairs(commands) do if not whitelist[command] then - return self:throw("Command '" .. command .. "' is not whitelisted w/ wire_expression2_concmd_whitelist", false) + return self:throw("Command '" .. command .. "' is not whitelisted wire_expression2_concmd_whitelist", false) end end @@ -103,7 +103,7 @@ local function checkConVar(self, var) if table.IsEmpty(whitelist) then return true end if whitelist[var] == nil then - return self:throw("Convar '" .. var .. "' is not whitelisted w/ wire_expression2_convar_whitelist ", false) + return self:throw("Convar '" .. var .. "' is not whitelisted wire_expression2_convar_whitelist ", false) end return true From 60a0f6952168199bc6ce68dee5b7f12afcbc15f6 Mon Sep 17 00:00:00 2001 From: Astralcircle <142503363+Astralcircle@users.noreply.github.com> Date: Mon, 20 Apr 2026 20:23:31 +0300 Subject: [PATCH 6/6] Change text a bit --- lua/entities/gmod_wire_expression2/core/console.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/core/console.lua b/lua/entities/gmod_wire_expression2/core/console.lua index 45b1b6b39e..c43a2250be 100644 --- a/lua/entities/gmod_wire_expression2/core/console.lua +++ b/lua/entities/gmod_wire_expression2/core/console.lua @@ -80,7 +80,7 @@ local function checkConCmd(self, cmd) for _, command in ipairs(commands) do if not whitelist[command] then - return self:throw("Command '" .. command .. "' is not whitelisted wire_expression2_concmd_whitelist", false) + return self:throw("Command '" .. command .. "' is not whitelisted in wire_expression2_concmd_whitelist", false) end end @@ -103,7 +103,7 @@ local function checkConVar(self, var) if table.IsEmpty(whitelist) then return true end if whitelist[var] == nil then - return self:throw("Convar '" .. var .. "' is not whitelisted wire_expression2_convar_whitelist ", false) + return self:throw("Convar '" .. var .. "' is not whitelisted in wire_expression2_convar_whitelist ", false) end return true