diff --git a/gamemode/modules/base/cl_entityvars.lua b/gamemode/modules/base/cl_entityvars.lua index f8e7b875a..0ff225c3a 100644 --- a/gamemode/modules/base/cl_entityvars.lua +++ b/gamemode/modules/base/cl_entityvars.lua @@ -1,4 +1,4 @@ -local DarkRPVars = {} +DarkRP.ClientsideDarkRPVars = DarkRP.ClientsideDarkRPVars or {} --[[--------------------------------------------------------------------------- Interface @@ -8,7 +8,7 @@ local pmeta = FindMetaTable("Player") -- enough to warrant optimizing. See https://github.com/FPtje/DarkRP/pull/3212 local get_user_id = pmeta.UserID function pmeta:getDarkRPVar(var, fallback) - local vars = DarkRPVars[get_user_id(self)] + local vars = DarkRP.ClientsideDarkRPVars[get_user_id(self)] if vars == nil then return fallback end local results = vars[var] @@ -22,14 +22,14 @@ Retrieve the information of a player var ---------------------------------------------------------------------------]] local function RetrievePlayerVar(userID, var, value) local ply = Player(userID) - DarkRPVars[userID] = DarkRPVars[userID] or {} + DarkRP.ClientsideDarkRPVars[userID] = DarkRP.ClientsideDarkRPVars[userID] or {} - hook.Call("DarkRPVarChanged", nil, ply, var, DarkRPVars[userID][var], value) - DarkRPVars[userID][var] = value + hook.Call("DarkRPVarChanged", nil, ply, var, DarkRP.ClientsideDarkRPVars[userID][var], value) + DarkRP.ClientsideDarkRPVars[userID][var] = value -- Backwards compatibility if IsValid(ply) then - ply.DarkRPVars = DarkRPVars[userID] + ply.DarkRPVars = DarkRP.ClientsideDarkRPVars[userID] end end @@ -50,7 +50,7 @@ Retrieve the message to remove a DarkRPVar ---------------------------------------------------------------------------]] local function doRetrieveRemoval() local userID = net.ReadUInt(16) - local vars = DarkRPVars[userID] or {} + local vars = DarkRP.ClientsideDarkRPVars[userID] or {} local var = DarkRP.readNetDarkRPVarRemoval() local ply = Player(userID) @@ -81,7 +81,7 @@ timer.Simple(0, fp{RunConsoleCommand, "_sendDarkRPvars"}) net.Receive("DarkRP_DarkRPVarDisconnect", function(len) local userID = net.ReadUInt(16) - DarkRPVars[userID] = nil + DarkRP.ClientsideDarkRPVars[userID] = nil end) --[[--------------------------------------------------------------------------- diff --git a/gamemode/modules/base/sh_entityvars.lua b/gamemode/modules/base/sh_entityvars.lua index ea856cdd9..6194a2cee 100644 --- a/gamemode/modules/base/sh_entityvars.lua +++ b/gamemode/modules/base/sh_entityvars.lua @@ -1,6 +1,6 @@ -local maxId = 0 -local DarkRPVars = {} -local DarkRPVarById = {} +DarkRP.RegisteredDarkRPVarsMaxId = DarkRP.RegisteredDarkRPVarsMaxId or 0 +DarkRP.RegisteredDarkRPVars = DarkRP.RegisteredDarkRPVars or {} +DarkRP.RegisteredDarkRPVarsById = DarkRP.RegisteredDarkRPVarsById or {} -- the amount of bits assigned to the value that determines which DarkRPVar we're sending/receiving local DARKRP_ID_BITS = 8 @@ -8,13 +8,20 @@ local UNKNOWN_DARKRPVAR = 255 -- Should be equal to 2^DARKRP_ID_BITS - 1 DarkRP.DARKRP_ID_BITS = DARKRP_ID_BITS function DarkRP.registerDarkRPVar(name, writeFn, readFn) - maxId = maxId + 1 + -- After a reload, only update the write and read function + if DarkRP.RegisteredDarkRPVars[name] then + DarkRP.RegisteredDarkRPVars[name].writeFn = writeFn + DarkRP.RegisteredDarkRPVars[name].readFn = readFn + return + end + + DarkRP.RegisteredDarkRPVarsMaxId = DarkRP.RegisteredDarkRPVarsMaxId + 1 -- UNKNOWN_DARKRPVAR is reserved for unknown values - if maxId >= UNKNOWN_DARKRPVAR then DarkRP.error(string.format("Too many DarkRPVar registrations! DarkRPVar '%s' triggered this error", name), 2) end + if DarkRP.RegisteredDarkRPVarsMaxId >= UNKNOWN_DARKRPVAR then DarkRP.error(string.format("Too many DarkRPVar registrations! DarkRPVar '%s' triggered this error", name), 2) end - DarkRPVars[name] = {id = maxId, name = name, writeFn = writeFn, readFn = readFn} - DarkRPVarById[maxId] = DarkRPVars[name] + DarkRP.RegisteredDarkRPVars[name] = {id = DarkRP.RegisteredDarkRPVarsMaxId, name = name, writeFn = writeFn, readFn = readFn} + DarkRP.RegisteredDarkRPVarsById[DarkRP.RegisteredDarkRPVarsMaxId] = DarkRP.RegisteredDarkRPVars[name] end -- Unknown values have unknown types and unknown identifiers, so this is sent inefficiently @@ -41,7 +48,7 @@ local function warnRegistration(name) end function DarkRP.writeNetDarkRPVar(name, value) - local DarkRPVar = DarkRPVars[name] + local DarkRPVar = DarkRP.RegisteredDarkRPVars[name] if not DarkRPVar then warnRegistration(name) @@ -53,7 +60,7 @@ function DarkRP.writeNetDarkRPVar(name, value) end function DarkRP.writeNetDarkRPVarRemoval(name) - local DarkRPVar = DarkRPVars[name] + local DarkRPVar = DarkRP.RegisteredDarkRPVars[name] if not DarkRPVar then warnRegistration(name) @@ -67,7 +74,7 @@ end function DarkRP.readNetDarkRPVar() local DarkRPVarId = net.ReadUInt(DARKRP_ID_BITS) - local DarkRPVar = DarkRPVarById[DarkRPVarId] + local DarkRPVar = DarkRP.RegisteredDarkRPVarsById[DarkRPVarId] if DarkRPVarId == UNKNOWN_DARKRPVAR then local name, value = readUnknown() @@ -82,7 +89,7 @@ end function DarkRP.readNetDarkRPVarRemoval() local id = net.ReadUInt(DARKRP_ID_BITS) - return id == 255 and net.ReadString() or DarkRPVarById[id].name + return id == 255 and net.ReadString() or DarkRP.RegisteredDarkRPVarsById[id].name end -- The money is a double because it accepts higher values than Int and UInt, which are undefined for >32 bits diff --git a/gamemode/modules/base/sv_entityvars.lua b/gamemode/modules/base/sv_entityvars.lua index 6035519d4..a11a9a0ed 100644 --- a/gamemode/modules/base/sv_entityvars.lua +++ b/gamemode/modules/base/sv_entityvars.lua @@ -1,7 +1,7 @@ local meta = FindMetaTable("Player") -local DarkRPVars = {} -local privateDarkRPVars = {} +DarkRP.ServerDarkRPVars = DarkRP.ServerDarkRPVars or {} +DarkRP.ServerPrivateDarkRPVars = DarkRP.ServerPrivateDarkRPVars or {} --[[--------------------------------------------------------------------------- Pooled networking strings @@ -19,12 +19,12 @@ Player vars Remove a player's DarkRPVar ---------------------------------------------------------------------------]] function meta:removeDarkRPVar(var, target) - local vars = DarkRPVars[self] + local vars = DarkRP.ServerDarkRPVars[self] hook.Call("DarkRPVarChanged", nil, self, var, vars and vars[var], nil) target = target or player.GetAll() - DarkRPVars[self] = DarkRPVars[self] or {} - DarkRPVars[self][var] = nil + DarkRP.ServerDarkRPVars[self] = DarkRP.ServerDarkRPVars[self] or {} + DarkRP.ServerDarkRPVars[self][var] = nil net.Start("DarkRP_PlayerVarRemoval") net.WriteUInt(self:UserID(), 16) @@ -40,11 +40,11 @@ function meta:setDarkRPVar(var, value, target) if value == nil then return self:removeDarkRPVar(var, target) end - local vars = DarkRPVars[self] + local vars = DarkRP.ServerDarkRPVars[self] hook.Call("DarkRPVarChanged", nil, self, var, vars and vars[var], value) - DarkRPVars[self] = DarkRPVars[self] or {} - DarkRPVars[self][var] = value + DarkRP.ServerDarkRPVars[self] = DarkRP.ServerDarkRPVars[self] or {} + DarkRP.ServerDarkRPVars[self][var] = value net.Start("DarkRP_PlayerVar") net.WriteUInt(self:UserID(), 16) @@ -56,8 +56,8 @@ end Set a private DarkRPVar ---------------------------------------------------------------------------]] function meta:setSelfDarkRPVar(var, value) - privateDarkRPVars[self] = privateDarkRPVars[self] or {} - privateDarkRPVars[self][var] = true + DarkRP.ServerPrivateDarkRPVars[self] = DarkRP.ServerPrivateDarkRPVars[self] or {} + DarkRP.ServerPrivateDarkRPVars[self][var] = true self:setDarkRPVar(var, value, self) end @@ -66,7 +66,7 @@ end Get a DarkRPVar ---------------------------------------------------------------------------]] function meta:getDarkRPVar(var, fallback) - local vars = DarkRPVars[self] + local vars = DarkRP.ServerDarkRPVars[self] if vars == nil then return fallback end local results = vars[var] @@ -79,11 +79,11 @@ end Backwards compatibility: Set ply.DarkRPVars attribute ---------------------------------------------------------------------------]] function meta:setDarkRPVarsAttribute() - DarkRPVars[self] = DarkRPVars[self] or {} + DarkRP.ServerDarkRPVars[self] = DarkRP.ServerDarkRPVars[self] or {} -- With a reference to the table, ply.DarkRPVars should always remain - -- up-to-date. One needs only be careful that DarkRPVars[ply] is never - -- replaced by a different table. - self.DarkRPVars = DarkRPVars[self] + -- up-to-date. One needs only be careful that DarkRP.ServerDarkRPVars[ply] + -- is never replaced by a different table. + self.DarkRPVars = DarkRP.ServerDarkRPVars[self] end @@ -101,15 +101,15 @@ function meta:sendDarkRPVars() net.WriteUInt(target:UserID(), 16) local vars = {} - for var, value in pairs(DarkRPVars[target] or {}) do - if self ~= target and (privateDarkRPVars[target] or {})[var] then continue end + for var, value in pairs(DarkRP.ServerDarkRPVars[target] or {}) do + if self ~= target and (DarkRP.ServerPrivateDarkRPVars[target] or {})[var] then continue end table.insert(vars, var) end local vars_cnt = #vars net.WriteUInt(vars_cnt, DarkRP.DARKRP_ID_BITS + 2) -- Allow for three times as many unknown DarkRPVars than the limit for i = 1, vars_cnt, 1 do - DarkRP.writeNetDarkRPVar(vars[i], DarkRPVars[target][vars[i]]) + DarkRP.writeNetDarkRPVar(vars[i], DarkRP.ServerDarkRPVars[target][vars[i]]) end end net.Send(self) @@ -279,7 +279,7 @@ end) hook.Add("EntityRemoved", "DarkRP_VarRemoval", function(ent) -- We use EntityRemoved to clear players of tables, because it is always called after the PlayerDisconnected hook if ent:IsPlayer() then - DarkRPVars[ent] = nil - privateDarkRPVars[ent] = nil + DarkRP.ServerDarkRPVars[ent] = nil + DarkRP.ServerPrivateDarkRPVars[ent] = nil end end)