1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-13 04:09:55 +00:00
opus/sys/apis/injector.lua

177 lines
4.1 KiB
Lua
Raw Normal View History

2018-01-15 21:28:10 +00:00
local DEFAULT_UPATH = 'https://raw.githubusercontent.com/kepler155c/opus/' .. _ENV.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'
2017-10-08 21:45:01 +00:00
local fs = _G.fs
local http = _G.http
local os = _G.os
2017-10-09 00:03:01 +00:00
if not http._patched then
-- fix broken http get
local syncLocks = { }
2016-12-18 19:38:48 +00:00
2017-10-09 00:03:01 +00:00
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
2017-10-09 04:26:19 +00:00
fn()
2017-10-09 00:03:01 +00:00
local co = table.remove(syncLocks[key], 1)
if co then
os.queueEvent('sync_lock', co)
else
syncLocks[key] = nil
end
2016-12-11 19:24:52 +00:00
end
2017-10-09 00:03:01 +00:00
-- todo -- completely replace http.get with function that
-- checks for success on permanent redirects (minecraft 1.75 bug)
http._patched = http.get
function http.get(url, headers)
local s, m
sync(url, function()
s, m = http._patched(url, headers)
end)
return s, m
2016-12-18 19:38:48 +00:00
end
end
2016-12-11 19:24:52 +00:00
2016-12-18 19:38:48 +00:00
local function loadUrl(url)
local c
2017-10-09 00:03:01 +00:00
local h = http.get(url)
if h then
c = h.readAll()
h.close()
end
2016-12-18 19:38:48 +00:00
if c and #c > 0 then
return c
end
2016-12-11 19:24:52 +00:00
end
local function requireWrapper(env)
2017-10-08 21:45:01 +00:00
local function standardSearcher(modname)
if package.loaded[modname] then
return function()
return package.loaded[modname]
end
2016-12-18 19:38:48 +00:00
end
end
2016-12-11 19:24:52 +00:00
2017-10-08 21:45:01 +00:00
local function shellSearcher(modname)
local fname = modname:gsub('%.', '/') .. '.lua'
2017-10-08 21:45:01 +00:00
if env.shell and type(env.shell.dir) == 'function' then
local path = env.shell.resolve(fname)
if fs.exists(path) and not fs.isDir(path) then
return loadfile(path, env)
end
2016-12-18 19:38:48 +00:00
end
end
2016-12-11 19:24:52 +00:00
2017-10-08 21:45:01 +00:00
local function pathSearcher(modname)
local fname = modname:gsub('%.', '/') .. '.lua'
2016-12-11 19:24:52 +00:00
for dir in string.gmatch(package.path, "[^:]+") do
local path = fs.combine(dir, fname)
if fs.exists(path) and not fs.isDir(path) then
return loadfile(path, env)
end
end
end
-- require('BniCQPVf')
2017-10-08 21:45:01 +00:00
local function pastebinSearcher(modname)
if #modname == 8 and not modname:match('%W') then
local url = PASTEBIN_URL .. '/' .. modname
2016-12-18 19:38:48 +00:00
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
-- require('kepler155c.opus.master.sys.apis.util')
2017-10-08 21:45:01 +00:00
local function gitSearcher(modname)
local fname = modname:gsub('%.', '/') .. '.lua'
local _, count = fname:gsub("/", "")
if count >= 3 then
local url = GIT_URL .. '/' .. fname
local c = loadUrl(url)
if c then
return load(c, modname, nil, env)
end
end
end
2016-12-11 19:24:52 +00:00
2017-10-08 21:45:01 +00:00
local function urlSearcher(modname)
local fname = modname:gsub('%.', '/') .. '.lua'
2016-12-11 19:24:52 +00:00
if fname:sub(1, 1) ~= '/' then
for entry in string.gmatch(package.upath, "[^;]+") do
local url = entry .. '/' .. fname
local c = loadUrl(url)
if c then
return load(c, modname, nil, env)
end
end
end
end
2016-12-11 19:24:52 +00:00
-- place package and require function into env
2017-10-08 21:45:01 +00:00
env.package = {
path = LUA_PATH or 'sys/apis',
upath = LUA_UPATH or DEFAULT_UPATH,
config = '/\n:\n?\n!\n-',
loaded = {
math = math,
string = string,
table = table,
io = io,
os = os,
},
loaders = {
standardSearcher,
shellSearcher,
pathSearcher,
pastebinSearcher,
gitSearcher,
urlSearcher,
}
}
2016-12-11 19:24:52 +00:00
2017-10-08 21:45:01 +00:00
function env.require(modname)
2016-12-11 19:24:52 +00:00
for _,searcher in ipairs(package.loaders) do
2017-10-08 21:45:01 +00:00
local fn, msg = searcher(modname)
2016-12-18 19:38:48 +00:00
if fn then
2017-10-08 21:45:01 +00:00
local module, msg2 = fn(modname, env)
2016-12-18 19:38:48 +00:00
if not module then
2017-10-08 21:45:01 +00:00
error(msg2 or (modname .. ' module returned nil'), 2)
2016-12-18 19:38:48 +00:00
end
package.loaded[modname] = module
2016-12-18 19:38:48 +00:00
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
2017-10-08 21:45:01 +00:00
return env.require -- backwards compatible
2016-12-11 19:24:52 +00:00
end
return function(env)
2017-10-08 21:45:01 +00:00
env = env or getfenv(2)
2016-12-11 19:24:52 +00:00
setfenv(requireWrapper, env)
return requireWrapper(env)
end