Skip to content

Commit

Permalink
- Fixed Lua-Error: lua/streamradio_core/client/cl_vgui.lua:22: attemp…
Browse files Browse the repository at this point in the history
…t to index field 'Stream' (a nil value)
  • Loading branch information
Grocel committed Mar 7, 2023
1 parent 22423c5 commit 8eb3c20
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 120 deletions.
4 changes: 3 additions & 1 deletion lua/autorun/streamradio_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ local function saveinclude(lua, force)
return true, result
end

-- Anything below is too ensure that the addon has loaded correctly without errors
-- 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
Expand Down
35 changes: 18 additions & 17 deletions lua/entities/base_streamradio.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function ENT:SetDupePoses( PoseParameter )
end
end

function ENT:CreateStream()
function ENT:GetOrCreateStream()
if not self.__IsLibLoaded then
if IsValid(self.StreamObj) then
self.StreamObj:Remove()
Expand All @@ -113,8 +113,8 @@ function ENT:CreateStream()
return self.StreamObj
end

self.StreamObj = StreamRadioLib.CreateOBJ("stream")
if not IsValid( self.StreamObj ) then
local stream = StreamRadioLib.CreateOBJ("stream")
if not IsValid( stream ) then
self.StreamObj = nil
return nil
end
Expand All @@ -133,39 +133,41 @@ function ENT:CreateStream()
return func(self, ...)
end

self.StreamObj.OnConnect = function( ... )
stream.OnConnect = function( ... )
return call("StreamOnConnect", ...)
end

self.StreamObj.OnError = function( ... )
stream.OnError = function( ... )
return call("StreamOnError", ...)
end

self.StreamObj.OnClose = function( ... )
stream.OnClose = function( ... )
return call("StreamOnClose", ...)
end

self.StreamObj.OnRetry = function( ... )
stream.OnRetry = function( ... )
return call("StreamOnRetry", ...)
end

self.StreamObj.OnSearch = function( ... )
stream.OnSearch = function( ... )
return call("StreamOnSearch", ...)
end

self.StreamObj.OnMute = function( ... )
stream.OnMute = function( ... )
return call("StreamOnMute", ...)
end

self.StreamObj:SetEvent("OnPlayModeChange", tostring(self) .. "_base", function(...)
stream:SetEvent("OnPlayModeChange", tostring(self) .. "_base", function(...)
return call("StreamOnPlayModeChange", ...)
end)

self.StreamObj:SetName("stream")
self.StreamObj:SetEntity(self)
self.StreamObj:ActivateNetworkedMode()
self.StreamObj:OnClose()
return self.StreamObj
stream:SetName("stream")
stream:SetEntity(self)
stream:ActivateNetworkedMode()
stream:OnClose()

self.StreamObj = stream
return stream
end

function ENT:StreamOnConnect()
Expand Down Expand Up @@ -284,7 +286,7 @@ function ENT:Initialize()
self._WireOutputCache = {}
end

self:CreateStream()
self:GetOrCreateStream()

self:CheckTransmitState()
end
Expand Down Expand Up @@ -459,7 +461,6 @@ end
if SERVER then
function ENT:SetStreamURL(...)
if not IsValid(self.StreamObj) then return end

self.StreamObj:SetURL(...)
end

Expand Down
12 changes: 3 additions & 9 deletions lua/streamradio_core/classes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,11 @@ function StreamRadioLib.CreateOBJ(name, ...)
name = normalize_classname(name)

local class = StreamRadioLib.Classes[name]
assert(class, "Class '" .. name .. "' does not exist!")

if not class.new then
return nil
end
assert(istable(class), "Class '" .. name .. "' does not exist!")
assert(class.new, "Bad class table '" .. name .. "' detected!")

local obj = class:new()

if not obj then
return nil
end
assert(istable(obj), "Object from class '" .. name .. "' could not be created!")

if obj.Create then
obj:Create(...)
Expand Down
7 changes: 6 additions & 1 deletion lua/streamradio_core/client/cl_lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,12 @@ if ( StreamRadioLib.TestChannel ) then
end

local function testchannel( args )
local TestChannel = StreamRadioLib.TestChannel or StreamRadioLib.CreateStream()
local TestChannel = StreamRadioLib.TestChannel or StreamRadioLib.CreateOBJ("stream")

if not IsValid(TestChannel) then
ErrorNoHaltWithStack("Could not create the TestChannel!\n")
return
end

TestChannel:SetVolume( tonumber( args[2] ) )
TestChannel:Play()
Expand Down
176 changes: 98 additions & 78 deletions lua/streamradio_core/client/cl_vgui.lua
Original file line number Diff line number Diff line change
@@ -1,74 +1,14 @@
local StreamRadioLib = StreamRadioLib or {}
local StreamRadioLibDraw = StreamRadioLib.Surface
local RadioAddon = StreamRadioLibDraw and StreamRadioLib.Loaded and StreamRadioLib.GetDefaultColors and true

local surface = surface
local draw = draw
local vgui = vgui
local string = string
local math = math

local Color = Color
local CurTime = CurTime
local pairs = pairs
local RunConsoleCommand = RunConsoleCommand

local PANEL = {}
AccessorFunc( PANEL, "m_strValue", "Value" )
AccessorFunc( PANEL, "m_strValue", "Text" )

function PANEL:Init( )
self.Stream = StreamRadioLib.CreateStream()
self.Stream:Set3D(false)
self.Stream:SetLoop(false)
self.Stream:SetVolume(0)

self.Stream.OnConnect = function( stream, channel )
stream:Stop()

self.Error = nil

if not IsValid(self) then
return
end

self:UpdateURLState(true)
end

self.Stream.OnError = function( stream, err )
stream:Stop()

self.Error = err

if not IsValid(self) then
return
end

self:UpdateURLState(false)
end

self.Stream.OnRetry = function( stream, err )
if not IsValid(self) then
return false
end

self:UpdateURLState()
return true
end

self.Stream.OnSearch = function( stream, err )
if not IsValid( self ) then
return false
end

self:UpdateURLState()
return true
end

self.Stream.OnDownload = function( stream, url, interface )
return false
end
local STATE_FOUND = 2
local STATE_ERROR = 1
local STATE_IDLE = 0

function PANEL:Init( )
self:SetPaintBackground( false )
self.URLIcon = self:Add( "DImageButton" )
self.URLIcon:SetImage( "icon16/arrow_refresh.png" )
Expand All @@ -90,12 +30,13 @@ function PANEL:Init( )
return
end

if not IsValid(self.Stream) then
local stream = self.Stream
if not IsValid(stream) then
return
end

local err = self.Error
local url = self.Stream:GetURL()
local url = stream:GetURL()

if not err then
return
Expand Down Expand Up @@ -178,6 +119,82 @@ You can enter this:
self:SetValue("")
end

function PANEL:GetOrCreateStream()
if not StreamRadioLib and StreamRadioLib.Loaded then
if IsValid(self.Stream) then
self.Stream:Remove()
end

self.Stream = nil
self.Error = nil

return nil
end

if IsValid(self.Stream) then
return self.Stream
end

local stream = StreamRadioLib.CreateOBJ("stream")
if not IsValid( stream ) then
self.Stream = nil
self.Error = nil

return nil
end

stream:Set3D(false)
stream:SetLoop(false)
stream:SetVolume(0)

stream.OnConnect = function( thisStream, channel )
thisStream:Stop()

if not IsValid(self) then
return
end

self.Error = nil
self:UpdateURLState(STATE_FOUND)
end

stream.OnError = function( thisStream, err )
thisStream:Stop()

if not IsValid(self) then
return
end

self.Error = err
self:UpdateURLState(STATE_ERROR)
end

stream.OnRetry = function( thisStream, err )
if not IsValid(self) then
return false
end

self:UpdateURLState(STATE_IDLE)
return true
end

stream.OnSearch = function( thisStream, err )
if not IsValid( self ) then
return false
end

self:UpdateURLState(STATE_IDLE)
return true
end

stream.OnDownload = function( thisStream, url, interface )
return false
end

self.Stream = stream
return stream
end

function PANEL:SetValue(value)
self.m_strValue = tostring(value or "")
self.URLText:SetValue(self.m_strValue)
Expand All @@ -198,8 +215,8 @@ function PANEL:SetConVar(convar)
self.URLText:SetConVar(convar)
end

function PANEL:UpdateURLState(bool)
if bool == nil then
function PANEL:UpdateURLState(state)
if state == STATE_IDLE then
self.URLIcon:SetImage("icon16/arrow_refresh.png")
self.URLIcon:SetTooltip("Checking URL...")
self.URLText:SetTooltip(self.URLTooltip .. "\n\nChecking URL...")
Expand All @@ -216,7 +233,7 @@ function PANEL:UpdateURLState(bool)
url = self.Stream:GetURL()
end

if bool == false then
if state == STATE_ERROR then

local tooltipbase = "The URL is not valid!"
local tooltip = ""
Expand Down Expand Up @@ -247,7 +264,7 @@ function PANEL:UpdateURLState(bool)
return
end

if bool == true then
if state == STATE_FOUND then
self.URLIcon:SetImage("icon16/accept.png")
self.URLIcon:SetTooltip("The URL is valid!")
self.URLText:SetTooltip(self.URLTooltip)
Expand All @@ -262,20 +279,23 @@ function PANEL:UpdateURLState(bool)
end

function PANEL:CheckURL()
self.Stream:TimerRemove("gui_url_checker")
self:UpdateURLState()
local stream = self:GetOrCreateStream()

if IsValid(stream) then
stream:TimerRemove("gui_url_checker")
end

local stream = self.Stream
self:UpdateURLState(STATE_IDLE)

if not IsValid(stream) then
self.Error = nil
self:UpdateURLState(false)
self:UpdateURLState(STATE_ERROR)
return
end

if not self.m_strValue then
self.Error = nil
self:UpdateURLState(false)
self:UpdateURLState(STATE_ERROR)
stream:SetURL("")
stream:Stop()

Expand All @@ -284,14 +304,14 @@ function PANEL:CheckURL()

if self.m_strValue == "" then
self.Error = nil
self:UpdateURLState(false)
self:UpdateURLState(STATE_ERROR)
stream:SetURL("")
stream:Stop()

return false
end

self.Stream:TimerOnce("gui_url_checker", 1, function()
stream:TimerOnce("gui_url_checker", 1, function()
if not IsValid(stream) then
return
end
Expand All @@ -300,7 +320,7 @@ function PANEL:CheckURL()
return
end

self:UpdateURLState()
self:UpdateURLState(STATE_IDLE)
stream:SetURL(self.m_strValue)
stream:Play()
end)
Expand Down
Loading

0 comments on commit 8eb3c20

Please sign in to comment.