1
1
mirror of https://git.shadowkat.net/izaya/OC-PsychOS2.git synced 2024-11-10 15:48:36 -05:00
OC-PsychOS2/module/loadfile.lua

32 lines
1.2 KiB
Lua
Raw Normal View History

function loadfile(p) -- reads file *p* and returns a function if possible
local f = io.open(p,"rb")
2018-11-02 12:05:41 -04:00
local c = f:read("*a")
f:close()
return load(c,p,"t")
end
function runfile(p,...) -- runs file *p* with arbitrary arguments in the current thread
return loadfile(p)(...)
2018-11-02 12:05:41 -04:00
end
function os.spawnfile(p,n,...) -- spawns a new process from file *p* with name *n*, with arguments following *n*.
local tA = {...}
return os.spawn(function() local res={pcall(loadfile(p), table.unpack(tA))} computer.pushSignal("process_finished", os.pid(), table.unpack(res)) dprint(table.concat(res)) end,n or p)
end
2020-03-17 10:11:53 -04:00
_G.package = {}
package.loaded = {computer=computer,component=component,fs=fs}
function require(f) -- searches for a library with name *f* and returns what the library returns, if possible
2020-03-17 10:11:53 -04:00
if not package.loaded[f] then
2019-12-19 09:51:27 -05:00
local lib = os.getenv("LIB") or "/boot/lib"
for d in lib:gmatch("[^\n]+") do
if fs.exists(d.."/"..f) then
2020-03-17 10:11:53 -04:00
package.loaded[f] = runfile(d.."/"..f)
2019-12-19 09:51:27 -05:00
elseif fs.exists(d.."/"..f..".lua") then
2020-03-17 10:11:53 -04:00
package.loaded[f] = runfile(d.."/"..f..".lua")
2019-12-19 09:51:27 -05:00
end
end
end
2020-03-17 10:11:53 -04:00
if package.loaded[f] then
return package.loaded[f]
end
2019-12-19 09:51:27 -05:00
error("library not found: "..f)
2018-11-02 12:05:41 -04:00
end