OC-misc/tapeloader/tapeloader.lua

59 lines
1.0 KiB
Lua

local fs = component.proxy(computer.tmpAddress())
local init = function() end
if fs.exists("/init.lua") then
do
local tape = component.proxy(component.list("tape_drive")())
local function toint(s)
s=s or ""
local n = 0
local i = 1
while true do
local p = s:sub(i,i)
if p == "" then break end
local b = string.byte(p)
n = n << 8
n = n | b
i=i+1
end
return n
end
local function fwrite(name,len)
local dir = name:match("(.+)/.*%.?.+")
if (dir) then
fs.makeDirectory("/"..dir)
end
local fh = fs.open(name, "w")
fs.write(fh,tape.read(len))
fs.close(fh)
end
tape.seek(-math.huge)
while true do
local nlen = toint(tape.read(2))
if nlen == 0 then
break
end
local name = tape.read(nlen)
local fsize = toint(tape.read(2))
fwrite(name,fsize)
computer.beep()
end
end
end
do
local f=fs.open("/init.lua","rb")
if not f then error("no init.lua") end
local initstr = ""
local data = ""
repeat
initstr = initstr .. data
data = fs.read(f,2048)
until data == nil
init = load(initstr)
end
computer.getBootAddress = computer.tmpAddress
init()