Advertisement
HandieAndy

installer.lua

Sep 13th, 2023 (edited)
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.46 KB | None | 0 0
  1. --[[
  2. A general-purpose installation script that can be run on any cc-rail-router
  3. hardware to set it up for its purpose.
  4. ]]--
  5.  
  6. local function clearScreen()
  7.     if term.isColor() then
  8.         term.setTextColor(colors.white)
  9.         term.setBackgroundColor(colors.black)
  10.     end
  11.     term.clear()
  12.     term.setCursorPos(1, 1)
  13. end
  14.  
  15. local function promptAnyKey()
  16.     print("Press any key to continue.")
  17.     os.pullEvent("key")
  18. end
  19.  
  20. local function readString(prompt, allowEmpty)
  21.     if allowEmpty == nil then allowEmpty = true end
  22.     local txt = nil
  23.     repeat
  24.         clearScreen()
  25.         print(prompt)
  26.         local x, y = term.getCursorPos()
  27.         term.setCursorPos(1, y + 2)
  28.         term.setCursorBlink(true)
  29.         txt = io.read()
  30.         term.setCursorBlink(false)
  31.         if not allowEmpty and (not txt or #txt == 0) then
  32.             term.setCursorPos(1, y + 4)
  33.             print("Empty input not allowed.")
  34.             os.sleep(2)
  35.         end
  36.     until (txt ~= nil and #txt > 0) or allowEmpty
  37.     return txt
  38. end
  39.  
  40. local function readNumber(prompt, minVal, maxVal, defaultVal)
  41.     local num = nil
  42.     while num == nil or num < minVal or num > maxVal do
  43.         local txt = readString(prompt, defaultVal ~= nil)
  44.         if not txt or #txt == 0 then return defaultVal end
  45.         num = tonumber(txt)
  46.         if num == nil or num < minVal or num > maxVal then
  47.             print("Invalid number input. Should be between "..minVal.." and "..maxVal..".")
  48.             os.sleep(2)
  49.         end
  50.     end
  51.     return num
  52. end
  53.  
  54. local function readChoice(prompt, choices, defaultChoice)
  55.     while true do
  56.         clearScreen()
  57.         print(prompt)
  58.         for i, choice in pairs(choices) do
  59.             print(i..". "..choice)
  60.         end
  61.         local x, y = term.getCursorPos()
  62.         term.setCursorPos(1, y + 2)
  63.         local txt = io.read()
  64.         if (not txt or #txt == 0) and defaultChoice ~= nil then
  65.             return defaultChoice
  66.         end
  67.         local txtNum = tonumber(txt)
  68.         if txtNum then
  69.             if txtNum < 1 or txtNum > #choices then
  70.                 print("Invalid numeric choice. Should be between 1 and "..#choices..".")
  71.                 os.sleep(2)
  72.             else
  73.                 return choices[txtNum]
  74.             end
  75.         else
  76.             for i, choice in pairs(choices) do
  77.                 if choice == txt then
  78.                     return choice
  79.                 end
  80.             end
  81.             print("Invalid choice. Please choose one of the options.")
  82.             os.sleep(2)
  83.         end
  84.     end
  85. end
  86.  
  87. local function waitForPeripheralAttach(side)
  88.     while true do
  89.         local event, s = os.pullEvent("peripheral")
  90.         if side == nil or s == side then return s end
  91.     end
  92. end
  93.  
  94. local function waitForPeripheralDetach(side)
  95.     while true do
  96.         local event, s = os.pullEvent("peripheral_detach")
  97.         if side == nil or s == side then return s end
  98.     end
  99. end
  100.  
  101. local function waitForModemPresent(side, wireless)
  102.     while true do
  103.         local modem = peripheral.wrap(side)
  104.         if modem == nil then
  105.             print("Please attach modem to side "..side..".")
  106.             promptAnyKey()
  107.         elseif not modem.isWireless or modem.isWireless() ~= wireless then
  108.             local name = "modem"
  109.             if wireless then name = "wireless " .. name end
  110.             print("The peripheral on side "..side.." is not a compatible "..name..". Please detach this peripheral and add the correct one now.")
  111.             promptAnyKey()
  112.         end
  113.     end
  114. end
  115.  
  116. local function saveTable(filename, table)
  117.     local f = io.open(filename, "w")
  118.     f:write(textutils.serialize(table))
  119.     f:close()
  120. end
  121.  
  122. local function createStartupScript(program)
  123.     local sf = io.open("startup.lua", "w")
  124.     sf:write("shell.execute(\""..program.."\")\n")
  125.     sf:close()
  126. end
  127.  
  128. local function installStation()
  129.     clearScreen()
  130.     print("Installing station beacon software.")
  131.     os.sleep(1)
  132.     waitForModemPresent("top", true)
  133.     local config = {}
  134.     config.name = readString("Enter the station's codename.", false)
  135.     config.displayName = readString("Enter the station's display name.", false)
  136.     config.range = readNumber("Enter the broadcast range for this station, in blocks.", 4, 64)
  137.     clearScreen()
  138.     saveTable("station_config.tbl", config)
  139.     print("Saved station configuration to station_config.tbl.")
  140.     if fs.exists("station.lua") then
  141.         print("Deleting existing station.lua.")
  142.         fs.delete("station.lua")
  143.     end
  144.     shell.execute("wget", "https://github.com/andrewlalis/cc-rail-router/raw/main/station.lua", "station.lua")
  145.     print("Downloaded station.lua.")
  146.     createStartupScript("station.lua")
  147. end
  148.  
  149. local function installSwitch()
  150.     clearScreen()
  151.     print("Installing switch controller software.")
  152.     os.sleep(1)
  153.     waitForModemPresent("top", true)
  154.  
  155. end
  156.  
  157. local function installRouter()
  158.     clearScreen()
  159.     print("Installing handheld router software.")
  160.     os.sleep(1)
  161.     waitForModemPresent("back", true)
  162.  
  163. end
  164.  
  165. clearScreen()
  166. print("Rail Software Installation Wizard")
  167. os.sleep(2)
  168. local choice = readChoice(
  169.     "What type of software would you like to install?",
  170.     {"Station Beacon", "Switch Controller", "Handheld Router", "Exit"}
  171. )
  172. if choice == "Station Beacon" then
  173.     installStation()
  174. elseif choice == "Switch Controller" then
  175.     installSwitch()
  176. elseif choice == "Handheld Router" then
  177.     installRouter()
  178. else
  179.     print("Exiting.")
  180. end
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement