Advertisement
Derek1017

CC Youtube

Jan 18th, 2015
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. --[[
  2.  
  3. Installer program for ComputerCraft YouTube.
  4.  
  5. --]]
  6.  
  7. --[[ Variables ]]--
  8. local sourceFolder = ".ccYouTube"
  9. screenX, screenY = term.getSize()
  10. tArgs = {...}
  11. if fs.exists(sourceFolder) and #tArgs == 0 then print("Source folder already exists;\nPlease use -redownload as the argument to delete the current source folder and download new; stopping") return end
  12. local backups = {}
  13.  
  14. -- Tables
  15. local t_Folders = {
  16. { sFolderName = "apis", sPath = sourceFolder .. "/apis" },
  17. { sFolderName = "downloads", sPath = sourceFolder .. "/downloads" },
  18. { sFolderName = "logos", sPath = sourceFolder .. "/logos" },
  19. { sFolderName = "recycleBin", sPath = sourceFolder .. "/recycleBin" },
  20. }
  21.  
  22. local t_NeededFiles = {
  23. { sType = "api", sUrl = "https://raw.github.com/remiX-/ccYouTube/master/apis/loadingScreen", sPath = sourceFolder .. "/apis/loadingScreen"},
  24. { sType = "api", sUrl = "https://raw.github.com/remiX-/ccYouTube/master/apis/extStrLib", sPath = sourceFolder .. "/apis/extStrLib"},
  25. { sType = "api", sUrl = "https://raw.github.com/remiX-/ccYouTube/master/apis/box", sPath = sourceFolder .. "/apis/box"},
  26. { sType = "logo", sUrl = "https://raw.github.com/remiX-/ccYouTube/master/logos/ccytLogo", sPath = sourceFolder .. "/logos/ccytLogo"},
  27. { sType = "logo", sUrl = "https://raw.github.com/remiX-/ccYouTube/master/logos/home", sPath = sourceFolder .. "/logos/home"},
  28. { sType = "file", sUrl = "https://raw.github.com/remiX-/ccYouTube/master/ComputerCraft%20YouTube", sPath = "ccYouTube"}
  29. }
  30.  
  31. local t_startLogo = {
  32. " f f eeeee e ",
  33. " f f e e ",
  34. " 9999 9999 ffff fff f f e e e eee eee",
  35. "9 9 f f f f f e e e e e e e",
  36. "9 9 ffff f f f f f e e e e e eee",
  37. "9 9 f f f f f e e e e e e ",
  38. " 9999 9999 ffff fff fff e eee eee eee",
  39. }
  40. --[[ End of Variables ]]--
  41.  
  42. term.clear() term.setCursorPos(1, 1)
  43.  
  44. term.setTextColour(colours.red)
  45. if not term.isColor() or turtle then
  46. print("Error; Advanced Computer Required.")
  47. term.setTextColour(colours.black)
  48. return
  49. elseif not http then
  50. print("Error; HTTP needs to be enabled.")
  51. term.setTextColour(colours.black)
  52. return
  53. end
  54. term.setTextColour(colours.black)
  55.  
  56. local function colorW(...)
  57. local curColor
  58. for i=1, #arg do -- arg is ...
  59. if type(arg[i]) == 'number' then
  60. curColor = arg[i]
  61. else
  62. if curColor then
  63. term.setTextColor(curColor)
  64. end
  65. write(arg[i])
  66. end
  67. end
  68. print() -- this is a print function, so it needs a new line.
  69. end
  70.  
  71. function printLogo()
  72. local function hexLookup( char )
  73. local value = tonumber(char, 16)
  74. return (value and math.pow(2, value) or nil)
  75. end
  76.  
  77. local function pCent(text, yLine, nCol)
  78. term.setCursorPos( ( screenX - #text ) / 2, yLine )
  79. term.setTextColour(nCol)
  80. write(text)
  81. end
  82.  
  83. term.setBackgroundColour(colours.white)
  84. term.clear()
  85. for i = 1, #t_startLogo do
  86. term.setCursorPos(5, 3+i)
  87. for z = 1, #t_startLogo[i] do
  88. tmp = string.sub(t_startLogo[i], z, z)
  89. if tmp == " " then
  90. term.setBackgroundColour(colours.white)
  91. write(' ')
  92. else
  93. term.setBackgroundColour(hexLookup(string.sub(t_startLogo[i], z, z)))
  94. write(' ')
  95. end
  96. end
  97. print()
  98. end
  99. term.setBackgroundColour(colours.white)
  100. pCent("Initilizing Installer for CC YouTube ...", 15, colours.black)
  101. pCent("By derekseitz", screenY, colours.cyan)
  102. sleep(2)
  103. end
  104.  
  105. term.setBackgroundColour(colours.black)
  106.  
  107. function initiate()
  108. print("Creating/Downloading the required files ... \n")
  109. -- Checking files and folders
  110. if not fs.isDir(sourceFolder) then
  111. fs.makeDir(sourceFolder)
  112. colorW(colours.yellow, "[", colours.red, "FOLDER", colours.yellow, "] ", colours.cyan, sourceFolder)
  113. end
  114. for i = 1, #t_Folders do
  115. if not fs.exists(t_Folders[i].sPath) then
  116. fs.makeDir(t_Folders[i].sPath)
  117. colorW(colours.yellow, "[", colours.red, "FOLDER", colours.yellow, "] ", colours.cyan, t_Folders[i].sPath)
  118. end
  119. end
  120. for i = 1, #t_NeededFiles do
  121. if not fs.exists(t_NeededFiles[i].sPath) then
  122. colorW(colours.yellow, "[", colours.lime, t_NeededFiles[i].sType, colours.yellow, "]", string.rep(" ", 8 - #t_NeededFiles[i].sType), colours.cyan, t_NeededFiles[i].sPath)
  123. local response = http.get( t_NeededFiles[i].sUrl )
  124. local fHandle = fs.open(t_NeededFiles[i].sPath, "w")
  125. fHandle.write(response.readAll())
  126. fHandle.close()
  127. end
  128. end
  129. end
  130.  
  131. --[[ Main part of the installer ]]--
  132. printLogo()
  133.  
  134. term.setBackgroundColour(colours.black)
  135. term.setTextColour(colours.red)
  136. term.setCursorPos(1, 1)
  137. term.clear()
  138. local fileList = fs.list(sourceFolder .. "/downloads/" )
  139. if #tArgs == 1 and tArgs[1] == "-redownload" then
  140. if #fileList > 0 then
  141. print( "Backing up current videos ..." )
  142. for i, v in pairs( fileList ) do
  143. local f = fs.open( sourceFolder .. "/downloads/" .. v, "r" )
  144. table.insert( backups, {
  145. name = v,
  146. content = f.readAll()
  147. })
  148. f.close()
  149. end
  150. print( '\nBacked up ' .. #fileList .. ' files.\n' )
  151. end
  152. sleep(0.5)
  153. fs.delete(sourceFolder)
  154. fs.delete("ccYouTube")
  155. end
  156.  
  157. initiate()
  158.  
  159. if #tArgs == 1 and tArgs[1] == "-redownload" and #backups ~= 0 then
  160. print( "\nCopying backed up files ... " )
  161. for i, v in pairs(backups) do
  162. local f = fs.open( sourceFolder .. '/downloads/' .. v.name, "w" )
  163. f.write( v.content )
  164. f.close()
  165. end
  166. end
  167.  
  168. print("\nDone!\nType ccYouTube to start!\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement