Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Package Manager for CC Tweaked
- local packages = "/disk/packages/packages.json" -- Path to store installed packages
- local packageDir = "/disk/packages/" -- Path to store downloaded programs
- -- Ensure the package directory exists
- if not fs.exists(packageDir) then
- fs.makeDir(packageDir)
- end
- -- Helper to load package data
- local function loadPackages()
- if fs.exists(packages) then
- local file = fs.open(packages, "r")
- local data = textutils.unserialize(file.readAll())
- file.close()
- return data or {}
- end
- return {}
- end
- -- Helper to save package data
- local function savePackages(data)
- local file = fs.open(packages, "w")
- file.write(textutils.serialize(data))
- file.close()
- end
- -- Show a stable progress bar that stays on the same line
- local function progressBar(current, total)
- local percent = math.floor((current / total) * 100)
- local bar = "["
- -- Construct the progress bar
- for i = 1, 20 do
- if i <= (percent / 5) then
- bar = bar .. "="
- else
- bar = bar .. " "
- end
- end
- bar = bar .. "] " .. percent .. "%"
- -- Set cursor to the same line and clear the line for a clean update
- term.setCursorPos(1, select(2, term.getCursorPos()))
- term.clearLine()
- term.write(bar)
- end
- -- Install a package by Pastebin ID
- local function installPackage(pastebinId, name)
- local programPath = packageDir .. name
- if fs.exists(programPath) then
- print("Package already installed!")
- return
- end
- -- Progress tracking
- print("\nInstalling package: " .. name)
- local totalSteps = 100
- local current = 0
- while current < totalSteps do
- if math.random(1, 10) <= 3 then
- sleep(math.random(1, 3) / 10)
- end
- local increment = math.random(1, 5)
- if math.random(1, 10) <= 2 then
- increment = increment + math.random(5, 10)
- end
- current = math.min(current + increment, totalSteps)
- progressBar(current, totalSteps)
- sleep(0.1)
- end
- -- Download from Pastebin
- shell.run("pastebin get " .. pastebinId .. " " .. programPath)
- if fs.exists(programPath) then
- print("\nPackage installed: " .. name)
- local pkgData = loadPackages()
- pkgData[name] = { id = pastebinId, version = "1.0" }
- savePackages(pkgData)
- else
- print("\nFailed to download the package.")
- end
- end
- -- Remove a package
- local function removePackage(name)
- local programPath = packageDir .. name
- if fs.exists(programPath) then
- print("\nRemoving package: " .. name)
- local totalSteps = 100
- local current = 0
- while current < totalSteps do
- if math.random(1, 10) <= 3 then
- sleep(math.random(1, 3) / 10)
- end
- local increment = math.random(1, 5)
- if math.random(1, 10) <= 2 then
- increment = increment + math.random(5, 10)
- end
- current = math.min(current + increment, totalSteps)
- progressBar(current, totalSteps)
- sleep(0.1)
- end
- fs.delete(programPath)
- local pkgData = loadPackages()
- pkgData[name] = nil
- savePackages(pkgData)
- print("\nPackage removed: " .. name)
- else
- print("Package not found.")
- end
- end
- -- List installed packages
- local function listPackages()
- local pkgData = loadPackages()
- if next(pkgData) == nil then
- print("No packages installed.")
- return
- end
- print("\nInstalled packages:")
- for name, data in pairs(pkgData) do
- print(name .. " - Version: " .. data.version .. " - Pastebin ID: " .. data.id)
- end
- end
- -- Search for a package by name
- local function searchPackages(query)
- local pkgData = loadPackages()
- local found = false
- print("\nSearch results for '" .. query .. "':")
- for name, data in pairs(pkgData) do
- if string.find(name:lower(), query:lower()) then
- found = true
- print(name .. " - Version: " .. data.version .. " - Pastebin ID: " .. data.id)
- end
- end
- if not found then
- print("No packages found matching the query.")
- end
- end
- -- Update all packages with visual feedback
- local function updatePackages()
- local pkgData = loadPackages()
- if next(pkgData) == nil then
- print("No packages to update.")
- return
- end
- local total = #pkgData
- local count = 0
- print("\nStarting update of all packages...\n")
- print("Step 1: Removing old packages\n")
- for name, data in pairs(pkgData) do
- count = count + 1
- removePackage(name)
- sleep(0.2)
- end
- print("\nAll packages removed.\n")
- print("Step 2: Reinstalling packages\n")
- count = 0
- for name, data in pairs(pkgData) do
- count = count + 1
- installPackage(data.id, name)
- sleep(0.2)
- end
- print("\nUpdate completed! All packages are reinstalled.")
- end
- -- Clear the terminal and set cursor position
- local function clearScreen()
- term.clear()
- term.setCursorPos(1, 1)
- end
- -- Display the GUI menu
- local function displayMenu()
- clearScreen()
- print("=== Package Manager ===")
- print("1. Install Package")
- print("2. Remove Package")
- print("3. List Installed Packages")
- print("4. Search Package")
- print("5. Update Packages")
- print("6. Exit")
- end
- -- Launch user-friendly GUI
- local function launchGUI()
- while true do
- displayMenu() -- Display the menu every time
- term.setCursorPos(1, 9)
- write("Select an option (1-6): ")
- local choice = read()
- if choice == "1" then
- term.setCursorPos(1, 11)
- write("Enter Pastebin ID: ")
- local pastebinId = read()
- term.setCursorPos(1, 12)
- write("Enter package name: ")
- local name = read()
- installPackage(pastebinId, name)
- elseif choice == "2" then
- term.setCursorPos(1, 11)
- write("Enter package name: ")
- local name = read()
- removePackage(name)
- elseif choice == "3" then
- listPackages()
- elseif choice == "4" then
- term.setCursorPos(1, 11)
- write("Enter search query: ")
- local query = read()
- searchPackages(query)
- elseif choice == "5" then
- updatePackages()
- elseif choice == "6" then
- print("Exiting...")
- break
- else
- print("Invalid choice. Please select a valid option.")
- end
- sleep(2) -- Pause for a moment before re-displaying the menu
- end
- end
- -- Display help screen
- local function displayHelp()
- clearScreen()
- print("=== Package Manager Help ===")
- print("Commands:")
- print(" gui Launch user-friendly GUI")
- print(" install <id> <name> Install a package from Pastebin")
- print(" remove <name> Remove an installed package")
- print(" list List all installed packages")
- print(" search <query> Search for packages by name")
- print(" update Update all installed packages")
- print(" exit Exit the package manager")
- print("===========================")
- end
- -- Command handler
- local args = {...}
- if #args < 1 then
- displayHelp() -- Show help screen if no arguments are provided
- else
- local command = args[1]
- if command == "gui" then
- launchGUI() -- Launch GUI if "gui" argument is called
- elseif command == "install" and #args == 3 then
- installPackage(args[2], args[3])
- elseif command == "remove" and #args == 2 then
- removePackage(args[2])
- elseif command == "list" then
- listPackages()
- elseif command == "update" then
- updatePackages()
- elseif command == "search" and #args == 2 then
- searchPackages(args[2])
- else
- print("Invalid command or arguments.")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement