Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- install.lua — One-shot installer for your CC project (MAIN or CPU)
- -- Usage:
- -- install main <BASE_URL>
- -- install cpu <BASE_URL>
- --
- -- BASE_URL should be the folder that contains all files (lib/*.lua, main.lua, craft_cpu.lua, audio/*.dfpwm, etc.)
- local tArgs = {...}
- local ROLE = (tArgs[1] or ""):lower()
- local BASE = tArgs[2]
- local function die(msg) printError(msg) error(msg, 0) end
- if (ROLE ~= "main" and ROLE ~= "cpu") then
- die("Usage: install main|cpu <BASE_URL>")
- end
- if not BASE then die("Missing <BASE_URL>") end
- if not http then die("HTTP API is disabled on this server.") end
- local function join(a,b)
- if a:sub(-1) == "/" then return a..b end
- return a.."/"..b
- end
- local function mkdirs(path)
- local dir = fs.getDir(path)
- if dir ~= "" and not fs.exists(dir) then fs.makeDir(dir) end
- end
- local function save(path, data)
- mkdirs(path)
- local mode = path:match("%.dfpwm$") and "wb" or "w"
- local f = fs.open(path, mode)
- if not f then die("Cannot write "..path) end
- f.write(data)
- f.close()
- print("✓ "..path)
- end
- local function get(url, binary)
- local res = http.get(url, nil, binary and true or false)
- if not res then return nil, "http.get failed" end
- local data = res.readAll()
- res.close()
- return data
- end
- local function fetch(remote, localPath, opts)
- opts = opts or {}
- local url = join(BASE, remote)
- local bin = opts.binary or remote:match("%.dfpwm$")
- local data, err = get(url, bin)
- if not data then
- if opts.optional then
- print("… skip (missing): "..localPath)
- return false
- end
- die("Download failed: "..url.." ("..tostring(err)..")")
- end
- save(localPath, data)
- return true
- end
- -- manifest of files expected at <BASE>
- local COMMON = {
- {"lib/ui.lua", "/lib/ui.lua"},
- {"lib/net.lua", "/lib/net.lua"},
- {"lib/sound.lua", "/lib/sound.lua"},
- {"lib/storage.lua", "/lib/storage.lua"},
- {"lib/util.lua", "/lib/util.lua"},
- }
- local MAIN_ONLY = {
- {"main.lua", "/main.lua"},
- }
- local CPU_ONLY = {
- {"craft_cpu.lua", "/craft_cpu.lua"},
- }
- local AUDIO_OPT = {
- {"audio/bios_intro.dfpwm", "/audio/bios_intro.dfpwm"},
- {"audio/overlay.dfpwm", "/audio/overlay.dfpwm"},
- {"audio/ambient_loop.dfpwm","/audio/ambient_loop.dfpwm"},
- {"audio/error.dfpwm","/audio/error.dfpwm"}
- }
- print("== Installing ("..ROLE..") from "..BASE)
- -- download libs
- for _,p in ipairs(COMMON) do fetch(p[1], p[2]) end
- -- program files per role
- if ROLE=="main" then
- for _,p in ipairs(MAIN_ONLY) do fetch(p[1], p[2]) end
- elseif ROLE=="cpu" then
- for _,p in ipairs(CPU_ONLY) do fetch(p[1], p[2]) end
- end
- -- audio (optional: won’t error if not present at BASE)
- for _,p in ipairs(AUDIO_OPT) do fetch(p[1], p[2], {optional=true, binary=true}) end
- -- seed configs if missing
- local function write_if_missing(path, tbl)
- if fs.exists(path) then return end
- mkdirs(path)
- local f = fs.open(path, "w")
- f.write(textutils.serializeJSON(tbl, true))
- f.close()
- print("✓ "..path.." (new)")
- end
- if ROLE=="main" then
- write_if_missing("/config.json", {
- ROLE="MAIN",
- HOSTNAME_MAIN="main",
- HOSTNAME_CPU="autocraft-cpu",
- STORAGE_PERIPHERAL=nil,
- MONITOR_NAME=nil,
- UI_TARGET_COLS=60,
- UI_LAYOUT="monitor",
- AMBIENT_VOLUME=0.4,
- BOOT_SECONDS=10
- })
- elseif ROLE=="cpu" then
- write_if_missing("/config.json", {
- ROLE="CPU",
- HOSTNAME_MAIN="main",
- HOSTNAME_CPU="autocraft-cpu",
- STORAGE_PERIPHERAL=nil,
- START_DELAY=5
- })
- end
- write_if_missing("/recipes.json", {recipes={}})
- -- startup wrapper (auto-restart on crash, clean exit respected)
- local startup
- if ROLE=="main" then
- startup = [[
- local function run() shell.run("/main.lua") end
- while true do
- local ok, err = pcall(run)
- if ok then break end
- printError(err); sleep(2)
- end
- ]]
- else
- startup = [[
- local function run() shell.run("/craft_cpu.lua") end
- while true do
- local ok, err = pcall(run)
- if ok then break end
- printError(err); sleep(2)
- end
- ]]
- end
- save("/startup.lua", startup)
- print("\nAll set. Rebooting...")
- sleep(0.5)
- os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment