Advertisement
DOGGYWOOF

Doggy OS Package manager for user system

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