Advertisement
Guest User

cl_util.lua

a guest
Nov 1st, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.77 KB | None | 0 0
  1. --[[
  2.     Title: Utilities
  3.  
  4.     Some client-side utilties
  5. ]]
  6.  
  7. local function ULibRPC()
  8.     local fn_string = net.ReadString()
  9.     local args = net.ReadTable()
  10.     local fn = ULib.findVar( fn_string )
  11.     if type( fn ) ~= "function" then return error( "Received bad RPC, invalid function (" .. tostring( fn_string ) .. ")!" ) end
  12.  
  13.     -- Since the table length operator can't always be trusted if there are holes in it, find the length by ourself
  14.     local max = 0
  15.     for k, v in pairs( args ) do
  16.         local n = tonumber( k )
  17.         if n and n > max then
  18.             max = n
  19.         end
  20.     end
  21.  
  22.     fn( unpack( args, 1, max ) )
  23. end
  24. net.Receive( "URPC", ULibRPC )
  25.  
  26. --[[
  27.     Function: umsgRcv
  28.  
  29.     Receive a umsg sent by ULib.umsgSend
  30.  
  31.     Parameters:
  32.  
  33.         um - The user message object
  34.  
  35.     Returns:
  36.  
  37.         The variable from the umsg.
  38. ]]
  39. function ULib.umsgRcv( um, control )
  40.     local tv = control or um:ReadChar()
  41.  
  42.     local ret -- Our return value
  43.     if tv == ULib.TYPE_STRING then
  44.         ret = um:ReadString()
  45.     elseif tv == ULib.TYPE_FLOAT then
  46.         ret = um:ReadFloat()
  47.     elseif tv == ULib.TYPE_SHORT then
  48.         ret = um:ReadShort()
  49.     elseif tv == ULib.TYPE_LONG then
  50.         ret = um:ReadLong()
  51.     elseif tv == ULib.TYPE_BOOLEAN then
  52.         ret = um:ReadBool()
  53.     elseif tv == ULib.TYPE_ENTITY then
  54.         ret = um:ReadEntity()
  55.     elseif tv == ULib.TYPE_VECTOR then
  56.         ret = um:ReadVector()
  57.     elseif tv == ULib.TYPE_ANGLE then
  58.         ret = um:ReadAngle()
  59.     elseif tv == ULib.TYPE_CHAR then
  60.         ret = um:ReadChar()
  61.     elseif tv == ULib.TYPE_TABLE_BEGIN then
  62.         ret = {}
  63.         while true do -- Yes an infite loop. We have a break inside.
  64.             local key = ULib.umsgRcv( um )
  65.             if key == nil then break end -- Here's our break
  66.             ret[ key ] = ULib.umsgRcv( um )
  67.         end
  68.     elseif tv == ULib.TYPE_TABLE_END then
  69.         return nil
  70.     elseif tv == ULib.TYPE_NIL then
  71.         return nil
  72.     else
  73.         ULib.error( "Unknown type passed to umsgRcv - " .. tv )
  74.     end
  75.  
  76.     return ret
  77. end
  78.  
  79. -- This will play sounds client side
  80. local function rcvSound( um )
  81.     local str = um:ReadString()
  82.     if not ULib.fileExists( "sound/" .. str ) then
  83.         Msg( "[LC ULib ERROR] Received invalid sound\n" )
  84.         return
  85.     end
  86.  
  87.     if LocalPlayer():IsValid() then
  88.         LocalPlayer():EmitSound( Sound( str ) )
  89.     end
  90. end
  91. usermessage.Hook( "ulib_sound", rcvSound )
  92.  
  93. local cvarinfo = {} -- Stores the client cvar object indexed by name of the server cvar
  94. local reversecvar = {} -- Stores the name of server cvars indexed by the client cvar
  95.  
  96. -- When our client side cvar is changed, notify the server to change it's cvar too.
  97. local function clCvarChanged( cl_cvar, oldvalue, newvalue )
  98.     if not reversecvar[ cl_cvar ] then -- Error
  99.         return
  100.     elseif reversecvar[ cl_cvar ].ignore then -- ignore
  101.         reversecvar[ cl_cvar ].ignore = nil
  102.         return
  103.     end
  104.  
  105.     local sv_cvar = reversecvar[ cl_cvar ].sv_cvar
  106.     RunConsoleCommand( "ulib_update_cvar", sv_cvar, newvalue )
  107. end
  108.  
  109. -- This is the counterpart to <replicatedWithWritableCvar>. See that function for more info. We also add callbacks from here.
  110. local function readCvar( um )
  111.     local sv_cvar = um:ReadString()
  112.     local cl_cvar = um:ReadString()
  113.     local default_value = um:ReadString()
  114.     local current_value = um:ReadString()
  115.  
  116.     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)
  117.     reversecvar[ cl_cvar ] = { sv_cvar=sv_cvar }
  118.  
  119.     ULib.queueFunctionCall( function() -- Queued to ensure we don't overload the client console
  120.         hook.Call( ULib.HOOK_REPCVARCHANGED, _, sv_cvar, cl_cvar, nil, nil, current_value )
  121.         if cvarinfo[ sv_cvar ]:GetString() ~= current_value then
  122.             reversecvar[ cl_cvar ].ignore = true -- Flag so hook doesn't do anything. Flag is removed at hook.
  123.             RunConsoleCommand( cl_cvar, current_value )
  124.         end
  125.     end )
  126.  
  127.     cvars.AddChangeCallback( cl_cvar, clCvarChanged )
  128. end
  129. usermessage.Hook( "ulib_repWriteCvar", readCvar )
  130.  
  131. -- This is called when they've attempted to change a cvar they don't have access to.
  132. local function changeCvar( um )
  133.     local ply = um:ReadEntity()
  134.     local cl_cvar = um:ReadString()
  135.     local oldvalue = um:ReadString()
  136.     local newvalue = um:ReadString()
  137.     local changed = oldvalue ~= newvalue
  138.  
  139.     if not reversecvar[ cl_cvar ] then -- Error!
  140.         return
  141.     end
  142.  
  143.     local sv_cvar = reversecvar[ cl_cvar ].sv_cvar
  144.  
  145.     ULib.queueFunctionCall( function() -- Queued so we won't overload the client console and so that changes are always going to be called via the hook AFTER the initial hook is called
  146.         if changed then
  147.             hook.Call( ULib.HOOK_REPCVARCHANGED, _, sv_cvar, cl_cvar, ply, oldvalue, newvalue )
  148.         end
  149.  
  150.         if GetConVarString( cl_cvar ) ~= newvalue then
  151.             reversecvar[ cl_cvar ].ignore = true -- Flag so hook doesn't do anything. Flag is removed at hook.
  152.             RunConsoleCommand( cl_cvar, newvalue)
  153.         end
  154.     end )
  155. end
  156. usermessage.Hook( "ulib_repChangeCvar", changeCvar )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement