Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ChorOS
- -- Govos fork for Chorus City infrastructure
- _G.os.pullEvent = coroutine.yield
- _ENV.os.pullEvent = coroutine.yield
- function randbytes(len)
- local out = ""
- for i = 1, len do
- out = out .. string.char(math.random(0, 255))
- end
- return out
- end
- function fetch(u)
- local h, e = http.get(u)
- if not h then error(("failed to fetch URL %s: %s"):format(u, tostring(e))) end
- local c = h.readAll()
- h.close()
- return c
- end
- function fwrite(n, c)
- local f = fs.open(n, "w")
- f.write(c)
- f.close()
- end
- -- Read file "n"
- function fread(n)
- local f = fs.open(n, "r")
- local out = f.readAll()
- f.close()
- return out
- end
- local function set(k, v)
- settings.set(k, v)
- settings.save(".settings")
- end
- local function map(f, t)
- local mapper = function(t)
- local new = {}
- for k, v in pairs(t) do
- local new_v, new_k = f(v, k)
- new[new_k or k] = new_v
- end
- return new
- end
- if t then return mapper(t) else return mapper end
- end
- local function arrayize(t)
- local out = {}
- for k, v in pairs(t) do table.insert(out, v) end
- return out
- end
- local this_file = "autorun"
- local this_file_URL = "https://osmarks.net/stuff/choros/ChorOS.lua"
- local files = {
- ["https://osmarks.net/stuff/choros/polychoron.lua"] = "startup", -- "Polychoron" process manager - needs to be startup for TLCOing
- [this_file_URL] = this_file,
- ["https://osmarks.net/stuff/choros/yafss.lua"] = "yafss",
- ["https://osmarks.net/stuff/choros/json.lua"] = "json",
- ["https://osmarks.net/stuff/choros/skynet.lua"] = "skynet",
- ["https://osmarks.net/stuff/choros/bigfont.lua"] = "bigfont",
- ["https://osmarks.net/stuff/choros/ccss.lua"] = "ccss",
- ["https://osmarks.net/stuff/choros/ccposcast.lua"] = "ccposcast",
- ["https://osmarks.net/stuff/choros/ccss_player_positions_agent.lua"] = "ccss_player_positions_agent",
- }
- local function install()
- fs.makeDir "userdata"
- local fns = arrayize(map(function(filename, URL)
- return function()
- local dir = fs.getDir(filename)
- if dir ~= "" then
- if not fs.isDir(dir) then fs.makeDir(dir) end
- end
- if fs.isDir(filename) then fs.delete(filename) end
- fwrite(filename, fetch(URL))
- if not hidden then print("Downloaded", filename) end
- end
- end, files))
- parallel.waitForAll(unpack(fns))
- set("shell.allow_disk_startup", false)
- set("shell.allow_startup", true)
- os.setComputerLabel("ChorOS/" .. os.getComputerID())
- os.reboot()
- end
- local args = table.concat({...}, " ")
- if (not polychoron or not fs.exists "json" or not fs.exists "userdata") or args:find "update" then install() end
- local background_task_interval = 300 + (os.getComputerID() % 20)
- local function update_checker()
- sleep()
- local this = fread(this_file)
- while true do
- local new = fetch(this_file_URL)
- local ok, err = load(new)
- if not ok then print "Syntax error in update:" printError(err)
- else
- if new ~= this then
- install()
- end
- end
- sleep(background_task_interval)
- end
- end
- local j = require "json"
- function safe_json_serialize(x, prev)
- local t = type(x)
- if t == "number" then
- if x ~= x or x <= -math.huge or x >= math.huge then
- return tostring(x)
- end
- return string.format("%.14g", x)
- elseif t == "string" then
- return j.encode(x)
- elseif t == "table" then
- prev = prev or {}
- local as_array = true
- local max = 0
- for k in pairs(x) do
- if type(k) ~= "number" then as_array = false break end
- if k > max then max = k end
- end
- if as_array then
- for i = 1, max do
- if x[i] == nil then as_array = false break end
- end
- end
- if as_array then
- local res = {}
- for i, v in ipairs(x) do
- table.insert(res, safe_json_serialize(v))
- end
- return "["..table.concat(res, ",").."]"
- else
- local res = {}
- for k, v in pairs(x) do
- table.insert(res, j.encode(tostring(k)) .. ":" .. safe_json_serialize(v))
- end
- return "{"..table.concat(res, ",").."}"
- end
- elseif t == "boolean" then
- return tostring(x)
- elseif x == nil then
- return "null"
- else
- return j.encode(tostring(x))
- end
- end
- function _G.os.await_event(filter)
- while true do
- local ev = {coroutine.yield(filter)}
- if filter == nil or ev[1] == filter then
- return unpack(ev)
- end
- end
- end
- -- Powered by SPUDNET, the simple way to include remote debugging services in *your* OS. Contact Gollark today.
- local function spudnet()
- if not http or not http.websocket then return "Websockets do not actually exist on this platform" end
- local ws
- local function send_packet(msg)
- --ws.send(safe_serialize(msg))
- ws.send(safe_json_serialize(msg))
- end
- local function send(data)
- send_packet { type = "send", channel = "client:potatOS", data = data }
- end
- local function connect()
- if ws then ws.close() end
- ws, err = http.websocket "wss://spudnet.osmarks.net/v4"
- if not ws then print("websocket failure %s", err) return false end
- ws.url = "wss://spudnet.osmarks.net/v4"
- send_packet { type = "identify", implementation = "ChorOS RDS" }
- send_packet { type = "set_channels", channels = { "client:potatOS" } }
- print("websocket connected")
- return true
- end
- local function try_connect_loop()
- while not connect() do
- sleep(0.5)
- end
- end
- try_connect_loop()
- local function recv()
- while true do
- local e, u, x = os.await_event "websocket_message"
- if u == ws.url then return j.decode(x) end
- end
- end
- local ping_timeout_timer = nil
- process.thread(function()
- while true do
- local _, t = os.await_event "timer"
- if t == ping_timeout_timer and ping_timeout_timer then
- -- 15 seconds since last ping, we probably got disconnected
- print "timed out, attempting reconnect"
- try_connect_loop()
- end
- end
- end, "ping-timeout")
- while true do
- -- Receive and run code which is sent via SPUDNET
- -- Also handle SPUDNETv4 protocol, primarily pings
- local packet = recv()
- --add_log("test %s", textutils.serialise(packet))
- if packet.type == "ping" then
- send_packet { type = "pong", seq = packet.seq }
- if ping_timeout_timer then os.cancelTimer(ping_timeout_timer) end
- ping_timeout_timer = os.startTimer(15)
- elseif packet.type == "error" then
- print("SPUDNET error %s %s %s %s", packet["for"], packet.error, packet.detail, textutils.serialise(packet))
- elseif packet.type == "message" then
- local code = packet.data
- if type(code) == "string" then
- _G.wsrecv = recv
- _G.wssend = send
- _G.envrequire = require
- --add_log("SPUDNET command - %s", code)
- local f, errr = load(code, "@<code>", "t", _G)
- if f then -- run safely in background, send back response
- process.thread(function() local resp = {pcall(f)} send(resp) end, "spudnetexecutor")
- else
- send {false, errr}
- end
- end
- end
- end
- end
- local fcache = {}
- local function fproxy(file)
- if fcache[file] then return fcache[file]
- else
- local ok, t = pcall(fread, file)
- if not ok then return 'printError "Error. Try again later, or reboot, or run upd."' end
- fcache[file] = t
- return t
- end
- end
- function loop(f)
- return function()
- while true do
- pcall(f)
- local tm = os.startTimer(0.2)
- while true do
- local _, t = coroutine.yield "timer"
- if tm == t then break end
- end
- end
- end
- end
- local function user_programs()
- if fs.exists "bigfont" then os.loadAPI "bigfont" end
- local ChorOS = {
- update = function()
- shell.run "autorun update"
- end,
- version = function()
- return "ChorOS"
- end,
- }
- local API_overrides = {
- ChorOS = ChorOS,
- os = { version = ChorOS.version },
- safe_serialize = safe_serialize,
- polychoron = polychoron,
- process = process,
- bigfont = bigfont,
- loop = loop,
- ["~expect"] = _G["~expect"], -- ??? added in new update for some reason
- }
- local function add(module)
- local ok, res = pcall(require, module)
- if ok then
- API_overrides[module] = res
- end
- end
- add "skynet"
- local pfldr = "/rom/programs/"
- local FS_overlay = {
- [pfldr .. "upd.lua"] = [[ChorOS.update()]],
- [pfldr .. "setloc.lua"] = [[
- local newloc = { gps.locate() }
- local key = "ChorOS.position"
- local oldloc = settings.get(key)
- settings.set(key, #newloc > 0 and newloc or oldloc)
- print("Newly scanned position:", textutils.serialise(newloc))
- settings.save ".settings"
- ]]
- }
- local function add_program(name)
- FS_overlay[pfldr .. name .. ".lua"] = fproxy(name)
- end
- add_program "ccss"
- add_program "ccposcast"
- add_program "ccss_player_positions_agent"
- require "yafss"(
- "/userdata/",
- FS_overlay,
- API_overrides,
- { URL = "https://pastebin.com/raw/JUEGJ4Bk" }
- )
- end
- local function power_on_others()
- while true do
- peripheral.find("computer", function(_, o)
- local l = o.getLabel()
- if l and (l:match "^P/" or l:match "ShutdownOS" or l:match "Govos" or l:match "ChorOS") and not o.isOn() then
- o.turnOn()
- end
- end)
- sleep(5)
- end
- end
- local palmap = { 32768, 4096, 8192, 2, 2048, 1024, 512, 256, 128, 16384, 32, 16, 8, 4, 64, 1 }
- local default_palette = { 0x000000, 0x7F664C, 0x57A64E, 0xF2B233, 0x3366CC, 0xB266E5, 0x4C99B2, 0x999999, 0x4C4C4C, 0xCC4C4C, 0x7FCC19, 0xDEDE6C, 0x99B2F2, 0xE57FD8, 0xF2B2CC, 0xFFFFFF }
- local function init_screen(t)
- for i, c in pairs(default_palette) do
- t.setPaletteColor(palmap[i], c)
- end
- end
- function init_screens()
- peripheral.find("monitor", function(_, o) init_screen(o) end)
- init_screen(term.native())
- end
- init_screens()
- process.spawn(loop(update_checker), "upd")
- process.spawn(loop(spudnet), "spudnet")
- process.spawn(user_programs, "user")
- process.spawn(loop(power_on_others), "onsys")
- while true do coroutine.yield() end
Add Comment
Please, Sign In to add comment