Skip to content

Commit

Permalink
New Features, Better Networking, Faster Performance
Browse files Browse the repository at this point in the history
- Added option for cursor in VR. It was requested by user Peekofwar on Steam.
- Added button for switching playback mode (no loop, song loop, playlist loop). Fixes #12
- Added option for the playback mode to the Toolgun.
- Added/Changed Wiremod ports represent the playback mode.
- Added support (including the GUI) for the Wiremod User entity. It can be used in contraptions to trigger use operations (+use) as like as play would do on other entities.
- Changed playlist icons to a new custom one.

- Moved networking from NW2Vars to NWVars, because NW2 is actually a buggy unfinished mess.
- Fixed multiple cases of networking being unreliable casing the radio being wonky in multiplayer. It should be much more robust now.
- Reduced networking overhead, by utilizing string tables for repeated strings.

- Improved overall performance.
- Fixed huge FPS drops when seeking songs. This happened also when the radio synchronizes between players or other radios (master / slave setup).
- The GUI is no longer re-rendered every frame when radio playback is paused or stopped. It still has too when it play backs a song, though.

- Fixed several UI / UX bugs, causing hiccups, stucked playback and other wonky behaviors.
- Replaced hacky hashing algorithms (for networking). Because the game actually ships with decent official ones now, we no longer have to abuse util.CRC()
  • Loading branch information
Grocel committed Jun 8, 2023
1 parent fcbd0f5 commit e950f54
Show file tree
Hide file tree
Showing 65 changed files with 3,222 additions and 2,087 deletions.
2 changes: 1 addition & 1 deletion glualint.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint_shadowing": false,
"lint_gotos": true,
"lint_doubleNegations": true,
"lint_redundantIfStatements": true,
"lint_redundantIfStatements": false,
"lint_redundantParentheses": true,
"lint_duplicateTableKeys": true,
"lint_profanity": true,
Expand Down
30 changes: 10 additions & 20 deletions lua/autorun/streamradio_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -239,31 +239,26 @@ local function saveinclude(lua, force)

-- Anything below is advanced error handling.
-- It is to ensure that the addon has loaded correctly and completely without errors.
-- CompileFile gives your control in this regard

if force then
g_loaded[lua] = nil
end

if g_loaded[lua] then
-- Prevent loading twice
return true, g_loaded[lua]
end

