Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local fs
- for f in component.list("filesystem") do
- if (f ~= computer.tmpAddress()) then
- fs = component.proxy(f)
- end
- end
- local lfile = fs.open("reactor.log", "a")
- --This really isn't ideal.
- function print(...)
- fs.write(lfile, "["..computer.uptime().."] "..table.concat({...}, "\t").."\n")
- end
- if (component.proxy(component.list("tape_drive")()) and component.proxy(component.list("tape_drive")()).write) then
- local prox = component.proxy(component.list("tape_drive")())
- function print(...)
- prox.write("["..computer.uptime().."] "..table.concat({...}, "\t").."\n")
- end
- end
- print("litekernel init started.")
- (function()
- -- lkern is the light kernel used by Zorya NEO to simplify development.
- local kpkg = {}
- kpkg.libs = {}
- local krlib = kpkg.libs
- kpkg.search = {}
- local krfind = kpkg.search
- function krequire(pkg)
- if (krlib[pkg]) then return krlib[pkg] end
- for i=1, #krfind do
- local r = krfind[i](pkg)
- if (r) then krlib[pkg] = r() return krlib[pkg] end
- end
- end
- local krequire = krequire
- krlib["fs"] = (function()
- local vfs = {}
- local fs = {}
- function fs.mount(path, proxy)
- vfs[#vfs+1] = {path, proxy}
- end
- function fs.resolve(path)
- for i=#vfs, 1, -1 do
- if (path:sub(1, #vfs[i][1]) == vfs[i][1]) or (path:sub(1, #vfs[i][1]).."/" == vfs[i][1]) then
- return path:sub(#vfs[i][1]), vfs[i][2]
- end
- end
- return nil, "not found"
- end
- fs.mount("/", {
- list = function()
- local lst = {}
- for i=1, #vfs do
- if (vfs[i][1]:match("^/.+/$")) then
- lst[#lst+1] = vfs[i][1]:sub(2)
- end
- end
- return lst
- end,
- exists = function(path)
- local lst = {}
- for i=1, #vfs do
- if (vfs[i][1]:match("^/.+/$")) then
- lst[vfs[i][1]:sub(1, #vfs[i][1]-1)] = true
- end
- end
- return (lst[path]~=nil)
- end,
- isDirectory = function(path)
- local lst = {}
- for i=1, #vfs do
- if (vfs[i][1]:match("^/.+/$")) then
- lst[vfs[i][1]:sub(1, #vfs[i][1]-1)] = true
- end
- end
- return (lst[path]~=nil)
- end,
- isReadOnly = function()
- return true
- end
- })
- -- Mount all filesystems.
- for c in component.list("filesystem") do
- fs.mount("/"..c:sub(1, 8).."/", component.proxy(c))
- end
- return fs
- end)()
- krlib["thd"] = (function()
- local thd = {}
- local threads = {}
- local idx = 1
- local computer = computer
- local unpack = unpack or table.unpack
- local coroutine = coroutine
- local c_create = coroutine.create
- local c_yield = coroutine.yield
- local c_resume = coroutine.resume
- local c_status = coroutine.status
- function thd.add(name, func)
- threads[#threads+1] = {name, c_create(func), {}, 0, ".+"}
- end
- local sigs = {}
- function thd.autosleep()
- local msleep = math.huge
- for i=1, #threads do
- if (threads[i][4] and threads[i][4] < msleep) then
- msleep = threads[i][4]
- end
- end
- local rsleep = msleep-computer.uptime()
- if (rsleep < 0 or #sigs > 0) then
- rsleep = 0
- end
- local sig = {ps(rsleep)}
- if (#sigs > 0) then
- if (#sig > 0) then
- sigs[#sigs+1] = sig
- end
- sig = sigs[1]
- table.remove(sigs, 1)
- end
- return sig
- end
- local last_sig = {}
- function thd.run()
- last_sig = thd.autosleep()
- for i=1, #threads do
- if (threads[i][4] <= computer.uptime() or #last_sig > 0) then
- if (c_status(threads[i][2]) ~= "running") then
- local s, dl = (c_resume(threads[i][2], unpack(last_sig)))
- if not s then
- if print then print("[lkos] [error] "..threads[i][1].." "..dl) end
- end
- dl = computer.uptime() + (dl or math.huge)
- threads[i][4] = dl
- sigs[#sigs+1] = {ps(0)}
- end
- end
- end
- local t = {}
- for i=1, #threads do
- if (c_status(threads[i][2]) ~= "dead" or threads[i][6]) then
- t[#t+1] = threads[i]
- end
- end
- threads = t
- return #threads > 0
- end
- function thd.kill(i)
- threads[i][6] = true
- end
- function thd.sched_end()
- return #threads == idx
- end
- function thd.get_threads()
- return threads
- end
- local pxy = component.proxy(component.list("sandbox")())
- local function dbg(...)
- pxy.log(table.concat({"[debug]", ...}, "\t"))
- return ...
- end
- function computer.pullSignal(t)
- return c_yield(t)
- end
- return thd
- end)()
- krlib["blt"] = (function()
- -- BLT, made for Lua 5.3
- local blt = {}
- local types
- local function serialize(val)
- local t = type(val)
- if (t == "number") then
- t = math.type(val)
- end
- local b, str = types["s"..t](val)
- b = (b << 3) | types[t]
- return string.char(b) .. str
- end
- local function deserialize(str, t)
- local tb = str:byte(1)
- local type_ = tb & 7
- local b = tb >> 3
- local v, l = types[type_](b, str:sub(2))
- return v, l+1
- end
- local function fromdouble(f)
- return 0, string.pack("<d", f)
- end
- local function todouble(b, str)
- return string.unpack("<d", str:sub(1, 8)), 8
- end
- local function _fromlongint(i)
- --print("longint")
- return 8, string.pack("<l", i)
- end
- local function fromint(i)
- --Time to rabidly optimize this.
- local len = 0
- local cmp2 = 0
- if (i > 0xFFFFFFFFFFFFFF) then return _fromlongint(i) end
- for j=0, 7 do
- len = len + 1
- cmp2 = cmp2 | (0xFF << j)
- --print("fromint", i+((cmp2//2)), cmp2)
- --if (i+((cmp2//2)) <= cmp2) then
- if (math.abs(i) <= cmp2//2) then
- break
- end
- end
- if (i < 0) then
- i = i + (cmp2//2)
- end
- --i = i + (cmp2//2)
- local tmp = ""
- for j=0, len-1 do
- tmp = tmp .. string.char((i & (0xFF << (j*8))) >> (j*8))
- end
- --local tmp = string.pack("<i["..len.."]", i)
- return len, tmp
- end
- local function _tolongint(str)
- --print("longint2")
- return string.unpack("<l", str:sub(1, 8)), 8
- end
- local function toint(b, str)
- if (b == 8) then return _tolongint(str) end
- --return string.unpack("<i["..b.."]", str:sub(1, b)), b
- local tmp = 0
- for i=0, b-1 do
- tmp = tmp | (str:byte(i+1) << (i*8))
- end
- local sign = (tmp & (0x80 << ((b-1)*8)))
- sign = sign << (63 - (b*8))
- local int = tmp & ((0x80 << ((b-1)*8)) ~ 0xFFFFFFFFFFFFFF)
- return int | sign, b
- end
- local function frombool(b)
- return b and 1 or 0, ""
- end
- local function tobool(b, str)
- return b ~= 0, 0
- end
- local function fromstr(s)
- local len, val = fromint(#s)
- return len, val .. s
- end
- local function tostr(b, str)
- local strl, l = toint(b, str)
- local rtn = str:sub(1+l, l+strl)
- return rtn, strl+l
- end
- local function fromarray(a)
- local b, tmp = fromint(#a)
- for i=1, #a do
- tmp = tmp .. serialize(a[i])
- end
- --print("alen_s", #tmp)
- return b, tmp
- end
- local function toarray(b, str, arr)
- local arrl, l = toint(b, str)
- --print("clen", l)
- --print("arr len", arrl)
- local arr = {}
- local i = 0
- for i=1, arrl do
- --print("adec", i)
- local v, z = deserialize(str:sub(1+l))
- --print("arr", i, v)
- l = l+z
- --print("clen", l, z)
- arr[i] = v
- end
- --print("alen", l)
- return arr, l
- end
- local function fromtbl(t)
- local tmp = ""
- --See if the numerical keys are a list, and, if so, write a list
- local nindex = 0
- local nmax = 0
- for k, v in pairs(t) do
- if (type(k) == "number") then
- if (math.type(k) == "integer") then
- nindex = nindex + 1
- if (nmax < k) then
- nmax = k
- end
- end
- else
- local ks = serialize(k)
- local vs = serialize(v)
- tmp = tmp .. ks .. vs
- end
- end
- if (nmax > 0) then
- if (nindex == nmax) then
- local ib, dat = fromarray(t)
- tmp = tmp .. string.char(0) .. string.char(types.table_array | (ib << 3)) .. dat
- else
- for k, v in pairs(t) do
- if (type(k) == "number" and math.type(k) == "integer") then
- local ks = serialize(k)
- local vs = serialize(v)
- tmp = tmp .. ks .. vs
- end
- end
- end
- end
- return 0, tmp .. string.char(0,0) --nil,nil terminated
- end
- local function totbl(b, str)
- local t = {}
- local k = ""
- local v = ""
- local pos = 1
- --print("topen")
- while true do
- --print("k", str:byte(pos), str:byte(pos) & 7)
- local k, l = deserialize(str:sub(pos))
- pos = pos + l
- --print("v", str:byte(pos), str:byte(pos) & 7)
- if (str:byte(pos) & 7 == 6) then
- --print("ailen", str:byte(pos) & (7 ~ 0xFF))
- local r, l = deserialize(str:sub(pos))
- pos = pos + l
- for i=1, #r do
- t[i] = r[i]
- end
- else
- local v, l = deserialize(str:sub(pos))
- pos = pos + l
- if (not v and not k) then
- --print("tclose")
- break
- end
- --print("decode", k, v)
- t[k] = v
- end
- end
- return t, pos-1 --how
- end
- -- Type LUT
- types = {
- ["nil"] = 0,
- float = 1,
- number = 1,
- integer = 2,
- string = 3,
- boolean = 4,
- table = 5,
- table_array = 6, --Meta-value
- [0] = function(b, str) return nil, 0 end,
- [1] = todouble,
- [2] = toint,
- [3] = tostr,
- [4] = tobool,
- [5] = totbl,
- [6] = toarray,
- snil = function()return 0, ""end,
- sfloat = fromdouble,
- sinteger = fromint,
- sstring = fromstr,
- sboolean = frombool,
- stable = fromtbl,
- stable_array = fromarray
- }
- function blt.serialize(...)
- local args = {...}
- local tmp = string.char(#args)
- for i=1, #args do
- local str = serialize(args[i])
- tmp = tmp .. str
- end
- return tmp
- end
- local unpack = unpack or table.unpack
- function blt.deserialize(str)
- local args = {}
- local pos = 2
- local amt = str:byte(1)
- local l
- for i=1, amt do
- local v, l = deserialize(str:sub(pos))
- args[i] = v
- pos = pos + l
- end
- return unpack(args)
- end
- return blt
- end)()
- krlib["microtel"] = (function()
- _G.net={}
- do
- local modems,packetQueue,packetCache,routeCache,C,Y = {},{},{},{},COMPUTER,UNPACK
- net.port,net.hostname,net.route,net.hook,U=4096,computer.address():sub(1,8),true,{},UPTIME
- for a in component.list("modem") do
- modems[a] = component.proxy(a)
- modems[a].open(net.port)
- end
- local function genPacketID()
- local packetID = ""
- for i = 1, 16 do
- packetID = packetID .. string.char(math.random(32,126))
- end
- return packetID
- end
- local function rawSendPacket(packetID,packetType,to,from,vport,data)
- if routeCache[to] then
- modems[routeCache[to][1]].send(routeCache[to][2],net.port,packetID,packetType,to,from,vport,data)
- else
- for k,v in pairs(modems) do
- v.broadcast(net.port,packetID,packetType,to,from,vport,data)
- end
- end
- end
- local function sendPacket(packetID,packetType,to,vport,data)
- packetCache[packetID] = computer.uptime()
- rawSendPacket(packetID,packetType,to,net.hostname,vport,data)
- end
- function net.send(to,vport,data,packetType,packetID)
- packetType,packetID = packetType or 1, packetID or genPacketID()
- packetQueue[packetID] = {packetType,to,vport,data,0}
- sendPacket(packetID,packetType,to,vport,data)
- end
- local function checkCache(packetID)
- for k,v in pairs(packetCache) do
- if k == packetID then
- return false
- end
- end
- return true
- end
- local realComputerPullSignal = computer.pullSignal
- function computer.pullSignal(t)
- local eventTab = {realComputerPullSignal(t)}
- for k,v in pairs(net.hook) do
- pcall(v,table.unpack(eventTab))
- end
- for k,v in pairs(packetCache) do
- if computer.uptime() > v+30 then
- packetCache[k] = nil
- end
- end
- for k,v in pairs(routeCache) do
- if computer.uptime() > v[3]+30 then
- routeCache[k] = nil
- end
- end
- if eventTab[1] == "modem_message" and (eventTab[4] == net.port or eventTab[4] == 0) and checkCache(eventTab[6]) then
- routeCache[eventTab[9]] = {eventTab[2],eventTab[3],computer.uptime()}
- if eventTab[8] == net.hostname then
- if eventTab[7] ~= 2 then
- computer.pushSignal("net_msg",eventTab[9],eventTab[10],eventTab[11])
- if eventTab[7] == 1 then
- sendPacket(genPacketID(),2,eventTab[9],eventTab[10],eventTab[6])
- end
- else
- packetQueue[eventTab[11]] = nil
- end
- elseif net.route and checkCache(eventTab[6]) then
- rawSendPacket(eventTab[6],eventTab[7],eventTab[8],eventTab[9],eventTab[10],eventTab[11])
- end
- packetCache[eventTab[6]] = computer.uptime()
- end
- for k,v in pairs(packetQueue) do
- if computer.uptime() > v[5] then
- sendPacket(k,table.unpack(v))
- v[5]=computer.uptime()+30
- end
- end
- return table.unpack(eventTab)
- end
- end
- net.mtu = 4096
- function net.lsend(to,vport,ldata)
- local tdata = {}
- for i = 1, ldata:len(), net.mtu do
- tdata[#tdata+1] = ldata:sub(1,net.mtu)
- ldata = ldata:sub(net.mtu+1)
- end
- for k,v in ipairs(tdata) do
- net.send(to,vport,v)
- end
- end
- function net.socket(address, port, sclose)
- local conn, rb = {}, ""
- conn.state, conn.buffer, conn.port, conn.address = "o", "", tonumber(port), address
- function conn.r(self,l)
- rb=self.buffer:sub(1,l)
- self.buffer=self.buffer:sub(l+1)
- return rb
- end
- function conn.w(self,data)
- net.lsend(self.address,self.port,data)
- end
- function conn.c(s)
- net.send(conn.address,conn.port,sclose)
- end
- function h(etype, from, port, data)
- if from == conn.address and port == conn.port then
- if data == sclose then
- net.hook[sclose] = nil
- conn.state = "c"
- return
- end
- conn.buffer = conn.buffer..data
- end
- end
- net.hook[sclose] = h
- return conn
- end
- function net.listen(vport)
- local from,port,data
- repeat
- _, from, port, data = computer.pullSignal(0)
- until port == vport and data == "openstream"
- local nport,sclose = math.random(2^15,2^16),tostring(math.random(-2^16,2^16))
- net.send(from,port,tostring(nport))
- net.send(from,nport,sclose)
- return net.socket(from,nport,sclose)
- end
- net.timeout = 60
- function net.open(address,vport)
- local st,from,port,data=computer.uptime()
- net.send(address,vport,"openstream")
- repeat
- _, from, port, data = computer.pullSignal(0.5)
- if computer.uptime() > st+net.timeout then return false end
- until from == address and port == vport and tonumber(data)
- vport=tonumber(data)
- repeat
- _, from, port, data = computer.pullSignal(0.5)
- until from == address and port == vport
- return net.socket(address,vport,data)
- end
- return net
- end)()
- krlib["zlan"] = (function()
- local zlan = {}
- local mt = krequire("microtel")
- local function zlan_frame(app, command)
- return string.char(2,0,#app)..app..command
- end
- local function zlan_recv(sock)
- local packets = {}
- local rpmx = 0
- while true do
- --Recieve first bit of packet
- if (sock:r(2) ~= "\2\0") then
- return nil, "invalid host zlan version"
- end
- local pid = sock:r(1):byte()
- local pmx = sock:r(1):byte()
- if (pmx > rpmx) then
- rpmx = pmx
- end
- local size = sock:r(1):byte() | (sock:r(1):byte() << 8)
- packets[pid] = sock:r(size)
- if (#packets == rpmx) then
- return table.concat(packets, "")
- end
- end
- end
- function zlan.information(hn, pkg)
- local sock = mt.open(hn, 9900)
- sock:write(zlan_frame(pkg, "i"))
- local dat = zlan_recv(sock)
- local size = (dat:byte(1) | (dat:byte(2) << 8) | (dat:byte(3) << 16))
- local namelen = dat:byte(4)
- local name = dat:sub(5, 5+namelen)
- local vlen = dat:byte(6+namelen)
- local ver = dat:sub(7+namelen, 7+namelen+vlen)
- local fmt = dat:byte(8+namelen+vlen)
- sock:c()
- return {
- size = size,
- name = name,
- version = ver,
- format = fmt
- }
- end
- function zlan.exists(hn, pkg)
- local sock = mt.open(hn, 9900)
- sock:write(zlan_frame(pkg, "i"))
- local dat = zlan_recv(sock)
- sock:c()
- return (dat:byte(1) ~= 0)
- end
- function zlan.download(hn, pkg)
- local sock = mt.open(hn, 9900)
- sock:write(zlan_frame(pkg, "d"))
- local dat = zlan_recv(sock)
- sock:c()
- return dat
- end
- return zlan
- end)()
- local ps = computer.pullSignal
- local thd = krequire("thd")
- local last_sig = {}
- krlib["system"] = (function()
- local sys = {}
- function sys.start()
- while thd.run() do end
- end
- local prun_i = 0
- function sys.protected_run(code, name)
- name = name or "lkprc$"..prun_i
- --Spin up a new thread
- local env = {}
- for k, v in pairs(_G) do
- env[k] = v
- end
- env._ENV = env
- env._G = env
- env.krequire = nil
- thd.add(name, assert(load(code, "="..name, "t", env)))
- end
- function sys.add_lib(lib, tbl)
- krlib[lib] = tbl
- end
- function sys.add_search(search)
- krfind[#krfind+1] = search
- end
- return sys
- end)()
- krlib["sys"] = krlib["system"]
- end)()
- print("litekernel init complete.")
- _OSVERSION="Reactor AMS 1.0"
- _OSVER=1.0
- print(_OSVERSION)
- local builtin_progs = {}
- print("[init] Loading builtins...")
- builtin_prog["ams"] = function(...)
- local microtel = krequire("microtel")
- local cfg = krequire("cfg")
- local reactor = component.proxy(component.list("br_reactor")())
- local turbine = component.proxy(component.list("br_turbine")())
- local get_energy = turbine.getEnergyStored
- local get_flow_rate = tubrine.getFluidFlowRate
- local get_rpm = tubrine.getRotorSpeed
- local get_heat = reactor.getFuelTemperature
- local set_active = reactor.setActive
- local get_fuel = reactor.getFuelAmount
- local get_fuel_max = reactor.getFuelAmountMAx
- while true do
- computer.pullSignal()
- end
- end
- builtin_prog["frequest_client"] = function(...)
- end
- builtin_prog["sysctl"] = function(...)
- end
- local thd = krequire("thd")
- local sys = krequire("sys")
- sys.add_lib("cfg", {
- max_temp=1500,
- warn_temp=1300,
- broadcast_temp_warn=true,
- broadcast_errors=true
- })
- sys.add_lib("sysupdate", function(hostname, pkg)
- local res, err = require("zlan").download(hostname, pkg)
- if not res then return err end
- local prom = component.proxy(component.list("ossm_prom")())
- print("[update] Erasing EEPROM...")
- prom.erase()
- print("[update] Writing EEPROM...")
- for i=0, math.ceil(#res/512)-1 do
- prom.blockWrite(i+1, res:sub((i*512)+1, (i+1)*512))
- end
- print("[update] Update complete.")
- print("[update] Rebooting...")
- end)
- _RID = -1
- for k, v in pairs(builtin_prog) do
- print("[init] Starting "..k)
- thd.add(k, v)
- end
- print("[init] Builtin loading complete.")
- print("[init] Starting system.")
- sys.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement