From 126c43bd6d8eddefb344b17d493808889d11e480 Mon Sep 17 00:00:00 2001 From: Vurv <56230599+Vurv78@users.noreply.github.com> Date: Mon, 16 Oct 2023 17:53:57 -0700 Subject: [PATCH] Remove wire_expression2_debug, type check field from registerType --- .../gmod_wire_expression2/core/angle.lua | 5 +- .../gmod_wire_expression2/core/array.lua | 4 +- .../gmod_wire_expression2/core/bone.lua | 6 +- .../gmod_wire_expression2/core/complex.lua | 5 +- .../core/custom/effects.lua | 6 +- .../gmod_wire_expression2/core/damage.lua | 6 +- .../gmod_wire_expression2/core/e2lib.lua | 46 ---- .../gmod_wire_expression2/core/entity.lua | 6 +- .../gmod_wire_expression2/core/globalvars.lua | 4 +- .../gmod_wire_expression2/core/init.lua | 80 +----- .../gmod_wire_expression2/core/matrix.lua | 15 +- .../gmod_wire_expression2/core/number.lua | 6 +- .../gmod_wire_expression2/core/player.lua | 15 +- .../gmod_wire_expression2/core/quaternion.lua | 5 +- .../gmod_wire_expression2/core/ranger.lua | 5 +- .../core/serialization.lua | 239 +----------------- .../gmod_wire_expression2/core/string.lua | 4 +- .../gmod_wire_expression2/core/table.lua | 4 +- .../gmod_wire_expression2/core/vector.lua | 5 +- .../gmod_wire_expression2/core/vector2.lua | 10 +- .../gmod_wire_expression2/core/wirelink.lua | 6 +- lua/wire/client/e2helper.lua | 3 +- 22 files changed, 41 insertions(+), 444 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/core/angle.lua b/lua/entities/gmod_wire_expression2/core/angle.lua index 0e5147431f..53f2aa6aeb 100644 --- a/lua/entities/gmod_wire_expression2/core/angle.lua +++ b/lua/entities/gmod_wire_expression2/core/angle.lua @@ -5,10 +5,7 @@ Angle support registerType("angle", "a", Angle(0, 0, 0), nil, function(self, output) return Angle(output) end, - function(retval) - if isangle(retval) then return end - error("Return value is not an Angle, but a "..type(retval).."!", 0) - end, + nil, function(v) return not isangle(v) end diff --git a/lua/entities/gmod_wire_expression2/core/array.lua b/lua/entities/gmod_wire_expression2/core/array.lua index 8599959a8c..999575dfd0 100644 --- a/lua/entities/gmod_wire_expression2/core/array.lua +++ b/lua/entities/gmod_wire_expression2/core/array.lua @@ -34,9 +34,7 @@ registerType("array", "r", {}, return ret end, nil, - function(retval) - if !istable(retval) then error("Return value is not a table, but a "..type(retval).."!",0) end - end, + nil, function(v) return !istable(v) end diff --git a/lua/entities/gmod_wire_expression2/core/bone.lua b/lua/entities/gmod_wire_expression2/core/bone.lua index 5906167b6f..92f0e068ff 100644 --- a/lua/entities/gmod_wire_expression2/core/bone.lua +++ b/lua/entities/gmod_wire_expression2/core/bone.lua @@ -79,11 +79,7 @@ E2Lib.isValidBone = isValidBone registerType("bone", "b", nil, nil, nil, - function(retval) - if retval == nil then return end - if type(retval) ~= "PhysObj" then error("Return value is neither nil nor a PhysObj, but a "..type(retval).."!",0) end - if not bone2entity[retval] then error("Return value is not a registered bone!",0) end - end, + nil, function(b) return not isValidBone(b) end diff --git a/lua/entities/gmod_wire_expression2/core/complex.lua b/lua/entities/gmod_wire_expression2/core/complex.lua index 87de8cb4af..4f38ebea92 100644 --- a/lua/entities/gmod_wire_expression2/core/complex.lua +++ b/lua/entities/gmod_wire_expression2/core/complex.lua @@ -44,10 +44,7 @@ __e2setcost(2) registerType("complex", "c", { 0, 0 }, function(self, input) return { input[1], input[2] } end, nil, - function(retval) - if !istable(retval) then error("Return value is not a table, but a "..type(retval).."!",0) end - if #retval ~= 2 then error("Return value does not have exactly 2 entries!",0) end - end, + nil, function(v) return !istable(v) or #v ~= 2 end diff --git a/lua/entities/gmod_wire_expression2/core/custom/effects.lua b/lua/entities/gmod_wire_expression2/core/custom/effects.lua index e5d5c29e60..672750463c 100644 --- a/lua/entities/gmod_wire_expression2/core/custom/effects.lua +++ b/lua/entities/gmod_wire_expression2/core/custom/effects.lua @@ -38,11 +38,7 @@ end registerType("effect", "xef", nil, nil, nil, - function(retval) - if retval == nil then return end - local _type = type(retval) - if _type~="CEffectData" then error("Return value is neither nil nor a CEffectData, but a "..type(retval).."!",0) end - end, + nil, function(v) return type(v)~="CEffectData" end diff --git a/lua/entities/gmod_wire_expression2/core/damage.lua b/lua/entities/gmod_wire_expression2/core/damage.lua index cb60455d29..18753b6721 100644 --- a/lua/entities/gmod_wire_expression2/core/damage.lua +++ b/lua/entities/gmod_wire_expression2/core/damage.lua @@ -4,11 +4,7 @@ local M_CTakeDamageInfo = FindMetaTable("CTakeDamageInfo") registerType("damage", "xdm", nil, nil, nil, - function(retval) - if retval == nil then return end - if not istable(retval) then error("Return value is neither nil nor a table, but a " .. type(retval) .. "!",0) end - if getmetatable(retval) ~= M_CTakeDamageInfo then error("Return value is not a CTakeDamageInfo!", 0) end - end, + nil, function(v) return not istable(v) or getmetatable(v) ~= M_CTakeDamageInfo end diff --git a/lua/entities/gmod_wire_expression2/core/e2lib.lua b/lua/entities/gmod_wire_expression2/core/e2lib.lua index 094b88e81a..64864fbc50 100644 --- a/lua/entities/gmod_wire_expression2/core/e2lib.lua +++ b/lua/entities/gmod_wire_expression2/core/e2lib.lua @@ -252,52 +252,6 @@ function E2Lib.canModifyPlayer(self, ply) return isOwner(self, vehicle) end --- ------------------------ type guessing ------------------------------------------ - -local type_lookup = { - number = "n", - string = "s", - Vector = "v", - PhysObj = "b", -} -local table_length_lookup = { - [2] = "xv2", - [3] = "v", - [4] = "xv4", - [9] = "m", - [16] = "xm4", -} - -function E2Lib.guess_type(value) - local vtype = type(value) - if type_lookup[vtype] then return type_lookup[vtype] end - if IsValid(value) then return "e" end - if value.EntIndex then return "e" end - if vtype == "table" then - if table_length_lookup[#value] then return table_length_lookup[#value] end - if value.HitPos then return "xrd" end - end - - for typeid, v in pairs(wire_expression_types2) do - if v[5] then - local ok = pcall(v[5], value) - if ok then return typeid end - end - end - - -- TODO: more type guessing here - - return "" -- empty string = unknown type, for now. -end - --- Types that cannot possibly be guessed correctly: --- angle (will be reported as vector) --- matrix2 (will be reported as vector4) --- wirelink (will be reported as entity) --- complex (will be reported as vector2) --- quaternion (will be reported as vector4) --- all kinds of nil stuff - -- ------------------------ list filtering ------------------------------------------------- function E2Lib.filterList(list, criterion) diff --git a/lua/entities/gmod_wire_expression2/core/entity.lua b/lua/entities/gmod_wire_expression2/core/entity.lua index 08a9df92c1..108d154085 100644 --- a/lua/entities/gmod_wire_expression2/core/entity.lua +++ b/lua/entities/gmod_wire_expression2/core/entity.lua @@ -1,11 +1,7 @@ registerType("entity", "e", nil, nil, function(self,output) return output or NULL end, - function(retval) - if IsValid(retval) then return end - if retval == nil then return end - if not retval.EntIndex then error("Return value is neither nil nor an Entity, but a "..type(retval).."!",0) end - end, + nil, function(v) return not isentity(v) end diff --git a/lua/entities/gmod_wire_expression2/core/globalvars.lua b/lua/entities/gmod_wire_expression2/core/globalvars.lua index 940ffd3d17..c9435a759a 100644 --- a/lua/entities/gmod_wire_expression2/core/globalvars.lua +++ b/lua/entities/gmod_wire_expression2/core/globalvars.lua @@ -22,9 +22,7 @@ end) registerType( "gtable", "xgt", {}, function(self) self.entity:Error("You may not input a gtable.") end, function(self) self.entity:Error("You may not output a gtable.") end, - function(retval) - if !istable(retval) then error("Return value is not a gtable, but a "..type(retval).."!",0) end - end, + nil, function(v) return !istable(v) end diff --git a/lua/entities/gmod_wire_expression2/core/init.lua b/lua/entities/gmod_wire_expression2/core/init.lua index d4811235ec..21038fe182 100644 --- a/lua/entities/gmod_wire_expression2/core/init.lua +++ b/lua/entities/gmod_wire_expression2/core/init.lua @@ -5,71 +5,6 @@ AddCSLuaFile() Andreas "Syranide" Svensson, me@syranide.com ]] --- functions to type-check function return values. - -local wire_expression2_debug = CreateConVar("wire_expression2_debug", 0, 0) - -if SERVER then - cvars.AddChangeCallback("wire_expression2_debug", function(CVar, PreviousValue, NewValue) - if PreviousValue == NewValue then return end - wire_expression2_reload() - end) -end - ---- This function ensures that the given function shows up by the given name in stack traces. ---- It does so by eval'ing a generated block of code which invokes the actual function. ---- Tail recursion optimization is specifically avoided by introducing a local variable in the generated code block. -local function namefunc(func, name) - -- Filter the name - name = name:gsub("[^A-Za-z_0-9]", "_") - - -- RunString doesn't have a return value, so we need to go via a global variable - wire_expression2_namefunc = func - RunString(([[ - local %s = wire_expression2_namefunc - function wire_expression2_namefunc(...) - local ret = %s(...) - return ret - end - ]]):format(name, name)) - local ret = wire_expression2_namefunc - wire_expression2_namefunc = nil - - -- Now ret contains the wrapped function and we can just return it. - return ret -end - --- Installs a typecheck in a function identified by the given signature. -local function makecheck(signature) - if signature == "op:seq()" then return end - local name = signature:match("^([^(]*)") - local entry = wire_expression2_funcs[signature] - local oldfunc, signature, rets, func = entry.oldfunc, unpack(entry) - - if oldfunc then return end - oldfunc = namefunc(func, "e2_" .. name) - - function func(...) - local retval = oldfunc(...) - - local checker = wire_expression_types2[rets][5] - if not checker then return retval end - - local ok, msg = pcall(checker, retval) - if ok then return retval end - debug.Trace() - local full_signature = E2Lib.generate_signature(signature, rets) - error(string.format("Type check for function %q failed: %s\n", full_signature, msg), 0) - - return retval - end - - entry[3] = func - entry.oldfunc = oldfunc -end - --- ---------------------------------------------------------------------- - function wire_expression2_reset_extensions() wire_expression_callbacks = { construct = {}, @@ -79,13 +14,10 @@ function wire_expression2_reset_extensions() } wire_expression_types = {} - wire_expression_types2 = { - [""] = { - [5] = function(retval) if retval ~= nil then error("Return value of void function is not nil.", 0) end end - } - } + wire_expression_types2 = { [""] = {} } -- TODO: do we really need ""? :\ wire_expression2_funcs = {} wire_expression2_funclist = {} + if CLIENT then wire_expression2_funclist_lowercase = {} end wire_expression2_constants = {} end @@ -100,7 +32,7 @@ end ---@param def T | nil ---@param input_serialize (fun(self, input: any): T)? ---@param output_serialize (fun(self, output: any): T)? ----@param type_check (fun(v: any))? +---@param type_check (fun(v: any))? # DEPRECATED, NO LONGER USED. Can pass nil here safely. ---@param is_invalid (fun(v: any): boolean)? function registerType(name, id, def, input_serialize, output_serialize, type_check, is_invalid, ...) if not isValidTypeId(id) then @@ -223,7 +155,6 @@ function registerOperator(name, pars, rets, func, cost, argnames, attributes) local signature = "op:" .. name .. "(" .. pars .. ")" wire_expression2_funcs[signature] = { signature, rets, func, cost or tempcost, argnames = argnames, attributes = attributes } - if wire_expression2_debug:GetBool() then makecheck(signature) end end function registerFunction(name, pars, rets, func, cost, argnames, attributes) @@ -239,11 +170,14 @@ function registerFunction(name, pars, rets, func, cost, argnames, attributes) wire_expression2_funcs[signature] = { signature, rets, func, cost or tempcost, argnames = argnames, extension = E2Lib.currentextension, attributes = attributes } wire_expression2_funclist[name] = true - if wire_expression2_debug:GetBool() then makecheck(signature) end end function E2Lib.registerConstant(name, value) if name:sub(1, 1) ~= "_" then name = "_" .. name end + + local ty = type(value) + assert(ty == "number" or ty == "string", "Invalid value passed to registerConstant (must be number or string)") + wire_expression2_constants[name] = value end diff --git a/lua/entities/gmod_wire_expression2/core/matrix.lua b/lua/entities/gmod_wire_expression2/core/matrix.lua index f51b0604ff..245e156748 100644 --- a/lua/entities/gmod_wire_expression2/core/matrix.lua +++ b/lua/entities/gmod_wire_expression2/core/matrix.lua @@ -23,10 +23,7 @@ registerType("matrix2", "xm2", { 0, 0, return ret end, nil, - function(retval) - if !istable(retval) then error("Return value is not a table, but a "..type(retval).."!",0) end - if #retval ~= 4 then error("Return value does not have exactly 4 entries!",0) end - end, + nil, function(v) return !istable(v) or #v ~= 4 end @@ -353,10 +350,7 @@ registerType("matrix", "m", { 0, 0, 0, return ret end, nil, - function(retval) - if !istable(retval) then error("Return value is not a table, but a "..type(retval).."!",0) end - if #retval ~= 9 then error("Return value does not have exactly 9 entries!",0) end - end, + nil, function(v) return !istable(v) or #v ~= 9 end @@ -878,10 +872,7 @@ registerType("matrix4", "xm4", { 0, 0, 0, 0, return ret end, nil, - function(retval) - if !istable(retval) then error("Return value is not a table, but a "..type(retval).."!",0) end - if #retval ~= 16 then error("Return value does not have exactly 16 entries!",0) end - end, + nil, function(v) return !istable(v) or #v ~= 16 end diff --git a/lua/entities/gmod_wire_expression2/core/number.lua b/lua/entities/gmod_wire_expression2/core/number.lua index bf24dc6db9..e264f9291d 100644 --- a/lua/entities/gmod_wire_expression2/core/number.lua +++ b/lua/entities/gmod_wire_expression2/core/number.lua @@ -34,11 +34,9 @@ local tanh = math.tanh registerType("normal", "n", 0, nil, nil, - function(retval) - if !isnumber(retval) then error("Return value is not a number, but a "..type(retval).."!",0) end - end, + nil, function(v) - return !isnumber(v) + return not isnumber(v) end ) diff --git a/lua/entities/gmod_wire_expression2/core/player.lua b/lua/entities/gmod_wire_expression2/core/player.lua index c1c3ad785c..fcbbd1548d 100644 --- a/lua/entities/gmod_wire_expression2/core/player.lua +++ b/lua/entities/gmod_wire_expression2/core/player.lua @@ -22,23 +22,16 @@ local M_CMoveData = FindMetaTable("CMoveData") registerType("usercmd", "xuc", nil, nil, nil, - function(retval) - if retval == nil then return end - if not istable(retval) then error("Return value is neither nil nor a table, but a " .. type(retval) .. "!",0) end - if getmetatable(retval) ~= M_CUserCmd then error("Return value is not a CUserCmd!", 0) end - end, + nil, function(v) return not istable(v) or getmetatable(v) ~= M_CUserCmd end ) registerType("movedata", "xmv", nil, - nil, nil, - function(retval) - if retval == nil then return end - if not istable(retval) then error("Return value is neither nil nor a table, but a " .. type(retval) .. "!",0) end - if getmetatable(retval) ~= M_CMoveData then error("Return value is not a CMoveData!", 0) end - end, + nil, + nil, + nil, function(v) return not istable(v) or getmetatable(v) ~= M_CMoveData end diff --git a/lua/entities/gmod_wire_expression2/core/quaternion.lua b/lua/entities/gmod_wire_expression2/core/quaternion.lua index 50a6c2debf..0f21fb0212 100644 --- a/lua/entities/gmod_wire_expression2/core/quaternion.lua +++ b/lua/entities/gmod_wire_expression2/core/quaternion.lua @@ -22,10 +22,7 @@ local rad2deg = 180/math.pi registerType("quaternion", "q", { 0, 0, 0, 0 }, nil, nil, - function(retval) - if !istable(retval) then error("Return value is not a table, but a "..type(retval).."!",0) end - if #retval ~= 4 then error("Return value does not have exactly 4 entries!",0) end - end, + nil, function(v) return !istable(v) or #v ~= 4 end diff --git a/lua/entities/gmod_wire_expression2/core/ranger.lua b/lua/entities/gmod_wire_expression2/core/ranger.lua index 5a42b0e071..248fc53000 100644 --- a/lua/entities/gmod_wire_expression2/core/ranger.lua +++ b/lua/entities/gmod_wire_expression2/core/ranger.lua @@ -6,10 +6,7 @@ registerType("ranger", "xrd", nil, nil, nil, - function(retval) - if retval == nil then return end - if !istable(retval) then error("Return value is neither nil nor a table, but a "..type(retval).."!",0) end - end, + nil, function(v) return !istable(v) or not v.HitPos end diff --git a/lua/entities/gmod_wire_expression2/core/serialization.lua b/lua/entities/gmod_wire_expression2/core/serialization.lua index 8dc94af3a9..f30fd63952 100644 --- a/lua/entities/gmod_wire_expression2/core/serialization.lua +++ b/lua/entities/gmod_wire_expression2/core/serialization.lua @@ -1,38 +1,8 @@ E2Lib.RegisterExtension("serialization", true, "Adds functions to serialize data structures into a string and back again.") --- GLON output validation +-- vON / JSON (De)Serialization local newE2Table = E2Lib.newE2Table ---[[ -wire_expression2_glon = {} -wire_expression2_glon.history = {} -wire_expression2_glon.players = WireLib.RegisterPlayerTable() - -local function logGlonCall( self, glonString, ret, safeGlonObject ) - local logEntry = - { - Expression2 = - { - Name = self.entity.name, - Owner = self.entity.player, - OwnerID = self.entity.player:IsValid() and self.entity.player:SteamID() or "[Unknown]", - OwnerName = self.entity.player:IsValid() and self.entity.player:Name() or "[Unknown]", - }, - GLON = glonString, - GLONOutput = ret, - SafeOutput = safeGlonObject, - Timestamp = os.date("%c") - } - - wire_expression2_glon.history[#wire_expression2_glon.history + 1] = logEntry - - if self.entity.player:IsValid() then - wire_expression2_glon.players[self.entity.player] = wire_expression2_glon.players[self.entity.player] or {} - wire_expression2_glon.players[self.entity.player][#wire_expression2_glon.players[self.entity.player] + 1] = logEntry - end -end -]] - local antispam_lookup = {} local function antispam( self ) if antispam_lookup[self.uid] and antispam_lookup[self.uid] > CurTime() then @@ -80,12 +50,12 @@ local forbiddenTypes = { local typeSanitizers -local function sanitizeGlonOutput ( self, glonOutputObject, objectType, safeGlonObjectMap ) +local function sanitizeOutput ( self, glonOutputObject, objectType, safeGlonObjectMap ) self.prf = self.prf + 1 - if not objectType then return nil end - if forbiddenTypes[objectType] then return nil end - if not wire_expression_types2[objectType] and not objecType == "external_t" then return nil end + if not objectType then return end + if forbiddenTypes[objectType] then return end + if not wire_expression_types2[objectType] and not objectType == "external_t" then return end safeGlonObjectMap = safeGlonObjectMap or { r = {}, @@ -100,104 +70,6 @@ local function sanitizeGlonOutput ( self, glonOutputObject, objectType, safeGlon return typeSanitizers[objectType] ( self, glonOutputObject, safeGlonObjectMap ) end -typeSanitizers = { - - -- Two sanitizers to help out with making older serialized data from before #2399 usable - ["v"] = function ( self, glonOutputObject, safeGlonObjectMap ) - if type(glonOutputObject) ~= "Vector" then - return Vector( glonOutputObject[1], glonOutputObject[2], glonOutputObject[3] ) - end - return glonOutputObject - end, - ["a"] = function ( self, glonOutputObject, safeGlonObjectMap ) - if type(glonOutputObject) ~= "Angle" then - return Angle( glonOutputObject[1], glonOutputObject[2], glonOutputObject[3] ) - end - return glonOutputObject - end, - - ["r"] = function ( self, glonOutputObject, safeGlonObjectMap ) - if safeGlonObjectMap["r"][glonOutputObject] then - return safeGlonObjectMap["r"][glonOutputObject] - end - - local safeArray = {} - if not glonOutputObject then return safeArray end - safeGlonObjectMap["r"][glonOutputObject] = safeArray - - if !istable(glonOutputObject) then return safeArray end - - for k, v in pairs(glonOutputObject) do - if type (k) == "number" then - safeArray[k] = v - end - end - - return safeArray - end, - ["t"] = function ( self, glonOutputObject, safeGlonObjectMap ) - if safeGlonObjectMap["t"][glonOutputObject] then - return safeGlonObjectMap["t"][glonOutputObject] - end - - local safeTable = newE2Table() - if not glonOutputObject then return safeTable end - safeGlonObjectMap["t"][glonOutputObject] = safeTable - - if !istable(glonOutputObject) then return safeTable end - - if istable(glonOutputObject.s) and istable(glonOutputObject.stypes) then - for k, v in pairs(glonOutputObject.s) do - local objectType = glonOutputObject.stypes[k] - local safeObject = sanitizeGlonOutput( self, v, objectType, safeGlonObjectMap ) - if safeObject then - safeTable.s[tostring(k)] = safeObject - safeTable.stypes[tostring(k)] = objectType - safeTable.size = safeTable.size + 1 - end - end - end - - if istable(glonOutputObject.n) and istable(glonOutputObject.ntypes) then - for k, v in pairs(glonOutputObject.n) do - if isnumber(k) then - local objectType = glonOutputObject.ntypes[k] - local safeObject = sanitizeGlonOutput( self, v, objectType, safeGlonObjectMap ) - if safeObject then - safeTable.n[k] = safeObject - safeTable.ntypes[k] = objectType - safeTable.size = safeTable.size + 1 - end - end - end - end - - return safeTable - end, - ["external_t"] = function ( self, glonOutputObject, safeGlonObjectMap ) - if safeGlonObjectMap["t"][glonOutputObject] then - return safeGlonObjectMap["t"][glonOutputObject] - end - - local safeTable = {} - if not glonOutputObject then return safeTable end - safeGlonObjectMap["t"][glonOutputObject] = safeTable - - if !istable(glonOutputObject) then return safeTable end - - for k, v in pairs(glonOutputObject) do - local objectType, v = luaTypeToWireTypeid( v ) - if objectType == "t" then objectType = "external_t" end - - local safeObject = sanitizeGlonOutput( self, v, objectType, safeGlonObjectMap ) - if safeObject then - safeTable[k] = safeObject - end - end - - return safeTable - end, -} -- Default sanitizer for types that are arrays of numbers local numericArrayDataTypes = @@ -224,98 +96,6 @@ for objectType, arrayLength in pairs(numericArrayDataTypes) do end end --- Attempt to load glon -if not glon and file.Exists( 'includes/modules/glon.lua', 'LUA' ) then - pcall(require,"glon") -end - --- If glon STILL doesn't exist, don't load any of these functions -if glon then - local last_glon_error = "" - - __e2setcost(10) - - --- Encodes into a string, using [[GLON]]. - e2function string glonEncode(array data) - local ok, ret = pcall(glon.encode, data) - if not ok then - last_glon_error = ret - ErrorNoHalt("glon.encode error: "..ret) - return "" - end - - if ret then - self.prf = self.prf + #ret / 2 - end - - return ret or "" - end - - --- Decodes into an array, using [[GLON]]. - e2function array glonDecode(string data) - if not data or data == "" then return {} end - - self.prf = self.prf + #data / 2 - - local ok, ret = pcall(glon.decode, data) - - if not ok then - last_glon_error = ret - ErrorNoHalt("glon.decode error: "..ret) - return {} - end - - local safeArray = sanitizeGlonOutput( self, ret, "r" ) - -- logGlonCall( self, data, ret, safeArray ) - return safeArray or {} - end - - e2function string glonError() - return last_glon_error or "" - end - - hook.Add("InitPostEntity", "wire_expression2_glonfix", function() - -- Fixing other people's bugs... - for i = 1,20 do - local name, encode_types = debug.getupvalue(glon.Write, i) - if name == "encode_types" then - for _,tp in ipairs({"NPC","Vehicle","Weapon"}) do - if not encode_types[tp] then encode_types[tp] = encode_types.Entity end - end - break - end - end - end) - - --------------------------------------------------------------------------- - -- table glon - --------------------------------------------------------------------------- - - __e2setcost(15) - - --- Encodes into a string, using [[GLON]]. - e2function string glonEncode(table data) = e2function string glonEncode(array data) - - __e2setcost(25) - - -- decodes a glon string and returns an table - e2function table glonDecodeTable(string data) - if not data or data == "" then return newE2Table() end - - self.prf = self.prf + #data / 2 - - local ok, ret = pcall(glon.decode, data) - if not ok then - last_glon_error = ret - ErrorNoHalt("glon.decode error: "..ret) - return newE2Table() - end - - local safeTable = sanitizeGlonOutput( self, ret, "t" ) - return safeTable or newE2Table() - end -end - --------------------------------------------------------------------------- -- von --------------------------------------------------------------------------- @@ -355,8 +135,7 @@ e2function array vonDecode(string data) return {} end - local safeArray = sanitizeGlonOutput( self, ret, "r" ) - return safeArray or {} + return sanitizeOutput( self, ret, "r" ) or {} end __e2setcost(1) @@ -385,8 +164,7 @@ e2function table vonDecodeTable(string data) return newE2Table() end - local safeTable = sanitizeGlonOutput( self, ret, "t" ) - return safeTable or newE2Table() + return sanitizeOutput( self, ret, "t" ) or newE2Table() end --------------------------------------------------------------------------- @@ -427,8 +205,7 @@ local function jsonDecode( self, data, tp ) return {} end - local safeArray = sanitizeGlonOutput( self, ret, tp ) - return safeArray or {} + return sanitizeOutput( self, ret, tp ) or {} end __e2setcost(1) diff --git a/lua/entities/gmod_wire_expression2/core/string.lua b/lua/entities/gmod_wire_expression2/core/string.lua index de6fd63bbf..71c2e01f0e 100644 --- a/lua/entities/gmod_wire_expression2/core/string.lua +++ b/lua/entities/gmod_wire_expression2/core/string.lua @@ -13,9 +13,7 @@ local string_Replace, string_Explode = string.Replace, string.Explode registerType("string", "s", "", nil, nil, - function(retval) - if not isstring(retval) then error("Return value is not a string, but a "..type(retval).."!",0) end - end, + nil, function(v) return not isstring(v) end diff --git a/lua/entities/gmod_wire_expression2/core/table.lua b/lua/entities/gmod_wire_expression2/core/table.lua index e0f567fc80..b1a2fc1107 100644 --- a/lua/entities/gmod_wire_expression2/core/table.lua +++ b/lua/entities/gmod_wire_expression2/core/table.lua @@ -43,9 +43,7 @@ registerType("table", "t", newE2Table(), return input end, nil, - function(retval) - if not istable(retval) then error("Return value is not a table, but a "..type(retval).."!", 0) end - end, + nil, function(v) return not istable(v) end diff --git a/lua/entities/gmod_wire_expression2/core/vector.lua b/lua/entities/gmod_wire_expression2/core/vector.lua index ba9387275a..a453171f48 100644 --- a/lua/entities/gmod_wire_expression2/core/vector.lua +++ b/lua/entities/gmod_wire_expression2/core/vector.lua @@ -45,10 +45,7 @@ local cubicBezier = math.CubicBezier registerType("vector", "v", Vector(0, 0, 0), nil, function(self, output) return Vector(output) end, - function(retval) - if isvector(retval) then return end - error("Return value is not a Vector, but a " .. type(retval) .. "!", 0) - end, + nil, function(v) return not isvector(v) end diff --git a/lua/entities/gmod_wire_expression2/core/vector2.lua b/lua/entities/gmod_wire_expression2/core/vector2.lua index 7e06fd7b9b..6a2b910099 100644 --- a/lua/entities/gmod_wire_expression2/core/vector2.lua +++ b/lua/entities/gmod_wire_expression2/core/vector2.lua @@ -12,10 +12,7 @@ local pi = math.pi registerType("vector2", "xv2", { 0, 0 }, function(self, input) return { input[1], input[2] } end, nil, - function(retval) - if !istable(retval) then error("Return value is not a table, but a "..type(retval).."!",0) end - if #retval ~= 2 then error("Return value does not have exactly 2 entries!",0) end - end, + nil, function(v) return !istable(v) or #v ~= 2 end @@ -512,10 +509,7 @@ end registerType("vector4", "xv4", { 0, 0, 0, 0 }, function(self, input) return { input[1], input[2], input[3], input[4] } end, nil, - function(retval) - if !istable(retval) then error("Return value is not a table, but a "..type(retval).."!",0) end - if #retval ~= 4 then error("Return value does not have exactly 4 entries!",0) end - end, + nil, function(v) return !istable(v) or #v ~= 4 end diff --git a/lua/entities/gmod_wire_expression2/core/wirelink.lua b/lua/entities/gmod_wire_expression2/core/wirelink.lua index d0d704fcc2..f545d237e4 100644 --- a/lua/entities/gmod_wire_expression2/core/wirelink.lua +++ b/lua/entities/gmod_wire_expression2/core/wirelink.lua @@ -123,11 +123,7 @@ end registerType("wirelink", "xwl", nil, nil, nil, - function(retval) - if IsValid(retval) then return end - if retval == nil then return end - if not retval.EntIndex then error("Return value is neither nil nor an Entity (and thus not a wirelink), but a "..type(retval).."!",0) end - end, + nil, function(v) return not IsValid(v) end diff --git a/lua/wire/client/e2helper.lua b/lua/wire/client/e2helper.lua index 4e63fcd1f4..e73168f8b5 100644 --- a/lua/wire/client/e2helper.lua +++ b/lua/wire/client/e2helper.lua @@ -314,8 +314,7 @@ function E2Helper.Update() E2Helper.constants = {} if E2Helper.CurrentMode == true then for k, v in pairs(wire_expression2_constants) do - -- set the type according to the functions - local strType = E2Lib.guess_type(v) + local strType = isstring(v) and "s" or "n" -- constants have no arguments and no cost local name, args, rets, cost = k, nil, strType, 0