Advertisement
dadragon84

Opus API

Feb 25th, 2025
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.74 KB | Source Code | 0 0
  1. local GIT_URL        = 'https://raw.githubusercontent.com'
  2. local DEFAULT_UPATH  = GIT_URL .. '/kepler155c/opus-installer/master/sys/apis'
  3.  
  4. local http   = _G.http
  5. local os     = _G.os
  6. local string = _G.string
  7.  
  8. local function loadUrl(url)
  9.     local c
  10.     local h = http.get(url)
  11.     if h then
  12.         c = h.readAll()
  13.         h.close()
  14.     end
  15.     if c and #c > 0 then
  16.         return c
  17.     end
  18. end
  19.  
  20. -- Add require and package to the environment
  21. return function(env)
  22.     local function loadedSearcher(modname)
  23.             if env.package.loaded[modname] then
  24.             return function()
  25.                 return env.package.loaded[modname]
  26.             end
  27.         end
  28.     end
  29.  
  30.     local function urlSearcher(modname)
  31.         local fname = modname:gsub('%.', '/') .. '.lua'
  32.  
  33.         if fname:sub(1, 1) ~= '/' then
  34.             for entry in string.gmatch(env.package.upath, "[^;]+") do
  35.                 local url = entry .. '/' .. fname
  36.                 local c = loadUrl(url)
  37.                 if c then
  38.                     return load(c, modname, nil, env)
  39.                 end
  40.             end
  41.         end
  42.     end
  43.  
  44.     -- place package and require function into env
  45.     env.package = {
  46.         upath  = env.LUA_UPATH or _G.LUA_UPATH or DEFAULT_UPATH,
  47.         config = '/\n:\n?\n!\n-',
  48.         preload = { },
  49.         loaded = {
  50.             coroutine = coroutine,
  51.             io     = io,
  52.             math   = math,
  53.             os     = os,
  54.             string = string,
  55.             table  = table,
  56.         },
  57.         loaders = {
  58.             loadedSearcher,
  59.             urlSearcher,
  60.         }
  61.     }
  62.  
  63.     function env.require(modname)
  64.         for _,searcher in ipairs(env.package.loaders) do
  65.             local fn, msg = searcher(modname)
  66.             if fn then
  67.                 local module, msg2 = fn(modname, env)
  68.                 if not module then
  69.                     error(msg2 or (modname .. ' module returned nil'), 2)
  70.                 end
  71.                 env.package.loaded[modname] = module
  72.                 return module
  73.             end
  74.             if msg then
  75.                 error(msg, 2)
  76.             end
  77.         end
  78.         error('Unable to find module ' .. modname)
  79.     end
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement