aboutsummaryrefslogtreecommitdiff
path: root/gamemode/nrequire.lua
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2017-01-08 22:28:08 -0500
committerAlexander Pickering <alexandermpickering@gmail.com>2017-01-08 22:28:08 -0500
commit98e0462e4f6b13ff26af5211409352d45dd9453e (patch)
treefbff14dc9a0fffdda409d9989f2e34cd4bb265f6 /gamemode/nrequire.lua
parent4879eb1d78520ce0ac9b0bb0ef5244cf65ad7c99 (diff)
downloadartery-98e0462e4f6b13ff26af5211409352d45dd9453e.tar.gz
artery-98e0462e4f6b13ff26af5211409352d45dd9453e.tar.bz2
artery-98e0462e4f6b13ff26af5211409352d45dd9453e.zip
Add a ton of icons, more work on refactoring
Diffstat (limited to 'gamemode/nrequire.lua')
-rw-r--r--gamemode/nrequire.lua64
1 files changed, 47 insertions, 17 deletions
diff --git a/gamemode/nrequire.lua b/gamemode/nrequire.lua
index ba71f17..c666332 100644
--- a/gamemode/nrequire.lua
+++ b/gamemode/nrequire.lua
@@ -1,9 +1,13 @@
--[[
A replacement for require
]]
+--Don't run ourselves, or we'll get stuck in a recursive loop!
if nrequire ~= nil then return end
print("hello from nrequire!")
local path = (GM or GAMEMODE).Folder:gsub("gamemodes/","") .. "/gamemode"
+--[[
+ Calls func on all the files under dir
+]]
local function TraverseFolder(dir, func)
local fpath = table.concat({path,dir,"/*"})
local files, directories = file.Find(fpath,"LUA")
@@ -19,6 +23,26 @@ local function TraverseFolder(dir, func)
end
end
+--[[
+ Creates a funny kind of table. The root points to tables with file names, each file name points to a table containing the folder name it is under. If that folder is under more folders, then the folder table points to more tables in reverse order. The leaf contains the file path.
+ Ex:
+ {
+ [file.lua] = {
+ [some] = {
+ [foldername] = "foldername/some/file.lua"
+ }
+ [other] = {
+ [foldername] = "foldername/other/file.lua"
+ }
+ }
+ }
+ is created from
+ foldername/
+ some/
+ file.lua
+ other/
+ file.lua
+]]
local function rebuild_include_table(f)
local ret = {}
for k,v in pairs(f) do
@@ -51,7 +75,7 @@ local function tbllen(tbl)
end
--[[
- Finds all the paths from a pretable
+ Finds all the paths from a include table
]]
local function collect_paths(pretbl)
local ret = {}
@@ -70,6 +94,7 @@ end
--[[
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.
]]
local function scan(pretbl, name)
local pathparts = {}
@@ -78,18 +103,19 @@ local function scan(pretbl, name)
end
local filename = name:gfind("[%w_]+%.lua")()
local cursor = pretbl[filename]
- assert(cursor ~= nil,string.format("Scan did not find a file named %q, valid files are:\n\t%s",filename,table.concat(pretbl,"\n\t")))
+ assert(cursor ~= nil,string.format("Scan did not find a file named %q, valid files are:\n\t%s",filename,table.concat(collect_paths(pretbl),"\n\t")))
local rev = {}
for i = 1, #pathparts do rev[#pathparts - i + 1] = pathparts[i] end
for k,v in ipairs(rev) do cursor = cursor[v] end
while type(cursor) ~= "string" do
assert(type(cursor) ~= "nil",string.format("Could not find a valid file for path %q, file paths:\n\t%s",name,table.concat(collect_paths(pretbl),"\n\t")))
- assert(tbllen(cursor) == 1,string.format("Ambiguous scan, there are two or more paths that match %q\n\t%s",name,table.concat(collect_paths(cursor),"\n\t")))
+ assert(tbllen(cursor) == 1,string.format("Ambiguous scan, there are two or more paths that match %q\n\t%s\nSpecify more of the file path.",name,table.concat(collect_paths(cursor),"\n\t")))
cursor = cursor[next(cursor)]
end
return cursor
end
+
local paths = {}
local ins = function(n) paths[#paths + 1] = n end
TraverseFolder("",ins)
@@ -102,32 +128,36 @@ for k,v in pairs(paths) do
end
end
+
local pathstack = {}
local incyields = {}
+--[[
+ Returns the table returned by executing a file. The table is cached after is is loaded, so calling nrequire() on a file twice will only run it once.
+]]
function nrequire(req)
- print("nrequire")
local tpath = scan(ntbl,req)
if reqtbl[tpath] then
- print("nrequire cache hit")
+ print("Cache hit! returning",reqtbl[tpath])
+ --for k,v in pairs(reqtbl[tpath]) do print(k,":",v) end
return reqtbl[tpath]
end
for k,v in pairs(pathstack) do
- assert(v ~= tpath,string.format("Circular dependancy detected:\n\t%s",table.concat(pathstack,"\n\t\t|\n\t\tv\n\t")))
+ assert(v ~= tpath,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
- print(string.format("Including %q\n",tpath))
+ local tab_rep = {}
+ for k=1,#pathstack do tab_rep[k] = "\t" end
+ print(string.format("%sIncluding %q",table.concat(tab_rep),tpath))
pathstack[#pathstack + 1] = tpath
- reqtbl[#reqtbl + 1] = include(tpath)
- print(string.format("Included %q\n",tpath))
- return reqtbl[#reqtbl]
- --[[
- incyields[#incyields + 1] = coroutine.create(function()
- print(string.format("Including %q\n",tpath))
- reqtbl[#reqtbl + 1] = include(tpath)
- print(string.format("Included %q\n",tpath))
- end)
- ]]
+ reqtbl[tpath] = include(tpath)
+ pathstack[#pathstack] = nil
+ print(string.format("%sIncluded %q",table.concat(tab_rep),tpath))
+ return reqtbl[tpath]
end
+--[[
+ 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.
+]]
local function doincludes()
print("doing includes")
paths = {}