Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- install.lua
- -- ComputerCraft Network System Installer (Stage 1)
- -- Reboot-gated safe install:
- -- - Reads netinst.cfg.start_background_processes
- -- - If true, set false and reboot so startup.lua won't spawn daemons
- -- - After reboot (flag=false), set true again and run GUI installer
- -- Also:
- -- - Ensures netinst.cfg exists with sane defaults (including blacklist)
- -- - (Re)installs Basalt per policy: "always" | "never" | "ask"
- -- - Generates install_network.lua (Basalt v2-friendly) and runs it
- local CONFIG_PATH = "netinst.cfg"
- local DEFAULT_CFG = [[
- -- netinst.cfg
- -- Network installer configuration (Lua table).
- return {
- -- Files or directories to ignore (never deleted or overwritten).
- -- Matches by prefix, e.g. "logs" ignores "logs/..." as well.
- blacklist = {
- "cleanup.lua",
- "logs",
- "netinst.cfg", -- protect the config itself
- "basalt.lua" -- don't overwrite Basalt during file copy
- },
- -- Where to write installer logs (created if missing). Set to nil to disable file logging.
- log_file = "logs/installer.log",
- -- How many retries for a failed download (1..3)
- retries = 2,
- -- Install mode for files:
- -- "ask" -> dialog (clean/update/cancel)
- -- "clean" -> clean install automatically (respects blacklist)
- -- "update"-> update/overwrite automatically (respects blacklist)
- install_mode = "ask",
- -- Basalt reinstall policy for Stage-1:
- -- "always" (default first-run behavior), "never", or "ask"
- reinstall_basalt = "always",
- -- First-run flag. On the first GUI run, you'll be asked for your preferred
- -- reinstall_basalt policy; the installer will set hasInstalled=true afterward.
- hasInstalled = false,
- -- Handshake with startup.lua. When false, startup.lua will not start daemons
- -- and will chain into install.lua. This script flips it back to true on entry.
- start_background_processes = true,
- -- Optional: cooperative process cleanup inside the GUI (usually not needed
- -- after the reboot gate, but harmless to keep)
- kill_processes = true,
- }
- ]]
- -- ===== small utilities =====
- local function printColored(text, color)
- local old = term.getTextColor and term.getTextColor() or colors.white
- if term.isColor and term.isColor() and color then term.setTextColor(color) end
- print(text)
- if term.isColor and term.isColor() then term.setTextColor(old) end
- end
- local function ensureConfig()
- if not fs.exists(CONFIG_PATH) then
- local dir = fs.getDir(CONFIG_PATH)
- if dir ~= "" and not fs.exists(dir) then fs.makeDir(dir) end
- local f = fs.open(CONFIG_PATH, "w"); f.write(DEFAULT_CFG); f.close()
- end
- end
- local function loadConfig()
- ensureConfig()
- local fn = loadfile(CONFIG_PATH, nil, {})
- local ok, cfg = pcall(fn or function() end)
- if not ok or type(cfg) ~= "table" then
- cfg = (load(DEFAULT_CFG, "cfg", "t", {}))()
- end
- -- sanitize
- cfg.retries = math.max(1, math.min(3, tonumber(cfg.retries or 2) or 2))
- if type(cfg.blacklist) ~= "table" then cfg.blacklist = {} end
- if cfg.reinstall_basalt ~= "always" and cfg.reinstall_basalt ~= "never" and cfg.reinstall_basalt ~= "ask" then
- cfg.reinstall_basalt = "always"
- end
- if cfg.install_mode ~= "clean" and cfg.install_mode ~= "update" then
- cfg.install_mode = "ask"
- end
- if cfg.hasInstalled ~= true then cfg.hasInstalled = false end
- if cfg.start_background_processes == nil then cfg.start_background_processes = true end
- if cfg.kill_processes == nil then cfg.kill_processes = true end
- return cfg
- end
- local function saveConfig(cfg)
- local function q(s) return string.format("%q", s) end
- local t = {}
- table.insert(t, "return {")
- table.insert(t, " blacklist = {")
- for _,p in ipairs(cfg.blacklist or {}) do table.insert(t, " " .. q(p) .. ",") end
- table.insert(t, " },")
- table.insert(t, " log_file = " .. (cfg.log_file and q(cfg.log_file) or "nil") .. ",")
- table.insert(t, " retries = " .. tostring(math.max(1, math.min(3, tonumber(cfg.retries or 2) or 2))) .. ",")
- table.insert(t, " install_mode = " .. q(cfg.install_mode or "ask") .. ",")
- table.insert(t, " reinstall_basalt = " .. q(cfg.reinstall_basalt or "always") .. ",")
- table.insert(t, " hasInstalled = " .. tostring(cfg.hasInstalled and true or false) .. ",")
- table.insert(t, " start_background_processes = " .. tostring(cfg.start_background_processes ~= false) .. ",")
- table.insert(t, " kill_processes = " .. tostring(cfg.kill_processes ~= false) .. ",")
- table.insert(t, "}")
- local dir = fs.getDir(CONFIG_PATH)
- if dir ~= "" and not fs.exists(dir) then fs.makeDir(dir) end
- local f = fs.open(CONFIG_PATH, "w"); f.write(table.concat(t, "\n")); f.close()
- end
- -- ===== check internet (event-based, no hang) =====
- local function checkInternet(timeout_s)
- local url = "https://www.google.com"
- if http.checkURL and http.checkURL(url) == false then return false end
- local tid = os.startTimer(timeout_s or 3)
- http.request(url)
- while true do
- local ev, a = os.pullEvent()
- if ev == "http_success" then
- local h = a; if h and h.close then h.close() end
- return true
- elseif ev == "http_failure" then
- return false
- elseif ev == "timer" and a == tid then
- return false
- end
- end
- end
- -- ===== Basalt policy =====
- local function installBasalt()
- print("Installing Basalt GUI framework...")
- local ok = shell.run("wget", "run", "https://raw.githubusercontent.com/Pyroxenium/Basalt2/main/install.lua")
- if not ok then printColored("ERROR: Failed to install Basalt", colors.red); return false end
- printColored("Basalt installed successfully", colors.green)
- return true
- end
- local function promptYesNo(msg, defaultNo)
- io.write(msg .. (defaultNo and " [y/N] " or " [Y/n] "))
- local ans = (read() or ""):lower():gsub("^%s+",""):gsub("%s+$","")
- if ans == "" then return not defaultNo end
- return (ans == "y" or ans == "yes")
- end
- local function handleBasaltPolicy(cfg)
- local policy = cfg.reinstall_basalt or "always"
- local exists = fs.exists("basalt.lua")
- if policy == "always" then
- return installBasalt()
- elseif policy == "never" then
- if exists then print("Basalt present (policy=never), skipping reinstall"); return true
- else print("Basalt missing (policy=never), installing once"); return installBasalt() end
- else
- local doInstall = promptYesNo("Reinstall Basalt now?", true)
- if doInstall then return installBasalt() end
- print("Skipping Basalt reinstall (asked)")
- if not exists then print("Basalt not present; installing required dependency anyway"); return installBasalt() end
- return true
- end
- end
- -- ===== write the Basalt GUI installer (v2-friendly) =====
- local function writeGUIInstaller()
- local code = [=[
- -- install_network.lua
- -- Basalt GUI installer (safe reboot gate already done by stage-1)
- local basalt = require("basalt")
- -- ===== Config I/O =====
- local CONFIG_PATH = "netinst.cfg"
- local function loadConfig()
- local fn = loadfile(CONFIG_PATH, nil, {})
- if not fn then return {} end
- local ok, cfg = pcall(fn)
- if not ok or type(cfg) ~= "table" then return {} end
- cfg.retries = math.max(1, math.min(3, tonumber(cfg.retries or 2) or 2))
- if type(cfg.blacklist) ~= "table" then cfg.blacklist = {} end
- if cfg.reinstall_basalt ~= "always" and cfg.reinstall_basalt ~= "never" and cfg.reinstall_basalt ~= "ask" then
- cfg.reinstall_basalt = "always"
- end
- cfg.install_mode = (cfg.install_mode == "clean" or cfg.install_mode == "update") and cfg.install_mode or "ask"
- if cfg.hasInstalled ~= true then cfg.hasInstalled = false end
- if cfg.start_background_processes == nil then cfg.start_background_processes = true end
- if cfg.kill_processes == nil then cfg.kill_processes = true end
- return cfg
- end
- local function saveConfig(cfg)
- local function q(s) return string.format("%q", s) end
- local t = {}
- table.insert(t, "return {")
- table.insert(t, " blacklist = {")
- for _,p in ipairs(cfg.blacklist or {}) do table.insert(t, " " .. q(p) .. ",") end
- table.insert(t, " },")
- table.insert(t, " log_file = " .. (cfg.log_file and q(cfg.log_file) or "nil") .. ",")
- table.insert(t, " retries = " .. tostring(math.max(1, math.min(3, tonumber(cfg.retries or 2) or 2))) .. ",")
- table.insert(t, " install_mode = " .. q(cfg.install_mode or "ask") .. ",")
- table.insert(t, " reinstall_basalt = " .. q(cfg.reinstall_basalt or "always") .. ",")
- table.insert(t, " hasInstalled = " .. tostring(cfg.hasInstalled and true or false) .. ",")
- table.insert(t, " start_background_processes = " .. tostring(cfg.start_background_processes ~= false) .. ",")
- table.insert(t, " kill_processes = " .. tostring(cfg.kill_processes ~= false) .. ",")
- table.insert(t, "}")
- local d = fs.getDir(CONFIG_PATH)
- if d ~= "" and not fs.exists(d) then fs.makeDir(d) end
- local f = fs.open(CONFIG_PATH, "w"); f.write(table.concat(t, "\n")); f.close()
- end
- local CFG = loadConfig()
- -- ===== repo & files =====
- local GITHUB_REPO = "https://raw.githubusercontent.com/Temperdox/CC_Networking/master/src"
- local FILE_MAP = {
- ["startup.lua"]="startup.lua",
- ["router_startup.lua"]="router_startup.lua",
- ["wifi_client.lua"]="wifi_client.lua",
- ["dhcp_client.lua"]="dhcp_client.lua",
- ["firewall_manager.lua"]="firewall_manager.lua",
- ["router_monitor.lua"]="router_monitor.lua",
- ["network_demo.lua"]="network_demo.lua",
- ["user_startup.lua"]="user_startup.lua",
- ["test_server.lua"]="test_server.lua",
- ["test_network.lua"]="test_network.lua",
- ["tests/test_websocket.lua"]="tests/test_websocket.lua",
- ["tests/test_http.lua"]="tests/test_http.lua",
- ["tests/test_tcp.lua"]="tests/test_tcp.lua",
- ["tests/test_udp.lua"]="tests/test_udp.lua",
- ["tests/test_https.lua"]="tests/test_https.lua",
- ["tests/test_webrtc.lua"]="tests/test_webrtc.lua",
- ["tests/test_mqtt.lua"]="tests/test_mqtt.lua",
- ["tests/test_ftp.lua"]="tests/test_ftp.lua",
- ["tests/test_ssh.lua"]="tests/test_ssh.lua",
- ["tests/test_client.lua"]="tests/test_client.lua",
- ["tests/servers/http_test_server.lua"]="tests/servers/http_test_server.lua",
- ["tests/servers/udp_server.lua"]="tests/servers/udp_server.lua",
- ["tests/servers/websocket_server.lua"]="tests/servers/websocket_server.lua",
- ["tests/servers/launcher.lua"]="tests/servers/launcher.lua",
- ["hardware_watchdog.lua"]="hardware_watchdog.lua",
- ["cleanup.lua"]="cleanup.lua",
- ["bin/netd.lua"]="bin/netd.lua",
- ["bin/routerd.lua"]="bin/routerd.lua",
- ["lib/network.lua"]="lib/network.lua",
- ["etc/network.cfg"]="config/network.cfg",
- ["etc/router.cfg"]="config/router.cfg",
- ["etc/wireless.cfg"]="config/wireless.cfg",
- ["etc/wifi.cfg"]="config/wifi.cfg",
- ["config/network.cfg"]="config/network.cfg",
- ["protocols/protocol_names.lua"]="protocols/protocol_names.lua",
- ["protocols/statuses.lua"]="protocols/statuses.lua",
- ["protocols/network_adapter.lua"]="protocols/network_adapter.lua",
- ["protocols/websocket.lua"]="protocols/websocket.lua",
- ["protocols/http_client.lua"]="protocols/http_client.lua",
- ["protocols/tcp.lua"]="protocols/tcp.lua",
- ["protocols/udp.lua"]="protocols/udp.lua",
- ["protocols/mqtt.lua"]="protocols/mqtt.lua",
- ["protocols/ftp.lua"]="protocols/ftp.lua",
- ["protocols/ssh.lua"]="protocols/ssh.lua",
- ["protocols/webrtc.lua"]="protocols/webrtc.lua",
- ["util/logger.lua"]="util/logger.lua",
- ["util/connection_manager.lua"]="util/connection_manager.lua",
- ["usr/lib/router/install.lua"]="usr/lib/router/install.lua",
- ["usr/lib/router/routerd.lua"]="usr/lib/router/routerd.lua",
- ["usr/lib/router/web_admin.lua"]="usr/lib/router/web_admin.lua",
- ["usr/lib/router/qos.lua"]="usr/lib/router/qos.lua",
- }
- local DIRECTORIES = {
- "bin","lib","etc","config","protocols","util","tests",
- "usr","usr/lib","usr/lib/router",
- "var","var/run","var/lib","var/lib/dhcp","var/log","var/cache",
- "logs"
- }
- local function isBlacklisted(path)
- for _,p in ipairs(CFG.blacklist or {}) do
- if path == p or path:sub(1, #p + 1) == p .. "/" then return true end
- end
- return false
- end
- -- ===== logging =====
- local logFileHandle=nil
- local function logInit()
- if CFG.log_file then
- local d=fs.getDir(CFG.log_file); if d~="" and not fs.exists(d) then fs.makeDir(d) end
- logFileHandle=fs.open(CFG.log_file,"a")
- if logFileHandle then logFileHandle.write(("[%s] --- installer start ---\n"):format(os.date("%H:%M:%S"))); logFileHandle.flush() end
- end
- end
- local function logClose()
- if logFileHandle then logFileHandle.write(("[%s] --- installer end ---\n"):format(os.date("%H:%M:%S"))); logFileHandle.close() end
- end
- local function logWriteGUI(listWidget, msg, color)
- local entry = ("[%s] %s"):format(os.date("%H:%M:%S"), msg)
- if listWidget then
- listWidget:addItem(entry, color or colors.white, colors.black)
- if listWidget.scrollToBottom then listWidget:scrollToBottom() end
- end
- if logFileHandle then logFileHandle.write(entry.."\n"); logFileHandle.flush() end
- end
- local function termSize() local w,h=term.getSize(); return w,h end
- -- ===== state =====
- local state = { current_file="", progress=0, total_files=0, success_count=0, failed_files={}, skipped_blacklisted={} }
- -- ===== GUI =====
- local main, statusLabel, progressLabel, progressBar, logList
- local function removeElement(el)
- if not el then return end
- if el.remove then
- el:remove()
- return
- end
- local parent = (el.getParent and el:getParent()) or main
- if parent and parent.removeChild then
- parent:removeChild(el)
- elseif el.setVisible then
- el:setVisible(false)
- end
- end
- local function createGUI()
- local w,h=termSize()
- main=basalt.getMainFrame():setBackground(colors.blue):setSize(w,h)
- main:addLabel():setPosition(1,1):setSize(w,1):setText("ComputerCraft Network System Installer")
- :setForeground(colors.white):setBackground(colors.blue)
- local statusPanel=main:addFrame():setPosition(2,3):setSize(w-2,8):setBackground(colors.white)
- statusPanel:addLabel():setPosition(1,1):setText("Installation Status"):setForeground(colors.black)
- statusLabel=statusPanel:addLabel():setPosition(1,3):setText("Initializing..."):setForeground(colors.gray)
- progressLabel=statusPanel:addLabel():setPosition(1,5):setText("0 / 0 files"):setForeground(colors.gray)
- progressBar=statusPanel:addProgressBar():setPosition(1,6):setSize(w-2,1)
- :setBackground(colors.lightGray):setProgressColor(colors.lime):setProgress(0)
- local logPanel=main:addFrame():setPosition(2,12):setSize(w-2,8):setBackground(colors.black)
- logPanel:addLabel():setPosition(1,1):setText("Installation Log"):setForeground(colors.white)
- logList=logPanel:addList():setPosition(1,2):setSize(w-2,6):setBackground(colors.black):setForeground(colors.white)
- end
- local function addLog(msg, level)
- local color = colors.white
- if level == "ERROR" then color = colors.red
- elseif level == "SUCCESS" then color = colors.lime
- elseif level == "WARNING" then color = colors.yellow end
- logWriteGUI(logList, msg, color)
- end
- local function updateProgress()
- local pct=0; if state.total_files>0 then pct=math.floor((state.progress/state.total_files)*100) end
- progressLabel:setText(("%d / %d (%d%%)"):format(state.progress,state.total_files,pct))
- progressBar:setProgress(pct)
- if state.current_file~="" then statusLabel:setText("Installing: "..state.current_file) end
- end
- -- first-run policy
- local function firstRunPolicyDialog()
- local w,h=termSize()
- local dw=math.max(34, math.floor(w*3/4)); if dw>w-2 then dw=w-2 end
- local dh=12; local dx,dy=math.floor((w-dw)/2), math.floor(h/3)
- local dialog=main:addFrame():setPosition(dx,dy):setSize(dw,dh):setBackground(colors.cyan)
- dialog:addLabel():setPosition(2,2):setSize(dw-4,1):setText("Set Basalt reinstall policy:"):setForeground(colors.white)
- local choice=nil; local btnX,btnW,btnH=2, dw-4, 2; local y=4
- dialog:addButton():setPosition(btnX,y):setSize(btnW,btnH):setText("Always"):setBackground(colors.green):setForeground(colors.white)
- :onClick(function() choice="always" end); y=y+btnH
- dialog:addButton():setPosition(btnX,y):setSize(btnW,btnH):setText("Never"):setBackground(colors.red):setForeground(colors.white)
- :onClick(function() choice="never" end); y=y+btnH
- dialog:addButton():setPosition(btnX,y):setSize(btnW,btnH):setText("Ask"):setBackground(colors.gray):setForeground(colors.white)
- :onClick(function() choice="ask" end)
- while not choice do sleep(0.05) end
- removeElement(dialog)
- CFG.reinstall_basalt = choice
- CFG.hasInstalled = true
- saveConfig(CFG)
- addLog("Saved Basalt policy: "..choice, "SUCCESS")
- end
- local function computeTotals()
- local n=0; for path,_ in pairs(FILE_MAP) do if not isBlacklisted(path) then n=n+1 end end
- state.total_files = n
- end
- local function cleanInstall()
- addLog("Starting clean installation...", "INFO")
- local protected={"rom","ROM","install.lua","install_network.lua","basalt.lua","basalt","netinst.cfg"}
- for _,p in ipairs(CFG.blacklist or {}) do table.insert(protected, p) end
- local items = fs.list("/")
- local deleted=0
- for _, it in ipairs(items) do
- local prot=false; for _,p in ipairs(protected) do if it==p or it:sub(1,#p+1)==p.."/" then prot=true; break end end
- if not prot then fs.delete(it); deleted=deleted+1; addLog("Removed: "..it, "INFO") end
- end
- addLog("Clean install completed - removed "..deleted.." items", "SUCCESS")
- end
- local function createDirectories()
- addLog("Creating directory structure...", "INFO")
- for _,d in ipairs(DIRECTORIES) do
- if not isBlacklisted(d) and not fs.exists(d) then fs.makeDir(d) end
- end
- addLog("Directory structure created", "SUCCESS")
- end
- local function httpGetWithRetries(url, retries)
- local attempt=0
- while attempt < (retries or 2) do
- attempt = attempt + 1
- local r = http.get(url)
- if r then return r, attempt end
- sleep(0.2)
- end
- return nil, attempt
- end
- local function downloadFile(local_path, github_path)
- if isBlacklisted(local_path) then
- table.insert(state.skipped_blacklisted, local_path)
- addLog("Skipped (blacklisted): "..local_path, "WARNING")
- state.progress = state.progress + 1; updateProgress()
- return true
- end
- state.current_file = local_path; updateProgress()
- local url = GITHUB_REPO.."/"..github_path
- local resp, tries = httpGetWithRetries(url, CFG.retries or 2)
- if not resp then
- addLog(("Failed to download after %d attempt(s): %s"):format(tries, local_path), "ERROR")
- table.insert(state.failed_files, local_path)
- state.progress = state.progress + 1; updateProgress()
- return false
- end
- local content = resp.readAll(); local code = resp.getResponseCode(); resp.close()
- if code ~= 200 or not content then
- addLog(("HTTP %s on %s"):format(tostring(code), local_path), "ERROR")
- table.insert(state.failed_files, local_path)
- state.progress = state.progress + 1; updateProgress()
- return false
- end
- local dir = fs.getDir(local_path)
- if dir ~= "" and not fs.exists(dir) and not isBlacklisted(dir) then fs.makeDir(dir) end
- if local_path == "basalt.lua" and isBlacklisted("basalt.lua") then
- addLog("Skipped overwrite of basalt.lua (blacklisted)", "WARNING")
- else
- local f = fs.open(local_path, "w")
- if not f then
- addLog("Cannot create file: "..local_path, "ERROR")
- table.insert(state.failed_files, local_path)
- state.progress = state.progress + 1; updateProgress()
- return false
- end
- f.write(content); f.close()
- addLog("Downloaded: "..local_path, "SUCCESS")
- end
- state.success_count = state.success_count + 1
- state.progress = state.progress + 1; updateProgress()
- return true
- end
- local function installFiles()
- addLog("Starting file installation...", "INFO")
- computeTotals()
- state.progress, state.success_count = 0, 0; updateProgress()
- for lp, gp in pairs(FILE_MAP) do
- downloadFile(lp, gp)
- sleep(0) -- yield to UI
- end
- statusLabel:setText("Installation completed")
- state.progress = state.total_files; state.current_file = ""; updateProgress(); sleep(0)
- local failed = #state.failed_files
- if failed > 0 then
- addLog("Installation completed with "..failed.." failure(s)", "ERROR")
- else
- if #state.skipped_blacklisted > 0 then
- addLog("Skipped blacklisted: "..table.concat(state.skipped_blacklisted, ", "), "WARNING")
- end
- addLog("All files installed successfully!", "SUCCESS")
- end
- return failed == 0
- end
- local function showChoiceDialog()
- local w,h=termSize()
- local dw=math.max(34, math.floor(w*3/4)); if dw>w-2 then dw=w-2 end
- local dh=14; local dx,dy=math.floor((w-dw)/2), math.floor(h/3)
- local d=main:addFrame():setPosition(dx,dy):setSize(dw,dh):setBackground(colors.orange)
- d:addLabel():setPosition(2,2):setSize(dw-4,1):setText("Existing Installation Found"):setForeground(colors.white)
- d:addLabel():setPosition(2,4):setSize(dw-4,1):setText("Choose installation method:"):setForeground(colors.white)
- local choice=nil; local btnX,btnW,btnH=2, dw-4, 2; local y=7
- d:addButton():setPosition(btnX,y):setSize(btnW,btnH):setText("Clean Install"):setBackground(colors.red):setForeground(colors.white)
- :onClick(function() choice="clean" end); y=y+btnH
- d:addButton():setPosition(btnX,y):setSize(btnW,btnH):setText("Update Files"):setBackground(colors.green):setForeground(colors.white)
- :onClick(function() choice="update" end); y=y+btnH
- d:addButton():setPosition(btnX,y):setSize(btnW,btnH):setText("Cancel"):setBackground(colors.gray):setForeground(colors.white)
- :onClick(function() choice="cancel" end)
- while not choice do sleep(0.05) end
- removeElement(d)
- return choice
- end
- local function showEndDialog(success)
- local w,h=termSize()
- local dw=math.max(34, math.floor(w*3/4)); if dw>w-2 then dw=w-2 end
- local dh=8; local dx,dy=math.floor((w-dw)/2), math.floor(h/3)
- local d=main:addFrame():setPosition(dx,dy):setSize(dw,dh):setBackground(success and colors.green or colors.red)
- d:addLabel():setPosition(2,2):setSize(dw-4,1):setText(success and "Installation Successful!" or "Installation Failed!"):setForeground(colors.white)
- d:addLabel():setPosition(2,4):setSize(dw-4,1):setText(success and "Network system ready to use" or "Check log for error details"):setForeground(colors.white)
- local done=false
- d:addButton():setPosition(math.floor(dw/2)-4,6):setSize(8,1):setText("OK")
- :setBackground(colors.white):setForeground(colors.black):onClick(function() done=true end)
- while not done do sleep(0.05) end
- removeElement(d)
- end
- local function runInstaller()
- -- init UI + logs
- createGUI(); logInit()
- addLog("Network installer started","INFO")
- addLog("Computer ID: "..os.getComputerID(),"INFO")
- -- optional cooperative cleanup (usually quiet after reboot already)
- if CFG.kill_processes then
- addLog("Process cleanup enabled (cooperative)","INFO")
- end
- -- first GUI run: persist Basalt policy
- if CFG.hasInstalled ~= true then firstRunPolicyDialog() end
- local has_existing = fs.exists("bin") or fs.exists("lib") or fs.exists("protocols") or fs.exists("startup.lua")
- local mode = CFG.install_mode or "ask"
- if mode == "ask" and has_existing then
- mode = showChoiceDialog()
- if mode == "cancel" then addLog("Installation cancelled by user","WARNING"); logClose(); _G.__INSTALLER_RUNNING=false; return end
- end
- if mode == "clean" then cleanInstall() end
- createDirectories()
- local ok = installFiles()
- showEndDialog(ok)
- logClose()
- _G.__INSTALLER_RUNNING=false
- end
- local function uiLoop()
- _G.__INSTALLER_RUNNING=true
- while _G.__INSTALLER_RUNNING do
- local ev={os.pullEventRaw()}
- basalt.update(table.unpack(ev))
- end
- end
- parallel.waitForAny(uiLoop, runInstaller)
- -- Back to a clean terminal and hand off to startup.lua
- term.clear(); term.setCursorPos(1,1)
- if fs.exists("startup.lua") then shell.run("startup.lua") end
- ]=]
- local f = fs.open("install_network.lua", "w")
- if not f then error("Cannot create install_network.lua") end
- f.write(code)
- f.close()
- end
- -- ===== main (reboot gate then GUI) =====
- term.clear(); term.setCursorPos(1,1)
- printColored("CC Network System Installer - Stage 1", colors.cyan)
- printColored("=====================================", colors.cyan)
- print()
- local CFG = loadConfig()
- -- Reboot gate using start_background_processes
- if CFG.start_background_processes ~= false then
- -- First invocation: turn off background processes and reboot.
- CFG.start_background_processes = false
- saveConfig(CFG)
- print("Preparing system for installation...")
- print("Disabling background processes and rebooting safely.")
- sleep(1)
- os.reboot()
- return
- else
- -- Post-reboot: turn flag back on for future normal boots
- CFG.start_background_processes = true
- saveConfig(CFG)
- end
- -- HTTP API available?
- if not http then
- printColored("\nERROR: HTTP API not available", colors.red)
- printColored("Enable HTTP API in ComputerCraft config", colors.red)
- return
- end
- -- Quick connectivity test
- print("\nTesting internet connection...")
- if not checkInternet(3) then
- printColored("ERROR: No internet connection", colors.red)
- printColored("Please check your connection and try again", colors.yellow)
- return
- end
- printColored("Internet: OK", colors.green)
- print()
- -- Basalt dependency
- print("Handling Basalt installation...")
- if not handleBasaltPolicy(CFG) then
- printColored("\nERROR: Basalt is required for the GUI installer", colors.red)
- printColored("Please fix the issue and try again", colors.yellow)
- return
- end
- -- Generate GUI installer and run it
- local okWrite, errWrite = pcall(writeGUIInstaller)
- if not okWrite then
- printColored("\nERROR: Failed to write installer", colors.red)
- printColored("Error: "..tostring(errWrite), colors.red)
- return
- end
- printColored("\nNetwork installer created successfully", colors.green)
- print("Starting GUI installer in 2 seconds...")
- sleep(2)
- local okRun, errRun = pcall(function() shell.run("install_network.lua") end)
- if not okRun then
- printColored("\nERROR: GUI installer failed to run", colors.red)
- printColored("Error: "..tostring(errRun), colors.red)
- end
- print("\nInstaller completed.")
Advertisement