Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- https://github.com/cc-tweaked/CC-Tweaked/blob/f4e542b4db686dad6f3fe1c4fe0006fc06dd2bd9/src/main/resources/data/computercraft/lua/bios.lua#L6-L15
- local expect
- do
- local h = fs.open("rom/modules/main/cc/expect.lua", "r")
- local f, err = loadstring(h.readAll(), "@expect.lua")
- h.close()
- if not f then error(err) end
- expect = f().expect
- end
- -- https://github.com/cc-tweaked/CC-Tweaked/blob/f4e542b4db686dad6f3fe1c4fe0006fc06dd2bd9/src/main/resources/data/computercraft/lua/bios.lua#L520-L564
- local tAPIsLoading = {}
- function os.loadAPI(_sPath)
- expect(1, _sPath, "string")
- local sName = fs.getName(_sPath)
- if sName:sub(-4) == ".lua" then
- sName = sName:sub(1, -5)
- end
- if tAPIsLoading[sName] == true then
- printError("API " .. sName .. " is already being loaded")
- return false
- end
- tAPIsLoading[sName] = true
- local tEnv = {}
- setmetatable(tEnv, { __index = _G })
- local fnAPI, err = loadfile(_sPath, nil, tEnv)
- if fnAPI then
- local ok, err = pcall(fnAPI)
- if not ok then
- tAPIsLoading[sName] = nil
- return error("Failed to load API " .. sName .. " due to " .. err, 1)
- end
- else
- tAPIsLoading[sName] = nil
- return error("Failed to load API " .. sName .. " due to " .. err, 1)
- end
- local tAPI = {}
- for k, v in pairs(tEnv) do
- if k ~= "_ENV" then
- tAPI[k] = v
- end
- end
- _G[sName] = tAPI
- tAPIsLoading[sName] = nil
- return true
- end
- function os.unloadAPI(_sName)
- expect(1, _sName, "string")
- if _sName ~= "_G" and type(_G[_sName]) == "table" then
- _G[_sName] = nil
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement