Guest User

Untitled

a guest
Sep 26th, 2025
4
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. -- install.lua — One-shot installer for your CC project (MAIN or CPU)
  2. -- Usage:
  3. -- install main <BASE_URL>
  4. -- install cpu <BASE_URL>
  5. --
  6. -- BASE_URL should be the folder that contains all files (lib/*.lua, main.lua, craft_cpu.lua, audio/*.dfpwm, etc.)
  7.  
  8. local tArgs = {...}
  9. local ROLE = (tArgs[1] or ""):lower()
  10. local BASE = tArgs[2]
  11.  
  12. local function die(msg) printError(msg) error(msg, 0) end
  13. if (ROLE ~= "main" and ROLE ~= "cpu") then
  14. die("Usage: install main|cpu <BASE_URL>")
  15. end
  16. if not BASE then die("Missing <BASE_URL>") end
  17.  
  18. if not http then die("HTTP API is disabled on this server.") end
  19.  
  20. local function join(a,b)
  21. if a:sub(-1) == "/" then return a..b end
  22. return a.."/"..b
  23. end
  24.  
  25. local function mkdirs(path)
  26. local dir = fs.getDir(path)
  27. if dir ~= "" and not fs.exists(dir) then fs.makeDir(dir) end
  28. end
  29.  
  30. local function save(path, data)
  31. mkdirs(path)
  32. local mode = path:match("%.dfpwm$") and "wb" or "w"
  33. local f = fs.open(path, mode)
  34. if not f then die("Cannot write "..path) end
  35. f.write(data)
  36. f.close()
  37. print("✓ "..path)
  38. end
  39.  
  40. local function get(url, binary)
  41. local res = http.get(url, nil, binary and true or false)
  42. if not res then return nil, "http.get failed" end
  43. local data = res.readAll()
  44. res.close()
  45. return data
  46. end
  47.  
  48. local function fetch(remote, localPath, opts)
  49. opts = opts or {}
  50. local url = join(BASE, remote)
  51. local bin = opts.binary or remote:match("%.dfpwm$")
  52. local data, err = get(url, bin)
  53. if not data then
  54. if opts.optional then
  55. print("… skip (missing): "..localPath)
  56. return false
  57. end
  58. die("Download failed: "..url.." ("..tostring(err)..")")
  59. end
  60. save(localPath, data)
  61. return true
  62. end
  63.  
  64. -- manifest of files expected at <BASE>
  65. local COMMON = {
  66. {"lib/ui.lua", "/lib/ui.lua"},
  67. {"lib/net.lua", "/lib/net.lua"},
  68. {"lib/sound.lua", "/lib/sound.lua"},
  69. {"lib/storage.lua", "/lib/storage.lua"},
  70. {"lib/util.lua", "/lib/util.lua"},
  71. }
  72.  
  73. local MAIN_ONLY = {
  74. {"main.lua", "/main.lua"},
  75. }
  76.  
  77. local CPU_ONLY = {
  78. {"craft_cpu.lua", "/craft_cpu.lua"},
  79. }
  80.  
  81. local AUDIO_OPT = {
  82. {"audio/bios_intro.dfpwm", "/audio/bios_intro.dfpwm"},
  83. {"audio/overlay.dfpwm", "/audio/overlay.dfpwm"},
  84. {"audio/ambient_loop.dfpwm","/audio/ambient_loop.dfpwm"},
  85. {"audio/error.dfpwm","/audio/error.dfpwm"}
  86. }
  87.  
  88. print("== Installing ("..ROLE..") from "..BASE)
  89.  
  90. -- download libs
  91. for _,p in ipairs(COMMON) do fetch(p[1], p[2]) end
  92.  
  93. -- program files per role
  94. if ROLE=="main" then
  95. for _,p in ipairs(MAIN_ONLY) do fetch(p[1], p[2]) end
  96. elseif ROLE=="cpu" then
  97. for _,p in ipairs(CPU_ONLY) do fetch(p[1], p[2]) end
  98. end
  99.  
  100. -- audio (optional: won’t error if not present at BASE)
  101. for _,p in ipairs(AUDIO_OPT) do fetch(p[1], p[2], {optional=true, binary=true}) end
  102.  
  103. -- seed configs if missing
  104. local function write_if_missing(path, tbl)
  105. if fs.exists(path) then return end
  106. mkdirs(path)
  107. local f = fs.open(path, "w")
  108. f.write(textutils.serializeJSON(tbl, true))
  109. f.close()
  110. print("✓ "..path.." (new)")
  111. end
  112.  
  113. if ROLE=="main" then
  114. write_if_missing("/config.json", {
  115. ROLE="MAIN",
  116. HOSTNAME_MAIN="main",
  117. HOSTNAME_CPU="autocraft-cpu",
  118. STORAGE_PERIPHERAL=nil,
  119. MONITOR_NAME=nil,
  120. UI_TARGET_COLS=60,
  121. UI_LAYOUT="monitor",
  122. AMBIENT_VOLUME=0.4,
  123. BOOT_SECONDS=10
  124. })
  125. elseif ROLE=="cpu" then
  126. write_if_missing("/config.json", {
  127. ROLE="CPU",
  128. HOSTNAME_MAIN="main",
  129. HOSTNAME_CPU="autocraft-cpu",
  130. STORAGE_PERIPHERAL=nil,
  131. START_DELAY=5
  132. })
  133. end
  134. write_if_missing("/recipes.json", {recipes={}})
  135.  
  136. -- startup wrapper (auto-restart on crash, clean exit respected)
  137. local startup
  138. if ROLE=="main" then
  139. startup = [[
  140. local function run() shell.run("/main.lua") end
  141. while true do
  142. local ok, err = pcall(run)
  143. if ok then break end
  144. printError(err); sleep(2)
  145. end
  146. ]]
  147. else
  148. startup = [[
  149. local function run() shell.run("/craft_cpu.lua") end
  150. while true do
  151. local ok, err = pcall(run)
  152. if ok then break end
  153. printError(err); sleep(2)
  154. end
  155. ]]
  156. end
  157.  
  158. save("/startup.lua", startup)
  159. print("\nAll set. Rebooting...")
  160. sleep(0.5)
  161. os.reboot()
  162.  
Advertisement
Add Comment
Please, Sign In to add comment