Advertisement
DOGGYWOOF

Untitled

Oct 23rd, 2024 (edited)
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 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. local logFile = "/disk/packages/log.txt" -- Log file for package actions
  5.  
  6. -- Ensure the package directory exists
  7. if not fs.exists(packageDir) then
  8. fs.makeDir(packageDir)
  9. end
  10.  
  11. -- Helper to load package data
  12. local function loadPackages()
  13. if fs.exists(packages) then
  14. local file = fs.open(packages, "r")
  15. local data = textutils.unserialize(file.readAll())
  16. file.close()
  17. return data or {}
  18. end
  19. return {}
  20. end
  21.  
  22. -- Helper to save package data
  23. local function savePackages(data)
  24. local file = fs.open(packages, "w")
  25. file.write(textutils.serialize(data))
  26. file.close()
  27. end
  28.  
  29. -- Helper to log actions
  30. local function logAction(action)
  31. local file = fs.open(logFile, "a")
  32. file.write(os.date("%Y-%m-%d %H:%M:%S") .. " - " .. action .. "\n")
  33. file.close()
  34. end
  35.  
  36. -- Show a stable progress bar that stays on the same line
  37. local function progressBar(current, total)
  38. local percent = math.floor((current / total) * 100)
  39. local bar = "["
  40.  
  41. -- Construct the progress bar
  42. for i = 1, 20 do
  43. if i <= (percent / 5) then
  44. bar = bar .. "="
  45. else
  46. bar = bar .. " "
  47. end
  48. end
  49.  
  50. bar = bar .. "] " .. percent .. "%"
  51.  
  52. -- Set cursor to the same line and clear the line for a clean update
  53. term.setCursorPos(1, select(2, term.getCursorPos()))
  54. term.clearLine()
  55. term.write(bar)
  56. end
  57.  
  58. -- Install a package by Pastebin ID
  59. local function installPackage(pastebinId, name)
  60. local programPath = packageDir .. name
  61. if fs.exists(programPath) then
  62. print("Package already installed!")
  63. return
  64. end
  65.  
  66. -- Progress tracking
  67. print("\nInstalling package: " .. name)
  68. local totalSteps = 100 -- More steps for smoother progress
  69. local current = 0
  70.  
  71. while current < totalSteps do
  72. -- Randomly decide to stutter
  73. if math.random(1, 10) <= 3 then -- 30% chance to stutter
  74. sleep(math.random(1, 3) / 10) -- Short random delay
  75. end
  76.  
  77. -- Random increment and speed burst
  78. local increment = math.random(1, 5) -- Random increment between 1 and 5
  79. if math.random(1, 10) <= 2 then -- 20% chance for a speed burst
  80. increment = increment + math.random(5, 10) -- Random burst
  81. end
  82.  
  83. current = math.min(current + increment, totalSteps) -- Prevent exceeding total
  84. progressBar(current, totalSteps)
  85. sleep(0.1) -- Simulate downloading time per step
  86. end
  87.  
  88. -- Download from Pastebin
  89. local success, err = pcall(function()
  90. shell.run("pastebin get " .. pastebinId .. " " .. programPath)
  91. end)
  92.  
  93. if success and fs.exists(programPath) then
  94. print("\nPackage installed: " .. name)
  95. logAction("Installed package: " .. name)
  96.  
  97. -- Update package metadata
  98. local pkgData = loadPackages()
  99. pkgData[name] = { id = pastebinId, version = "1.0" } -- You can add versioning later
  100. savePackages(pkgData)
  101. else
  102. print("\nFailed to download the package. Error: " .. (err or "Unknown error"))
  103. end
  104. end
  105.  
  106. -- Remove a package
  107. local function removePackage(name)
  108. local programPath = packageDir .. name
  109. if fs.exists(programPath) then
  110. print("\nRemoving package: " .. name)
  111. local totalSteps = 100 -- More steps for smoother progress
  112. local current = 0
  113.  
  114. while current < totalSteps do
  115. -- Randomly decide to stutter
  116. if math.random(1, 10) <= 3 then -- 30% chance to stutter
  117. sleep(math.random(1, 3) / 10) -- Short random delay
  118. end
  119.  
  120. -- Random increment and speed burst
  121. local increment = math.random(1, 5) -- Random increment between 1 and 5
  122. if math.random(1, 10) <= 2 then -- 20% chance for a speed burst
  123. increment = increment + math.random(5, 10) -- Random burst
  124. end
  125.  
  126. current = math.min(current + increment, totalSteps) -- Prevent exceeding total
  127.  
  128. progressBar(current, totalSteps)
  129. sleep(0.1) -- Simulate removal time per step
  130. end
  131.  
  132. fs.delete(programPath)
  133.  
  134. -- Update package metadata
  135. local pkgData = loadPackages()
  136. pkgData[name] = nil
  137. savePackages(pkgData)
  138.  
  139. print("\nPackage removed: " .. name)
  140. logAction("Removed package: " .. name)
  141. else
  142. print("Package not found.")
  143. end
  144. end
  145.  
  146. -- List installed packages
  147. local function listPackages()
  148. local pkgData = loadPackages()
  149. if next(pkgData) == nil then
  150. print("No packages installed.")
  151. return
  152. end
  153.  
  154. print("\nInstalled packages:")
  155. for name, data in pairs(pkgData) do
  156. print(name .. " - Version: " .. data.version .. " - Pastebin ID: " .. data.id)
  157. end
  158. end
  159.  
  160. -- Check for updates for installed packages
  161. local function checkForUpdates()
  162. local pkgData = loadPackages()
  163. if next(pkgData) == nil then
  164. print("No packages to check for updates.")
  165. return
  166. end
  167.  
  168. print("\nChecking for updates...\n")
  169. for name, data in pairs(pkgData) do
  170. -- Simulate checking for updates (replace with actual check logic)
  171. print("Package: " .. name .. " - Latest version: 1.1 (you have " .. data.version .. ")")
  172. end
  173. end
  174.  
  175. -- Update all packages with visual feedback
  176. local function updatePackages()
  177. local pkgData = loadPackages()
  178. if next(pkgData) == nil then
  179. print("No packages to update.")
  180. return
  181. end
  182.  
  183. local total = #pkgData
  184. local count = 0
  185.  
  186. print("\nStarting update of all packages...\n")
  187.  
  188. -- Remove all installed packages
  189. print("Step 1: Removing old packages\n")
  190. for name, data in pairs(pkgData) do
  191. count = count + 1
  192. removePackage(name)
  193. sleep(0.2) -- Simulate process time
  194. end
  195.  
  196. print("\nAll packages removed.\n")
  197.  
  198. -- Reinstall all packages from Pastebin
  199. print("Step 2: Reinstalling packages\n")
  200. count = 0
  201. for name, data in pairs(pkgData) do
  202. count = count + 1
  203. installPackage(data.id, name)
  204. sleep(0.2) -- Simulate process time
  205. end
  206.  
  207. print("\nUpdate completed! All packages are reinstalled.")
  208. end
  209.  
  210. -- Search for an installed package
  211. local function searchPackage(name)
  212. local pkgData = loadPackages()
  213. for pkgName, data in pairs(pkgData) do
  214. if string.find(pkgName:lower(), name:lower()) then
  215. print("Found package: " .. pkgName .. " - Version: " .. data.version .. " - Pastebin ID: " .. data.id)
  216. end
  217. end
  218. end
  219.  
  220. -- Help command
  221. local function displayHelp()
  222. print("\nUsage: pkg <command> [args]")
  223. print("Commands:")
  224. print(" install <pastebin_id> <name> - Install a package from Pastebin.")
  225. print(" remove <name> - Remove an installed package.")
  226. print(" list - List all installed packages.")
  227. print(" check - Check for updates for installed packages.")
  228. print(" update - Update all installed packages.")
  229. print(" search <name> - Search for an installed package.")
  230. print(" help - Display this help message.")
  231. end
  232.  
  233. -- Main command line interface
  234. local function main()
  235. while true do
  236. write("> ")
  237. local input = read()
  238. local args = {}
  239.  
  240. for word in input:gmatch("%S+") do
  241. table.insert(args, word)
  242. end
  243.  
  244. if #args == 0 then
  245. print("No command entered.")
  246. elseif args[1] == "install" and args[3] then
  247. installPackage(args[2], args[3])
  248. elseif args[1] == "remove" and args[2] then
  249. removePackage(args[2])
  250. elseif args[1] == "list" then
  251. listPackages()
  252. elseif args[1] == "check" then
  253. checkForUpdates()
  254. elseif args[1] == "update" then
  255. updatePackages()
  256. elseif args[1] == "search" and args[2] then
  257. searchPackage(args[2])
  258. elseif args[1] == "help" then
  259. displayHelp()
  260. else
  261. print("Unknown command. Type 'help' for a list of commands.")
  262. end
  263. end
  264. end
  265.  
  266. -- Run the main function
  267. main()
  268.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement