-- fpml app launcher installer -- Run in game with: wget run https://launcher.pees.moe local SERVER_ID = 3 local PROTOCOL = "appstore" local TIMEOUT = 5 print("fpml launcher installer") local function fail(lines) printError("Install failed") for _, line in ipairs(lines) do print(line) end error("", 0) end -- Find a modem that can reach the app store local modem = peripheral.find("modem", function(_, m) return m.isWireless() end) if not modem then fail({ "No wireless modem found.", "Attach an ender modem to this device", "and run the installer again.", }) end local isEnder = false for _, t in ipairs({ peripheral.getType(modem) }) do if tostring(t):find("ender") then isEnder = true end end local name = peripheral.getName(modem) if not isEnder then -- Some versions report ender modems as plain "modem"; check the item too isEnder = (name:find("ender") ~= nil) end -- Ask the app store server for the current launcher rednet.open(name) rednet.send(SERVER_ID, { type = "get", name = "launcher" }, PROTOCOL) local code, hash local deadline = os.clock() + TIMEOUT while true do local left = deadline - os.clock() if left <= 0 then break end local id, reply = rednet.receive(PROTOCOL, left) if not id then break end if id == SERVER_ID and type(reply) == "table" and reply.reply == "get" and reply.name == "launcher" then if reply.files and reply.files["main.lua"] then code, hash = reply.files["main.lua"], reply.hash else fail({ "The app store sent a bad reply:", tostring(reply.error) }) end break end end if not code then if isEnder then fail({ "Couldn't reach the app store server.", "It may be turned off - ask the server", "owner to check computer " .. SERVER_ID .. ".", }) else fail({ "Couldn't reach the app store server.", "Normal wireless modems only reach about", "64 blocks. Swap it for an ender modem", "and run the installer again.", }) end end -- Never install something that doesn't compile local ok, err = load(code, "=launcher") if not ok then fail({ "The launcher on the server is broken:", tostring(err) }) end -- Keep any existing startup.lua that isn't a launcher if fs.exists("startup.lua") then local f = fs.open("startup.lua", "r") local old = f.readAll() f.close() if not old:find("app launcher & store", 1, true) then if fs.exists("startup.lua.bak") then fs.delete("startup.lua.bak") end fs.move("startup.lua", "startup.lua.bak") print("Old startup.lua saved as startup.lua.bak") end end local f = fs.open("startup.lua", "w") f.write(code) f.close() -- Record the version so the launcher doesn't reinstall itself on first boot local installed = {} if fs.exists(".installed") then local h = fs.open(".installed", "r") installed = textutils.unserialize(h.readAll()) or {} h.close() end installed.launcher = hash local h = fs.open(".installed", "w") h.write(textutils.serialize(installed)) h.close() print("Installed! Rebooting in 3s...") sleep(3) os.reboot()