Skip to content

Commit 44a98f2

Browse files
committed
Debounce url logging for same content. Avoids spam.
- Improve output of the radio entity also. - Entity reload keeps motion state.
1 parent 4d21d94 commit 44a98f2

5 files changed

Lines changed: 91 additions & 23 deletions

File tree

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
449
2-
1733522924
1+
450
2+
1750700988

lua/entities/base_streamradio.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ ENT.Editable = false
2020
ENT.Spawnable = false
2121
ENT.AdminOnly = false
2222

23-
ENT.WireDebugName = "Stream Radio"
24-
2523
function ENT:AddDTNetworkVar(datatype, name, ...)
2624
if not g_isLoaded then
2725
return

lua/entities/sent_streamradio/init.lua

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ include( "shared.lua" )
44
DEFINE_BASECLASS( "base_streamradio_gui" )
55

66
local StreamRadioLib = StreamRadioLib
7+
local LIBPrint = StreamRadioLib.Print
78
local LIBError = StreamRadioLib.Error
89
local LIBWire = StreamRadioLib.Wire
910
local LIBUtil = StreamRadioLib.Util
@@ -208,25 +209,49 @@ function ENT:OnTakeDamage( dmg )
208209
self:TakePhysicsDamage( dmg )
209210
end
210211

211-
function ENT:OnReloaded( )
212-
if not IsValid( self ) then return end
212+
function ENT:OnReloaded()
213+
if not IsValid(self) then return end
213214
if not g_isLoaded then return end
214215

215-
local ply, model, pos, ang = self:GetRealRadioOwner(), self:GetModel( ), self:GetPos( ), self:GetAngles( )
216-
StreamRadioLib.Print.Msg(ply, "Reloaded %s", tostring(self))
217-
self:Remove( )
218-
219-
StreamRadioLib.Timedcall( function( ply, model, pos, ang )
220-
local ent = StreamRadioLib.SpawnRadio( ply, model, pos, ang )
221-
if ( not IsValid( ent ) ) then return end
222-
if ( not IsValid( ply ) ) then return end
223-
local TOOL_Class = "streamradio"
224-
undo.Create( TOOL_Class )
225-
undo.AddEntity( ent )
226-
undo.SetPlayer( ply )
227-
undo.Finish( )
228-
ply:AddCleanup( TOOL_Class, ent )
229-
end, ply, model, pos, ang )
216+
local ply = self:GetRealRadioOwner()
217+
local model = self:GetModel()
218+
local pos = self:GetPos()
219+
local ang = self:GetAngles()
220+
local radioName = LIBPrint.GetRadioEntityString(self)
221+
222+
local motion = false
223+
224+
local selfPhys = self:GetPhysicsObject()
225+
if IsValid(selfPhys) then
226+
motion = selfPhys:IsMotionEnabled()
227+
end
228+
229+
self:Remove()
230+
231+
StreamRadioLib.Timedcall(function(ply, model, pos, ang)
232+
local ent = StreamRadioLib.SpawnRadio(ply, model, pos, ang)
233+
if not IsValid(ent) then
234+
return
235+
end
236+
237+
local entPhys = ent:GetPhysicsObject()
238+
if IsValid(entPhys) then
239+
entPhys:EnableMotion(motion)
240+
end
241+
242+
LIBPrint.Msg(ply, "%s respawned after reload.", radioName)
243+
244+
if IsValid(ply) then
245+
local TOOL_Class = "streamradio"
246+
247+
undo.Create(TOOL_Class)
248+
undo.AddEntity(ent)
249+
undo.SetPlayer(ply)
250+
undo.Finish()
251+
252+
ply:AddCleanup(TOOL_Class, ent)
253+
end
254+
end, ply, model, pos, ang)
230255
end
231256

232257
function ENT:UpdateVolume()

lua/streamradio_core/print.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ function LIB.GetPlayerString(ply)
7878
return playerStr
7979
end
8080

81+
function LIB.GetRadioEntityString(ent)
82+
if not IsValid(ent) then
83+
return tostring(ent or NULL)
84+
end
85+
86+
if not ent.__IsRadio then
87+
return tostring(ent)
88+
end
89+
90+
local radioName = string.format(
91+
"%s [%s]",
92+
ent.PrintName,
93+
ent:EntIndex()
94+
)
95+
96+
return radioName
97+
end
98+
8199
local g_colorSeparator = Color(255, 255, 255)
82100
local g_colorDateTime = Color(180, 180, 180)
83101
local g_colorAddonName = Color(0, 200, 0)

lua/streamradio_core/server/sv_lib.lua

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@ local StreamRadioLib = StreamRadioLib
33
local LIBNet = StreamRadioLib.Net
44
local LIBUrl = StreamRadioLib.Url
55
local LIBHook = StreamRadioLib.Hook
6+
local LIBUtil = StreamRadioLib.Util
67
local LIBPrint = StreamRadioLib.Print
78

89
LIBNet.Receive("Control", function( len, ply )
910
local trace = StreamRadioLib.Trace( ply )
1011
StreamRadioLib.Control(ply, trace, net.ReadBool())
1112
end)
1213

14+
local g_logDebounce = LIBUtil.CreateCacheArray(4096)
15+
16+
LIBHook.Add("PostCleanupMap", "ResetDebounceUrlLogging", function()
17+
g_logDebounce:Empty()
18+
end)
19+
1320
LIBHook.AddCustom("OnPlayStream", "UrlLogging", function(url, name, ent, user)
1421
local mode = StreamRadioLib.GetStreamLogMode()
1522

@@ -32,16 +39,36 @@ LIBHook.AddCustom("OnPlayStream", "UrlLogging", function(url, name, ent, user)
3239
nameHasUrl = true
3340
end
3441

42+
local debounceKey = string.format(
43+
"%s_%d_%s_%s",
44+
LIBPrint.GetPlayerString(user),
45+
ent:GetCreationID(),
46+
url,
47+
name
48+
)
49+
50+
-- Prevent shenanigans with ultra long urls or player names.
51+
debounceKey = StreamRadioLib.Util.Hash(debounceKey)
52+
local now = RealTime()
53+
54+
if g_logDebounce:Has(debounceKey, now) then
55+
return
56+
end
57+
3558
local msgstring = nil
3659
local onlinestring = offline and "file" or "online"
60+
local radioName = LIBPrint.GetRadioEntityString(ent)
3761

3862
if nameHasUrl then
39-
msgstring = LIBPrint.Format("STREAM - Radio '%s' plays %s => %s", ent, onlinestring, name)
63+
msgstring = LIBPrint.Format("STREAM - Radio '%s' plays %s => %s", radioName, onlinestring, name)
4064
else
41-
msgstring = LIBPrint.Format("STREAM - Radio '%s' plays %s => %s: %s", ent, onlinestring, name, url)
65+
msgstring = LIBPrint.Format("STREAM - Radio '%s' plays %s => %s: %s", radioName, onlinestring, name, url)
4266
end
4367

4468
LIBPrint.Log(user, msgstring)
69+
70+
-- Allow a new log entry/print with the same content after one minute.
71+
g_logDebounce:Set(debounceKey, true, now + 60)
4572
end)
4673

4774
return true

0 commit comments

Comments
 (0)