shadowndacorner

GLua Sending Tables to Clients

Mar 19th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.23 KB | None | 0 0
  1. --SERVER--
  2. if SERVER then
  3.     function SendTableToClient(tab, name, ply, containing_table, ...)
  4.         for f, v in pairs(tab) do
  5.             local t=type(f)
  6.             local t2=type(v)
  7.            
  8.             if !ValidEntity(ply) then
  9.                 if !type(ply)=="table" then
  10.                     ply=RecipientFilter():AddAllPlayers()
  11.                 else
  12.                     ply=RecipientFilter()
  13.                     for k, pl in pairs(ply) do
  14.                         if ValidEntity(pl) then
  15.                             ply:AddPlayer(pl)
  16.                         end
  17.                     end
  18.                 end
  19.             end
  20.            
  21.             if !containing_table then -- If it wasn't already part of a table --
  22.                 if t2=="table" then
  23.                     SendTableToClient(v, name, ply, true, {[1]=f}) -- if the value IS a table --
  24.                 else
  25.                     umsg.Start("_ServersideTable", ply)
  26.                         umsg.String(name)
  27.                         umsg.String(t) -- Send key type --
  28.                        
  29.                         -- Below sends key --
  30.                        
  31.                         if ( t == "string" ) then
  32.                             umsg.String( v )
  33.                         elseif ( IsEntity( v ) ) then
  34.                             umsg.Entity( v )
  35.                         elseif ( t == "number" ) then
  36.                             umsg.Long( v )
  37.                         elseif ( t == "Vector" ) then
  38.                             umsg.Vector( v )
  39.                         elseif ( t == "Angle" ) then
  40.                             umsg.Angle( v )
  41.                         elseif ( t == "boolean" ) then
  42.                             umsg.Bool( v )
  43.                         end
  44.                        
  45.                         umsg.String(t2) -- Send value type --
  46.                        
  47.                         -- Below sends value --
  48.                        
  49.                         if ( t2 == "string" ) then
  50.                             umsg.String( v )
  51.                         elseif ( IsEntity( v ) ) then
  52.                             umsg.Entity( v )
  53.                         elseif ( t2 == "number" ) then
  54.                             umsg.Long( v )
  55.                         elseif ( t2 == "Vector" ) then
  56.                             umsg.Vector( v )
  57.                         elseif ( t2 == "Angle" ) then
  58.                             umsg.Angle( v )
  59.                         elseif ( t2 == "boolean" ) then
  60.                             umsg.Bool( v )
  61.                         end
  62.                     umsg.End()
  63.                 else -- otherwise --
  64.                     if type(v)=="table" then
  65.                         local hierarchy={...}
  66.                         hierarchy[#hierarchy+1]=f
  67.                         SendTableToClient(v, name, ply, true, hierarchy)
  68.                     else
  69.                         umsg.Start("_ServersideTableCont", ply)
  70.                             umsg.String(name)
  71.                             umsg.String(glon.encode({...}))
  72.                             umsg.String(t) -- Send key type
  73.                            
  74.                             -- Below sends key --
  75.                            
  76.                             if ( t == "string" ) then
  77.                                 umsg.String( f )
  78.                             elseif ( IsEntity( f ) ) then
  79.                                 umsg.Entity( f )
  80.                             elseif ( t == "number" ) then
  81.                                 umsg.Long( f )
  82.                             elseif ( t == "Vector" ) then
  83.                                 umsg.Vector( f )
  84.                             elseif ( t == "Angle" ) then
  85.                                 umsg.Angle( f )
  86.                             elseif ( t == "boolean" ) then
  87.                                 umsg.Bool( f )
  88.                             end
  89.                            
  90.                             umsg.String(t2) -- Send value type
  91.                            
  92.                             -- Below sends value --
  93.                            
  94.                             if ( t2 == "string" ) then
  95.                                 umsg.String( v )
  96.                             elseif ( IsEntity( v ) ) then
  97.                                 umsg.Entity( v )
  98.                             elseif ( t2 == "number" ) then
  99.                                 umsg.Long( v )
  100.                             elseif ( t2 == "Vector" ) then
  101.                                 umsg.Vector( v )
  102.                             elseif ( t2 == "Angle" ) then
  103.                                 umsg.Angle( v )
  104.                             elseif ( t2 == "boolean" ) then
  105.                                 umsg.Bool( v )
  106.                             end
  107.                         umsg.End()
  108.                     end
  109.                 end
  110.             end
  111.         end
  112.     end
  113.  
  114.     local tab={}
  115.     tab["Twenty"]=20
  116.     tab["TwentyOne"]=21
  117.     tab["TwentyTwo"]=22
  118.     tab["MoreNumbers"]={23, 24}
  119.     tab["MoreNumbers"].Wha="Testing"
  120.  
  121.     SendTableToClient(tab, "Numbers")
  122. end
  123.  
  124. --CLIENT--
  125. if !CLIENT then return end
  126.  
  127. ServerData={}
  128. usermessage.Hook("_ServersideTable", function(data)
  129.     local tabname=data:ReadString()
  130.     local t=data:ReadString()
  131.     local f
  132.     local v
  133.    
  134.     if ( t == "string" ) then
  135.         f=data:ReadString()
  136.     elseif ( t=="Entity" ) then
  137.         f=data:ReadEntity()
  138.     elseif ( t == "number" ) then
  139.         f=data:ReadLong()
  140.     elseif ( t == "Vector" ) then
  141.         f=data:ReadVector()
  142.     elseif ( t == "Angle" ) then
  143.         f=data:ReadAngle()
  144.     elseif ( t == "boolean" ) then
  145.         f=data:ReadBool()
  146.     end
  147.    
  148.     local t2=data:ReadString()
  149.     if ( t2 == "string" ) then
  150.         v=data:ReadString()
  151.     elseif ( t2=="Entity" ) then
  152.         v=data:ReadEntity()
  153.     elseif ( t2 == "number" ) then
  154.         v=data:ReadLong()
  155.     elseif ( t2 == "Vector" ) then
  156.         v=data:ReadVector()
  157.     elseif ( t2 == "Angle" ) then
  158.         v=data:ReadAngle()
  159.     elseif ( t2 == "boolean" ) then
  160.         v=data:ReadBool()
  161.     end
  162.    
  163.     if ( !(tabname) || !(f) || !(v) ) then return end
  164.    
  165.     ServerData.tabname=ServerData.tabname or {}
  166.     ServerData.tabname[f]=v
  167. end)
  168.  
  169. usermessage.Hook("_ServersideTableCont", function(data)
  170.     local tabname=data:ReadString()
  171.     local cont=glon.decode(data:ReadString())
  172.     local t=data:ReadString()
  173.     local f
  174.     local v
  175.    
  176.     if ( t == "string" ) then
  177.         f=data:ReadString()
  178.     elseif ( t=="Entity" ) then
  179.         f=data:ReadEntity()
  180.     elseif ( t == "number" ) then
  181.         f=data:ReadLong()
  182.     elseif ( t == "Vector" ) then
  183.         f=data:ReadVector()
  184.     elseif ( t == "Angle" ) then
  185.         f=data:ReadAngle()
  186.     elseif ( t == "boolean" ) then
  187.         f=data:ReadBool()
  188.     end
  189.    
  190.     local t2=data:ReadString()
  191.     if ( t2 == "string" ) then
  192.         v=data:ReadString()
  193.     elseif ( t2=="Entity" ) then
  194.         v=data:ReadEntity()
  195.     elseif ( t2 == "number" ) then
  196.         v=data:ReadLong()
  197.     elseif ( t2 == "Vector" ) then
  198.         v=data:ReadVector()
  199.     elseif ( t2 == "Angle" ) then
  200.         v=data:ReadAngle()
  201.     elseif ( t2 == "boolean" ) then
  202.         v=data:ReadBool()
  203.     end
  204.    
  205.     if ( !(tabname) || !(f) || !(v) || !(cont) ) then return end
  206.    
  207.     ServerData.tabname=ServerData.tabname or {}
  208.     ServerData.tabname[cont]=ServerData.tabname[cont] or {}
  209.     ServerData.tabname[cont][f]=v
  210. end)
  211.  
  212. function GetServerTable(name)
  213.     return ServerData[name] or nil
  214. end
Advertisement
Add Comment
Please, Sign In to add comment