diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2018-01-14 15:40:04 -0500 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2018-01-14 15:40:04 -0500 |
| commit | af74cb5987d38c8efc820aa8d41a516ade40bcf2 (patch) | |
| tree | 59f37b7b4dd2898f0d470665c89f722e2aa3ef5f /gamemode/nrequire.lua | |
| parent | 5e4b759e6b4fd8af9e66f76090ee868368bee467 (diff) | |
| download | artery-af74cb5987d38c8efc820aa8d41a516ade40bcf2.tar.gz artery-af74cb5987d38c8efc820aa8d41a516ade40bcf2.tar.bz2 artery-af74cb5987d38c8efc820aa8d41a516ade40bcf2.zip | |
Massive updates
updates to nrequire, only re-includes things out of date
changes to cl_inventory, and config
Diffstat (limited to 'gamemode/nrequire.lua')
| -rw-r--r-- | gamemode/nrequire.lua | 134 |
1 files changed, 122 insertions, 12 deletions
diff --git a/gamemode/nrequire.lua b/gamemode/nrequire.lua index 24f0266..3fbe9d7 100644 --- a/gamemode/nrequire.lua +++ b/gamemode/nrequire.lua @@ -119,24 +119,23 @@ local lastcall = {} --Holds when things were loaded local deptbl = {} --Holds the dependencies between files local pathstack = {} function nrequire(req,...) - - + local tpath = scan(ntbl,req) -- Find the full path for the nrequired() module - - local trace = debug.getinfo(1,"flnSu") + + local trace = debug.getinfo(2,"flnSu") local source = trace.source deptbl[source] = tpath - - --If we've already run it, just return it - if reqtbl[tpath[1]] then + + --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 return reqtbl[tpath[1]] end - + --Otherwise, make sure we don't have a circular depedency for k,v in pairs(pathstack) do assert(v ~= tpath[1],string.format("Circular dependancy detected:\n\t%s\n\t\t|\n\t\tV\n\t%s",table.concat(pathstack,"\n\t\t|\n\t\tV\n\t"),v)) end - + --Actually run the file pathstack[#pathstack + 1] = tpath[1] local filetxt = file.Read(tpath[1],tpath[2]) @@ -160,6 +159,74 @@ function nrequire(req,...) end +if SERVER then + --[[Server tells the client to load a file]] + util.AddNetworkString("artery_loadfile") + --[[Client tells server we don't have that file]] + util.AddNetworkString("artery_requestcsfile") + --[[Server gives client the contents of a file]] + util.AddNetworkString("artery_respondfile") + net.Receive("artery_requestcsfile",function(ln,ply) + local which = net.ReadString() + local path = net.ReadString() + local found = false + for k,v in pairs(clienttoload) do + if v.name == which and v.directory == path then + net.Start("artery_respondfile") + net.WriteString(which) + net.WriteString(path) + net.WriteString(file.Read(which,path)) + net.Send(ply) + return + end + end + end) +else + local function run_csfile(txt,name) + assert(#txt > 0, "File was size 0 on: " .. name) + local ptr = CompileString(txt,name,false) + if type(ptr) == "function" then + ptr() + else + log.error(ptr) + end + end + net.Receive("artery_loadfile",function() + local filename = net.ReadString() + local hash = net.ReadUInt(32) + local path = net.ReadString() + local cache = file.Read("artery/client/files/" .. path .. "/" .. filename,"DATA") + if cache == nil then --We don't have this file downloaded! + net.Start("artery_requestcsfile") + net.WriteString(filename) + net.WriteString(path) + net.SendToServer() + return + end + local thash = tonumber(util.CRC(cache)) + if hash != thash then + print("I need to load a file",filename," but my hash was old, redownloading!") + net.Start("artery_requestcsfile") + net.WriteString(filename) + net.WriteString(path) + net.SendToServer() + else + run_csfile(cache,filename) + end + end) + net.Receive("artery_respondfile",function() + local filename = net.ReadString() + local filetext = net.ReadString() + local dirname = string.GetPathFromFilename(filename) + file.CreateDir("artery/client/files/" .. dirname) + assert(#filetext > 0, "Retreived a size 0 file: " .. filename) + file.Write("artery/client/files/" .. filename,filetext) + run_csfile(filetext,filename) + end) +end + +local clienttoload = {} + --[[ Automatically include all the files in the gamemode directory based on the file name. If the file starts with cl_ it will only be included on the client, if it starts with sv_ it will only be included on the server. If it starts with anything else, it will be shared. Will detect and error on circuar dependancy. @@ -187,11 +254,19 @@ local function doincludes() if CLIENT then nrequire(filename) else - AddCSLuaFile(filename) + clienttoload[#clienttoload+1] = { + name = filename, + hash = util.CRC(file.Read(filename,v[2])), + directory = v[2] + } end else if SERVER then - AddCSLuaFile(filename) + clienttoload[#clienttoload + 1] = { + name = filename, + hash = util.CRC(file.Read(filename,v[2])), + directory = v[2] + } end nrequire(filename) end @@ -200,6 +275,23 @@ local function doincludes() end doincludes() +hook.Call("artery_core_loaded") +local function loadclient(who) + local routine = coroutine.create(function() + for k,v in pairs(clienttoload) do + print("Asking client to load",k) + net.Start("artery_loadfile") + net.WriteString(v.name) + net.WriteUInt(v.hash,32) + net.WriteString(v.directory) + net.Send(who) + coroutine.yield() + end + end) + timer.Create("artery_load_client_timer",1,#clienttoload,function() + coroutine.resume(routine) + end) +end concommand.Add("PrintDepTbl",function(ply,cmd,args) PrintTable(deptbl) @@ -207,9 +299,27 @@ end) concommand.Add("PrintReqTbl",function(ply,cmd,args) PrintTable(reqtbl) end) +concommand.Add("PrintClientToLoad",function(ply,cmd,args) + PrintTable(clienttoload) +end) +concommand.Add("Loadclient",function(ply,cmd,args) + loadclient(ply) +end) if SERVER then util.AddNetworkString("art_refresh") + concommand.Add("art_refresh",function(ply,cmd,args) + print("Doing soft refresh") + doincludes() + net.Start("art_refresh") + net.Broadcast() + end) +else + net.Receive("art_manualrefresh",doincludes) +end + +if SERVER then + util.AddNetworkString("art_manualrefresh") concommand.Add("art_manualrefresh",function(ply,cmd,args) if not ply:IsAdmin() then return end reqtbl = {} @@ -218,7 +328,7 @@ if SERVER then net.Broadcast() end) end -if CLIENT then net.Receive("art_refresh",doincludes) end +if CLIENT then net.Receive("art_manualrefresh",doincludes) end concommand.Add("artery_manualrefresh",function(ply,cmd,args) doincludes() |
