Advertisement
DOGGYWOOF

dpm BETA

Oct 26th, 2024 (edited)
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 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. -- Paths and directories for package data
  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. for i = 1, 20 do
  57. if i <= (percent / 5) then
  58. bar = bar .. "="
  59. else
  60. bar = bar .. " "
  61. end
  62. end
  63.  
  64. bar = bar .. "] " .. percent .. "%"
  65.  
  66. term.setCursorPos(1, select(2, term.getCursorPos()))
  67. term.clearLine()
  68. term.write(bar)
  69. end
  70.  
  71. -- Download helper function
  72. local function downloadFile(url, destPath)
  73. local response = http.get(url)
  74. if response then
  75. local file = fs.open(destPath, "w")
  76. file.write(response.readAll())
  77. file.close()
  78. response.close()
  79. return true
  80. else
  81. print("Failed to download from URL:", url)
  82. return false
  83. end
  84. end
  85.  
  86. -- Install a package by Pastebin ID or URL
  87. local function installPackage(identifier, name, fromURL)
  88. local programPath = packageDir .. name
  89. if fs.exists(programPath) then
  90. print("Package already installed!")
  91. return
  92. end
  93.  
  94. print("\nInstalling package: " .. name)
  95. local totalSteps = 100
  96. local current = 0
  97.  
  98. while current < totalSteps do
  99. if math.random(1, 10) <= 3 then
  100. sleep(math.random(1, 3) / 10)
  101. end
  102.  
  103. local increment = math.random(1, 5)
  104. if math.random(1, 10) <= 2 then
  105. increment = increment + math.random(5, 10)
  106. end
  107.  
  108. current = math.min(current + increment, totalSteps)
  109. progressBar(current, totalSteps)
  110. sleep(0.1)
  111. end
  112.  
  113. local success
  114. if fromURL then
  115. success = downloadFile(identifier, programPath)
  116. else
  117. success = shell.run("pastebin get " .. identifier .. " " .. programPath)
  118. end
  119.  
  120. if success and fs.exists(programPath) then
  121. print("\nPackage installed: " .. name)
  122. local pkgData = loadPackages()
  123. pkgData[name] = { id = identifier, version = "1.0", fromURL = fromURL }
  124. savePackages(pkgData)
  125. else
  126. print("\nFailed to install the package.")
  127. end
  128. end
  129.  
  130. -- Update a single package
  131. local function updatePackage(name)
  132. local pkgData = loadPackages()
  133. local packageInfo = pkgData[name]
  134.  
  135. if not packageInfo then
  136. print("Package not found.")
  137. return
  138. end
  139.  
  140. print("\nUpdating package: " .. name)
  141. removePackage(name)
  142.  
  143. installPackage(packageInfo.id, name, packageInfo.fromURL)
  144. end
  145.  
  146. -- Remove a package
  147. local function removePackage(name)
  148. local programPath = packageDir .. name
  149. if fs.exists(programPath) then
  150. print("\nRemoving package: " .. name)
  151. fs.delete(programPath)
  152.  
  153. local pkgData = loadPackages()
  154. pkgData[name] = nil
  155. savePackages(pkgData)
  156.  
  157. print("Package removed: " .. name)
  158. else
  159. print("Package not found.")
  160. end
  161. end
  162.  
  163. -- List installed packages
  164. local function listPackages()
  165. local pkgData = loadPackages()
  166. if next(pkgData) == nil then
  167. print("No packages installed.")
  168. return
  169. end
  170.  
  171. print("\nInstalled packages:")
  172. for name, data in pairs(pkgData) do
  173. print(name .. " - Version: " .. data.version .. " - ID: " .. data.id .. (data.fromURL and " (URL)" or " (Pastebin)"))
  174. end
  175. end
  176.  
  177. -- Main menu
  178. local function displayMenu()
  179. term.clear()
  180. term.setCursorPos(1, 1)
  181. print("=== Package Manager ===")
  182. print("1. Install Package")
  183. print("2. Remove Package")
  184. print("3. List Installed Packages")
  185. print("4. Update Single Package")
  186. print("5. Update All Packages")
  187. print("6. Exit")
  188. end
  189.  
  190. local function launchGUI()
  191. while true do
  192. displayMenu()
  193. write("Select an option (1-6): ")
  194. local choice = read()
  195.  
  196. if choice == "1" then
  197. write("Enter Pastebin ID or URL: ")
  198. local identifier = read()
  199. write("Enter package name: ")
  200. local name = read()
  201. write("Is this a URL? (y/n): ")
  202. local isURL = read():lower() == "y"
  203. installPackage(identifier, name, isURL)
  204. elseif choice == "2" then
  205. write("Enter package name to remove: ")
  206. local name = read()
  207. removePackage(name)
  208. elseif choice == "3" then
  209. listPackages()
  210. elseif choice == "4" then
  211. write("Enter package name to update: ")
  212. local name = read()
  213. updatePackage(name)
  214. elseif choice == "5" then
  215. local pkgData = loadPackages()
  216. for name, _ in pairs(pkgData) do
  217. updatePackage(name)
  218. end
  219. print("All packages updated.")
  220. elseif choice == "6" then
  221. print("Exiting...")
  222. break
  223. else
  224. print("Invalid choice. Please select a valid option.")
  225. end
  226.  
  227. sleep(2)
  228. end
  229. end
  230.  
  231. -- Start GUI
  232. launchGUI()
  233.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement