Update cl_util.lua

This commit is contained in:
Zach P 2021-06-12 16:03:30 -07:00 committed by GitHub
parent 050f1a9909
commit 9b4e0f4d4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,62 +24,10 @@ end
net.Receive( "URPC", ULibRPC )
--[[
Function: umsgRcv
Receive a umsg sent by ULib.umsgSend
Parameters:
um - The user message object
Returns:
The variable from the umsg.
]]
function ULib.umsgRcv( um, control )
local tv = control or um:ReadChar()
local ret -- Our return value
if tv == ULib.TYPE_STRING then
ret = um:ReadString()
elseif tv == ULib.TYPE_FLOAT then
ret = um:ReadFloat()
elseif tv == ULib.TYPE_SHORT then
ret = um:ReadShort()
elseif tv == ULib.TYPE_LONG then
ret = um:ReadLong()
elseif tv == ULib.TYPE_BOOLEAN then
ret = um:ReadBool()
elseif tv == ULib.TYPE_ENTITY then
ret = um:ReadEntity()
elseif tv == ULib.TYPE_VECTOR then
ret = um:ReadVector()
elseif tv == ULib.TYPE_ANGLE then
ret = um:ReadAngle()
elseif tv == ULib.TYPE_CHAR then
ret = um:ReadChar()
elseif tv == ULib.TYPE_TABLE_BEGIN then
ret = {}
while true do -- Yes an infite loop. We have a break inside.
local key = ULib.umsgRcv( um )
if key == nil then break end -- Here's our break
ret[ key ] = ULib.umsgRcv( um )
end
elseif tv == ULib.TYPE_TABLE_END then
return nil
elseif tv == ULib.TYPE_NIL then
return nil
else
ULib.error( "Unknown type passed to umsgRcv - " .. tv )
end
return ret
end
-- This will play sounds client side
local function rcvSound( um )
local str = um:ReadString()
net.Receive( "ulib_sound", function( ln )
local str = net.ReadString()
if not ULib.fileExists( "sound/" .. str ) then
Msg( "[LC ULib ERROR] Received invalid sound\n" )
return
@ -88,8 +36,7 @@ local function rcvSound( um )
if LocalPlayer():IsValid() then
LocalPlayer():EmitSound( Sound( str ) )
end
end
usermessage.Hook( "ulib_sound", rcvSound )
end )
local cvarinfo = {} -- Stores the client cvar object indexed by name of the server cvar
local reversecvar = {} -- Stores the name of server cvars indexed by the client cvar
@ -108,11 +55,13 @@ local function clCvarChanged( cl_cvar, oldvalue, newvalue )
end
-- This is the counterpart to <replicatedWithWritableCvar>. See that function for more info. We also add callbacks from here.
local function readCvar( um )
local sv_cvar = um:ReadString()
local cl_cvar = um:ReadString()
local default_value = um:ReadString()
local current_value = um:ReadString()
net.Receive( "ulib_repWriteCvar", function( len )
local sv_cvar = net.ReadString()
local cl_cvar = net.ReadString()
local default_value = net.ReadString()
local current_value = net.ReadString()
cvarinfo[ sv_cvar ] = GetConVar( cl_cvar ) or CreateClientConVar( cl_cvar, default_value, false, false ) -- Make sure it's created one way or another (second case is most common)
reversecvar[ cl_cvar ] = { sv_cvar=sv_cvar }
@ -124,17 +73,19 @@ local function readCvar( um )
RunConsoleCommand( cl_cvar, current_value )
end
end )
cvars.AddChangeCallback( cl_cvar, clCvarChanged )
end
usermessage.Hook( "ulib_repWriteCvar", readCvar )
end )
-- This is called when they've attempted to change a cvar they don't have access to.
local function changeCvar( um )
local ply = um:ReadEntity()
local cl_cvar = um:ReadString()
local oldvalue = um:ReadString()
local newvalue = um:ReadString()
net.Receive( "ulib_repChangeCvar", function( len )
local ply = net.ReadEntity()
local cl_cvar = net.ReadString()
local oldvalue = net.ReadString()
local newvalue = net.ReadString()
local changed = oldvalue ~= newvalue
if not reversecvar[ cl_cvar ] then -- Error!
@ -153,5 +104,4 @@ local function changeCvar( um )
RunConsoleCommand( cl_cvar, newvalue)
end
end )
end
usermessage.Hook( "ulib_repChangeCvar", changeCvar )
end )