Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Dynamic clientside lua reloading from the server
- -- by Shadowndacorner
- /*USAGE
- Everything is serverside only, nothing is actually modified clientside
- Lua Commands:
- Note: Removes 255 byte limit from Player.SendLua
- clientload.SendCodeToClient(string code, optional [Entity player/RecipientFilter])
- Sends code to the client as a string; almost the same functionality as Player.SendLua
- clientload.SendFileToClient(string filename, optional [Entity player/RecipientFilter])
- Sends a lua file to the client; same folder structure as lua_openscript
- Console Commands Added:
- lua_openscript_cl [file]
- Sends a lua file client and opens it
- lua_openscript_sh [file]
- Sends a lua file to the client and opens it clientside and serverside
- */
- module("clientload")
- umsg.PoolString "plugin_addcode"
- umsg.PoolString "plugin_runcode"
- local table = table
- local string = string
- local umsg = umsg
- local concommand = concommand
- local util = util
- local hook = hook
- local math = math
- local type = type
- local pairs = pairs
- local tostring = tostring
- local tonumber = tonumber
- local RecipientFilter = RecipientFilter
- local pcall = pcall
- local error = error
- local rawget = rawget
- local rawset = rawset
- local setmetatable = setmetatable
- local _R = _R
- local print = print
- local ErrorNoHalt = ErrorNoHalt
- local IsValid = IsValid
- PluginCode={}
- if SERVER then
- function SendCodeToClient(v, ply)
- local code=v
- if !ValidEntity(ply) then
- ply=RecipientFilter():AddAllPlayers()
- end
- if code then
- for i=1, string.len(code)/200+1 do
- local ncode=string.sub(code, (i-1)*200, (i-1)*200+99)
- --Msg(ncode)
- --MsgN(string.len(ncode))
- umsg.Start("plugin_addcode", ply)
- umsg.String("runCode")
- umsg.String(ncode)
- umsg.End()
- end
- umsg.Start("plugin_runcode", ply)
- umsg.String("runCode")
- umsg.End()
- else
- ErrorNoHalt("File "..v.." not found (anywhere)!\n")
- end
- end
- function _R.Player:SendLua(str)
- SendCodeToClient(str, self)
- end
- function SendLuaToClient(v, ply)
- local code=file.Read("lua/"..v, true)
- if !code then
- local hasFound=false
- for ind, addon in pairs(file.FindDir("addons/*", true)) do
- if file.Read("addons/"..addon.."/lua/"..v) and !hasFound then
- code=file.Read("addons/"..addon.."/lua/"..v)
- hasFound=true
- end
- end
- if file.Read("gamemodes/"..v) and !hasFound then
- code=file.Read("gamemodes/"..v)
- hasFound=true
- end
- end
- if !ValidEntity(ply) then
- ply=RecipientFilter():AddAllPlayers()
- end
- if code then
- for i=1, string.len(code)/100+1 do
- local ncode=string.sub(code, (i-1)*100, (i-1)*100+99)
- --Msg(ncode)
- --MsgN(string.len(ncode))
- umsg.Start("plugin_addcode", ply)
- umsg.String(v)
- umsg.String(ncode)
- umsg.End()
- end
- umsg.Start("plugin_runcode", ply)
- umsg.String(v)
- umsg.End()
- if !table.HasValue(PluginCode, v) then
- PluginCode[#PluginCode+1]=v
- end
- else
- ErrorNoHalt("File "..v.." not found (anywhere)!\n")
- end
- end
- concommand.Add("lua_sendfiletoclient", function(ply, cmd, args)
- if !ply:IsPlayer() then
- SendLuaToClient(string.Implode(" ", args))
- end
- end)
- concommand.Add("lua_openscript_cl", function(ply, cmd, args)
- if !ply:IsPlayer() then
- SendLuaToClient(string.Implode(" ", args))
- end
- end)
- concommand.Add("lua_openscript_sh", function(ply, cmd, args)
- if !ply:IsPlayer() then
- SendLuaToClient(string.Implode(" ", args))
- include(string.Implode(" ", args))
- end
- end)
- hook.Add("PlayerInitialSpawn", "sendPluginCode", function(ply)
- for f, v in pairs(PluginCode) do
- SendLuaToClient(v, ply)
- end
- end)
- end
- if CLIENT then
- usermessage.Hook("lua_fromserver", function(data)
- local code=data:ReadString()
- print("lua_fromserver")
- print(code)
- RunString(code)
- end)
- usermessage.Hook("plugin_addcode", function(data)
- local fname=data:ReadString()
- local code=data:ReadString()
- if PluginCode[fname] then
- PluginCode[fname]=PluginCode[fname]..code
- else
- PluginCode[fname]=code
- end
- end)
- usermessage.Hook("plugin_runcode", function(data)
- local fname=data:ReadString()
- if !(fname=="runCode") then
- MsgN(fname.." loaded from server.")
- end
- RunString(PluginCode[fname])
- PluginCode[fname]=nil
- end)
- end
Advertisement
Add Comment
Please, Sign In to add comment