Advertisement
RabaGhast

Updater 1.1

Oct 29th, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ----Variables----
  2. names = {"t", "Menu"}
  3. adresses = {"5yu5bhR3", "fwGLRkYK"}
  4. amount = 2
  5. nameFile = nil
  6. adressFile = nil
  7.  
  8. function showList()
  9.   if amount == 0 then
  10.     print("No programs installed")
  11.     return
  12.   end
  13.   print("-------------------------")
  14.   print("program list")
  15.   print("-------------------------")
  16.    for _,v in pairs(names) do
  17.      print(v.." - "..adresses[_])
  18.   end
  19. end
  20.  
  21. function add(name, adress)
  22.   print("Adding "..name.." to list at index "..amount+1)
  23.   names[amount+1] = name
  24.   adresses[amount+1] = adress
  25.   amount = amount+1
  26.   updateList()
  27. end
  28.  
  29. function clearList()
  30.   names = nil
  31.   adresses = nil
  32.   amount = 0
  33.   fs.delete("names")
  34.   fs.delete("adresses")
  35.   fs.delete("amount")
  36. end
  37. function check(list, entry)
  38.   for _,v in pairs(list) do
  39.     if v == entry then
  40.      -- print("found "..entry.."/"..v.." at ".._)
  41.      -- sleep(3)
  42.       return _
  43.     end
  44.   end
  45.   return -1
  46. end
  47.  
  48. function save(table,name)
  49.   local file = fs.open(name,"w")
  50.   file.write(textutils.serialize(table))
  51.   file.close()
  52. end
  53.  
  54. function load(name)
  55.   if not fs.exists(name) then
  56.     print("Error! cant find file!")
  57.     return
  58.   end
  59.   local file = fs.open(name,"r")
  60.   local data = file.readAll()
  61.   file.close()
  62.   return textutils.unserialize(data)
  63. end
  64.  
  65. function updateList()
  66.   save(names,"names")
  67.   save(adresses, "adresses")
  68.   save(amount, "amount")
  69. end
  70.  
  71. -----------Formating-----------------
  72.  
  73. function draw_text_term(x, y, text, text_color, bg_color)
  74.   term.setTextColor(text_color)
  75.   term.setBackgroundColor(bg_color)
  76.   term.setCursorPos(x,y)
  77.   write(text)
  78. end
  79.  
  80. function draw_line_term(x, y, length, color)
  81.     term.setBackgroundColor(color)
  82.     term.setCursorPos(x,y)
  83.     term.write(string.rep(" ", length))
  84. end
  85.  
  86. function progress_bar_term(x, y, length, minVal, maxVal, bar_color, bg_color)
  87.   local barSize = math.floor((minVal/maxVal) * length)
  88.  
  89.   draw_line_term(x, y, length, bg_color) --background bar
  90.   draw_line_term(x, y, barSize, bar_color)  --progress so far
  91. end
  92.  
  93. function menu_bars()
  94.  -- draw_line_term(1, 1, 55, colors.white)
  95.  -- draw_text_term(14, 1, "ComputerCraft Test Program", colors.black, colors.white)
  96.  
  97.  -- draw_line_term(1, 19, 55, colors.white)
  98.  -- draw_text_term(20, 19, "", colors.black, colors.black)
  99. end
  100.  
  101. --------------Installer---------------------
  102.  
  103. function install(program, pastebin)
  104.   term.clear()
  105.   menu_bars()
  106.  
  107.   draw_text_term(1, 4, "Installing: "..program.."", colors.white, colors.black)
  108.   term.setCursorPos(1,6)
  109.   term.setTextColor(colors.white)
  110.   sleep(0.5)
  111.  
  112.   --delete any old backups
  113.   if fs.exists(program.."_old") then
  114.     fs.delete(program.."_old")
  115.     end
  116.  
  117.   --remove old configs
  118.   if fs.exists("config.txt") then
  119.     fs.delete("config.txt")
  120.     end
  121.  
  122.   --backup current program
  123.   if fs.exists(program) then
  124.     fs.copy(program, program.."_old")
  125.     fs.delete(program)
  126.     end
  127.  
  128.   --remove program and fetch new copy
  129.   shell.run("pastebin get "..pastebin.." "..program)
  130.  
  131.   sleep(0.5)
  132.  
  133. end
  134.  
  135. ----------Main-------------
  136.  
  137. args = {...}
  138. print(args[1])
  139. print(args[2])
  140. print(args[3])
  141.  
  142. --initializing tables
  143. if (fs.exists("names") and fs.exists("adresses") and fs.exists("amount")) then
  144.   names = load("names")
  145.   adresses = load("adresses")
  146.   amount = load("amount")
  147. end
  148.  
  149. --sleep(1)
  150.  
  151. if amount > 0 then
  152.   nCheck = check(names, args[1])
  153.   --print("nCheck = "..nCheck)
  154. else
  155.   nCheck = -1
  156. end
  157.  
  158. if args[1] == "showList" then
  159.   showList()
  160.   return
  161. end
  162.  
  163. if args[1] == "init" then
  164.   for _,v in pairs(names) do
  165.     install(v, adresses[_])
  166.   end
  167.   updateList()
  168.   error()
  169. end
  170.  
  171. if args[1] == "clear" then
  172.   clearList()
  173.   error()
  174. end
  175.  
  176. if args[1] == "showItem" then
  177.   print(names[arg[2]].." - "..adresses[arg[2]])
  178.   error()
  179. end
  180.  
  181. if args[1] == "addAmount" then
  182.   amount = amount + args[2]
  183.   error()
  184. end
  185.  
  186. if args[1] == "amount" then
  187.   print("Amounts of programs: "..amount)
  188.   error()
  189. end
  190.  
  191. if (nCheck > -1 and args[2] == nil) then
  192.   install(args[1], adresses[nCheck])
  193.   error()
  194. end
  195.  
  196. if (nCheck > -1 and args[2] ~= adresses[nCheck]) then
  197.   adresses[nCheck] = args[2]
  198.   install(args[1], args[2])
  199.   updateList()
  200.   error()
  201. end
  202.  
  203. if (nCheck == -1 and args[2] == nil) then
  204.   term.setTextColor(colors.red)
  205.   print("Name is not in the list. Adding a new program requires pastebin adress")
  206.   term.setTextColor(colors.white)
  207.   error()
  208. end
  209.  
  210. if (nCheck == -1 and args[2]) then
  211.   add(args[1], args[2])
  212.   install(args[1], args[2])
  213.   error()
  214. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement