Advertisement
npexception

CC file installer

Feb 27th, 2013
1,958
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- folder where to put the APIs
  2. local apiFolder = "npeAPIs"
  3.  
  4. -- list of files
  5. local fileInfos= {
  6.   -- APIs
  7.   {
  8.     name = "updater.lua",
  9.     pbcode = "HF7vwabd",
  10.     isApi = true,
  11.     startupParams = "--noupdatecheck",
  12.     needsTurtle = false
  13.   },
  14.   {
  15.     name = "betterturtle.lua",
  16.     pbcode = "6XL8EYXe",
  17.     isApi = true,
  18.     needsTurtle = true
  19.   },
  20.   -- programs
  21.   {
  22.     name = "quarry.lua",
  23.     pbcode = "HqXCPzCg",
  24.     needsTurtle = true
  25.   },
  26.   {
  27.     name = "quarry-minion.lua",
  28.     pbcode = "5pFNPRZv",
  29.     needsTurtle = true
  30.   },
  31.   {
  32.     name = "ignorelist-generator.lua",
  33.     pbcode = "fw5C7u8t",
  34.     needsTurtle = true
  35.   },
  36.   {
  37.     name = "oredict-sorter.lua",
  38.     pbcode = "94eCzump",
  39.     needsTurtle = true
  40.   },
  41.   {
  42.     name = "testcode.lua",
  43.     pbcode = "dP7Rm6p7"
  44.   },
  45.   -- files
  46.   {
  47.     name = "ignore.list",
  48.     pbcode = "ymBVBbt1"
  49.   },
  50.   {
  51.     name = "oredict.config",
  52.     pbcode = "iPPApNHk"
  53.   },
  54. }
  55.  
  56. local function status(text, color, delay)
  57.   term.clear()
  58.   term.setCursorPos(1,1)
  59.   if term.isColor() then
  60.     term.setTextColor(colors.yellow)
  61.   end
  62.   print(" NPE Installation Tool ")
  63.   print("-----------------------")
  64.   print()
  65.   if term.isColor() then
  66.     term.setTextColor(colors.white)
  67.   end
  68.   term.write("--> ")
  69.   if term.isColor() then
  70.     if color == nil then
  71.       color = colors.white
  72.     end
  73.     term.setTextColor(color)
  74.   end
  75.   print(text)
  76.   if term.isColor() then
  77.     term.setTextColor(colors.white)
  78.   end
  79.  
  80.   if delay then
  81.     sleep(delay)
  82.   end
  83. end
  84.  
  85.  
  86. local function getFileContent( path )
  87.   local result
  88.   if fs.exists(path) then
  89.     local file = fs.open(path, "r")
  90.     result = file.readAll()
  91.     file.close()
  92.   end
  93.   return result
  94. end
  95.  
  96.  
  97. local function key()
  98.   local event, c = os.pullEvent("char")
  99.   return c
  100. end
  101.  
  102.  
  103. -- main program part
  104. local startupLines = nil
  105. local wait = 0.5
  106.  
  107.  
  108. if not fs.exists(apiFolder) then
  109.   fs.makeDir(apiFolder)
  110. end
  111.  
  112. for _,fileInfo in ipairs(fileInfos) do
  113.   local filePath = fileInfo.isApi and fs.combine(apiFolder, fileInfo.name)
  114.                    or fileInfo.name
  115.  
  116.   if fileInfo.needsTurtle and (not turtle) then
  117.     status("Skipping \""..fileInfo.name.."\" (Turtle)", colors.orange, wait)
  118.   elseif fs.exists(filePath) then
  119.     -- ignore if the file already exists
  120.     status("Skip: \""..fileInfo.name.."\" already exists", colors.orange, wait)
  121.   else
  122.     local install = true
  123.     -- always install APIs
  124.     if not fileInfo.isApi then
  125.       local c = nil
  126.       repeat
  127.         status("Install "..fileInfo.name.."? -> y/n")
  128.         c = key()
  129.         install = c == "y"
  130.       until (c == "y" or c == "n")
  131.     end
  132.  
  133.     if install then
  134.       status("Installing file: \""..fileInfo.name.."\"", colors.lightBlue, wait)
  135.  
  136.       shell.run("pastebin", "get", fileInfo.pbcode, filePath)
  137.  
  138.       if fileInfo.isApi then
  139.         if (startupLines == nil) then
  140.           startupLines = { "--- LOAD APIs ---" }
  141.         end
  142.  
  143.         local startupline = "shell.run(\""..filePath
  144.         if fileInfo.startupParams then
  145.           startupline = startupline.."\", \""..fileInfo.startupParams
  146.         end
  147.         startupline = startupline.."\")"
  148.         startupLines[#startupLines+1] = startupline
  149.       end
  150.  
  151.       status("File installed!", colors.lime, wait)
  152.     end
  153.   end
  154. end
  155.  
  156.  
  157. -- merge the new startup lines in at the front of old startup file (or create a new one)
  158. if startupLines then
  159.   local oldStartup = getFileContent("startup")
  160.   if oldStartup then
  161.     status("Modifying startup file...", colors.lightBlue, wait)
  162.   else
  163.     status("Creating startup file...", colors.lightBlue, wait)
  164.   end
  165.  
  166.   local file = fs.open("startup", "w")
  167.   for i=1,#startupLines do
  168.     file.writeLine(startupLines[i])
  169.   end
  170.   if oldStartup then
  171.     file.writeLine()
  172.     file.writeLine("--- BEGIN OF STARTUP ---")
  173.     file.writeLine(oldStartup)
  174.   end
  175.   file.close()
  176. end
  177.  
  178. -- print success message
  179. status("Installation complete!", colors.lime, wait)
  180.  
  181. shell.run("reboot")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement