Advertisement
DOGGYWOOF

sys

Oct 23rd, 2024 (edited)
3
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.30 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. -- Show a stable progress bar that stays on the same line
  29. local function progressBar(current, total)
  30. local percent = math.floor((current / total) * 100)
  31. local bar = "["
  32.  
  33. -- Construct the progress bar
  34. for i = 1, 20 do
  35. if i <= (percent / 5) then
  36. bar = bar .. "="
  37. else
  38. bar = bar .. " "
  39. end
  40. end
  41.  
  42. bar = bar .. "] " .. percent .. "%"
  43.  
  44. -- Set cursor to the same line and clear the line for a clean update
  45. term.setCursorPos(1, select(2, term.getCursorPos()))
  46. term.clearLine()
  47. term.write(bar)
  48. end
  49.  
  50. -- Install a package by Pastebin ID
  51. local function installPackage(pastebinId, name)
  52. local programPath = packageDir .. name
  53. if fs.exists(programPath) then
  54. print("Package already installed!")
  55. return
  56. end
  57.  
  58. -- Progress tracking
  59. print("\nInstalling package: " .. name)
  60. local totalSteps = 100
  61. local current = 0
  62.  
  63. while current < totalSteps do
  64. if math.random(1, 10) <= 3 then
  65. sleep(math.random(1, 3) / 10)
  66. end
  67.  
  68. local increment = math.random(1, 5)
  69. if math.random(1, 10) <= 2 then
  70. increment = increment + math.random(5, 10)
  71. end
  72.  
  73. current = math.min(current + increment, totalSteps)
  74. progressBar(current, totalSteps)
  75. sleep(0.1)
  76. end
  77.  
  78. -- Download from Pastebin without showing output
  79. local command = "pastebin get " .. pastebinId .. " " .. programPath .. " > /dev/null"
  80. shell.run(command)
  81.  
  82. if fs.exists(programPath) then
  83. print("\nPackage installed: " .. name)
  84.  
  85. local pkgData = loadPackages()
  86. pkgData[name] = { id = pastebinId, version = "1.0" }
  87. savePackages(pkgData)
  88. else
  89. print("\nFailed to download the package.")
  90. end
  91. end
  92.  
  93. -- Remove a package
  94. local function removePackage(name)
  95. local programPath = packageDir .. name
  96. if fs.exists(programPath) then
  97. print("\nRemoving package: " .. name)
  98. local totalSteps = 100
  99. local current = 0
  100.  
  101. while current < totalSteps do
  102. if math.random(1, 10) <= 3 then
  103. sleep(math.random(1, 3) / 10)
  104. end
  105.  
  106. local increment = math.random(1, 5)
  107. if math.random(1, 10) <= 2 then
  108. increment = increment + math.random(5, 10)
  109. end
  110.  
  111. current = math.min(current + increment, totalSteps)
  112.  
  113. progressBar(current, totalSteps)
  114. sleep(0.1)
  115. end
  116.  
  117. fs.delete(programPath)
  118.  
  119. local pkgData = loadPackages()
  120. pkgData[name] = nil
  121. savePackages(pkgData)
  122.  
  123. print("\nPackage removed: " .. name)
  124. else
  125. print("Package not found.")
  126. end
  127. end
  128.  
  129. -- List installed packages
  130. local function listPackages()
  131. local pkgData = loadPackages()
  132. if next(pkgData) == nil then
  133. print("No packages installed.")
  134. return
  135. end
  136.  
  137. print("\nInstalled packages:")
  138. for name, data in pairs(pkgData) do
  139. print(name .. " - Version: " .. data.version .. " - Pastebin ID: " .. data.id)
  140. end
  141. end
  142.  
  143. -- Search for a package by name
  144. local function searchPackages(query)
  145. local pkgData = loadPackages()
  146. local found = false
  147.  
  148. print("\nSearch results for '" .. query .. "':")
  149. for name, data in pairs(pkgData) do
  150. if string.find(name:lower(), query:lower()) then
  151. found = true
  152. print(name .. " - Version: " .. data.version .. " - Pastebin ID: " .. data.id)
  153. end
  154. end
  155.  
  156. if not found then
  157. print("No packages found matching the query.")
  158. end
  159. end
  160.  
  161. -- Update all packages with visual feedback
  162. local function updatePackages()
  163. local pkgData = loadPackages()
  164. if next(pkgData) == nil then
  165. print("No packages to update.")
  166. return
  167. end
  168.  
  169. local total = #pkgData
  170. local count = 0
  171.  
  172. print("\nStarting update of all packages...\n")
  173.  
  174. print("Step 1: Removing old packages\n")
  175. for name, data in pairs(pkgData) do
  176. count = count + 1
  177. removePackage(name)
  178. sleep(0.2)
  179. end
  180.  
  181. print("\nAll packages removed.\n")
  182.  
  183. print("Step 2: Reinstalling packages\n")
  184. count = 0
  185. for name, data in pairs(pkgData) do
  186. count = count + 1
  187. installPackage(data.id, name)
  188. sleep(0.2)
  189. end
  190.  
  191. print("\nUpdate completed! All packages are reinstalled.")
  192. end
  193.  
  194. -- Clear the terminal and set cursor position
  195. local function clearScreen()
  196. term.clear()
  197. term.setCursorPos(1, 1)
  198. end
  199.  
  200. -- Display the GUI menu
  201. local function displayMenu()
  202. clearScreen()
  203. print("=== Package Manager ===")
  204. print("1. Install Package")
  205. print("2. Remove Package")
  206. print("3. List Installed Packages")
  207. print("4. Search Package")
  208. print("5. Update Packages")
  209. print("6. Exit")
  210. end
  211.  
  212. -- Launch user-friendly GUI
  213. local function launchGUI()
  214. while true do
  215. displayMenu() -- Display the menu every time
  216. term.setCursorPos(1, 9)
  217. write("Select an option (1-6): ")
  218. local choice = read()
  219.  
  220. if choice == "1" then
  221. term.setCursorPos(1, 11)
  222. write("Enter Pastebin ID: ")
  223. local pastebinId = read()
  224. term.setCursorPos(1, 12)
  225. write("Enter package name: ")
  226. local name = read()
  227. installPackage(pastebinId, name)
  228. elseif choice == "2" then
  229. term.setCursorPos(1, 11)
  230. write("Enter package name: ")
  231. local name = read()
  232. removePackage(name)
  233. elseif choice == "3" then
  234. listPackages()
  235. elseif choice == "4" then
  236. term.setCursorPos(1, 11)
  237. write("Enter search query: ")
  238. local query = read()
  239. searchPackages(query)
  240. elseif choice == "5" then
  241. updatePackages()
  242. elseif choice == "6" then
  243. print("Exiting...")
  244. break
  245. else
  246. print("Invalid choice. Please select a valid option.")
  247. end
  248.  
  249. sleep(2) -- Pause for a moment before re-displaying the menu
  250. end
  251. end
  252.  
  253. -- Display help screen
  254. local function displayHelp()
  255. clearScreen()
  256. print("=== Package Manager Help ===")
  257. print("Commands:")
  258. print(" gui Launch user-friendly GUI")
  259. print(" install <id> <name> Install a package from Pastebin")
  260. print(" remove <name> Remove an installed package")
  261. print(" list List all installed packages")
  262. print(" search <query> Search for packages by name")
  263. print(" update Update all installed packages")
  264. print(" exit Exit the package manager")
  265. print("===========================")
  266. end
  267.  
  268. -- Command handler
  269. local args = {...}
  270. if #args < 1 then
  271. displayHelp() -- Show help screen if no arguments are provided
  272. else
  273. local command = args[1]
  274. if command == "gui" then
  275. launchGUI() -- Launch GUI if "gui" argument is called
  276. elseif command == "install" and #args == 3 then
  277. installPackage(args[2], args[3])
  278. elseif command == "remove" and #args == 2 then
  279. removePackage(args[2])
  280. elseif command == "list" then
  281. listPackages()
  282. elseif command == "update" then
  283. updatePackages()
  284. elseif command == "search" and #args == 2 then
  285. searchPackages(args[2])
  286. else
  287. print("Invalid command")
  288.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement