Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.08 KB | None | 0 0
  1. --[[
  2.     MetaRPG: sv.network.lua
  3.     ver 1.0.0000 ~ 12/Feb/2010
  4.     by Xalphox
  5. --]]
  6.  
  7.  
  8. MetaRPG = MetaRPG or {}
  9. MetaRPG.Network = {}
  10.  
  11. -- MetaRPG.Network.ClientObj
  12. -- This object is a simplified model for server-client interaction, allowing a scripter
  13. -- or module maker to simply write NETOBJ(ElementID):FuncName(Param) rather than having
  14. -- to establish an event and et cetera.
  15. --
  16. -- One limitation it has is it cannot recieve returns yet.
  17.  
  18. __ClientObj = {}
  19.  
  20.  
  21. -- MetaRPG.Network.GetObject(Element) || NETOBJ(Element)
  22. -- Gets a networked object of said element, which is an empty table
  23. -- with it's metatable set to __ClientObj.
  24.  
  25. function MetaRPG.Network.GetObject(Element)
  26.     local Obj = {}
  27.     local __Obj = table.copy(__ClientObj)
  28.     __Obj.TargetElement = Element
  29.    
  30.     setmetatable(Obj, __Obj)
  31.     return Obj
  32. end
  33. NETOBJ = MetaRPG.Network.GetObject
  34.  
  35.  
  36. -- __ClientObj:__index(t, k, v)
  37. -- Emulates a function, fetching the function name and a list of
  38. -- parameters.
  39.  
  40. function __ClientObj:__index(t, k, v)
  41.     local function F(...)
  42.         local FuncData = {}
  43.         FuncData.Name = t
  44.         FuncData.Param = {}
  45.         FuncData.ParamType = {}
  46.        
  47.         for k, v in pairs(arg) do
  48.             if k ~= "n" then
  49.                 FuncData.Param[k] = v
  50.                 FuncData.ParamType[k] = type(v)
  51.             end
  52.         end
  53.         return self:__SendCommand(FuncData)
  54.     end
  55.     return pcall(rawget, t, k, v) or F -- : This is incase a method actually exists inside the table.
  56. end
  57.  
  58.  
  59. -- __ClientObj:__SendCommand(FuncData)
  60. -- Sends the function and parameters, as parsed by __ClientObj:__index, to
  61. -- the client; replacing the unsendable data types (functions & userdata) with
  62. -- string representations.
  63.  
  64. function __ClientObj:__SendCommand(FuncData)
  65.     if not self.TargetElement then
  66.         error("You can't directly access the ClientObj object!")
  67.     end
  68.    
  69.     for k, v in pairs(FuncData.Param) do
  70.         if ParamType[k] == "function" then
  71.             FuncData.Param[k] = "__FUNCTION__"
  72.         end
  73.        
  74.         if ParamType[k] == "userdata" then
  75.             FuncData.Param[k] = "__USERDATA__"
  76.         end
  77.     end
  78.    
  79.     return triggerClientEvent(self.TargetElement, "MT.NET.SendCommand", getRootElement(), FuncData.Param)
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement