shadowndacorner

DynLoad

Aug 8th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.23 KB | None | 0 0
  1. AddCSLuaFile("autorun/dynload.lua")
  2. dynLoad={}
  3. if SERVER then
  4.     dynLoad.UpdatedFiles={}
  5.     function dynLoad.SendCodeToClient(v, ply)
  6.         local code=v
  7.         if !ValidEntity(ply) then
  8.             ply=RecipientFilter():AddAllPlayers()
  9.         end
  10.         if code then
  11.             for i=1, string.len(code)/200+1 do
  12.                 local ncode=string.sub(code, (i-1)*200, (i-1)*200+99)
  13.                 --Msg(ncode)
  14.                 --MsgN(string.len(ncode))
  15.                 umsg.Start("plugin_addcode", ply)
  16.                     umsg.String("runCode")
  17.                     umsg.String(ncode)
  18.                 umsg.End()
  19.             end
  20.             umsg.Start("plugin_runcode", ply)
  21.                 umsg.String("runCode")
  22.             umsg.End()
  23.         else
  24.             error("Error!  Code not sent!", 2)
  25.         end
  26.     end
  27.    
  28.     function _R.Player:SendLua(str)
  29.         dynLoad.SendCodeToClient(str, self)
  30.     end
  31.    
  32.     function dynLoad.SendLuaToClient(v, ply)
  33.         local code=file.Read("lua/"..v, true)
  34.         if !code then
  35.             local hasFound=false
  36.             for ind, addon in pairs(file.FindDir("addons/*", true)) do
  37.                 if file.Read("addons/"..addon.."/lua/"..v, true) and !hasFound then
  38.                     code=file.Read("addons/"..addon.."/lua/"..v, true)
  39.                     hasFound=true
  40.                 end
  41.             end
  42.             if file.Read("gamemodes/"..v, true) and !hasFound then
  43.                 code=file.Read("gamemodes/"..v, true)
  44.                 hasFound=true
  45.             end
  46.         end
  47.         if !ValidEntity(ply) then
  48.             ply=RecipientFilter():AddAllPlayers()
  49.         end
  50.         if code then
  51.             for i=1, string.len(code)/100+1 do
  52.                 local ncode=string.sub(code, (i-1)*100, (i-1)*100+99)
  53.                 --Msg(ncode)
  54.                 --MsgN(string.len(ncode))
  55.                 umsg.Start("plugin_addcode", ply)
  56.                     umsg.String(v)
  57.                     umsg.String(ncode)
  58.                 umsg.End()
  59.             end
  60.             umsg.Start("plugin_runcode", ply)
  61.                 umsg.String(v)
  62.             umsg.End()
  63.             if !table.HasValue(dynLoad.UpdatedFiles, v) then
  64.                 dynLoad.UpdatedFiles[#dynLoad.UpdatedFiles+1]=v
  65.             end
  66.         else
  67.             ErrorNoHalt("File "..v.." not found (anywhere)!\n")
  68.         end
  69.     end
  70.    
  71.     concommand.Add("lua_sendfiletoclient", function(ply, cmd, args)
  72.         if !ply:IsPlayer() then
  73.             dynLoad.SendLuaToClient(string.Implode(" ", args))
  74.         end
  75.     end)
  76.    
  77.     concommand.Add("lua_openscript_cl", function(ply, cmd, args)
  78.         if !ply:IsPlayer() then
  79.             dynLoad.SendLuaToClient(string.Implode(" ", args))
  80.         end
  81.     end)
  82.    
  83.     concommand.Add("lua_openscript_dl", function(ply, cmd, args)
  84.         if !ply:IsPlayer() then
  85.             dynLoad.SendLuaToClient(string.Implode(" ", args))
  86.             include(string.Implode(" ", args))
  87.         end
  88.     end)
  89.    
  90.     concommand.Add("lua_openscript_sh", function(ply, cmd, args)
  91.         if !ply:IsPlayer() then
  92.             dynLoad.SendLuaToClient(string.Implode(" ", args))
  93.             include(string.Implode(" ", args))
  94.         end
  95.     end)
  96.    
  97.     hook.Add("PlayerInitialSpawn", "sendUpdatedFiles", function(ply)
  98.         for f, v in pairs(dynLoad.UpdatedFiles) do
  99.             dynLoad.SendLuaToClient(v, ply)
  100.         end
  101.     end)
  102. end
  103.  
  104. if CLIENT then
  105.     dynLoad.PluginCode={}
  106.  
  107.     usermessage.Hook("plugin_addcode", function(data)
  108.         local fname=data:ReadString()
  109.         local code=data:ReadString()
  110.         if dynLoad.PluginCode[fname] then
  111.             dynLoad.PluginCode[fname]=dynLoad.PluginCode[fname]..code
  112.         else
  113.             dynLoad.PluginCode[fname]=code
  114.         end
  115.     end)
  116.  
  117.     usermessage.Hook("plugin_runcode", function(data)
  118.         local fname=data:ReadString()
  119.         if !(fname=="runCode") then
  120.             MsgN(fname.." loaded from server.")
  121.         end
  122.         RunString(dynLoad.PluginCode[fname])
  123.         dynLoad.PluginCode[fname]=nil
  124.     end)
  125. end
Advertisement
Add Comment
Please, Sign In to add comment