Advertisement
joebodo

sys.apis.injector.lua

Dec 18th, 2016
1,267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.93 KB | None | 0 0
  1. local DEFAULT_UPATH = 'https://raw.githubusercontent.com/kepler155c/opus/master/sys/apis'
  2. local PASTEBIN_URL  = 'http://pastebin.com/raw'
  3. local GIT_URL       = 'https://raw.githubusercontent.com'
  4.  
  5. -- fix broken http get
  6. local syncLocks = { }
  7.  
  8. local function sync(obj, fn)
  9.   local key = tostring(obj)
  10.   if syncLocks[key] then
  11.     local cos = tostring(coroutine.running())
  12.     table.insert(syncLocks[key], cos)
  13.     repeat
  14.       local _, co = os.pullEvent('sync_lock')
  15.     until co == cos
  16.   else
  17.     syncLocks[key] = { }
  18.   end
  19.   local s, m = pcall(fn)
  20.   local co = table.remove(syncLocks[key], 1)
  21.   if co then
  22.     os.queueEvent('sync_lock', co)
  23.   else
  24.     syncLocks[key] = nil
  25.   end
  26.   if not s then
  27.     error(m)
  28.   end
  29. end
  30.  
  31. local function loadUrl(url)
  32.   local c
  33.   sync(url, function()
  34.     local h = http.get(url)
  35.     if h then
  36.       c = h.readAll()
  37.       h.close()
  38.     end
  39.   end)
  40.   if c and #c > 0 then
  41.     return c
  42.   end
  43. end
  44.  
  45. local function requireWrapper(env)
  46.  
  47.   local function standardSearcher(modname, env, shell)
  48.     if package.loaded[modname] then
  49.       return function()
  50.         return package.loaded[modname]
  51.       end
  52.     end
  53.   end
  54.  
  55.   local function shellSearcher(modname, env, shell)
  56.     local fname = modname:gsub('%.', '/') .. '.lua'
  57.  
  58.     if shell and type(shell.dir) == 'function' then
  59.       local path = shell.resolve(fname)
  60.       if fs.exists(path) and not fs.isDir(path) then
  61.         return loadfile(path, env)
  62.       end
  63.     end
  64.   end
  65.  
  66.   local function pathSearcher(modname, env, shell)
  67.     local fname = modname:gsub('%.', '/') .. '.lua'
  68.  
  69.     for dir in string.gmatch(package.path, "[^:]+") do
  70.       local path = fs.combine(dir, fname)
  71.       if fs.exists(path) and not fs.isDir(path) then
  72.         return loadfile(path, env)
  73.       end
  74.     end
  75.   end
  76.  
  77.   -- require('BniCQPVf')
  78.   local function pastebinSearcher(modname, env, shell)
  79.     if #modname == 8 and not modname:match('%W') then
  80.       local url = PASTEBIN_URL .. '/' .. modname
  81.       local c = loadUrl(url)
  82.       if c then
  83.         return load(c, modname, nil, env)
  84.       end
  85.     end
  86.   end
  87.  
  88.   -- require('kepler155c.opus.master.sys.apis.util')
  89.   local function gitSearcher(modname, env, shell)
  90.     local fname = modname:gsub('%.', '/') .. '.lua'
  91.     local _, count = fname:gsub("/", "")
  92.     if count >= 3 then
  93.       local url = GIT_URL .. '/' .. fname
  94.       local c = loadUrl(url)
  95.       if c then
  96.         return load(c, modname, nil, env)
  97.       end
  98.     end
  99.   end
  100.  
  101.   local function urlSearcher(modname, env, shell)
  102.     local fname = modname:gsub('%.', '/') .. '.lua'
  103.  
  104.     if fname:sub(1, 1) ~= '/' then
  105.       for entry in string.gmatch(package.upath, "[^;]+") do
  106.         local url = entry .. '/' .. fname
  107.         local c = loadUrl(url)
  108.         if c then
  109.           return load(c, modname, nil, env)
  110.         end
  111.       end
  112.     end
  113.   end
  114.  
  115.   -- place package and require function into env
  116.   package = {
  117.     path = LUA_PATH or 'sys/apis',
  118.     upath = LUA_UPATH or DEFAULT_UPATH,
  119.     config = '/\n:\n?\n!\n-',
  120.     loaded = {
  121.       math   = math,
  122.       string = string,
  123.       table  = table,
  124.       io     = io,
  125.       os     = os,
  126.     },
  127.     loaders = {
  128.       standardSearcher,
  129.       shellSearcher,
  130.       pathSearcher,
  131.       pastebinSearcher,
  132.       gitSearcher,
  133.       urlSearcher,
  134.     }
  135.   }
  136.  
  137.   function require(modname)
  138.  
  139.     for _,searcher in ipairs(package.loaders) do
  140.       local fn, msg = searcher(modname, env, shell)
  141.       if fn then
  142.         local module, msg = fn(modname, env)
  143.         if not module then
  144.           error(msg or (modname .. ' module returned nil'), 2)
  145.         end
  146.         package.loaded[modname] = module
  147.         return module
  148.       end
  149.       if msg then
  150.         error(msg, 2)
  151.       end
  152.     end
  153.     error('Unable to find module ' .. modname)
  154.   end
  155.  
  156.   return require -- backwards compatible
  157. end
  158.  
  159. return function(env)
  160.   setfenv(requireWrapper, env)
  161.   return requireWrapper(env)
  162. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement