Advertisement
GravityScore

Firewolf Rom Installer

Dec 30th, 2012
1,072
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.10 KB | None | 0 0
  1.  
  2. --  
  3. --  Firewolf Rom Installer
  4. --  Made by GravityScore
  5. --  
  6.  
  7.  
  8. --  -------- Variables
  9.  
  10. -- Download URL
  11. local downloadURL = "https://raw.github.com/1lann/firewolf/master/entities/other.lua"
  12.  
  13. local function isAdvanced()
  14.     if term.isColor then return term.isColor()
  15.     else return false end
  16. end
  17.  
  18. -- Theme
  19. local theme = {}
  20. theme.background = colors.gray
  21. theme.top = colors.red
  22. theme.bottom = colors.orange
  23. theme.text = colors.white
  24. if not(isAdvanced()) then
  25.     theme.background = colors.black
  26.     theme.top = colors.black
  27.     theme.bottom = colors.black
  28.     theme.text = colors.white
  29. end
  30.  
  31. -- Term size
  32. local w, h = term.getSize()
  33.  
  34.  
  35. --  ------- Utility Functions
  36.  
  37. local function prompt(list, dir)
  38.     if isAdvanced() then
  39.         for _, v in pairs(list) do
  40.             if v.bg then term.setBackgroundColor(v.bg) end
  41.             if v.tc then term.setTextColor(v.tc) end
  42.             if v[2] == -1 then v[2] = math.ceil((w + 1)/2 - (v[1]:len() + 6)/2) end
  43.  
  44.             term.setCursorPos(v[2], v[3])
  45.             write("[- " .. v[1])
  46.             term.setCursorPos(v[2] + v[1]:len() + 3, v[3])
  47.             write(" -]")
  48.         end
  49.  
  50.         while true do
  51.             local e, but, x, y = os.pullEvent()
  52.             if e == "mouse_click" then
  53.                 for _, v in pairs(list) do
  54.                     if x >= v[2] and x <= v[2] + v[1]:len() + 5 and y == v[3] then
  55.                         return v[1]
  56.                     end
  57.                 end
  58.             end
  59.         end
  60.     else
  61.         for _, v in pairs(list) do
  62.             term.setBackgroundColor(colors.black)
  63.             term.setTextColor(colors.white)
  64.             if v[2] == -1 then v[2] = math.ceil((w + 1)/2 - (v[1]:len() + 4)/2) end
  65.  
  66.             term.setCursorPos(v[2], v[3])
  67.             write("  " .. v[1])
  68.             term.setCursorPos(v[2] + v[1]:len() + 2, v[3])
  69.             write("  ")
  70.         end
  71.  
  72.         local key1 = 200
  73.         local key2 = 208
  74.         if dir == "horizontal" then
  75.             key1 = 203
  76.             key2 = 205
  77.         end
  78.  
  79.         local curSel = 1
  80.         term.setCursorPos(list[curSel][2], list[curSel][3])
  81.         write("[")
  82.         term.setCursorPos(list[curSel][2] + list[curSel][1]:len() + 3, list[curSel][3])
  83.         write("]")
  84.  
  85.         while true do
  86.             local e, key = os.pullEvent()
  87.             term.setCursorPos(list[curSel][2], list[curSel][3])
  88.             write(" ")
  89.             term.setCursorPos(list[curSel][2] + list[curSel][1]:len() + 3, list[curSel][3])
  90.             write(" ")
  91.             if e == "key" and key == key1 and curSel > 1 then
  92.                 curSel = curSel - 1
  93.             elseif e == "key" and key == key2 and curSel < #list then
  94.                 curSel = curSel + 1
  95.             elseif e == "key" and key == 28 then
  96.                 return list[curSel][1]
  97.             end
  98.             term.setCursorPos(list[curSel][2], list[curSel][3])
  99.             write("[")
  100.             term.setCursorPos(list[curSel][2] + list[curSel][1]:len() + 3, list[curSel][3])
  101.             write("]")
  102.         end
  103.     end
  104. end
  105.  
  106. local function centerPrint(text)
  107.     local x, y = term.getCursorPos()
  108.     term.setCursorPos(math.ceil((w + 1)/2 - text:len()/2), y)
  109.     print(text)
  110. end
  111.  
  112. local function centerWrite(text)
  113.     local x, y = term.getCursorPos()
  114.     term.setCursorPos(math.ceil((w + 1)/2 - text:len()/2), y)
  115.     write(text)
  116. end
  117.  
  118. local function download(url, path)
  119.     for i = 1, 3 do
  120.         local response = http.get(url)
  121.         if response then
  122.             local data = response.readAll()
  123.             response.close()
  124.             if path then
  125.                 local f = io.open(path, "w")
  126.                 f:write(data)
  127.                 f:close()
  128.             end
  129.             return true
  130.         end
  131.     end
  132.  
  133.     return false
  134. end
  135.  
  136.  
  137. --  -------- Main
  138.  
  139. -- Splashscreen
  140. term.setTextColor(theme.text)
  141. term.setBackgroundColor(theme.background)
  142. term.clear()
  143. term.setCursorPos(1, 2)
  144. term.setBackgroundColor(theme.top)
  145. centerPrint(string.rep(" ", 47))
  146. centerPrint([[          ______ ____ ____   ______            ]])
  147. centerPrint([[ ------- / ____//  _// __ \ / ____/            ]])
  148. centerPrint([[ ------ / /_    / / / /_/ // __/               ]])
  149. centerPrint([[ ----- / __/  _/ / / _  _// /___               ]])
  150. centerPrint([[ ---- / /    /___//_/ |_|/_____/               ]])
  151. centerPrint([[ --- / /       _       __ ____   __     ______ ]])
  152. centerPrint([[ -- /_/       | |     / // __ \ / /    / ____/ ]])
  153. centerPrint([[              | | /| / // / / // /    / /_     ]])
  154. centerPrint([[              | |/ |/ // /_/ // /___ / __/     ]])
  155. centerPrint([[              |__/|__/ \____//_____//_/        ]])
  156. centerPrint(string.rep(" ", 47))
  157. print("\n")
  158.  
  159. term.setBackgroundColor(theme.bottom)
  160. for i = 1, 3 do centerPrint(string.rep(" ", 47)) end
  161.  
  162. if not(http) then
  163.     term.setCursorPos(1, 17)
  164.     centerWrite("HTTP API Not Enabled! ")
  165.     if isAdvanced() then write("Click to exit...")
  166.     else write("Press any key to exit...") end
  167.     while true do
  168.         local e = os.pullEvent()
  169.         if e == "key" or e == "mouse_click" then break end
  170.     end
  171.  
  172.     error()
  173. end
  174.  
  175. -- Prompt
  176. local opt = prompt({{"Install Firewolf", 5, 17}, {"Cancel", w - 16, 17}}, "horizontal")
  177. if opt == "Install Firewolf" then
  178.     -- Install
  179.     term.setCursorPos(1, 17)
  180.     centerWrite(string.rep(" ", 47))
  181.     centerWrite("Installing...")
  182.     local a = download(downloadURL, "/firewolf")
  183.     centerWrite(string.rep(" ", 47))
  184.     if a then
  185.         centerWrite("Done!")
  186.     else
  187.         centerWrite("Download Failed!")
  188.     end
  189. elseif opt == "Cancel" then
  190.     -- Cancel
  191.     term.setCursorPos(1, 17)
  192.     centerWrite(string.rep(" ", 47))
  193.     centerWrite("Cancelled!")
  194. end
  195.  
  196. -- Clear screen
  197. sleep(1.3)
  198. term.setTextColor(colors.white)
  199. term.setBackgroundColor(colors.black)
  200. term.clear()
  201. term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement