Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a destroy method to lights and cleans up other destroy methods #1820

Merged
merged 4 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions lua/starfall/libs_cl/light.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,41 @@ SF.RegisterType("Light", true, false)
return function(instance)
local checkpermission = instance.player ~= SF.Superuser and SF.Permissions.check or function() end

local light_library = instance.Libraries.light
local light_methods, light_meta, wrap, unwrap = instance.Types.Light.Methods, instance.Types.Light, instance.Types.Light.Wrap, instance.Types.Light.Unwrap
local vec_meta, vwrap, vunwrap = instance.Types.Vector, instance.Types.Vector.Wrap, instance.Types.Vector.Unwrap
local col_meta, cwrap, cunwrap = instance.Types.Color, instance.Types.Color.Wrap, instance.Types.Color.Unwrap

local numlights = 0
local lights = {}
local function registerLight(light)
lights[light] = true
gSFLights[light.slot] = light
numlights = numlights + 1
end
local function destroyLight(light)
if lights[light] then
lights[light] = nil
gSFLights[light.slot] = nil
numlights = numlights - 1
end
end

instance.data.light = {lights = lights}
instance:AddHook("deinitialize", function()
for light in pairs(lights) do
gSFLights[light.slot] = nil
end
end)


local light_library = instance.Libraries.light
local light_methods, light_meta, wrap, unwrap = instance.Types.Light.Methods, instance.Types.Light, instance.Types.Light.Wrap, instance.Types.Light.Unwrap
local vec_meta, vwrap, vunwrap = instance.Types.Vector, instance.Types.Vector.Wrap, instance.Types.Vector.Unwrap
local col_meta, cwrap, cunwrap = instance.Types.Color, instance.Types.Color.Wrap, instance.Types.Color.Unwrap


--- Creates a dynamic light (make sure to draw it)
-- @param Vector pos The position of the light
-- @param number size The size of the light. Must be lower than sf_light_maxsize
-- @param number brightness The brightness of the light
-- @param Color color The color of the light
-- @return Light Dynamic light
function light_library.create(pos, size, brightness, color)
if table.Count(lights) >= 256 then SF.Throw("Too many lights have already been allocated (max 256)", 2) end
if numlights >= 256 then SF.Throw("Too many lights have already been allocated (max 256)", 2) end
if maxSize:GetFloat() == 0 then SF.Throw("sf_light_maxsize is set to 0", 2) end
checkpermission(instance, nil, "light.create")
checkluatype(size, TYPE_NUMBER)
Expand All @@ -124,9 +136,7 @@ function light_library.create(pos, size, brightness, color)
dietime = 1
}

lights[light] = true
gSFLights[slot] = light

registerLight(light)
return wrap(light)
end

Expand Down Expand Up @@ -242,4 +252,12 @@ function light_methods:setColor(color)
data.b = col.b
end

--- Destroys the light object freeing up whatever slot it was using
function light_methods:destroy()
local light = unwrap(self)
destroyLight(light)
light_meta.sf2sensitive[self] = nil
light_meta.sensitive2sf[light] = nil
end

end
9 changes: 2 additions & 7 deletions lua/starfall/libs_cl/material.lua
Original file line number Diff line number Diff line change
Expand Up @@ -586,21 +586,16 @@ end

--- Frees a user created material allowing you to create others
function material_methods:destroy()

local m = unwrap(self)
if not m then SF.Throw("The material is already destroyed?", 2) end

local name = m:GetName()
local rt = instance.data.render.rendertargets[name]
if rt then
instance.env.render.destroyRenderTarget(name)
end

local sensitive2sf, sf2sensitive = material_meta.sensitive2sf, material_meta.sf2sensitive
sensitive2sf[m] = nil
sf2sensitive[self] = nil
dsetmeta(self, nil)

material_meta.sf2sensitive[self] = nil
material_meta.sensitive2sf[m] = nil
usermaterials[m] = nil
material_bank:free(instance.player, m, m:GetShader())
end
Expand Down
63 changes: 31 additions & 32 deletions lua/starfall/libs_sh/sound.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ local soundsByEntity = SF.EntityTable("soundsByEntity", function(e, t)
end
end)

local function deleteSound(ply, ent, sound)
sound:Stop()
plyCount:free(ply, 1)
if soundsByEntity[ent] then
soundsByEntity[ent][sound] = nil
end
end


--- Sound library.
-- @name sound
-- @class library
Expand All @@ -41,23 +32,41 @@ SF.RegisterType("Sound", true, false)
return function(instance)
local checkpermission = instance.player ~= SF.Superuser and SF.Permissions.check or function() end

local sound_library = instance.Libraries.sound
local sound_methods, sound_meta, wrap, unwrap = instance.Types.Sound.Methods, instance.Types.Sound, instance.Types.Sound.Wrap, instance.Types.Sound.Unwrap
local ent_meta, ewrap, eunwrap = instance.Types.Entity, instance.Types.Entity.Wrap, instance.Types.Entity.Unwrap

local sounds = {}
local function registerSound(ent, sound)
local sndsByEnt = soundsByEntity[ent]
if not sndsByEnt then sndsByEnt = {} soundsByEntity[ent] = sndsByEnt end
sndsByEnt[sound] = true
sounds[sound] = ent
end

local function destroySound(sound)
sound:Stop()
local ent = sounds[sound]
if ent then
sounds[sound] = nil
plyCount:free(instance.player, 1)
if soundsByEntity[ent] then
soundsByEntity[ent][sound] = nil
end
end
end

local getent
instance:AddHook("initialize", function()
getent = instance.Types.Entity.GetEntity
end)

instance:AddHook("deinitialize", function()
for s, ent in pairs(sounds) do
deleteSound(instance.player, ent, s)
for s in pairs(sounds) do
destroySound(s)
end
end)

local sound_library = instance.Libraries.sound
local sound_methods, sound_meta, wrap, unwrap = instance.Types.Sound.Methods, instance.Types.Sound, instance.Types.Sound.Wrap, instance.Types.Sound.Unwrap
local ent_meta, ewrap, eunwrap = instance.Types.Entity, instance.Types.Entity.Wrap, instance.Types.Entity.Unwrap


--- Creates a sound and attaches it to an entity
-- @param Entity ent Entity to attach sound to.
-- @param string path Filepath to the sound file.
Expand All @@ -82,13 +91,10 @@ function sound_library.create(ent, path, nofilter)
filter = RecipientFilter()
filter:AddAllPlayers()
end
local soundPatch = CreateSound(e, path, filter)
local snds = soundsByEntity[e]
if not snds then snds = {} soundsByEntity[e] = snds end
snds[soundPatch] = true
sounds[soundPatch] = e

return wrap(soundPatch)
local sound = CreateSound(e, path, filter)
registerSound(e, sound)
return wrap(sound)
end


Expand Down Expand Up @@ -133,16 +139,9 @@ end
--- Removes the sound from the game so new one can be created if limit is reached
function sound_methods:destroy()
local snd = unwrap(self)
if snd and sounds[snd] then
deleteSound(instance.player, sounds[snd], snd)
sounds[snd] = nil
local sensitive2sf, sf2sensitive = sound_meta.sensitive2sf, sound_meta.sf2sensitive
sensitive2sf[snd] = nil
sf2sensitive[self] = nil
debug.setmetatable(self, nil)
else
SF.Throw("Tried to destroy invalid sound", 2)
end
destroySound(snd)
sound_meta.sf2sensitive[self] = nil
sound_meta.sensitive2sf[snd] = nil
end

--- Sets the volume of the sound. Won't work unless the sound is playing.
Expand Down
2 changes: 1 addition & 1 deletion lua/starfall/libs_sh/trace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ local function convertFilter(filter)
if filter == nil then
return nil
elseif istable(filter) then
if ent_meta.sf2sensitive[filter]==nil then
if debug.getmetatable(filter)~=ent_meta then
local l = {}
for i, v in ipairs(filter) do
l[i] = eunwrap(v)
Expand Down
6 changes: 2 additions & 4 deletions lua/starfall/libs_sv/navmesh.lua
Original file line number Diff line number Diff line change
Expand Up @@ -653,10 +653,8 @@ return function(instance)
function navarea_methods:remove()
local nav = navunwrap(self)
entList:remove(instance, nav)

local sensitive2sf, sf2sensitive = navarea_meta.sensitive2sf, navarea_meta.sf2sensitive
sensitive2sf[nav] = nil
sf2sensitive[self] = nil
navarea_meta.sf2sensitive[self] = nil
navarea_meta.sensitive2sf[nav] = nil
end

--- Removes the given NavArea from the Closed List
Expand Down
Loading