gelatine87

installer.lua

May 9th, 2026 (edited)
4,543
0
Never
52
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.08 KB | Gaming | 0 0
  1. -- Configuration
  2. local owner = "Zonk1987"
  3. local repo = "CC-Tweaked-Programs"
  4. local branch = "main"
  5. local apiUrl = "https://api.github.com/repos/" .. owner .. "/" .. repo .. "/contents/"
  6. local rawUrl = "https://raw.githubusercontent.com/" .. owner .. "/" .. repo .. "/" .. branch .. "/"
  7.  
  8. local function clear()
  9.     term.clear()
  10.     term.setCursorPos(1, 1)
  11. end
  12.  
  13. -- Hardcoded fallback list with file manifests
  14. local fallbackProjects = {
  15.     {
  16.         name = "Powah Energizing Orb Automation",
  17.         path = "Powah%20Energizing%20Orb%20Automation",
  18.         files = { "startup.lua", "PowahSystem.lua", "RecipeManager.lua", "Dashboard.lua", "InventoryComponent.lua", "Orb.lua", "Chest.lua", "README.md" }
  19.     },
  20.     {
  21.         name = "Mekanism Portal Dialer Hub",
  22.         path = "Mekanism%20Portal%20Dialer%20Hub",
  23.         files = { "startup.lua", "PortalSystem.lua", "README.md" }
  24.     },
  25.     {
  26.         name = "Mekanism Portal Dialer Recall Sender",
  27.         path = "Mekanism%20Portal%20Dialer%20Recall%20Sender",
  28.         files = { "startup.lua", "README.md" }
  29.     },
  30.     {
  31.         name = "CC Developer Suite",
  32.         path = "CC%20Developer%20Suite",
  33.         files = { "startup.lua", "README.md" }
  34.     },
  35.     {
  36.         name = "Create Mechanical Crafter Automation",
  37.         path = "Create%20Mechanical%20Crafter%20Automation",
  38.         files = { "startup.lua", "README.md" }
  39.     }
  40. }
  41.  
  42. clear()
  43. if term.isColor() then term.setTextColor(colors.cyan) end
  44. print("=======================================")
  45. print("      Zonk's CC-Tweaked Installer      ")
  46. print("=======================================")
  47. if term.isColor() then term.setTextColor(colors.white) end
  48. print("Connecting to GitHub...\n")
  49.  
  50. -- Attempt to fetch root contents via API
  51. local projects = {}
  52. local isApiDown = false
  53. local response = http.get(apiUrl)
  54.  
  55. if response then
  56.     local responseData = response.readAll()
  57.     response.close()
  58.     local data = textutils.unserializeJSON(responseData)
  59.    
  60.     if data and not data.message then
  61.         for _, item in ipairs(data) do
  62.             if item.type == "dir" and not item.name:find("^%.") and item.name ~= "installer" then
  63.                 table.insert(projects, {
  64.                     name = item.name:gsub("%%20", " "),
  65.                     path = item.name
  66.                 })
  67.             end
  68.         end
  69.     else
  70.         isApiDown = true
  71.     end
  72. else
  73.     isApiDown = true
  74. end
  75.  
  76. -- If API failed or returned empty, use fallback list
  77. if #projects == 0 or isApiDown then
  78.     if term.isColor() then term.setTextColor(colors.yellow) end
  79.     print("Warning: GitHub API unreachable.")
  80.     print("Using built-in project list...\n")
  81.     projects = fallbackProjects
  82.     isApiDown = true
  83.     os.sleep(1)
  84. end
  85.  
  86. -- User Selection Menu
  87. clear()
  88. if term.isColor() then term.setTextColor(colors.cyan) end
  89. print("=======================================")
  90. print("      Zonk's CC-Tweaked Installer      ")
  91. print("=======================================")
  92. if term.isColor() then term.setTextColor(colors.white) end
  93. print("Select a project to install:\n")
  94.  
  95. for i, project in ipairs(projects) do
  96.     print(string.format("[%d] %s", i, project.name))
  97. end
  98.  
  99. print("\n[Q] Quit")
  100.  
  101. local selectedProject
  102. while true do
  103.     write("\nChoice: ")
  104.     local input = read()
  105.     if input:lower() == "q" then return end
  106.  
  107.     local num = tonumber(input)
  108.     if num and projects[num] then
  109.         selectedProject = projects[num]
  110.         break
  111.     else
  112.         print("Invalid choice!")
  113.     end
  114. end
  115.  
  116. -- Setup installation
  117. local targetDir = "" -- Install directly to root
  118. clear()
  119. print("Installing: " .. selectedProject.name)
  120. print("Target: Root Directory (/)")
  121. print("---------------------------------------\n")
  122.  
  123. --- Helper: Ensure URL is safe for CC (encode spaces)
  124. local function safeUrl(url)
  125.     return (url:gsub(" ", "%%20"))
  126. end
  127.  
  128. --- Smart download function (Handles API or Fallback)
  129. local function executeDownload()
  130.     local success = true
  131.    
  132.     -- If API works, use recursive discovery
  133.     if not isApiDown then
  134.         local function downloadViaApi(path, currentTarget)
  135.             local resp = http.get(safeUrl(apiUrl .. path))
  136.             if not resp then return false end
  137.             local items = textutils.unserializeJSON(resp.readAll())
  138.             resp.close()
  139.            
  140.             if currentTarget ~= "" and not fs.exists(currentTarget) then
  141.                 fs.makeDir(currentTarget)
  142.             end
  143.  
  144.             for _, item in ipairs(items) do
  145.                 local localPath = (currentTarget == "" and item.name or currentTarget .. "/" .. item.name)
  146.                 if item.type == "dir" then
  147.                     if not downloadViaApi(item.path, localPath) then return false end
  148.                 else
  149.                     print("Downloading: " .. item.name .. "...")
  150.                     local fResp = http.get(item.download_url)
  151.                     if fResp then
  152.                         local f = fs.open(localPath, "w")
  153.                         f.write(fResp.readAll())
  154.                         f.close()
  155.                         fResp.close()
  156.                     else
  157.                         return false
  158.                     end
  159.                 end
  160.             end
  161.             return true
  162.         end
  163.         success = downloadViaApi(selectedProject.path, targetDir)
  164.     else
  165.         -- FALLBACK: Use raw URLs with known file list
  166.         for _, fileName in ipairs(selectedProject.files) do
  167.             local branches_to_try = { branch, "master", "main" }
  168.             local success_file = false
  169.            
  170.             for _, b in ipairs(branches_to_try) do
  171.                 local currentRawUrl = string.format("https://raw.githubusercontent.com/%s/%s/%s/", owner, repo, b)
  172.                 local url = safeUrl(currentRawUrl .. selectedProject.path .. "/" .. fileName)
  173.                
  174.                 local resp = http.get(url)
  175.                 if resp then
  176.                     print("Downloading: " .. fileName .. "...")
  177.                     local localPath = (targetDir == "" and fileName or targetDir .. "/" .. fileName)
  178.                     local f = fs.open(localPath, "w")
  179.                     f.write(resp.readAll())
  180.                     f.close()
  181.                     resp.close()
  182.                     success_file = true
  183.                     break
  184.                 end
  185.             end
  186.            
  187.             if not success_file then
  188.                 print("Failed: " .. fileName)
  189.                 success = false
  190.             end
  191.         end
  192.     end
  193.     return success
  194. end
  195.  
  196. -- Execute
  197. if executeDownload() then
  198.     if term.isColor() then term.setTextColor(colors.lime) end
  199.     print("\nInstallation successful!")
  200.    
  201.     local selfPath = shell.getRunningProgram()
  202.     if fs.exists(selfPath) and not selfPath:find("^rom/") and selfPath ~= "pastebin" then
  203.         pcall(fs.delete, selfPath)
  204.     end
  205.    
  206.     print("Rebooting in 3 seconds...")
  207.     os.sleep(3)
  208.     os.reboot()
  209. else
  210.     if term.isColor() then term.setTextColor(colors.red) end
  211.     print("\nInstallation failed!")
  212.     print("Check your connection to raw.githubusercontent.com")
  213. end
Advertisement