diff options
| author | Alexander M Pickering <alex@cogarr.net> | 2025-12-27 17:21:49 -0600 |
|---|---|---|
| committer | Alexander M Pickering <alex@cogarr.net> | 2025-12-27 17:21:49 -0600 |
| commit | 0c6a674999333a10317ee8a2b43f72bde03068c5 (patch) | |
| tree | 71e1839fafaba516d2472d02847fe65d469a5550 /gamemode/nrequire.lua | |
| parent | aacf5fbcfe1e6f2cdb0c6ab19b3e8dd87e3fd488 (diff) | |
| download | artery-0c6a674999333a10317ee8a2b43f72bde03068c5.tar.gz artery-0c6a674999333a10317ee8a2b43f72bde03068c5.tar.bz2 artery-0c6a674999333a10317ee8a2b43f72bde03068c5.zip | |
Diffstat (limited to 'gamemode/nrequire.lua')
| -rw-r--r-- | gamemode/nrequire.lua | 116 |
1 files changed, 96 insertions, 20 deletions
diff --git a/gamemode/nrequire.lua b/gamemode/nrequire.lua index ab1c191..5f0a063 100644 --- a/gamemode/nrequire.lua +++ b/gamemode/nrequire.lua @@ -1,11 +1,15 @@ ---[[ - A thing that kinda works like require, or at least, works how I wish require worked. +--[[ A thing that kinda works like require, or at least, works how I wish require worked. calls hooks: artery_nrequire_defined artery_core_loaded + +The interface is pretty good, but it needs a rewrite to add state machines, one for each file so that refreshes are more sane. ]] if nrequire ~= nil then return end +local nrequire_debugging = false +local debug_file = "artery_log.txt" + --The files we want clients to load local clienttoload = {} @@ -15,6 +19,22 @@ local searchpaths = { {"data/artery/global","GAME"}, } +nrequire = {} + +local watched_paths = {} +function nrequire.watch(func, path, path_part) + table.insert(watched_paths,{func,path,path_part}) +end + +local search_paths = {} +function nrequire.addSearchPath(path, path_part) + table.insert(search_paths, path, path_part) +end +nrequire.addSearchPath("gamemodes/artery/gamemode","GAME") +nrequire.addSearchPath("artery/global","DATA") +nrequire.addSearchPath("data/artery/global","GAME") + + if SERVER then --Network messages util.AddNetworkString("artery_downloadfile") @@ -100,6 +120,7 @@ local function collect_paths(pretbl) return ret end +local file_wtc = {} -- A write through cache for files on the client --[[ Scans the prefix table built by rebuild_include_table to find the file path for the partial name of an included file. If two files are named the same, you will need to include part of the file path until it can be resolved. @@ -136,6 +157,21 @@ local lastcall = {} --Holds when things were loaded local location = {} --Holds the location of files ("DATA","GAME","LUA",...) local deptbl = {} --Holds the dependencies between files local pathstack = {} + +local function nrequire_err(msg,throw) + local msgcolor,err_f + if CLIENT then + msgcolor = Color(209,96,196) + else + msgcolor = Color(160,49,150) + end + MsgC(msgcolor,msg) + if throw then + error(debug.traceback()) + else + MsgC(msgcolor,debug.traceback()) + end +end function nrequire(req,...) local varargs = {...} local tpath-- Find the full path for the nrequired() module @@ -149,8 +185,7 @@ function nrequire(req,...) tpath = scan(ntbl,req) end if tpath == nil then - MsgC(Color(209,96,196), "Unable to find file for include:" .. req) - error(debug.traceback()) + nrequire_err("Unable to find file for include:" .. req, true) end local trace = debug.getinfo(2,"flnSu") @@ -197,8 +232,7 @@ function nrequire(req,...) 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") + nrequire_err("nrequire Error:" .. err .. "\n") end) end @@ -209,9 +243,11 @@ function nrequire(req,...) end local update_file_co = coroutine.create(function() + if CLIENT then return end while true do for k,v in pairs(reqtbl) do local loc = location[k] + print(string.format("Checking file %s at %s",k,loc)) if file.Time(k,loc) > lastcall[k] then print(string.format("Found updated file:%s",k)) nrequire(k) @@ -231,16 +267,18 @@ local update_file_co = coroutine.create(function() net.WriteString(file.Read(k,loc)) net.Broadcast() end - coroutine.yield() + for i = 1,100 do + coroutine.yield() + end end end end) -hook.Add("Tick","nrequire_update_tick",function() - if coroutine.status(update_file_co) == "suspended" then - coroutine.resume(update_file_co) - end -end) +--hook.Add("Tick","nrequire_update_tick",function() + --if coroutine.status(update_file_co) == "suspended" then + --coroutine.resume(update_file_co) + --end +--end) if SERVER then net.Receive("artery_requestcsfile",function(ln,ply) @@ -267,8 +305,7 @@ else xpcall(function() ret = ptr() end,function(err) - MsgC(Color(209,96,196),debug.traceback(),"\n") - MsgC(Color(209,96,196),"nrequire Error:",err,"\n") + nrequire_err("nrequire Error:" .. err .. "\n") end) else error(ptr) @@ -411,6 +448,9 @@ if SERVER then print("artery_request_client_download received by server") downloadclient(pl) end) + net.Receive("art_requestclientreload",function(ln,pl) + downloadclient(pl) + end) net.Receive("art_requestfile",function(ln,pl) local filedata = net.ReadTable() local filetxt = file.Read(filedata.name,filedata.directory) @@ -477,9 +517,16 @@ else file.CreateDir(path) print("Writing to ", fullpath) file.Write(fullpath,tbl.text) - nrequire(tbl.name) - --timer.Simple(10,function() RunConsoleCommand("retry") end) - redoincludes() + file_wtc[fullpath] = tbl.text + timer.Simple(0,function() + --nrequire(tbl.name) + timer.Simple(10,function() + if needs_restart then + RunConsoleCommand("retry") + end + end) + --redoincludes() + end) end) net.Receive("artery_downloadfile",function(ln,pl) @@ -521,9 +568,6 @@ if SERVER then end if SERVER then - net.Receive("art_requestclientreload",function(ln,pl) - loadclient(pl) - end) else concommand.Add("artery_LoadClient",function(ply,cmd,args) net.Start("art_requestclientreload") @@ -568,3 +612,35 @@ if CLIENT then net.Receive("art_manualrefresh",doincludes) end concommand.Add("artery_manualrefresh",function(ply,cmd,args) doincludes() end) + +if nrequire_debugging then + local domain = "SERVER" + if CLIENT then + domain = "CLIENT" + end + local filename = string.format("%s_%s",domain,debug_file) + file.Open(filename,"w","DATA"):Close() + local oldprint = print + function print(...) + local f = file.Open(filename, "a", "DATA") + for _,v in pairs({...}) do + f:Write(tostring(v)) + f:Write("\t") + end + f:Write("\n") + f:Flush() + f:Close() + oldprint(...) + end + local function trace(t) + local dbg = debug.getinfo(2,"S") + if not dbg.short_src:find("artery") then + return + end + print(string.format("%s : %d - %s",dbg.short_src, dbg.linedefined, t)) + if t == "call" then + print(debug.traceback("",2)) + end + end + debug.sethook(trace,"cr",0) +end |
