Add sound flag checking to soundExists (#3133)

* Add sound flag checking to soundExists

* Also remove some precache sound calls

* Made pathcheck only when there's not a player specified

* Optimize
This commit is contained in:
thegrb93 2024-09-19 04:26:53 -04:00 committed by GitHub
parent 1dbd2bd1c5
commit ee7e76e4e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 26 additions and 11 deletions

View File

@ -72,7 +72,7 @@ end
local function soundCreate(self, entity, index, time, path, fade)
path = WireLib.SoundExists(path)
path = WireLib.SoundExists(path, self.player)
if not path then return end
local data = self.data.sound_data
if not isAllowed( self ) then return end

View File

@ -16,7 +16,6 @@ local DefaultSamples = {
"synth/tri.wav",
"synth/sine.wav"
}
for _, str in pairs(DefaultSamples) do util.PrecacheSound(str) end
function ENT:Initialize()
self:PhysicsInit( SOLID_VPHYSICS )
@ -169,7 +168,6 @@ function ENT:SetSound(soundName)
soundName = WireLib.SoundExists(soundName)
if not soundName then return end
util.PrecacheSound(soundName)
self.sound = soundName
self.SoundProperties = sound.GetProperties(soundName)

View File

@ -338,8 +338,6 @@ local function PlaySound(file, volume, pitch)
local ply = LocalPlayer()
if not IsValid(ply) then return end
util.PrecacheSound(file)
SoundObj = CreateSound(ply, file)
if SoundObj then
SoundObj:PlayEx(tonumber(volume) or 1, tonumber(pitch) or 100)
@ -358,8 +356,6 @@ local function PlaySoundNoEffect(file)
local ply = LocalPlayer()
if not IsValid(ply) then return end
util.PrecacheSound(file)
SoundObjNoEffect = CreateSound(ply, file)
if SoundObjNoEffect then
SoundObjNoEffect:PlayEx(1, 100)

View File

@ -1536,11 +1536,32 @@ if not WireLib.PatchedDuplicator then
end
end
function WireLib.SoundExists(path)
path = string.GetNormalizedFilepath(string.gsub(string.sub(path, 1, 260), '["?]', ''))
if istable(sound.GetProperties(path)) or file.Exists("sound/" .. path, "GAME") then
return path
local uniqueSoundsTbl = setmetatable({}, {__index=function(t,k) local r={[1]=0} t[k]=r return r end})
local maxUniqueSounds = CreateConVar("wire_sounds_unique_max", "200", FCVAR_ARCHIVE, "The maximum number of sound paths a player is allowed to cache")
function WireLib.SoundExists(path, ply)
-- Limit length and remove invalid chars
path = string.GetNormalizedFilepath(string.gsub(string.sub(path, 1, 260), "[\"?']", ""))
-- Extract sound flags. Only allowed flags are '<', '>', '^', ')'
local flags, checkpath = string.match(path, "^([^%w_/%.]*)(.*)")
if #flags>2 or string.match(flags, "[^<>%^%)]") then
path = checkpath
end
if ply then
-- A player can only use a certain number of unique sound paths
local playerSounds = uniqueSoundsTbl[ply:SteamID()]
if not playerSounds[checkpath] then
if playerSounds[1] >= maxUniqueSounds:GetInt() then return end
playerSounds[checkpath] = true
playerSounds[1] = playerSounds[1] + 1
end
elseif not (istable(sound.GetProperties(checkpath)) or file.Exists("sound/" .. checkpath, "GAME")) then
return
end
return path
end
-- Notify --