-
Notifications
You must be signed in to change notification settings - Fork 1
/
module-loader.lua
76 lines (64 loc) · 1.7 KB
/
module-loader.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
local _defaultCPath = ""
local os = ""
if(love.system == nil) then
os = "Thread"
else
os = love.system.getOS()
end
if(os ~= "Web") then
_defaultCPath = love.filesystem.getCRequirePath()
end
local pathStack = {}
local _defaultLPath = love.filesystem.getRequirePath()
local _lastLPath = _defaultLPath
local _lastCPath = _defaultCPath
local function setReqPath(path)
table.insert(pathStack, path);
path = table.concat(pathStack, "/");
_lastLPath = path
if(os ~= "Web") then
love.filesystem.setCRequirePath(path.."/??;??")
end
love.filesystem.setRequirePath(path.."/?.lua;"..path.."/?/init.lua;?.lua;?/init.lua")
end
local function restorePath()
local newPath;
if(#pathStack > 0) then
table.remove(pathStack, #pathStack)
end
if(pathStack ~= 0) then
newPath = table.concat(pathStack, "/")
else
newPath = _defaultLPath
end
if(os ~= "Web") then
love.filesystem.setCRequirePath(_defaultCPath)
end
love.filesystem.setRequirePath(newPath)
end
function loadFromLib(libsPath, ...)
setReqPath(libsPath)
for _, files in ipairs({...}) do
require(files)
end
restorePath()
end
--- Used only for the threaded REST
function sendCurrentReqPath(channel)
channel:supply(_lastLPath)
channel:supply(_lastCPath)
end
--- Used only for the threaded REST
function setCurrentReqPath(channel)
-- print("Now waiting for messages")
_lastLPath = channel:demand()
_lastCPath = channel:demand()
setReqPath(_lastLPath)
-- print("Received from main ".._lastLPath)
end
function requireFromLib(libPath, fileName)
setReqPath(libPath)
local ret = require(fileName)
restorePath()
return ret
end