local status, err = pcall(function()
local status, errOrResult = pcall(function()
if not file.Exists(lua, "LUA") then
error("Couldn't include file '" .. lua .. "' (File not found)", 0)
end

local func = CompileFile(lua)
if not func then
error("Couldn't include file '" .. lua .. "' (Syntax error)", 0)
end

return func()
return include(lua)
end)

if not status then
err = tostring(err or "")
local err = tostring(errOrResult or "")

if err == "" then
err = "Unknown error"
Expand All @@ -285,8 +280,8 @@ local function saveinclude(lua, force)
return nil
end

g_loaded[lua] = err
return status, err
g_loaded[lua] = errOrResult
return status, errOrResult
end

local function loadBASS3()
Expand Down Expand Up @@ -532,9 +527,9 @@ else
end

if SV then
util.AddNetworkString("3D_StreamRadio_LoadError")
util.AddNetworkString("3DStreamRadio/LoadError")

hook.Add("PlayerInitialSpawn", "3D_StreamRadio_LoadError", function(ply)
hook.Add("PlayerInitialSpawn", "3DStreamRadio/LoadError", function(ply)
if not IsValid(ply) then
return
end
Expand All @@ -547,12 +542,12 @@ if SV then
return
end

net.Start("3D_StreamRadio_LoadError")
net.Start("3DStreamRadio/LoadError")
net.WriteString(StreamRadioLib.ErrorString or "")
net.Send(ply)
end)
else
net.Receive("3D_StreamRadio_LoadError", function()
net.Receive("3DStreamRadio/LoadError", function()
local err = net.ReadString()
if err == "" then return end
if not StreamRadioLib then return end
Expand All @@ -565,9 +560,4 @@ else
end)
end

concommand.Add("debug_streamradio_reload", function()
if not StreamRadioLib then return end
StreamRadioLib.LoadSH(thisfile)
end)

collectgarbage( "collect" )
76 changes: 65 additions & 11 deletions lua/entities/base_streamradio.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ AddCSLuaFile()

DEFINE_BASECLASS("base_anim")

local StreamRadioLib = StreamRadioLib
local LIBNetwork = StreamRadioLib.Network
local LIBWire = StreamRadioLib.Wire

local WireLib = WireLib

local IsValid = IsValid
Expand All @@ -28,7 +32,7 @@ local CLIENT = CLIENT

ENT.__IsRadio = true
ENT.__IsLibLoaded = StreamRadioLib and StreamRadioLib.Loaded
ENT.__IsWiremodLoaded = ENT.__IsLibLoaded and StreamRadioLib.HasWiremod()
ENT.__IsWiremodLoaded = ENT.__IsLibLoaded and LIBWire.HasWiremod()

ENT.Editable = false
ENT.Spawnable = false
Expand All @@ -41,15 +45,15 @@ function ENT:AddDTNetworkVar(datatype, name, ...)
return
end

return StreamRadioLib.Network.AddDTNetworkVar(self, datatype, name, ...)
return LIBNetwork.AddDTNetworkVar(self, datatype, name, ...)
end

function ENT:SetupDataTables()
if not self.__IsLibLoaded then
return
end

StreamRadioLib.Network.SetupDataTables(self)
LIBNetwork.SetupDataTables(self)
end

function ENT:SetAnim( Animation, Frame, Rate )
Expand Down Expand Up @@ -162,6 +166,7 @@ function ENT:GetOrCreateStream()
end)

stream:SetName("stream")
stream:SetNWName("str")
stream:SetEntity(self)
stream:ActivateNetworkedMode()
stream:OnClose()
Expand All @@ -172,16 +177,19 @@ end

function ENT:StreamOnConnect()
self:CheckTransmitState()

return true
end

function ENT:StreamOnSearch()
self:CheckTransmitState()

return true
end

function ENT:StreamOnRetry()
self:CheckTransmitState()

return true
end

Expand Down Expand Up @@ -257,7 +265,11 @@ function ENT:GetSoundPosAng()
return pos, ang
end

function ENT:DistanceToPlayer(ply, pos1, pos2)
function ENT:DistanceToEntity(ent, pos1, pos2)
if not self.__IsLibLoaded then
return 0
end

if not pos1 then
pos1 = self:GetSoundPosAng()
end
Expand All @@ -266,9 +278,7 @@ function ENT:DistanceToPlayer(ply, pos1, pos2)
return pos2:Distance(pos1)
end

if self.__IsLibLoaded then
pos2 = StreamRadioLib.GetCameraPos(ply)
end
pos2 = StreamRadioLib.GetCameraPos(ent)

if not pos2 then
return 0
Expand All @@ -277,6 +287,39 @@ function ENT:DistanceToPlayer(ply, pos1, pos2)
return pos2:Distance(pos1)
end

function ENT:DistToSqrToEntity(ent, pos1, pos2)
if not self.__IsLibLoaded then
return 0
end

if not pos1 then
pos1 = self:GetSoundPosAng()
end

if pos2 then
return pos2:DistToSqr(pos1)
end

pos2 = StreamRadioLib.GetCameraPos(ent)

if not pos2 then
return 0
end

return pos2:DistToSqr(pos1)
end

function ENT:CheckDistanceToEntity(ent, maxDist, pos1, pos2)
local maxDistSqr = maxDist * maxDist
local distSqr = self:DistToSqrToEntity(ent, pos1, pos2)

if distSqr > maxDistSqr then
return false
end

return true
end

function ENT:Initialize()
if self.__IsLibLoaded then
StreamRadioLib.SpawnedRadios[self] = true
Expand All @@ -287,7 +330,6 @@ function ENT:Initialize()
end

self:GetOrCreateStream()

self:CheckTransmitState()
end

Expand All @@ -305,14 +347,26 @@ function ENT:IsMutedForPlayer(ply)
return true
end

if not IsValid(ply) and CLIENT then
ply = LocalPlayer()
end

if not IsValid(ply) then return true end
if not ply:IsPlayer() then return true end
if ply:IsBot() then return true end

if StreamRadioLib.IsMuted(ply) then
return true
end

local playerdist = self:DistanceToPlayer(ply)
local mutedist = math.min(self:GetRadius() + 1000, StreamRadioLib.GetMuteDistance(ply))
local camPos = nil

if CLIENT then
camPos = StreamRadioLib.GetCameraViewPos(ply)
end

if playerdist >= mutedist then
if not self:CheckDistanceToEntity(ply, mutedist, nil, camPos) then
return true
end

Expand Down Expand Up @@ -403,7 +457,7 @@ function ENT:FastThink()
return
end

StreamRadioLib.Network.Pull(self)
LIBNetwork.Pull(self)

if SERVER then
if self.__IsWiremodLoaded then
Expand Down
Loading

0 comments on commit e950f54

Please sign in to comment.