Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---
- --- Generated by EmmyLua(https://github.com/EmmyLua)
- --- Created by cdbab.
- --- DateTime: 11/26/2024 5:12 AM
- ---
- -- Update Script for Controller
- local PROTOCOL = "mobFarm"
- local LOCAL_ID = "controller"
- local pastebinID = "RkmNxEg1" -- Controller Pastebin ID
- local pastebinURL = "https://pastebin.com/raw/" .. pastebinID
- local startupScriptName = "startup.lua"
- local localVersionFile = "controller_version.txt"
- local maxRetries = 5
- local retryDelay = 2 -- seconds
- local version = "1.0.3" -- Version of the update.lua
- -- Peripheral Setup
- peripheral.find("modem", rednet.open)
- if not rednet.isOpen() then
- error("Rednet is not open! Check the modem.")
- end
- -- Function to check if the Pastebin file exists
- local function checkPastebinFileExists()
- print("Checking if Pastebin file exists...")
- local response = http.get(pastebinURL)
- if response then
- response.close()
- print("Pastebin file exists.")
- return true
- else
- print("Pastebin file does not exist or cannot be accessed.")
- return false
- end
- end
- -- Function to read the local version
- local function readLocalVersion()
- if fs.exists(localVersionFile) then
- local file = fs.open(localVersionFile, "r")
- local version = file.readLine()
- file.close()
- return version
- else
- return nil -- No local version exists
- end
- end
- -- Function to write the local version
- local function writeLocalVersion(version)
- local file = fs.open(localVersionFile, "w")
- file.writeLine(version)
- file.close()
- end
- -- Function to fetch the remote version from Pastebin
- local function fetchRemoteVersion()
- print("Fetching remote version from Pastebin...")
- local response = http.get(pastebinURL)
- if response then
- local content = response.readAll()
- response.close()
- -- Extract the version from the script content
- local remoteVersion = content:match('local version%s*=%s*"(.-)"')
- if remoteVersion then
- print("Remote version found: " .. remoteVersion)
- return remoteVersion
- else
- print("Failed to extract version from remote script.")
- return nil
- end
- else
- print("Failed to fetch remote script. Check network or Pastebin ID.")
- return nil
- end
- end
- -- Function to compare versions
- local function isUpdateRequired(localVersion, remoteVersion)
- if not localVersion then
- print("No local version found. Update required.")
- return true
- end
- if localVersion ~= remoteVersion then
- print("Version mismatch: Local (" .. localVersion .. ") vs Remote (" .. remoteVersion .. "). Update required.")
- return true
- else
- print("Local version (" .. localVersion .. ") is up-to-date.")
- return false
- end
- end
- -- Function to update the controller
- local function updateController()
- print("Starting controller update process...")
- -- Check if the Pastebin file exists
- if not checkPastebinFileExists() then
- print("Pastebin file does not exist. Aborting update.")
- return
- end
- -- Read local version
- local localVersion = readLocalVersion()
- -- Fetch remote version
- local remoteVersion = fetchRemoteVersion()
- -- Check if an update is required
- if remoteVersion and isUpdateRequired(localVersion, remoteVersion) then
- -- Attempt to fetch the startup.lua script
- for attempt = 1, maxRetries do
- print("Attempt " .. attempt .. " to download " .. startupScriptName .. "...")
- -- Remove any existing, potentially corrupt startup.lua file
- if fs.exists(startupScriptName) then
- fs.delete(startupScriptName)
- end
- -- Attempt to download the script
- local success = shell.run("pastebin get " .. pastebinID .. " " .. startupScriptName)
- -- Check if the file was successfully downloaded
- if success and fs.exists(startupScriptName) then
- print("Successfully downloaded " .. startupScriptName .. " on attempt " .. attempt)
- writeLocalVersion(remoteVersion) -- Update the local version
- print("Controller updated. Restarting...")
- os.reboot() -- Reboot the controller to apply changes
- return
- else
- print("Failed to download " .. startupScriptName .. ". Retrying in " .. retryDelay .. " seconds...")
- sleep(retryDelay)
- end
- end
- -- If all attempts fail
- print("Update failed after " .. maxRetries .. " attempts. Please check the network or Pastebin ID.")
- else
- print("No update required. Exiting.")
- end
- end
- -- Function to update all nodes
- local function updateNodes()
- print("Updating all nodes...")
- local nodes = {rednet.lookup(PROTOCOL)} -- Find all nodes
- if #nodes == 0 then
- print("No nodes found to update.")
- return
- end
- for _, nodeID in ipairs(nodes) do
- print("Sending update command to node ID:", nodeID)
- local updatePacket = {
- type = "update",
- command = "updateScript",
- }
- rednet.send(nodeID, updatePacket, PROTOCOL)
- print("Update command sent to node ID:", nodeID)
- os.sleep(1)
- end
- print("All nodes have been notified to update.")
- end
- -- Menu
- local function showMenu()
- print("Update Script Menu")
- print("1. Update Nodes")
- print("2. Update Controller")
- write("Enter your choice: ")
- local choice = read()
- if choice == "1" then
- updateNodes()
- elseif choice == "2" then
- updateController()
- else
- print("Invalid choice. Exiting...")
- end
- end
- -- Run the menu
- showMenu()
Advertisement
Comments
-
- This is the update script used for the mob farm controller
Add Comment
Please, Sign In to add comment