Ktlo

require recreation for ComputerCraft

Jun 9th, 2015
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.72 KB | None | 0 0
  1. --v2.0
  2. _G["shell"] = shell
  3. _G["multishell"] = multishell
  4. _G.package = {
  5.     ["loaded"] = {
  6.         ["os"] = os;
  7.         ["bit"] = bit;
  8.         ["colors"] = colors;
  9.         ["commands"] = commands;
  10.         ["coroutine"] = coroutine;
  11.         ["disk"] = disk;
  12.         ["fs"] = fs;
  13.         ["gps"] = gps;
  14.         ["help"] = help;
  15.         ["http"] = http;
  16.         ["io"] = io;
  17.         ["keys"] = keys;
  18.         ["math"] = math;
  19.         ["multishell"] = multishell;
  20.         ["paintutils"] = paintutils;
  21.         ["parallel"] = parallel;
  22.         ["peripheral"] = peripheral;
  23.         ["rednet"] = rednet;
  24.         ["redstone"] = redstone;
  25.         ["shell"] = shell;
  26.         ["string"] = string;
  27.         ["table"] = table;
  28.         ["term"] = term;
  29.         ["textutils"] = textutils;
  30.         ["turtle"] = turtle;
  31.         ["vector"] = vector;
  32.         ["window"] = window;
  33.     },
  34.     ["path"] = "/System/libs/?;/System/libs/?.lib;/System/libs/?.lua;/libs/?;/libs/?.lib;/libs/?.lua",
  35.     ["loaders"] = {
  36.         [1] = function(name)
  37.             if package.preload[name] then
  38.                 return true, package.preload[name]
  39.             else
  40.                 return false, 'in "package.preload,"'
  41.             end
  42.         end;
  43.         [2] = function(name)
  44.             local path = string.gsub(package.path, "?", name)
  45.             local result
  46.             local file
  47.             for subPath in string.gmatch(path, "([^;]+)") do
  48.                 file = io.open(subPath)
  49.                 if file then
  50.                     result = subPath
  51.                     break
  52.                 end
  53.             end
  54.             if not result then
  55.                 return false, 'in "package.path" with name "'..name..'"'
  56.             end
  57.             local text = file:read("*a")
  58.             local module, err = load(text, name)
  59.             if not module then
  60.                 return false, "Error in the module file: "..err
  61.             end
  62.             local env, meta = { }
  63.             env._ENV = env
  64.             meta = {
  65.                 __index = _G;
  66.                 name = tostring(env):gsub("table", "module");
  67.                 __tostring = function()
  68.                     return meta.name
  69.                 end;
  70.             }
  71.             setmetatable(env, meta)
  72.             setfenv(module, env)
  73.             return true, module
  74.         end
  75.     },
  76.     ["preload"] = { },
  77. }
  78. function _G.require( ... )
  79.     local modules = { }
  80.     for key, value in ipairs{ ... } do
  81.         local message = ""
  82.         if type(value) ~= "string" then error("Bad argument #"..key..": string expected, got "..type(value), 2) end
  83.         if package.loaded[value] then
  84.             modules[key] = package.loaded[value]
  85.         else
  86.             message = message..'Can\'t find module #'..key..' in "package.loaded",\n'
  87.             for i=1, #package.loaders do
  88.                 local boolean, foo = package.loaders[i](value)
  89.                 if boolean then
  90.                     local ok, module = pcall(foo)
  91.                     if not ok then message = message..'Error in module "'..value..'": '..module.."\n"
  92.                     else
  93.                         if module == nil then
  94.                             if next(getfenv(foo)) then
  95.                                 module = getfenv(foo)
  96.                             else
  97.                                 module = true
  98.                             end
  99.                         end
  100.                         if type(module) == "table" and getmetatable(module) == nil then
  101.                             local meta
  102.                             meta = {
  103.                                 __index = _G;
  104.                                 name = tostring(module):gsub("table", "module");
  105.                                 __tostring = function()
  106.                                     return meta.name
  107.                                 end;
  108.                             }
  109.                             setmetatable(module, meta)
  110.                         end
  111.                         modules[key] = module
  112.                         package.loaded[value] = module
  113.                         break
  114.                     end
  115.                 else
  116.                     message = message..foo.."\n"
  117.                 end
  118.             end
  119.             if not modules[key] then
  120.                 error(message, 2)
  121.             end
  122.         end
  123.     end
  124.     return unpack(modules)
  125. end
  126. function _G.module(name, lib)
  127.     if type(name) ~= "string" then error("Bad argument: string expected, got "..type(name), 2) end
  128.     if lib == nil then lib = { } end
  129.     if package.loaded[name] then error("Module with name \""..name.."\" has already loaded", 2)
  130.     else package.loaded[name] = lib end
  131.     return lib
  132. end
  133. function _G.unload(name)
  134.     package.loaded[name] = nil
  135. end
  136. function os.multiEvent( ... )
  137.     if not ... then
  138.         return coroutine.yield()
  139.     end
  140.     while true do
  141.         local var, var2 = { coroutine.yield() }
  142.         for _, value in ipairs{ ... } do
  143.             if var[1] == value then
  144.                 return unpack( var )
  145.             end
  146.         end
  147.     end
  148. end
Advertisement
Add Comment
Please, Sign In to add comment