Operating system for OpenComputers
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

36 lines
1.6KB

  1. function loadfile(p) -- string -- function -- reads file *p* and returns a function if possible
  2. local f = io.open(p,"rb")
  3. local c = f:read("*a")
  4. f:close()
  5. return load(c,p,"t")
  6. end
  7. function runfile(p,...) -- string -- -- runs file *p* with arbitrary arguments in the current thread
  8. return loadfile(p)(...)
  9. end
  10. function os.spawnfile(p,n,...) -- string string -- number -- spawns a new process from file *p* with name *n*, with arguments following *n*.
  11. local tA = {...}
  12. 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)
  13. end
  14. _G.package = {}
  15. package.loaded = {computer=computer,component=component,fs=fs,buffer=buffer,table=table,string=string,package=package,os=os,math=math,coroutine=coroutine}
  16. 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
  17. if not package.loaded[f] or force then
  18. local lib = os.getenv("LIB") or "/boot/lib\n/pkg/lib"
  19. local ln = f:gsub("%.","/")
  20. for d in lib:gmatch("[^\n]+") do
  21. if fs.isDirectory(d.."/"..ln) then
  22. package.loaded[f] = runfile(d.."/"..ln.."/init.lua")
  23. elseif fs.exists(d.."/"..ln..".lua") then
  24. package.loaded[f] = runfile(d.."/"..ln..".lua")
  25. end
  26. end
  27. end
  28. if package.loaded[f] then
  29. return package.loaded[f]
  30. end
  31. error("library not found: "..f)
  32. end
  33. function reload(f) -- string -- table -- Reloads library *f* from disk into memory.
  34. return require(f,true)
  35. end