2017-05-20 00:55:19 +00:00
|
|
|
local branch = 'master'
|
|
|
|
if fs.exists('.branch') then
|
|
|
|
local f = fs.open('.branch', "r")
|
|
|
|
if f then
|
|
|
|
branch = f.readAll()
|
|
|
|
f.close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local DEFAULT_UPATH = 'https://raw.githubusercontent.com/kepler155c/opus/' .. branch .. '/sys/apis'
|
2016-12-18 19:38:48 +00:00
|
|
|
local PASTEBIN_URL = 'http://pastebin.com/raw'
|
2016-12-23 04:22:04 +00:00
|
|
|
local GIT_URL = 'https://raw.githubusercontent.com'
|
|
|
|
|
|
|
|
local function standardSearcher(modname, env, shell)
|
2017-07-24 02:37:07 +00:00
|
|
|
if _G.package.loaded[modname] then
|
2016-12-23 04:22:04 +00:00
|
|
|
return function()
|
2017-07-24 02:37:07 +00:00
|
|
|
return _G.package.loaded[modname]
|
2016-12-23 04:22:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2016-12-18 19:38:48 +00:00
|
|
|
local function shellSearcher(modname, env, shell)
|
|
|
|
local fname = modname:gsub('%.', '/') .. '.lua'
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2016-12-18 19:38:48 +00:00
|
|
|
if shell and type(shell.dir) == 'function' then
|
|
|
|
local path = shell.resolve(fname)
|
|
|
|
if fs.exists(path) and not fs.isDir(path) then
|
|
|
|
return loadfile(path, env)
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
end
|
2016-12-18 19:38:48 +00:00
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2016-12-18 19:38:48 +00:00
|
|
|
local function pathSearcher(modname, env, shell)
|
|
|
|
local fname = modname:gsub('%.', '/') .. '.lua'
|
|
|
|
|
2017-07-24 02:37:07 +00:00
|
|
|
for dir in string.gmatch(_G.package.path, "[^:]+") do
|
2016-12-18 19:38:48 +00:00
|
|
|
local path = fs.combine(dir, fname)
|
2016-12-11 19:24:52 +00:00
|
|
|
if fs.exists(path) and not fs.isDir(path) then
|
2016-12-18 19:38:48 +00:00
|
|
|
return loadfile(path, env)
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
end
|
2016-12-18 19:38:48 +00:00
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2016-12-18 19:38:48 +00:00
|
|
|
-- fix broken http get
|
|
|
|
local syncLocks = { }
|
|
|
|
|
|
|
|
local function sync(obj, fn)
|
|
|
|
local key = tostring(obj)
|
|
|
|
if syncLocks[key] then
|
|
|
|
local cos = tostring(coroutine.running())
|
|
|
|
table.insert(syncLocks[key], cos)
|
|
|
|
repeat
|
|
|
|
local _, co = os.pullEvent('sync_lock')
|
|
|
|
until co == cos
|
|
|
|
else
|
|
|
|
syncLocks[key] = { }
|
|
|
|
end
|
|
|
|
local s, m = pcall(fn)
|
|
|
|
local co = table.remove(syncLocks[key], 1)
|
|
|
|
if co then
|
|
|
|
os.queueEvent('sync_lock', co)
|
|
|
|
else
|
|
|
|
syncLocks[key] = nil
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
2016-12-18 19:38:48 +00:00
|
|
|
if not s then
|
|
|
|
error(m)
|
|
|
|
end
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2016-12-18 19:38:48 +00:00
|
|
|
local function loadUrl(url)
|
|
|
|
local c
|
|
|
|
sync(url, function()
|
|
|
|
local h = http.get(url)
|
|
|
|
if h then
|
|
|
|
c = h.readAll()
|
|
|
|
h.close()
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
if c and #c > 0 then
|
|
|
|
return c
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
2016-12-18 19:38:48 +00:00
|
|
|
-- require('BniCQPVf')
|
|
|
|
local function pastebinSearcher(modname, env, shell)
|
|
|
|
if #modname == 8 and not modname:match('%W') then
|
|
|
|
local url = PASTEBIN_URL .. '/' .. modname
|
|
|
|
local c = loadUrl(url)
|
|
|
|
if c then
|
|
|
|
return load(c, modname, nil, env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2016-12-18 19:38:48 +00:00
|
|
|
-- require('kepler155c.opus.master.sys.apis.util')
|
|
|
|
local function gitSearcher(modname, env, shell)
|
|
|
|
local fname = modname:gsub('%.', '/') .. '.lua'
|
|
|
|
local _, count = fname:gsub("/", "")
|
|
|
|
if count >= 3 then
|
2016-12-23 04:22:04 +00:00
|
|
|
local url = GIT_URL .. '/' .. fname
|
2016-12-18 19:38:48 +00:00
|
|
|
local c = loadUrl(url)
|
|
|
|
if c then
|
|
|
|
return load(c, modname, nil, env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2016-12-18 19:38:48 +00:00
|
|
|
local function urlSearcher(modname, env, shell)
|
|
|
|
local fname = modname:gsub('%.', '/') .. '.lua'
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2016-12-18 19:38:48 +00:00
|
|
|
if fname:sub(1, 1) ~= '/' then
|
2017-07-24 02:37:07 +00:00
|
|
|
for entry in string.gmatch(_G.package.upath, "[^;]+") do
|
2016-12-18 19:38:48 +00:00
|
|
|
local url = entry .. '/' .. fname
|
|
|
|
local c = loadUrl(url)
|
|
|
|
if c then
|
|
|
|
return load(c, modname, nil, env)
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
2016-12-18 19:38:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
_G.package = {
|
|
|
|
path = LUA_PATH or 'sys/apis',
|
|
|
|
upath = LUA_UPATH or DEFAULT_UPATH,
|
2016-12-23 04:22:04 +00:00
|
|
|
config = '/\n:\n?\n!\n-',
|
|
|
|
loaded = {
|
|
|
|
math = math,
|
|
|
|
string = string,
|
|
|
|
table = table,
|
|
|
|
io = io,
|
|
|
|
os = os,
|
|
|
|
},
|
2016-12-18 19:38:48 +00:00
|
|
|
loaders = {
|
2016-12-23 04:22:04 +00:00
|
|
|
standardSearcher,
|
2016-12-18 19:38:48 +00:00
|
|
|
shellSearcher,
|
|
|
|
pathSearcher,
|
|
|
|
pastebinSearcher,
|
|
|
|
gitSearcher,
|
|
|
|
urlSearcher,
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2016-12-18 19:38:48 +00:00
|
|
|
local function requireWrapper(env)
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2016-12-18 19:38:48 +00:00
|
|
|
local loaded = { }
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2016-12-18 19:38:48 +00:00
|
|
|
return function(modname)
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2016-12-18 19:38:48 +00:00
|
|
|
if loaded[modname] then
|
|
|
|
return loaded[modname]
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
2017-07-24 02:37:07 +00:00
|
|
|
for _,searcher in ipairs(_G.package.loaders) do
|
2016-12-23 04:22:04 +00:00
|
|
|
local fn, msg = searcher(modname, env, shell)
|
2016-12-18 19:38:48 +00:00
|
|
|
if fn then
|
|
|
|
local module, msg = fn(modname, env)
|
|
|
|
if not module then
|
|
|
|
error(msg)
|
|
|
|
end
|
|
|
|
loaded[modname] = module
|
|
|
|
return module
|
|
|
|
end
|
2016-12-23 04:22:04 +00:00
|
|
|
if msg then
|
2016-12-27 03:26:43 +00:00
|
|
|
error(msg, 2)
|
2016-12-23 04:22:04 +00:00
|
|
|
end
|
2016-12-18 19:38:48 +00:00
|
|
|
end
|
|
|
|
error('Unable to find module ' .. modname)
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return function(env)
|
|
|
|
setfenv(requireWrapper, env)
|
|
|
|
return requireWrapper(env)
|
|
|
|
end
|