April_The_Sergal

updateScript for node

Nov 25th, 2024 (edited)
117
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.57 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. -- Node Update Script
  7.  
  8. local pastebinID = "yCChhW7d" -- Pastebin ID for node startup.lua
  9. local pastebinURL = "https://pastebin.com/raw/" .. pastebinID
  10. local startupScriptName = "startup.lua"
  11. local localVersionFile = "version.txt" -- File to store the local version
  12. local maxRetries = 5
  13. local retryDelay = 4 -- seconds
  14. local version = "1.0.4" -- Version of the update.lua
  15.  
  16. -- Function to check if the Pastebin file exists
  17. local function checkPastebinFileExists()
  18.     print("Checking if Pastebin file exists...")
  19.     local response = http.get(pastebinURL)
  20.     if response then
  21.         response.close()
  22.         print("Pastebin file exists.")
  23.         return true
  24.     else
  25.         print("Pastebin file does not exist or cannot be accessed.")
  26.         return false
  27.     end
  28. end
  29.  
  30. -- Function to read the local version
  31. local function readLocalVersion()
  32.     if fs.exists(localVersionFile) then
  33.         local file = fs.open(localVersionFile, "r")
  34.         local version = file.readLine()
  35.         file.close()
  36.         return version
  37.     else
  38.         return nil -- No local version exists
  39.     end
  40. end
  41.  
  42. -- Function to write the local version
  43. local function writeLocalVersion(version)
  44.     local file = fs.open(localVersionFile, "w")
  45.     file.writeLine(version)
  46.     file.close()
  47. end
  48.  
  49. -- Function to fetch the remote version from Pastebin
  50. local function fetchRemoteVersion()
  51.     print("Fetching remote version from Pastebin...")
  52.     local response = http.get(pastebinURL)
  53.     if response then
  54.         local content = response.readAll()
  55.         response.close()
  56.  
  57.         -- Extract the version from the script content
  58.         local remoteVersion = content:match('local version%s*=%s*"(.-)"')
  59.         if remoteVersion then
  60.             print("Remote version found: " .. remoteVersion)
  61.             return remoteVersion
  62.         else
  63.             print("Failed to extract version from remote script.")
  64.             return nil
  65.         end
  66.     else
  67.         print("Failed to fetch remote script. Check network or Pastebin ID.")
  68.         return nil
  69.     end
  70. end
  71.  
  72. -- Function to compare versions
  73. local function isUpdateRequired(localVersion, remoteVersion)
  74.     if not localVersion then
  75.         print("No local version found. Update required.")
  76.         return true
  77.     end
  78.  
  79.     if localVersion ~= remoteVersion then
  80.         print("Version mismatch: Local (" .. localVersion .. ") vs Remote (" .. remoteVersion .. "). Update required.")
  81.         return true
  82.     else
  83.         print("Local version (" .. localVersion .. ") is up-to-date.")
  84.         return false
  85.     end
  86. end
  87.  
  88. -- Function to attempt downloading the startup.lua script
  89. local function fetchStartupScript()
  90.     for attempt = 1, maxRetries do
  91.         print("Attempt " .. attempt .. " to download " .. startupScriptName .. "...")
  92.  
  93.         -- Remove any existing, potentially corrupt startup.lua file
  94.         if fs.exists(startupScriptName) then
  95.             fs.delete(startupScriptName)
  96.         end
  97.  
  98.         -- Attempt to download the script
  99.         local success = shell.run("pastebin get " .. pastebinID .. " " .. startupScriptName)
  100.  
  101.         -- Check if the file was successfully downloaded
  102.         if success and fs.exists(startupScriptName) then
  103.             print("Successfully downloaded " .. startupScriptName .. " on attempt " .. attempt)
  104.             return true
  105.         else
  106.             print("Failed to download " .. startupScriptName .. ". Retrying in " .. retryDelay .. " seconds...")
  107.             sleep(retryDelay)
  108.         end
  109.     end
  110.  
  111.     -- If all attempts fail, return false
  112.     return false
  113. end
  114.  
  115. -- Main update logic
  116. print("Starting update process...")
  117.  
  118. -- Check if the Pastebin file exists
  119. if not checkPastebinFileExists() then
  120.     print("Pastebin file does not exist. Aborting update.")
  121.     return
  122. end
  123.  
  124. -- Read local version
  125. local localVersion = readLocalVersion()
  126.  
  127. -- Fetch remote version
  128. local remoteVersion = fetchRemoteVersion()
  129.  
  130. -- Check if an update is required
  131. if remoteVersion and isUpdateRequired(localVersion, remoteVersion) then
  132.     -- Attempt to fetch the startup.lua script
  133.     if fetchStartupScript() then
  134.         -- Update local version
  135.         writeLocalVersion(remoteVersion)
  136.         print("Update successful. Rebooting...")
  137.         os.reboot() -- Reboot the node to apply changes
  138.     else
  139.         print("Update failed after " .. maxRetries .. " attempts. Please check the network or Pastebin ID.")
  140.     end
  141. else
  142.     print("No update required. Exiting.")
  143.     os.reboot()
  144. end
  145.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment