Advertisement
Marlingaming

Edited Version of Updater

Dec 2nd, 2021 (edited)
1,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- This is eniallator's updater program.
  2. -- The way it works is it will compare a version variable in 2 files; 1 pastebin link and 1 local file.
  3. -- NOTE: it does not compare the contents of the file, it only compares a variable.
  4. -- ================================================================================
  5. --
  6. -- To make a version variable in your file copy and paste the following into the file:
  7. --
  8. -- version = "*insert version here*"
  9. --
  10. -- This has to be in the local file aswell as the pastebin file to download.
  11. -- ================================================================================
  12. --
  13. -- To add your file to be updated when running this program add the following to the filesToUpdate table:
  14. --
  15. -- *insert file name* = "*insert pastebin code after 'http://pastebin.com/'*"
  16. --
  17. -- The file name must correspond to your local file on your computercraft computer and the pastebin link has to correspond to your file in pastebin.
  18.  
  19. local filesToUpdate = {Terminal = "DMKj3Jh2"}
  20. -- Table for files/pastebin codes
  21.  
  22. function getFile(fileName, link)
  23.  
  24.   local file = http.get("http://pastebin.com/raw.php?i=" .. textutils.urlEncode(link))
  25.  
  26.   if file then
  27.    
  28.      local out = file.readAll()
  29.      file.close()
  30.      return out
  31.      -- Returning contents of the pastebin file
  32.   else
  33.    
  34.      return false
  35.      -- Returning false if the link is invalid
  36.   end
  37. end
  38. -- Function for downloading the files from pastebin, needs HTTP to run
  39.  
  40. function getVersion(fileContents)
  41.  
  42.   if fileContents then
  43.     local _, numberChars = fileContents:lower():find('version = "')
  44.     local fileVersion = ""
  45.     local char = ""
  46.     -- Declaring variables aswell as finding where in the fileContents argument is 'version = "'
  47.  
  48.     if numberChars then
  49.       while char ~= '"' do
  50.      
  51.         numberChars = numberChars + 1
  52.         char = fileContents:sub(numberChars,numberChars)
  53.         fileVersion = fileVersion .. char
  54.       end
  55.       -- Making the version variable by putting every character from 'version = "' to '"'
  56.    
  57.       fileVersion = fileVersion:sub(1,#fileVersion-1)
  58.       return fileVersion
  59.     else
  60.    
  61.       return false
  62.       -- If the function didn't find 'version = "' in the fileContents then it returns false
  63.     end
  64.   else
  65.    
  66.     return ""
  67.   end
  68. end
  69. -- Finding the version number
  70.  
  71. for file, url in pairs(filesToUpdate) do
  72.  
  73.   if not fs.isDir(file) then
  74.    
  75.     local pastebinContents = getFile(file,url)
  76.     -- Getting the pastebin file's contents
  77.    
  78.     if pastebinContents then
  79.       if fs.exists(file) then
  80.        
  81.         local localFile = fs.open(file,"r")
  82.         localContents = localFile.readAll()
  83.         localFile.close()
  84.         -- Getting the local file's contents
  85.       end
  86.      
  87.       local pastebinVersion = getVersion(pastebinContents)
  88.       local localVersion = getVersion(localContents)
  89.       -- Defining version variables for each of the file's contents
  90.      
  91.       if not pastebinVersion then
  92.        
  93.         print("[Error  ] the pastebin code for " .. file .. " does not have a version variable")
  94.         -- Tests if the pastebin code's contents has a version variable or not
  95.        
  96.       elseif not localVersion then
  97.        
  98.         print("[Error  ] " .. file .. " does not have a version variable")
  99.         -- Tests if the local file doesn't have the version variable
  100.        
  101.       elseif pastebinVersion == localVersion then
  102.        
  103.         print("[Success] " .. file .. " is already the latest version")
  104.         -- If the pastebin file's version is equal to the local file's version then it does nothing
  105.       else
  106.        
  107.         endFile = fs.open(file,"w")
  108.         endFile.write(pastebinContents)
  109.         endFile.close()
  110.        
  111.         print("[Success] " .. file .. " has been updated to version " .. pastebinVersion)
  112.         -- If the versions are not the same then it will write over the current local file to update it to the pastebin version
  113.       end
  114.     else
  115.      
  116.       print("[Error  ] " .. file .. " has an invalid link")
  117.      end
  118.   else
  119.    
  120.     print("[Error  ] " .. file .. " is a directory")
  121.   end
  122. end
  123. -- Error messages catching different errors
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement