Advertisement
DOGGYWOOF

Untitled

Oct 23rd, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. -- Package Manager for CC Tweaked
  2. local packages = "/disk/packages/packages.json" -- Path to store installed packages
  3. local packageDir = "/disk/packages/" -- Path to store downloaded programs
  4.  
  5. -- Ensure the package directory exists
  6. if not fs.exists(packageDir) then
  7. fs.makeDir(packageDir)
  8. end
  9.  
  10. -- Helper to load package data
  11. local function loadPackages()
  12. if fs.exists(packages) then
  13. local file = fs.open(packages, "r")
  14. local data = textutils.unserialize(file.readAll())
  15. file.close()
  16. return data or {}
  17. end
  18. return {}
  19. end
  20.  
  21. -- Helper to save package data
  22. local function savePackages(data)
  23. local file = fs.open(packages, "w")
  24. file.write(textutils.serialize(data))
  25. file.close()
  26. end
  27.  
  28. -- Install a package by Pastebin ID
  29. local function installPackage(pastebinId, name)
  30. local programPath = packageDir .. name
  31. if fs.exists(programPath) then
  32. print("Package already installed!")
  33. return
  34. end
  35.  
  36. -- Download from Pastebin
  37. shell.run("pastebin get " .. pastebinId .. " " .. programPath)
  38. if fs.exists(programPath) then
  39. print("Package installed:", name)
  40.  
  41. -- Update package metadata
  42. local pkgData = loadPackages()
  43. pkgData[name] = { id = pastebinId, version = "1.0" } -- You can add versioning later
  44. savePackages(pkgData)
  45. else
  46. print("Failed to download the package.")
  47. end
  48. end
  49.  
  50. -- Remove a package
  51. local function removePackage(name)
  52. local programPath = packageDir .. name
  53. if fs.exists(programPath) then
  54. fs.delete(programPath)
  55.  
  56. -- Update package metadata
  57. local pkgData = loadPackages()
  58. pkgData[name] = nil
  59. savePackages(pkgData)
  60.  
  61. print("Package removed:", name)
  62. else
  63. print("Package not found.")
  64. end
  65. end
  66.  
  67. -- List installed packages
  68. local function listPackages()
  69. local pkgData = loadPackages()
  70. if next(pkgData) == nil then
  71. print("No packages installed.")
  72. return
  73. end
  74.  
  75. for name, data in pairs(pkgData) do
  76. print(name .. " - Version: " .. data.version .. " - Pastebin ID: " .. data.id)
  77. end
  78. end
  79.  
  80. -- Show a basic progress bar
  81. local function progressBar(current, total)
  82. local percent = math.floor((current / total) * 100)
  83. local bar = "["
  84. for i = 1, 20 do
  85. if i <= (percent / 5) then
  86. bar = bar .. "="
  87. else
  88. bar = bar .. " "
  89. end
  90. end
  91. bar = bar .. "] " .. percent .. "%"
  92. term.clearLine()
  93. term.write(bar)
  94. end
  95.  
  96. -- Update all packages with a nice screen
  97. local function updatePackages()
  98. local pkgData = loadPackages()
  99. if next(pkgData) == nil then
  100. print("No packages to update.")
  101. return
  102. end
  103.  
  104. local total = #pkgData
  105. local count = 0
  106.  
  107. print("Starting update of all packages...\n")
  108.  
  109. -- Remove all installed packages
  110. print("Step 1: Removing old packages\n")
  111. for name, data in pairs(pkgData) do
  112. count = count + 1
  113. print("Removing package: " .. name)
  114. removePackage(name)
  115. sleep(0.2) -- Simulate process time
  116. progressBar(count, total)
  117. end
  118.  
  119. print("\n\nAll packages removed.\n")
  120.  
  121. -- Reinstall all packages from Pastebin
  122. print("Step 2: Reinstalling packages\n")
  123. count = 0
  124. for name, data in pairs(pkgData) do
  125. count = count + 1
  126. print("Reinstalling package: " .. name)
  127. installPackage(data.id, name)
  128. sleep(0.2) -- Simulate process time
  129. progressBar(count, total)
  130. end
  131.  
  132. print("\n\nUpdate completed! All packages are reinstalled.")
  133. end
  134.  
  135. -- Command handler
  136. local args = {...}
  137. if #args < 1 then
  138. print("Usage: pkg <command> [args]")
  139. print("Commands: install <pastebin_id> <name>, remove <name>, list, update")
  140. return
  141. end
  142.  
  143. local command = args[1]
  144. if command == "install" and #args == 3 then
  145. installPackage(args[2], args[3])
  146. elseif command == "remove" and #args == 2 then
  147. removePackage(args[2])
  148. elseif command == "list" then
  149. listPackages()
  150. elseif command == "update" then
  151. updatePackages()
  152. else
  153. print("Invalid command or arguments.")
  154. end
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement