diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2019-05-04 16:06:48 -0400 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2019-05-04 16:06:48 -0400 |
| commit | 446dcbfdbd4166505518262f7c8c233a3005aea5 (patch) | |
| tree | 7f39e7ca547648042438ffe05a312883e695a2dc | |
| parent | 100342bdd0a979f283f90875663784f20282dd5e (diff) | |
| download | artery-446dcbfdbd4166505518262f7c8c233a3005aea5.tar.gz artery-446dcbfdbd4166505518262f7c8c233a3005aea5.tar.bz2 artery-446dcbfdbd4166505518262f7c8c233a3005aea5.zip | |
Fixed nrequire's auto-update
nrequire() now auto-updates when it detects that a file has
been changed.
| -rw-r--r-- | gamemode/nrequire.lua | 90 |
1 files changed, 60 insertions, 30 deletions
diff --git a/gamemode/nrequire.lua b/gamemode/nrequire.lua index 4313ce0..ab1c191 100644 --- a/gamemode/nrequire.lua +++ b/gamemode/nrequire.lua @@ -29,6 +29,7 @@ if SERVER then --[[Server gives client the contents of a file]] util.AddNetworkString("artery_respondfile") util.AddNetworkString("art_requestclientreload") + util.AddNetworkString("art_client_reload_file") util.AddNetworkString("art_refresh") util.AddNetworkString("art_manualrefresh") end @@ -137,7 +138,7 @@ local deptbl = {} --Holds the dependencies between files local pathstack = {} function nrequire(req,...) local varargs = {...} - local tpath + local tpath-- Find the full path for the nrequired() module if CLIENT then xpcall(function() tpath = scan(ntbl,req) @@ -151,15 +152,15 @@ function nrequire(req,...) MsgC(Color(209,96,196), "Unable to find file for include:" .. req) error(debug.traceback()) end - -- local tpath = scan(ntbl,req) -- Find the full path for the nrequired() module local trace = debug.getinfo(2,"flnSu") local source = trace.source - print("Adding to deptbl, src:",source,"tpath:",tpath) + --print("Adding to deptbl, src:",source,"tpath:",tpath) deptbl[source] = tpath --If we've already run it (and its up to date), just return it - if reqtbl[tpath[1]] --[[and lastcall[tpath[1] ] >= file.Time(tpath[1],tpath[2])]] then + local filetime = file.Time(tpath[1],tpath[2]) + if reqtbl[tpath[1]] and lastcall[tpath[1]] and lastcall[tpath[1]] > filetime then return reqtbl[tpath[1]] end @@ -169,34 +170,36 @@ function nrequire(req,...) end --Actually run the file - pathstack[#pathstack + 1] = tpath[1] + pathstack[#pathstack + 1] = tpath[1] --add this to circular dependency checker local filetxt = file.Read(tpath[1],tpath[2]) if #filetxt == 0 then pathstack[#pathstack] = nil return end - local filetime = file.Time(tpath[1],tpath[2]) - local skip_compile = true - if lastcall[tpath[1]] and lastcall[tpath[1]] < filetime then - skip_compile = false - end - if skip_compile then -- Needed to follow the scoping rules of lua in regards to goto - local filefunc = CompileString(filetxt,tpath[1],false) - if type(filefunc) ~= "function" then - error(filefunc) - else - xpcall(function() - local m = filefunc(varargs) - hook.Run("artery_file_included",tpath,m) - reqtbl[tpath[1]] = m - lastcall[tpath[1]] = file.Time(tpath[1],tpath[2]) - location[tpath[1]] = tpath[2] - end,function(err) - MsgC(Color(209,96,196),debug.traceback(),"\n") - MsgC(Color(209,96,196),"nrequire Error:",err,"\n") - end) - end + --Compile the file + local filefunc = CompileString(filetxt,tpath[1],false) + if type(filefunc) ~= "function" then + error(filefunc) + else + xpcall(function() + --and run it + local m = filefunc(varargs) + if reqtbl[tpath[1]] then --We already included the file before, we're updating + setmetatable(reqtbl[tpath[1]],{__index = m}) + else + local m_proxy = {} + setmetatable(m_proxy,{__index = m}) + reqtbl[tpath[1]] = m_proxy + end + hook.Run("artery_file_included",tpath,m) + --print(string.format("In nrequire(), not skipping compile and setting lastcall[%s] to %d",tpath[1],file.Time(tpath[1],tpath[2]))) + lastcall[tpath[1]] = os.time() + location[tpath[1]] = tpath[2] + end,function(err) + MsgC(Color(209,96,196),debug.traceback(),"\n") + MsgC(Color(209,96,196),"nrequire Error:",err,"\n") + end) end pathstack[#pathstack] = nil @@ -210,8 +213,23 @@ local update_file_co = coroutine.create(function() for k,v in pairs(reqtbl) do local loc = location[k] if file.Time(k,loc) > lastcall[k] then - print(string.format("Found updated file:%s calling nrequire",k)) + print(string.format("Found updated file:%s",k)) nrequire(k) + lastcall[k] = os.time() --file.Time(k,loc) + for ind,tbl in pairs(clienttoload) do + if tbl.name == k then + clienttoload[ind] = { + name = k, + hash = util.CRC(file.Read(k,loc)), + directory = loc, + } + end + end + net.Start("art_client_reload_file") + net.WriteString(k) + net.WriteString(loc) + net.WriteString(file.Read(k,loc)) + net.Broadcast() end coroutine.yield() end @@ -289,8 +307,10 @@ else assert(#filetext > 0, "Retreived a size 0 file: " .. filename) file.Write("artery/client/files/" .. filename,filetext) local mod = run_csfile(filetext,filename) - reqtbl[filepath] = mod - lastcall[filepath] = CurTime() + local mod_proxy = {} + setmetatable(mod_proxy,{__index = mod}) + reqtbl[filepath] = mod_proxy + lastcall[filepath] = os.time() end) end @@ -403,6 +423,14 @@ if SERVER then net.Send(pl) end) else + net.Receive("art_client_reload_file",function(ln,pl) + local filename = net.ReadString() + local directory = net.ReadString() + local filetext = net.ReadString() + local fullpath = "artery/client/" .. directory .. "/" .. filename .. ".txt" + file.Write(fullpath,filetext) + redoincludes() + end) net.Receive("art_downloadfiles",function(ln,pl) local clientfiles = net.ReadTable() local todownload = {} @@ -449,7 +477,9 @@ else file.CreateDir(path) print("Writing to ", fullpath) file.Write(fullpath,tbl.text) - timer.Simple(10,function() RunConsoleCommand("retry") end) + nrequire(tbl.name) + --timer.Simple(10,function() RunConsoleCommand("retry") end) + redoincludes() end) net.Receive("artery_downloadfile",function(ln,pl) |
