Advertisement
osmarks

PotatOS Tetrahedron

Feb 5th, 2019
1,922
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.97 KB | None | 0 0
  1. local hidden = false
  2.  
  3. local function randbytes(len)
  4.     local out = ""
  5.     for i = 1, len do
  6.         out = out .. string.char(math.random(0, 255))
  7.     end
  8.     return out
  9. end
  10.  
  11. local function fetch(u)
  12.     local h = http.get(u)
  13.     local c = h.readAll()
  14.     h.close()
  15.     return c
  16. end
  17.  
  18. local function fwrite(n, c)
  19.     local f = fs.open(n, "w")
  20.     f.write(c)
  21.     f.close()
  22. end
  23.  
  24. -- Read file "n"
  25. local function fread(n)
  26.     local f = fs.open(n, "r")
  27.     local out = f.readAll()
  28.     f.close()
  29.     return out
  30. end
  31.  
  32. local function set(k, v)
  33.     settings.set(k, v)
  34.     settings.save(".settings")
  35. end
  36.  
  37. local function map(f, t)
  38.     local mapper = function(t)
  39.         local new = {}
  40.         for k, v in pairs(t) do
  41.             local new_v, new_k = f(v, k)
  42.             new[new_k or k] = new_v
  43.         end
  44.         return new
  45.     end
  46.     if t then return mapper(t) else return mapper end
  47. end
  48.  
  49. local function arrayize(t)
  50.     local out = {}
  51.     for k, v in pairs(t) do table.insert(out, v) end
  52.     return out
  53. end
  54.  
  55. local this_file = "autorun"
  56. local this_file_URL = "https://pastebin.com/raw/T81NJMpt"
  57.  
  58. local files = {
  59.     -- CRITICAL FILES
  60.     ["https://pastebin.com/raw/HL0SZhJG"] = "startup", -- "Polychoron" process manager - needs to be startup for TLCOing
  61.     [this_file_URL] = this_file,
  62.     ["https://pastebin.com/raw/rxkE8N8b"] = "stack_trace",
  63.     ["https://pastebin.com/raw/LzEYgSZi"] = "bios",
  64.  
  65.     -- LIBRARIES
  66.     ["https://raw.githubusercontent.com/osmarks/skynet/master/client.lua"] = "lib/02-skynet",
  67.     ["https://raw.githubusercontent.com/rxi/json.lua/bee7ee3431133009a97257bde73da8a34e53c15c/json.lua"] = "lib/01-json",
  68.     ["https://pastebin.com/raw/Frv3xkB9"] = "lib/03-yafss",
  69.     ["https://pastebin.com/raw/KXHSsHkt"] = "lib/04-ser",
  70.     ["https://pastebin.com/raw/2kRenvr3"] = "lib/05-registry",
  71.     ["https://raw.githubusercontent.com/tmpim/luadash/master/library.lua"] = "lib/06-dash",
  72.     ["https://pastebin.com/raw/PkaFNfcq"] = "lib/50-potatOS",
  73.  
  74.     -- MODULES
  75.     ["https://pastebin.com/raw/7RDG0ueq"] = "mod/01-spudnetd",
  76.     ["https://pastebin.com/raw/690JY2dK"] = "mod/02-netbootd",
  77.     ["https://pastebin.com/raw/ABd8CGJB"] = "mod/03-locationd",
  78.  
  79.     -- MISCELLANEOUS FILES
  80.     ["https://pastebin.com/raw/NdUKJ07j"] = "LICENSES"
  81. }
  82.  
  83. local function install()
  84.     -- Make a folder where users' files will go.
  85.     fs.makeDir "userdata"
  86.  
  87.     -- Download all files in parallel.
  88.     local fns = arrayize(map(function(filename, URL)
  89.         return function()
  90.             local dir = fs.getDir(filename)
  91.             if dir ~= "" then
  92.                 if not fs.isDir(dir) then fs.makeDir(dir) end
  93.             end
  94.             if fs.isDir(filename) then fs.delete(filename) end
  95.             fwrite(filename, fetch(("%s?%d"):format(URL, math.random(0, 10000))))
  96.             if not hidden then print("Downloaded", filename) end
  97.         end
  98.     end, files))
  99.  
  100.     parallel.waitForAll(unpack(fns))
  101.  
  102.     -- Stop people using disks. Honestly, did they expect THAT to work?
  103.     set("shell.allow_disk_startup", false)
  104.     set("shell.allow_startup", true)
  105.  
  106.     os.setComputerLabel("P4/" .. randbytes(64))
  107.  
  108.     os.reboot()
  109. end
  110.  
  111. local background_task_interval = 300 + (os.getComputerID() % 20)
  112.  
  113. local function update_checker()
  114.     local this = fread(this_file)
  115.     while true do
  116.         local new = fetch(this_file_URL)
  117.         local ok, err = load(new)
  118.         if not ok then print("Syntax error in update:", printError(err))
  119.         else
  120.             if new ~= this then
  121.                 install()
  122.             end
  123.         end
  124.    
  125.         sleep(background_task_interval)
  126.     end
  127. end
  128.  
  129. local function load_module(E, mod_path)
  130.     local fn, err = load(fread(fs.combine("/mod", mod_path)), "@" .. mod_path, nil, E)
  131.     if not fn then
  132.         printError(("%s LOAD ERROR:\n%s"):format(mod_path, err))
  133.         return
  134.     end
  135.     local ok, mod = pcall(fn)
  136.     if not ok then printError(("%s PREEXEC ERROR:\n%s"):format(mod_path, mod)) end
  137.     if mod.async then process.spawn(mod.async, mod.name) end
  138.     if mod.sync then
  139.         local ok, err = pcall(mod.sync, mod)
  140.         if not ok then
  141.             printError(("%s EXEC ERROR:\n%s"):format(mod.name, err))
  142.         end
  143.     end
  144. end
  145.  
  146. local function load_library(E, lib)
  147.     local ok, res = pcall(loadfile(fs.combine("/lib", lib), E))
  148.     local libname = lib:match "^[^-]*\-(.*)$"
  149.     if ok then
  150.         E[libname] = res
  151.         package.loaded[libname] = res
  152.     else
  153.         printError(("%s ERROR:\n%s"):format(libname, res))
  154.     end
  155. end
  156.  
  157. local function main()
  158.     if fs.exists "stack_trace" then dofile "stack_trace" end
  159.     process.spawn(update_checker, "updd")
  160.     local E = {}
  161.     setmetatable(E, { __index = _ENV })
  162.     for _, file in pairs(fs.list "/lib") do
  163.         load_library(E, file)
  164.     end
  165.     local fns = {}
  166.     for _, file in pairs(fs.list "/mod") do
  167.         table.insert(fns, function() load_module(E, file) end)
  168.     end
  169.     parallel.waitForAll(unpack(fns))
  170.  
  171.     E.yafss("userdata", {}, E, fread "bios")
  172.     while true do coroutine.yield() end
  173. end
  174.  
  175. local args = table.concat({...}, " ")
  176. if args:find "update" or args:find "install" then install() end
  177.  
  178. if polychoron and fs.exists "startup" and fs.exists "LICENSES" then
  179.     local ok, err = pcall(main)
  180.     if not ok then polychoron.BSOD(err) end
  181. else
  182.     install()
  183. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement