Skip to content

Commit

Permalink
Unique sounds (#1852)
Browse files Browse the repository at this point in the history
* Unique sounds limit

Replaces filepath checking with more direct solution to prevent overflowing sound cache

* Sound exists function

* Account for sound properties
  • Loading branch information
Advers authored Sep 16, 2024
1 parent 68ecbcc commit 800241d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lua/starfall/libs_sh/entities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ end
-- @param number channel Default CHAN_AUTO or CHAN_WEAPON for weapons
function ents_methods:emitSound(snd, lvl, pitch, volume, channel)
checkluatype(snd, TYPE_STRING)
snd = SF.CheckSound(snd)
snd = SF.CheckSound(instance.player, snd)

local ent = getent(self)
checkpermission(instance, ent, "entities.emitSound")
Expand Down
10 changes: 9 additions & 1 deletion lua/starfall/libs_sh/sound.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ end)
function sound_library.create(ent, path, nofilter)
checkluatype(path, TYPE_STRING)
if nofilter~=nil then checkluatype(nofilter, TYPE_BOOL) end
path = SF.CheckSound(path)
path = SF.CheckSound(instance.player, path)

checkpermission(instance, { ent, path }, "sound.create")

Expand Down Expand Up @@ -116,6 +116,14 @@ function sound_library.duration(path)
return SoundDuration(path)
end

--- Returns true if the sound or sound property exists.
-- @param string path String path to the sound file
-- @return boolean True if exists, false if not
function sound_library.exists(path)
checkluatype(path, TYPE_STRING)
return istable(sound.GetProperties(path)) or file.Exists("sound/" .. path, "GAME")
end

--------------------------------------------------

--- Starts to play the sound.
Expand Down
16 changes: 13 additions & 3 deletions lua/starfall/sflib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,12 @@ function SF.CheckModel(model, player, prop)
return model
end

function SF.CheckSound(path)
SF.UniqueSounds = {}
setmetatable(SF.UniqueSounds, {["__index"]=function(t,k) local newTab = {} t[k] = {} return {} end})

local maxUniqueSounds = CreateConVar("sf_sounds_unique_max"..(CLIENT and "_cl" or ""), tostring(200), FCVAR_ARCHIVE, "The maximum number of unique sounds paths allowed")

function SF.CheckSound(ply, path)
-- Limit length and remove invalid chars
if #path>260 then SF.Throw("Sound path too long!", 3) end
path = SF.NormalizePath(string.gsub(path, "[\"?']", ""))
Expand All @@ -1461,8 +1466,13 @@ function SF.CheckSound(path)
SF.Throw("Invalid sound flags! "..flags, 3)
end

if not (istable(sound.GetProperties(checkpath)) or file.Exists("sound/" .. checkpath, "GAME")) then
SF.Throw("Invalid sound path! "..checkpath, 3)
local UserUniqueSounds = SF.UniqueSounds[ply:SteamID()]
if not UserUniqueSounds[checkpath] then
if table.Count(UserUniqueSounds) >= maxUniqueSounds:GetInt() then
SF.Throw("The unique sounds limit has been reached.", 3)
else
UserUniqueSounds[checkpath] = true
end
end

return path
Expand Down

0 comments on commit 800241d

Please sign in to comment.