OC-PsychOS2/module/loadfile.lua

33 lines
1.1 KiB
Lua
Raw Normal View History

function loadfile(p) -- string -- function -- reads file *p* and returns a function if possible
local f = io.open(p,"rb")
2018-11-03 03:05:41 +11:00
local c = f:read("*a")
f:close()
return load(c,p,"t")
end
function runfile(p,...) -- string -- -- runs file *p* with arbitrary arguments in the current thread
return loadfile(p)(...)
2018-11-03 03:05:41 +11:00
end
2020-03-18 01:11:53 +11:00
_G.package = {}
package.path="./?;./?.lua;/boot/lib/?.lua;/pkg/lib/?.lua;/boot/lib/?/init.lua;/pkg/lib/?/init.lua"
package.loaded = {computer=computer,component=component,fs=fs,buffer=buffer}
function require(f,force) -- string boolean -- table -- searches for a library with name *f* and returns what the library returns, if possible. if *force* is set, loads the library even if it is cached
if not package.loaded[f] or force then
2020-06-25 16:29:12 +10:00
local ln = f:gsub("%.","/")
for d in package.path:gmatch("[^;]+") do
local p = d:gsub("%?",ln)
if fs.exists(p) and not fs.isDirectory(p) then
package.loaded[f] = runfile(p)
break
2019-12-20 01:51:27 +11:00
end
end
end
2020-03-18 01:11:53 +11:00
if package.loaded[f] then
return package.loaded[f]
end
2019-12-20 01:51:27 +11:00
error("library not found: "..f)
2018-11-03 03:05:41 +11:00
end
function reload(f) -- string -- table -- Reloads library *f* from disk into memory.
return require(f,true)
end