Advertisement
YK7942

AutoUpdate

Jun 16th, 2015
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.10 KB | None | 0 0
  1. --Auto Updater
  2. --pastebin.com/3K8mAXvf
  3.  
  4. --[[TODO LIST
  5. 01. TODO check file for corruption. Check size and type of values.
  6. 02. TODO set a limit for the number of programs that can be managed.
  7. 03. TODO fix excess characters after read()/event.keypress() functions.
  8. 04. TODO include optional monitor functionality.
  9. 05. TODO embed some clearer instructions.
  10. 06. TODO write some comments in this code :\ .
  11. 07. TODO transfer Menu instructions to a separate screen to make more room for the list
  12. ]]
  13.  
  14. function Main()
  15.     --check for a save file
  16.     if fs.exists("UpdateList.txt") == false then --If no program list exists then...
  17.         UpdateProgram("viF3LUGT", "UpdateList.txt") --... download it
  18.     end
  19.    
  20.     --wait for user input
  21.     if CountdownBoolPrompt("Do you wish to view or change the settings?",5) then --..ask if the user wants to change the settings
  22.         SaveCSV(Menu(LoadCSV("UpdateList.txt")), "UpdateList.txt") --load the old list, run the menu, save the new settings
  23.     end
  24.    
  25.     --Open and download the list of programs
  26.     programsList = LoadCSV("UpdateList.txt")
  27.    
  28.     --update programs
  29.     term.clear() term.setCursorPos(1,1)
  30.     for program = 1,#programsList do --for all the listed programs...
  31.         if programsList[program][3] == true then --if the program is marked for update...
  32.             UpdateProgram(programsList[program][1], programsList[program][2])
  33.         end
  34.     end
  35.    
  36.     --run boot programs
  37.     for program = 1,#programsList do --for all the listed programs...
  38.         if programsList[program][4] == true then --if the program is marked for run...
  39.             RunProgram(programsList[program][2])
  40.         end
  41.     end
  42. end
  43.  
  44. function Menu(menuTable) --allows user to edit settings. returns a table of programs and settings
  45.     --find current selection
  46.     local currentSelection = 1
  47.     --if #menuTable == 0 then --if there are no menu items..
  48.     --  menuTable[1] = GetNewPaste() --..get one
  49.     --end
  50.     local menuLength = #menuTable
  51.    
  52.     repeat
  53.         --print the menu items
  54.         term.clear() term.setCursorPos(1,1)
  55.         print("Arrow keys to navigate. 'H' for help. 'Enter' to exit.")
  56.         local firstItemCol, firstItemRow = term.getCursorPos()
  57.         for menuRow = 1, menuLength do
  58.             --term.setCursorPos(1, menuRow+firstItemRow-1)
  59.             local printString = "  " .. menuTable[menuRow][1] .. " " .. menuTable[menuRow][2]
  60.             if menuTable[menuRow][3] == true then
  61.                 printString = printString .. " (U)"
  62.             end
  63.             if menuTable[menuRow][4] == true then
  64.                 printString = printString .. " (B)"
  65.             end
  66.             print(printString)
  67.         end
  68.         --print the indicator arrow
  69.         term.setCursorPos(1, currentSelection+firstItemRow-1)
  70.         print(">")
  71.        
  72.         --Receive and process keyboard input
  73.         sleep(0.2)
  74.         local eventtype, key = os.pullEvent()  --get keyboard input
  75.         if key == keys.up then --up key
  76.             currentSelection = LoopVariable(currentSelection, menuLength, "-")
  77.         elseif key == keys.down then --down key
  78.             currentSelection = LoopVariable(currentSelection, menuLength, "+")
  79.         elseif key == keys.a then --add a new item. 'a' key
  80.             menuTable[menuLength+1] = GetNewPaste()
  81.             menuLength = menuLength + 1
  82.         elseif key == keys.r then --remove the currently selected item. 'r' key
  83.             for i = currentSelection,menuLength-1 do
  84.                 menuTable[i] = menuTable[i+1]
  85.             end
  86.             menuTable[menuLength] = nil
  87.             menuLength = menuLength - 1
  88.         elseif key == keys.u then --toggle update
  89.             menuTable[currentSelection][3] = not menuTable[currentSelection][3]
  90.         elseif key == keys.b then --toggle run on boot
  91.             menuTable[currentSelection][4] = not menuTable[currentSelection][4]
  92.         elseif key == keys.m then
  93.             local tempRow = menuTable[currentSelection]
  94.             for i = currentSelection,menuLength-1 do
  95.                 menuTable[i] = menuTable[i+1]
  96.             end
  97.             menuTable[menuLength] = tempRow
  98.         elseif key == keys.h then
  99.             HelpScreen()
  100.         end
  101.     until key == keys.enter
  102.    
  103.     --return result
  104.     term.clear()
  105.     print("Saving settings...") sleep(2)
  106.     return menuTable
  107. end
  108.  
  109. function SaveCSV(dataTable, fileName) --saves a table as a CSV
  110.     --this routine will create/overwrite a text document and save the link and program name
  111.     local saveFile = fs.open(fileName,"w")
  112.     --iterate through each row of the table
  113.     for tableRow = 1, #dataTable do
  114.         local line = ""
  115.         --iterate through each column of a row
  116.         for tableCol = 1, #dataTable[tableRow] do
  117.             line = line .. tostring(dataTable[tableRow][tableCol]) .. ","
  118.         end
  119.         saveFile.writeLine(line)
  120.     end
  121.     saveFile.close()
  122. end
  123.  
  124. function LoadCSV(fileName) --loads a CSV and returns it as a table
  125.     print("Loading data from existing file..")
  126.     local file = fs.open(fileName,"r") --open the file
  127.     local lineString = file.readLine() --read the first line from the file
  128.     local dataMatrix = {} --create a matrix to store the values in
  129.     local lineNumber = 0
  130.    
  131.     while lineString ~= null do --until the line is empty
  132.         lineNumber = lineNumber + 1
  133.         dataMatrix[lineNumber] = {} --declare a new row in the matrix
  134.         local startChar = 1 --initialize start position for the string scan
  135.         local endChar   = string.find(lineString,",",startChar)  --initialize end position for the scan
  136.         while endChar ~= nil do
  137.             dataMatrix[lineNumber][#dataMatrix[lineNumber]+1] = ToBoolean(string.sub(lineString,startChar,endChar-1))
  138.             startChar = endChar + 1
  139.             endChar = string.find(lineString,",",startChar) --look in the string for "," starting from X
  140.         end
  141.         lineString = file.readLine() --read the next line
  142.     end
  143.  
  144.     file:close()
  145.     return dataMatrix
  146. end
  147.  
  148. function UpdateProgram(pasteLink, programName) --updates a program from pastebin if possile
  149.     print("\n>Updating " .. programName) --display message
  150.     shell.run("rename", programName, "Old" .. programName) --rename old program
  151.     if shell.run("pastebin", "get", pasteLink, programName) then --attempt to download a new version
  152.         shell.run("delete", "Old" .. programName) --if success, delete the old program
  153.     else
  154.         shell.run("rename", "Old" .. programName, programName) --if fail, restore old version
  155.         print("Failed to update. Old version kept.") --print error message
  156.     end
  157.     sleep(1)
  158. end
  159.  
  160. function HelpScreen() --shows a guide for program use.
  161.     term.clear() term.setCursorPos(1,1)
  162.     print("HELP 1 of 2")
  163.     print("1. Navigate the menu unsing UP and DOWN keys.")
  164.     print("2. Use 'A' to add a program to the end of the list.")
  165.     print("3. Use 'R' to remove a program from the list.")
  166.     print("4. Toggle 'Update file' with 'U'. Programs tagged with (U) will be updated from Pastebin on boot.")
  167.     read()
  168.     term.clear() term.setCursorPos(1,1)
  169.     print("HELP 2 of 2")
  170.     print("5. Toggle 'Run on boot' with 'B'. Programs tagged with (B) will be run on boot.")
  171.     print("6. Use 'M' to move a program to the end of the list.")
  172.     print("Note: Updating and Running is done sequentially, top to bottom.")
  173.     print("Hit 'enter' to return to the menu.")
  174.     read()
  175. end
  176.  
  177. function RunProgram(programName) --runs a program in a shell if possible
  178.     print("\n>Running " .. programName) --display message
  179.     if not shell.run(programName) then --run program. if unsuccessful...
  180.         print("Could not run " .. programName)
  181.         sleep(1)
  182.     end
  183. end
  184.  
  185. function GetNewPaste() --gets a new paste. LINK, NAME, FALSE, FALSE
  186.     term.clear() term.setCursorPos(1,1)
  187.     local link, progName
  188.     --get paste link
  189.     repeat
  190.         print("- Add a new program to the list.")
  191.         print("Enter the paste ID of the program you wish to add.\nMust be 8 characters long.")
  192.         print("")
  193.         link = tostring(read())
  194.         term.clear() term.setCursorPos(1,1)
  195.         if string.len(link) ~= 8 then
  196.             print("Link is wrong length. " .. string.len(link))
  197.         end
  198.     until string.len(link) == 8
  199.     term.clear() term.setCursorPos(1,1)
  200.    
  201.     --get program name
  202.     repeat
  203.         print("- Add a new program to the list.")
  204.         print("Enter program name. Must be over 3 characters")
  205.         print("")
  206.         progName = tostring(read())
  207.         term.clear() term.setCursorPos(1,1)
  208.         if string.len(progName) <= 3 then
  209.             print("Name is wrong length. " .. string.len(progName))
  210.         end
  211.     until string.len(progName) > 3
  212.     term.clear() term.setCursorPos(1,1)
  213.    
  214.     --return the link, name, do not update, do not run on boot
  215.     shell.run("pastebin", "get", link, progName)
  216.     return {link, progName, false, false}
  217. end
  218.  
  219. function ToBoolean(text) --converts a string to it's boolean if 'true' or 'false'
  220.     if string.lower(text) == "true" then return true end
  221.     if string.lower(text) == "false" then return false end
  222.     return text
  223. end
  224.  
  225. function LoopVariable(i, maximum, direction) --loops variable from 1->maximum->1
  226.     if direction == "+" and i >= maximum    then return 1       end
  227.     if direction == "+" and i  < maximum    then return i+1     end
  228.     if direction == "-" and i <= 1          then return maximum end
  229.     if direction == "-" and i  > 1          then return i-1     end
  230. end
  231.  
  232. function CountdownBoolPrompt(text, promptDuration) --prints text and counts down waiting for Y/N
  233.     for timeLeft = promptDuration,0,-1 do --for the numer of seconds specified...
  234.         os.startTimer(1) --start a 1s timer
  235.         term.clear() term.setCursorPos(1,1)
  236.         print(text.."\n(Y/N) Program will continue in "..timeLeft.."s") --print a prompt
  237.         local event, result = os.pullEvent() --detect key press for processing below
  238.         if result == keys.enter or result == keys.y then return true
  239.         elseif result == keys.n then return false
  240.         end
  241.     end
  242.     return false
  243. end
  244.  
  245. term.clear() term.setCursorPos(1,1)
  246. Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement