April_The_Sergal

updateScript for controller

Nov 25th, 2024 (edited)
108
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.83 KB | Gaming | 0 0
  1. ---
  2. --- Generated by EmmyLua(https://github.com/EmmyLua)
  3. --- Created by cdbab.
  4. --- DateTime: 11/26/2024 5:12 AM
  5. ---
  6. -- Update Script for Controller
  7.  
  8. local PROTOCOL = "mobFarm"
  9. local LOCAL_ID = "controller"
  10. local pastebinID = "RkmNxEg1" -- Controller Pastebin ID
  11. local pastebinURL = "https://pastebin.com/raw/" .. pastebinID
  12. local startupScriptName = "startup.lua"
  13. local localVersionFile = "controller_version.txt"
  14. local maxRetries = 5
  15. local retryDelay = 2 -- seconds
  16. local version = "1.0.3" -- Version of the update.lua
  17.  
  18. -- Peripheral Setup
  19. peripheral.find("modem", rednet.open)
  20.  
  21. if not rednet.isOpen() then
  22.     error("Rednet is not open! Check the modem.")
  23. end
  24.  
  25. -- Function to check if the Pastebin file exists
  26. local function checkPastebinFileExists()
  27.     print("Checking if Pastebin file exists...")
  28.     local response = http.get(pastebinURL)
  29.     if response then
  30.         response.close()
  31.         print("Pastebin file exists.")
  32.         return true
  33.     else
  34.         print("Pastebin file does not exist or cannot be accessed.")
  35.         return false
  36.     end
  37. end
  38.  
  39. -- Function to read the local version
  40. local function readLocalVersion()
  41.     if fs.exists(localVersionFile) then
  42.         local file = fs.open(localVersionFile, "r")
  43.         local version = file.readLine()
  44.         file.close()
  45.         return version
  46.     else
  47.         return nil -- No local version exists
  48.     end
  49. end
  50.  
  51. -- Function to write the local version
  52. local function writeLocalVersion(version)
  53.     local file = fs.open(localVersionFile, "w")
  54.     file.writeLine(version)
  55.     file.close()
  56. end
  57.  
  58. -- Function to fetch the remote version from Pastebin
  59. local function fetchRemoteVersion()
  60.     print("Fetching remote version from Pastebin...")
  61.     local response = http.get(pastebinURL)
  62.     if response then
  63.         local content = response.readAll()
  64.         response.close()
  65.  
  66.         -- Extract the version from the script content
  67.         local remoteVersion = content:match('local version%s*=%s*"(.-)"')
  68.         if remoteVersion then
  69.             print("Remote version found: " .. remoteVersion)
  70.             return remoteVersion
  71.         else
  72.             print("Failed to extract version from remote script.")
  73.             return nil
  74.         end
  75.     else
  76.         print("Failed to fetch remote script. Check network or Pastebin ID.")
  77.         return nil
  78.     end
  79. end
  80.  
  81. -- Function to compare versions
  82. local function isUpdateRequired(localVersion, remoteVersion)
  83.     if not localVersion then
  84.         print("No local version found. Update required.")
  85.         return true
  86.     end
  87.  
  88.     if localVersion ~= remoteVersion then
  89.         print("Version mismatch: Local (" .. localVersion .. ") vs Remote (" .. remoteVersion .. "). Update required.")
  90.         return true
  91.     else
  92.         print("Local version (" .. localVersion .. ") is up-to-date.")
  93.         return false
  94.     end
  95. end
  96.  
  97. -- Function to update the controller
  98. local function updateController()
  99.     print("Starting controller update process...")
  100.  
  101.     -- Check if the Pastebin file exists
  102.     if not checkPastebinFileExists() then
  103.         print("Pastebin file does not exist. Aborting update.")
  104.         return
  105.     end
  106.  
  107.     -- Read local version
  108.     local localVersion = readLocalVersion()
  109.  
  110.     -- Fetch remote version
  111.     local remoteVersion = fetchRemoteVersion()
  112.  
  113.     -- Check if an update is required
  114.     if remoteVersion and isUpdateRequired(localVersion, remoteVersion) then
  115.         -- Attempt to fetch the startup.lua script
  116.         for attempt = 1, maxRetries do
  117.             print("Attempt " .. attempt .. " to download " .. startupScriptName .. "...")
  118.  
  119.             -- Remove any existing, potentially corrupt startup.lua file
  120.             if fs.exists(startupScriptName) then
  121.                 fs.delete(startupScriptName)
  122.             end
  123.  
  124.             -- Attempt to download the script
  125.             local success = shell.run("pastebin get " .. pastebinID .. " " .. startupScriptName)
  126.  
  127.             -- Check if the file was successfully downloaded
  128.             if success and fs.exists(startupScriptName) then
  129.                 print("Successfully downloaded " .. startupScriptName .. " on attempt " .. attempt)
  130.                 writeLocalVersion(remoteVersion) -- Update the local version
  131.                 print("Controller updated. Restarting...")
  132.                 os.reboot() -- Reboot the controller to apply changes
  133.                 return
  134.             else
  135.                 print("Failed to download " .. startupScriptName .. ". Retrying in " .. retryDelay .. " seconds...")
  136.                 sleep(retryDelay)
  137.             end
  138.         end
  139.  
  140.         -- If all attempts fail
  141.         print("Update failed after " .. maxRetries .. " attempts. Please check the network or Pastebin ID.")
  142.     else
  143.         print("No update required. Exiting.")
  144.     end
  145. end
  146.  
  147. -- Function to update all nodes
  148. local function updateNodes()
  149.     print("Updating all nodes...")
  150.     local nodes = {rednet.lookup(PROTOCOL)} -- Find all nodes
  151.  
  152.     if #nodes == 0 then
  153.         print("No nodes found to update.")
  154.         return
  155.     end
  156.  
  157.     for _, nodeID in ipairs(nodes) do
  158.         print("Sending update command to node ID:", nodeID)
  159.         local updatePacket = {
  160.             type = "update",
  161.             command = "updateScript",
  162.         }
  163.         rednet.send(nodeID, updatePacket, PROTOCOL)
  164.         print("Update command sent to node ID:", nodeID)
  165.         os.sleep(1)
  166.     end
  167.  
  168.     print("All nodes have been notified to update.")
  169. end
  170.  
  171. -- Menu
  172. local function showMenu()
  173.     print("Update Script Menu")
  174.     print("1. Update Nodes")
  175.     print("2. Update Controller")
  176.     write("Enter your choice: ")
  177.     local choice = read()
  178.  
  179.     if choice == "1" then
  180.         updateNodes()
  181.     elseif choice == "2" then
  182.         updateController()
  183.     else
  184.         print("Invalid choice. Exiting...")
  185.     end
  186. end
  187.  
  188. -- Run the menu
  189. showMenu()
Advertisement
Comments
Add Comment
Please, Sign In to add comment