From 1f27b3d58f4870596ff3e50845d4fe0abe67e3c9 Mon Sep 17 00:00:00 2001 From: Beelzebub <34854689+Be1zebub@users.noreply.github.com> Date: Sat, 21 Dec 2024 11:45:39 +0700 Subject: [PATCH 1/5] Fix persistence plugin Any reason why this plugin exists? Except logs. I suggest deleting it, or at least updating it to match the naive behavior and save more stuff - eg dt. 1. rm bicycle code, just use duplicator lib (its saves dt & other helpful things) - make it better & native bahiavor 2. use native methods Get/SetPersistent - for better compatibility with addons 3. disable sbox persist hooks if plugin enabled - we make custom behaivor, default persist system should be disabled --- plugins/persistence.lua | 157 +++++++++++----------------------------- 1 file changed, 42 insertions(+), 115 deletions(-) diff --git a/plugins/persistence.lua b/plugins/persistence.lua index 2daa27aed..75762b301 100644 --- a/plugins/persistence.lua +++ b/plugins/persistence.lua @@ -1,10 +1,27 @@ - local PLUGIN = PLUGIN PLUGIN.name = "Persistence" PLUGIN.description = "Define entities to persist through restarts." -PLUGIN.author = "alexgrist" -PLUGIN.stored = PLUGIN.stored or {} +PLUGIN.author = "alexgrist & Beelzebub" + +-- disable default sbox persistence (because we override Get/SetPersistent behaivor) +function PLUGIN:OnLoaded() + self._PersistenceSave = self._PersistenceSave or (hook.GetTable().PersistenceSave or {}).PersistenceSave + self._PersistenceLoad = self._PersistenceLoad or (hook.GetTable().PersistenceLoad or {}).PersistenceLoad + + hook.Remove("PersistenceSave", "PersistenceSave") + hook.Remove("PersistenceLoad", "PersistenceLoad") +end + +-- restore it +function PLUGIN:OnUnload() + if self._PersistenceSave then + hook.Add("PersistenceSave", "PersistenceSave", self._PersistenceSave) + end + if self._PersistenceLoad then + hook.Add("PersistenceLoad", "PersistenceLoad", self._PersistenceLoad) + end +end local function GetRealModel(entity) return entity:GetClass() == "prop_effect" and entity.AttachedEntity:GetModel() or entity:GetModel() @@ -19,7 +36,7 @@ properties.Add("persist", { if (entity:IsPlayer() or entity:IsVehicle() or entity.bNoPersist) then return false end if (!gamemode.Call("CanProperty", client, "persist", entity)) then return false end - return !entity:GetNetVar("Persistent", false) + return !entity:GetPersistent() end, Action = function(self, entity) @@ -34,10 +51,7 @@ properties.Add("persist", { if (!IsValid(entity)) then return end if (!self:Filter(entity, client)) then return end - PLUGIN.stored[#PLUGIN.stored + 1] = entity - - entity:SetNetVar("Persistent", true) - + entity:SetPersistent(true) ix.log.Add(client, "persist", GetRealModel(entity), true) end }) @@ -51,7 +65,7 @@ properties.Add("persist_end", { if (entity:IsPlayer()) then return false end if (!gamemode.Call("CanProperty", client, "persist", entity)) then return false end - return entity:GetNetVar("Persistent", false) + return entity:GetPersistent() end, Action = function(self, entity) @@ -66,126 +80,39 @@ properties.Add("persist_end", { if (!IsValid(entity)) then return end if (!self:Filter(entity, client)) then return end - for k, v in ipairs(PLUGIN.stored) do - if (v == entity) then - table.remove(PLUGIN.stored, k) - - break - end - end - - entity:SetNetVar("Persistent", false) + entity:SetPersistent(false) ix.log.Add(client, "persist", GetRealModel(entity), false) end }) -function PLUGIN:PhysgunPickup(client, entity) - if (entity:GetNetVar("Persistent", false)) then - return false - end -end - if (SERVER) then function PLUGIN:LoadData() - local entities = self:GetData() or {} - - for _, v in ipairs(entities) do - local entity = ents.Create(v.Class) - - if (IsValid(entity)) then - entity:SetPos(v.Pos) - entity:SetAngles(v.Angle) - entity:SetModel(v.Model) - entity:SetSkin(v.Skin) - entity:SetColor(v.Color) - entity:SetMaterial(v.Material) - entity:Spawn() - entity:Activate() - - if (v.bNoCollision) then - entity:SetCollisionGroup(COLLISION_GROUP_WORLD) - end - - if (istable(v.BodyGroups)) then - for k2, v2 in pairs(v.BodyGroups) do - entity:SetBodygroup(k2, v2) - end - end - - if (istable(v.SubMaterial)) then - for k2, v2 in pairs(v.SubMaterial) do - if (!isnumber(k2) or !isstring(v2)) then - continue - end - - entity:SetSubMaterial(k2 - 1, v2) - end - end - - local physicsObject = entity:GetPhysicsObject() - - if (IsValid(physicsObject)) then - physicsObject:EnableMotion(v.Movable) - end - - self.stored[#self.stored + 1] = entity - - entity:SetNetVar("Persistent", true) - end + local data = self:GetData() + if !(data and data.Entities) then return end + + local entities = duplicator.Paste(nil, data.Entities, data.Constraints) + + for _, ent in pairs(entities) do + ent:SetPersistent(true) end end function PLUGIN:SaveData() - local entities = {} - - for _, v in ipairs(self.stored) do - if (IsValid(v)) then - local data = {} - data.Class = v.ClassOverride or v:GetClass() - data.Pos = v:GetPos() - data.Angle = v:GetAngles() - data.Model = GetRealModel(v) - data.Skin = v:GetSkin() - data.Color = v:GetColor() - data.Material = v:GetMaterial() - data.bNoCollision = v:GetCollisionGroup() == COLLISION_GROUP_WORLD - - local materials = v:GetMaterials() - - if (istable(materials)) then - data.SubMaterial = {} - - for k2, _ in pairs(materials) do - if (v:GetSubMaterial(k2 - 1) != "") then - data.SubMaterial[k2] = v:GetSubMaterial(k2 - 1) - end - end - end - - local bodyGroups = v:GetBodyGroups() - - if (istable(bodyGroups)) then - data.BodyGroups = {} - - for _, v2 in pairs(bodyGroups) do - if (v:GetBodygroup(v2.id) > 0) then - data.BodyGroups[v2.id] = v:GetBodygroup(v2.id) - end - end - end - - local physicsObject = v:GetPhysicsObject() - - if (IsValid(physicsObject)) then - data.Movable = physicsObject:IsMoveable() - end - - entities[#entities + 1] = data + local data = {Entities = {}, Constraints = {}} + + for _, ent in ipairs(ents.GetAll()) do + if (!ent:GetPersistent()) then continue end + + local tmpEntities = {} + duplicator.GetAllConstrainedEntitiesAndConstraints(ent, tmpEntities, data.Constraints) + + for k, v in pairs(tmpEntities) do + data.Entities[k] = duplicator.CopyEntTable(v) end end - self:SetData(entities) + self:SetData(data) end ix.log.AddType("persist", function(client, ...) From 35d00d1dfd38c5b28d76489cc12c3b48d1415514 Mon Sep 17 00:00:00 2001 From: Beelzebub <34854689+Be1zebub@users.noreply.github.com> Date: Sat, 21 Dec 2024 11:50:03 +0700 Subject: [PATCH 2/5] Resolve CI warning rm unused code --- plugins/stamina/sh_plugin.lua | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/plugins/stamina/sh_plugin.lua b/plugins/stamina/sh_plugin.lua index ff4d154c9..9cfade877 100644 --- a/plugins/stamina/sh_plugin.lua +++ b/plugins/stamina/sh_plugin.lua @@ -30,16 +30,6 @@ local function CalcStaminaChange(client) return 0 end - local runSpeed - - if (SERVER) then - runSpeed = ix.config.Get("runSpeed") + character:GetAttribute("stm", 0) - - if (client:WaterLevel() > 1) then - runSpeed = runSpeed * 0.775 - end - end - local walkSpeed = ix.config.Get("walkSpeed") local maxAttributes = ix.config.Get("maxAttributes", 100) local offset From 83e8cc9ebf64430d8d158929e1409c167b3e3c9f Mon Sep 17 00:00:00 2001 From: Beelzebub <34854689+Be1zebub@users.noreply.github.com> Date: Sat, 21 Dec 2024 12:31:39 +0700 Subject: [PATCH 3/5] Update persistence.lua add more properties save to default implementation --- plugins/persistence.lua | 182 +++++++++++++++++++++++++++++++--------- 1 file changed, 143 insertions(+), 39 deletions(-) diff --git a/plugins/persistence.lua b/plugins/persistence.lua index 75762b301..ebcef9e8e 100644 --- a/plugins/persistence.lua +++ b/plugins/persistence.lua @@ -1,27 +1,10 @@ + local PLUGIN = PLUGIN PLUGIN.name = "Persistence" PLUGIN.description = "Define entities to persist through restarts." -PLUGIN.author = "alexgrist & Beelzebub" - --- disable default sbox persistence (because we override Get/SetPersistent behaivor) -function PLUGIN:OnLoaded() - self._PersistenceSave = self._PersistenceSave or (hook.GetTable().PersistenceSave or {}).PersistenceSave - self._PersistenceLoad = self._PersistenceLoad or (hook.GetTable().PersistenceLoad or {}).PersistenceLoad - - hook.Remove("PersistenceSave", "PersistenceSave") - hook.Remove("PersistenceLoad", "PersistenceLoad") -end - --- restore it -function PLUGIN:OnUnload() - if self._PersistenceSave then - hook.Add("PersistenceSave", "PersistenceSave", self._PersistenceSave) - end - if self._PersistenceLoad then - hook.Add("PersistenceLoad", "PersistenceLoad", self._PersistenceLoad) - end -end +PLUGIN.author = "alexgrist" +PLUGIN.stored = PLUGIN.stored or {} local function GetRealModel(entity) return entity:GetClass() == "prop_effect" and entity.AttachedEntity:GetModel() or entity:GetModel() @@ -36,7 +19,7 @@ properties.Add("persist", { if (entity:IsPlayer() or entity:IsVehicle() or entity.bNoPersist) then return false end if (!gamemode.Call("CanProperty", client, "persist", entity)) then return false end - return !entity:GetPersistent() + return !entity:GetNetVar("Persistent", false) end, Action = function(self, entity) @@ -51,7 +34,10 @@ properties.Add("persist", { if (!IsValid(entity)) then return end if (!self:Filter(entity, client)) then return end - entity:SetPersistent(true) + PLUGIN.stored[#PLUGIN.stored + 1] = entity + + entity:SetNetVar("Persistent", true) + ix.log.Add(client, "persist", GetRealModel(entity), true) end }) @@ -65,7 +51,7 @@ properties.Add("persist_end", { if (entity:IsPlayer()) then return false end if (!gamemode.Call("CanProperty", client, "persist", entity)) then return false end - return entity:GetPersistent() + return entity:GetNetVar("Persistent", false) end, Action = function(self, entity) @@ -80,39 +66,157 @@ properties.Add("persist_end", { if (!IsValid(entity)) then return end if (!self:Filter(entity, client)) then return end - entity:SetPersistent(false) + for k, v in ipairs(PLUGIN.stored) do + if (v == entity) then + table.remove(PLUGIN.stored, k) + + break + end + end + + entity:SetNetVar("Persistent", false) ix.log.Add(client, "persist", GetRealModel(entity), false) end }) +function PLUGIN:PhysgunPickup(client, entity) + if (entity:GetNetVar("Persistent", false)) then + return false + end +end + if (SERVER) then function PLUGIN:LoadData() - local data = self:GetData() - if !(data and data.Entities) then return end - - local entities = duplicator.Paste(nil, data.Entities, data.Constraints) - - for _, ent in pairs(entities) do - ent:SetPersistent(true) + local entities = self:GetData() or {} + + for _, v in ipairs(entities) do + local entity = ents.Create(v.Class) + + if (IsValid(entity)) then + entity:SetPos(v.Pos) + entity:SetAngles(v.Angle) + entity:SetModel(v.Model) + entity:SetSkin(v.Skin) + entity:SetColor(v.Color) + entity:SetMaterial(v.Material) + entity:Spawn() + entity:Activate() + + if (v.Mins and v.Maxs) then + entity:SetCollisionBounds(v.Mins, v.Maxs) + end + + if (v.ColGroup) then + entity:SetCollisionGroup(v.ColGroup) + elseif (v.bNoCollision) then + entity:SetCollisionGroup(COLLISION_GROUP_WORLD) + end + + if (v.Name) then + entity:SetName(v.Name) + end + + if (v.CurHealth) then + entity:SetHealth(v.CurHealth) + end + if (v.MaxHealth) then + entity:SetMaxHealth(v.MaxHealth) + end + + if (istable(v.BodyGroups)) then + for k2, v2 in pairs(v.BodyGroups) do + entity:SetBodygroup(k2, v2) + end + end + + if (istable(v.SubMaterial)) then + for k2, v2 in pairs(v.SubMaterial) do + if (!isnumber(k2) or !isstring(v2)) then + continue + end + + entity:SetSubMaterial(k2 - 1, v2) + end + end + + local physicsObject = entity:GetPhysicsObject() + + if (IsValid(physicsObject)) then + physicsObject:EnableMotion(v.Movable) + end + + if (entity.RestoreNetworkVars and v.DT) then + entity:RestoreNetworkVars(v.DT) + end + + self.stored[#self.stored + 1] = entity + + entity:SetNetVar("Persistent", true) + end end end function PLUGIN:SaveData() - local data = {Entities = {}, Constraints = {}} + local entities = {} + + for _, v in ipairs(self.stored) do + if (IsValid(v)) then + local data = {} + data.Class = v.ClassOverride or v:GetClass() + data.Pos = v:GetPos() + data.Angle = v:GetAngles() + data.Model = GetRealModel(v) + data.Skin = v:GetSkin() + data.Color = v:GetColor() + data.Material = v:GetMaterial() + + local materials = v:GetMaterials() + + if (istable(materials)) then + data.SubMaterial = {} + + for k2, _ in pairs(materials) do + if (v:GetSubMaterial(k2 - 1) != "") then + data.SubMaterial[k2] = v:GetSubMaterial(k2 - 1) + end + end + end + + local bodyGroups = v:GetBodyGroups() + + if (istable(bodyGroups)) then + data.BodyGroups = {} + + for _, v2 in pairs(bodyGroups) do + if (v:GetBodygroup(v2.id) > 0) then + data.BodyGroups[v2.id] = v:GetBodygroup(v2.id) + end + end + end + + local physicsObject = v:GetPhysicsObject() + + if (IsValid(physicsObject)) then + data.Movable = physicsObject:IsMoveable() + end + + data.Mins, data.Maxs = v:GetCollisionBounds() + data.ColGroup = v:GetCollisionGroup() + data.Name = v:GetName() - for _, ent in ipairs(ents.GetAll()) do - if (!ent:GetPersistent()) then continue end + data.CurHealth = v:Health() + data.MaxHealth = v:GetMaxHealth() - local tmpEntities = {} - duplicator.GetAllConstrainedEntitiesAndConstraints(ent, tmpEntities, data.Constraints) + if (v.GetNetworkVars) then + data.DT = v:GetNetworkVars() + end - for k, v in pairs(tmpEntities) do - data.Entities[k] = duplicator.CopyEntTable(v) + entities[#entities + 1] = data end end - self:SetData(data) + self:SetData(entities) end ix.log.AddType("persist", function(client, ...) From a2046abd5bf5db9e5c39671db25f1bacfe968cf4 Mon Sep 17 00:00:00 2001 From: Beelzebub <34854689+Be1zebub@users.noreply.github.com> Date: Sun, 22 Dec 2024 00:38:39 +0700 Subject: [PATCH 4/5] Update persistence.lua add Entity:OnHelixPersistLoad/OnHelixPersistSave --- plugins/persistence.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/plugins/persistence.lua b/plugins/persistence.lua index ebcef9e8e..5e236e620 100644 --- a/plugins/persistence.lua +++ b/plugins/persistence.lua @@ -150,6 +150,10 @@ if (SERVER) then entity:RestoreNetworkVars(v.DT) end + if (entity.OnHelixPersistLoad) then + entity:OnHelixPersistLoad() + end + self.stored[#self.stored + 1] = entity entity:SetNetVar("Persistent", true) @@ -212,6 +216,10 @@ if (SERVER) then data.DT = v:GetNetworkVars() end + if (v.OnHelixPersistSave) then + v:OnHelixPersistSave(data) + end + entities[#entities + 1] = data end end From e2596119a8d8ae5decbe287d4e64e82c103b6842 Mon Sep 17 00:00:00 2001 From: Beelzebub <34854689+Be1zebub@users.noreply.github.com> Date: Mon, 23 Dec 2024 10:42:39 +0700 Subject: [PATCH 5/5] Update persistence.lua add data arg to OnHelixPersistLoad make custom data loading possible --- plugins/persistence.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/persistence.lua b/plugins/persistence.lua index 5e236e620..2ed82ec08 100644 --- a/plugins/persistence.lua +++ b/plugins/persistence.lua @@ -151,7 +151,7 @@ if (SERVER) then end if (entity.OnHelixPersistLoad) then - entity:OnHelixPersistLoad() + entity:OnHelixPersistLoad(v) end self.stored[#self.stored + 1] = entity