Advertisement
Unbox101

updater

Apr 28th, 2022 (edited)
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.76 KB | None | 0 0
  1. --pastebin get gxCvryX5 updater
  2.  
  3. --[[ USAGE:
  4. Run "pastebin get gxCvryX5 updater"
  5. Use commands:
  6. updater add program_name program_pastebin_id
  7. updater install program_name
  8. updater remove program_name
  9. updater updateall
  10. ]]
  11.  
  12. local arg1, arg2, arg3 = ...
  13. function saveTable(filePathIn, tableIn)
  14.     local file = fs.open(filePathIn,"w")
  15.     file.write(textutils.serialize(tableIn))
  16.     file.close()
  17. end
  18. function loadTable(filePathIn)
  19.     local file = fs.open(filePathIn,"r")
  20.     local data = file.readAll()
  21.     file.close()
  22.     return textutils.unserialize(data)
  23. end
  24.  
  25.  
  26.  
  27. local sPath = "UpdaterData.save"
  28. local programs = {
  29.     updater = "gxCvryX5"
  30. }
  31.  
  32. if not fs.exists(sPath) then
  33.     saveTable(sPath, programs)
  34. end
  35. programs = loadTable(sPath)
  36.  
  37. local function install(fileName)
  38.     if not programs[fileName] then
  39.         print("no file with name ", fileName)
  40.         return
  41.     end
  42.     if fs.exists(fileName) then
  43.         fs.delete(fileName)
  44.     end
  45.     shell.run("pastebin", "get", programs[fileName], fileName)
  46. end
  47.  
  48. local function updateAll()
  49.     for i,v in pairs(programs) do
  50.         if fs.exists(i) then
  51.             print("Updating program \""..tostring(i).."\" "..tostring(v))
  52.             install(i)
  53.             print()
  54.         else
  55.             print("Installing program \""..tostring(i).."\" "..tostring(v))
  56.             install(i)
  57.             print()
  58.         end
  59.     end
  60. end
  61. print()
  62.  
  63. if arg1 == "install" then
  64.     install(arg2)
  65. elseif arg1 == "list" then
  66.     for i,v in pairs(programs) do
  67.        print("\""..tostring(i).."\"", v)
  68.     end
  69. elseif arg1 == "add" then
  70.     local input1, input2
  71.     if not arg2 then
  72.         print("Please type the name of the program.")
  73.         input1 = read()
  74.     else
  75.         input1 = tostring(arg2)
  76.     end
  77.     if not arg3 then
  78.         print("Please type the pastebin id of the program.")
  79.         input2 = read()
  80.     else
  81.         input2 = tostring(arg3)
  82.     end
  83.    
  84.    
  85.     if input1 and input2 then
  86.         print("Adding new program \""..input1.."\" "..input2.. " to installer.")
  87.         programs[input1] = input2
  88.     else
  89.         print("Failed to add new program \""..tostring(input1).."\" "..tostring(input2))
  90.     end
  91. elseif arg1 == "remove" then
  92.     print("Please type the name of the program.")
  93.     input1 = read()
  94.     if input1 then
  95.         if (programs[input1]) then
  96.             print("Removing \""..input1.."\" from the installer and uninstalling it.")
  97.             if fs.exists(input1) then
  98.                 fs.delete(input1)
  99.             end
  100.             programs[input1] = nil
  101.         else
  102.             print("No program named \""..input1.."\" installed in the updater.")
  103.         end
  104.     else
  105.         print("Failed. Input must not be nil")
  106.     end
  107. else
  108.  
  109.     print("Updating Everything...")
  110.     updateAll()
  111. end
  112. saveTable(sPath, programs)
  113. print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement