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
- ---
- -- Node Update Script
- local pastebinID = "yCChhW7d" -- Pastebin ID for node startup.lua
- local pastebinURL = "https://pastebin.com/raw/" .. pastebinID
- local startupScriptName = "startup.lua"
- local localVersionFile = "version.txt" -- File to store the local version
- local maxRetries = 5
- local retryDelay = 4 -- seconds
- local version = "1.0.4" -- Version of the update.lua
- -- 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 attempt downloading the startup.lua script
- local function fetchStartupScript()
- 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)
- return true
- else
- print("Failed to download " .. startupScriptName .. ". Retrying in " .. retryDelay .. " seconds...")
- sleep(retryDelay)
- end
- end
- -- If all attempts fail, return false
- return false
- end
- -- Main update logic
- print("Starting 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
- if fetchStartupScript() then
- -- Update local version
- writeLocalVersion(remoteVersion)
- print("Update successful. Rebooting...")
- os.reboot() -- Reboot the node to apply changes
- else
- print("Update failed after " .. maxRetries .. " attempts. Please check the network or Pastebin ID.")
- end
- else
- print("No update required. Exiting.")
- os.reboot()
- end
Advertisement
Comments
-
- This is the update script used for the mob farm nodes
Add Comment
Please, Sign In to add comment