Advertisement
CapsAdmin

Untitled

Jan 27th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.60 KB | None | 0 0
  1. local functions =
  2. {
  3.     AddVelocity = { args = {"Vector"}, server = function(ent, vec) if ent:IsPlayer() then ent:SetVelocity(vec) return end local phys = ent:GetPhysicsObject() if phys:IsValid() then phys:AddVelocity(vec) end end },
  4.     SetVelocity = { args = {"Vector"}, server = function(ent, args) if ent:IsPlayer() then ent:SetVelocity(vec) return end  local phys = ent:GetPhysicsObject() if phys:IsValid() then phys:SetVelocity(vec) end end },
  5.     SetPos = { args = {"Vector"}, server = function(ent, vec) ent:SetPos(vec) end },
  6. }
  7.  
  8. local function check_args(args, ...)
  9.     for i = 1, #args do
  10.         local t = type(select(i, ...))
  11.        
  12.         if t ~= args[i] then
  13.             if CLIENT then
  14.                 error(("%q expected got %q"):format(args[i], t), 3)
  15.             else
  16.                 return false
  17.             end
  18.         end
  19.     end
  20.    
  21.     return true
  22. end
  23.  
  24. if CLIENT then
  25.  
  26.     local function run(typ, ...)
  27.         RunConsoleCommand("hm", typ, ...)
  28.     end
  29.    
  30.     local META = {}
  31.     META.__index = META
  32.    
  33.     for key, data in pairs(functions) do
  34.         META[key] = function(self, ...)
  35.             if not self:IsValid() then
  36.                 setmetatable(self, getmetatable(NULL))
  37.                 return
  38.             end
  39.            
  40.             check_args(data.args, ...)
  41.            
  42.             if data.server then
  43.                 net.Start("server_entity")
  44.                     net.WriteTable({key, self.__entid, ...})
  45.                 net.SendToServer()
  46.             end
  47.            
  48.             if data.client then
  49.                 data.client(self:GetEntity(), ...)
  50.             end
  51.         end    
  52.     end
  53.    
  54.     for func_name, func in pairs(FindMetaTable("Entity")) do
  55.         META[func_name] = META[func_name] or function(self, ...)
  56.             if self:IsValid() then
  57.                 return func(self:GetEntity(), ...)
  58.             end
  59.         end
  60.     end
  61.    
  62.     function META:GetEntity()
  63.         return Entity(self.__entid)
  64.     end
  65.    
  66.     function META:IsValid()
  67.         return self:GetEntity():IsValid()
  68.     end
  69.    
  70.     function ServerEntity(id)
  71.         if IsEntity(id) and id:IsValid() then
  72.             id = id:EntIndex()
  73.         end
  74.        
  75.         return setmetatable({__entid = id}, META)
  76.     end
  77.    
  78. end
  79.  
  80. if SERVER then
  81.     util.AddNetworkString("server_entity")
  82.    
  83.    net.Receive("server_entity", function(len, ply)
  84.         local args = net.ReadTable()
  85.         local func_name = args[1]
  86.         local ent = Entity(args[2]) or NULL
  87.        
  88.         if not ent:IsValid() then return end
  89.        
  90.         table.remove(args, 1)
  91.         table.remove(args, 1)
  92.                    
  93.         if functions[func_name] and IsEntity(ent) and ent:IsValid() and (ent == ply or ent:CPPICanTool(ply)) and check_args(functions[func_name].args, unpack(args)) then
  94.             functions[func_name].server(ent, unpack(args))
  95.         else
  96.             ErrorNoHalt(("bogus data received from %q (%s)"):format(ply:Nick(), ply:SteamID()))
  97.             PrintTable({player = ply, func_name = func_name, entity = ent, arguments = args})
  98.         end
  99.    end)
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement