Advertisement
DOGGYWOOF

Doggy OS Package manager

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