Skip to content

Commit

Permalink
Revert "Use new OnRemove update (#1696)" (#1771)
Browse files Browse the repository at this point in the history
* Revert "Use new OnRemove update (#1696)"

This reverts commit 3bffc9c.

* Cleanup

* Fix null entity index
  • Loading branch information
thegrb93 authored Jun 12, 2024
1 parent 295d8c4 commit dcc40be
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 37 deletions.
12 changes: 12 additions & 0 deletions lua/entities/starfall_processor/cl_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ function ENT:Initialize()
self.OverlayFade = 0
self.ActiveHuds = {}
self.reuploadOnReload = false

local instance
SF.CallOnRemove(self, "sf_processor", function()
instance = self.instance
self:SetReuploadOnReload(false)
end,
function()
if instance then
instance:runScriptHook("removed")
instance:deinitialize()
end
end)
end

function ENT:GetOverlayText()
Expand Down
4 changes: 4 additions & 0 deletions lua/entities/starfall_processor/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ function ENT:Use(activator)
end
end

function ENT:OnRemove()
self:Destroy()
end

function ENT:Think()
if self.instance then
local bufferAvg = self.instance.cpu_average
Expand Down
31 changes: 12 additions & 19 deletions lua/entities/starfall_processor/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ ENT.States = {

local IsValid = FindMetaTable("Entity").IsValid

function ENT:Compile()
function ENT:Compile(sfdata)
self:Destroy()
if sfdata then
self.sfdata = sfdata
self.owner = sfdata.owner
sfdata.proc = self
else
sfdata = self.sfdata
end

if not (sfdata and sfdata.files and sfdata.files[sfdata.mainfile]) then return end
self.error = nil

if not (self.sfdata and self.sfdata.files and self.sfdata.files[self.sfdata.mainfile]) then return end
local ok, instance = SF.Instance.Compile(self.sfdata.files, self.sfdata.mainfile, self.owner, self)
local ok, instance = SF.Instance.Compile(sfdata.files, sfdata.mainfile, self.owner, self)
if not ok then self:Error(instance) return end

if instance.ppdata.scriptnames and instance.mainfile and instance.ppdata.scriptnames[instance.mainfile] then
Expand All @@ -38,7 +46,6 @@ function ENT:Compile()
self.author = nil
end


self.instance = instance
instance.runOnError = function(err)
-- Have to make sure it's valid because the chip can be deleted before deinitialization and trigger errors
Expand Down Expand Up @@ -84,22 +91,8 @@ function ENT:Destroy()
end
end

function ENT:OnRemove(fullsnapshot)
if fullsnapshot then return end
self:Destroy()

-- This should remove the hook if it existed
if CLIENT then self:SetReuploadOnReload(false) end
end

function ENT:SetupFiles(sfdata)
self:Destroy()

self.sfdata = sfdata
self.owner = sfdata.owner
sfdata.proc = self

self:Compile()
self:Compile(sfdata)

if SERVER and self.instance then
self.sfsenddata = self:GetSendData()
Expand Down
13 changes: 5 additions & 8 deletions lua/entities/starfall_prop/cl_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ function ENT:Initialize()
self.rendermeshloaded = false
self:DrawShadow(false)
self:EnableCustomCollisions( true )

local mesh
SF.CallOnRemove(self, "sf_prop",
function() mesh = self.rendermesh end,
function() if mesh then mesh:Destroy() end end)
end

function ENT:BuildPhysics(mesh)
Expand Down Expand Up @@ -53,14 +58,6 @@ function ENT:GetRenderMesh()
end
end

function ENT:OnRemove(fullsnapshot)
if fullsnapshot then return end
local mesh = self.rendermesh
if mesh then
mesh:Destroy()
end
end

net.Receive("starfall_custom_prop", function()
local index = net.ReadUInt(16)
local creationindex = net.ReadUInt(32)
Expand Down
2 changes: 1 addition & 1 deletion lua/starfall/libs_sh/entities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ local soundsByEntity = SF.EntityTable("emitSoundsByEntity", function(e, t)
for snd, _ in pairs(t) do
e:StopSound(snd)
end
end)
end, true)

local sound_library = instance.Libraries.sound

Expand Down
2 changes: 1 addition & 1 deletion lua/starfall/libs_sh/hook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ end)
-- @class hook
-- @shared
-- @param Entity ent Entity being removed
-- @param boolean isSnapshot If clientside, will be true if the entity isn't actually removed
-- @param boolean fullupdate If clientside, will be true if the entity was removed by a fullupdate
add("EntityRemoved")

--- Called when an entity is broken
Expand Down
29 changes: 21 additions & 8 deletions lua/starfall/sflib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,26 @@ hook.Add("InitPostEntity","SF_SanitizeTypeMetatables",function()
end)

local removedHooks = setmetatable({}, {__index=function(t,k) local r={} t[k]=r return r end})
hook.Add("EntityRemoved","SF_CallOnRemove",function(ent, fullsnapshot)
if fullsnapshot then return end
hook.Add("EntityRemoved","SF_CallOnRemove",function(ent)
local hooks = removedHooks[ent]
if hooks then
for k, v in pairs(hooks) do
v(ent)
if v[1] then v[1](ent) end
end
if CLIENT then
timer.Simple(0, function()
if not IsValid(ent) then
for k, v in pairs(hooks) do
if v[2] then v[2](ent) end
end
end
end)
end
removedHooks[ent] = nil
end
end)
function SF.CallOnRemove(ent, key, func)
removedHooks[ent][key] = func
function SF.CallOnRemove(ent, key, func, deferedfunc)
removedHooks[ent][key] = {func, deferedfunc}
end
function SF.RemoveCallOnRemove(ent, key)
removedHooks[ent][key] = nil
Expand All @@ -113,17 +121,22 @@ end
-------------------------------------------------------------------------------

-- Returns a class that manages a table of entity keys
function SF.EntityTable(key, destructor)
function SF.EntityTable(key, destructor, dontwait)
return setmetatable({}, {
__newindex = function(t, e, v)
rawset(t, e, v)
if e ~= SF.Superuser then
SF.CallOnRemove(e, key, function()
local function ondestroy()
if t[e] then
if destructor then destructor(e, v) end
t[e] = nil
end
end)
end
if SERVER or dontwait then
SF.CallOnRemove(e, key, ondestroy)
else
SF.CallOnRemove(e, key, nil, ondestroy)
end
end
end
})
Expand Down

0 comments on commit dcc40be

Please sign in to comment.