shadowndacorner

ClientLoad Module

Apr 3rd, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.27 KB | None | 0 0
  1. -- Dynamic clientside lua reloading from the server
  2. -- by Shadowndacorner
  3.  
  4. /*USAGE
  5. Everything is serverside only, nothing is actually modified clientside
  6. Lua Commands:
  7.     Note: Removes 255 byte limit from Player.SendLua
  8.     clientload.SendCodeToClient(string code, optional [Entity player/RecipientFilter])
  9.         Sends code to the client as a string; almost the same functionality as Player.SendLua
  10.     clientload.SendFileToClient(string filename, optional [Entity player/RecipientFilter])
  11.         Sends a lua file to the client; same folder structure as lua_openscript
  12.        
  13. Console Commands Added:
  14. lua_openscript_cl [file]
  15.     Sends a lua file client and opens it
  16. lua_openscript_sh [file]
  17.     Sends a lua file to the client and opens it clientside and serverside
  18. */
  19. module("clientload")
  20. umsg.PoolString "plugin_addcode"
  21. umsg.PoolString "plugin_runcode"
  22.  
  23. local table = table
  24. local string = string
  25. local umsg = umsg
  26. local concommand = concommand
  27. local util = util
  28. local hook = hook
  29. local math = math
  30. local type = type
  31. local pairs = pairs
  32. local tostring = tostring
  33. local tonumber = tonumber
  34. local RecipientFilter = RecipientFilter
  35. local pcall = pcall
  36. local error = error
  37. local rawget = rawget
  38. local rawset = rawset
  39. local setmetatable = setmetatable
  40. local _R = _R
  41. local print = print
  42. local ErrorNoHalt = ErrorNoHalt
  43. local IsValid = IsValid
  44. PluginCode={}
  45. if SERVER then
  46.     function SendCodeToClient(v, ply)
  47.         local code=v
  48.         if !ValidEntity(ply) then
  49.             ply=RecipientFilter():AddAllPlayers()
  50.         end
  51.         if code then
  52.             for i=1, string.len(code)/200+1 do
  53.                 local ncode=string.sub(code, (i-1)*200, (i-1)*200+99)
  54.                 --Msg(ncode)
  55.                 --MsgN(string.len(ncode))
  56.                 umsg.Start("plugin_addcode", ply)
  57.                     umsg.String("runCode")
  58.                     umsg.String(ncode)
  59.                 umsg.End()
  60.             end
  61.             umsg.Start("plugin_runcode", ply)
  62.                 umsg.String("runCode")
  63.             umsg.End()
  64.         else
  65.             ErrorNoHalt("File "..v.." not found (anywhere)!\n")
  66.         end
  67.     end
  68.    
  69.     function _R.Player:SendLua(str)
  70.         SendCodeToClient(str, self)
  71.     end
  72.    
  73.     function SendLuaToClient(v, ply)
  74.         local code=file.Read("lua/"..v, true)
  75.         if !code then
  76.             local hasFound=false
  77.             for ind, addon in pairs(file.FindDir("addons/*", true)) do
  78.                 if file.Read("addons/"..addon.."/lua/"..v) and !hasFound then
  79.                     code=file.Read("addons/"..addon.."/lua/"..v)
  80.                     hasFound=true
  81.                 end
  82.             end
  83.             if file.Read("gamemodes/"..v) and !hasFound then
  84.                 code=file.Read("gamemodes/"..v)
  85.                 hasFound=true
  86.             end
  87.         end
  88.         if !ValidEntity(ply) then
  89.             ply=RecipientFilter():AddAllPlayers()
  90.         end
  91.         if code then
  92.             for i=1, string.len(code)/100+1 do
  93.                 local ncode=string.sub(code, (i-1)*100, (i-1)*100+99)
  94.                 --Msg(ncode)
  95.                 --MsgN(string.len(ncode))
  96.                 umsg.Start("plugin_addcode", ply)
  97.                     umsg.String(v)
  98.                     umsg.String(ncode)
  99.                 umsg.End()
  100.             end
  101.             umsg.Start("plugin_runcode", ply)
  102.                 umsg.String(v)
  103.             umsg.End()
  104.             if !table.HasValue(PluginCode, v) then
  105.                 PluginCode[#PluginCode+1]=v
  106.             end
  107.         else
  108.             ErrorNoHalt("File "..v.." not found (anywhere)!\n")
  109.         end
  110.     end
  111.    
  112.     concommand.Add("lua_sendfiletoclient", function(ply, cmd, args)
  113.         if !ply:IsPlayer() then
  114.             SendLuaToClient(string.Implode(" ", args))
  115.         end
  116.     end)
  117.    
  118.     concommand.Add("lua_openscript_cl", function(ply, cmd, args)
  119.         if !ply:IsPlayer() then
  120.             SendLuaToClient(string.Implode(" ", args))
  121.         end
  122.     end)
  123.    
  124.     concommand.Add("lua_openscript_sh", function(ply, cmd, args)
  125.         if !ply:IsPlayer() then
  126.             SendLuaToClient(string.Implode(" ", args))
  127.             include(string.Implode(" ", args))
  128.         end
  129.     end)
  130.    
  131.     hook.Add("PlayerInitialSpawn", "sendPluginCode", function(ply)
  132.         for f, v in pairs(PluginCode) do
  133.             SendLuaToClient(v, ply)
  134.         end
  135.     end)
  136. end
  137.  
  138. if CLIENT then
  139.     usermessage.Hook("lua_fromserver", function(data)
  140.         local code=data:ReadString()
  141.         print("lua_fromserver")
  142.         print(code)
  143.         RunString(code)
  144.     end)
  145.  
  146.     usermessage.Hook("plugin_addcode", function(data)
  147.         local fname=data:ReadString()
  148.         local code=data:ReadString()
  149.         if PluginCode[fname] then
  150.             PluginCode[fname]=PluginCode[fname]..code
  151.         else
  152.             PluginCode[fname]=code
  153.         end
  154.     end)
  155.  
  156.     usermessage.Hook("plugin_runcode", function(data)
  157.         local fname=data:ReadString()
  158.         if !(fname=="runCode") then
  159.             MsgN(fname.." loaded from server.")
  160.         end
  161.         RunString(PluginCode[fname])
  162.         PluginCode[fname]=nil
  163.     end)
  164. end
Advertisement
Add Comment
Please, Sign In to add comment