Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Package Manager for CC Tweaked
- local statusFile = "/disk/users/currentusr.status" -- Path to the file that stores the username
- local username = ""
- -- Load the current username from the status file
- local function loadUsername()
- if fs.exists(statusFile) then
- local file = fs.open(statusFile, "r")
- username = file.readLine()
- file.close()
- else
- print("Error: User status file not found.")
- return nil
- end
- end
- -- Load the username at the beginning
- loadUsername()
- if not username or username == "" then
- error("Error: No username found in " .. statusFile)
- end
- -- Paths and directories for package data
- local packageDir = "/disk/users/" .. username .. "/packages/"
- local packages = packageDir .. "packages.json"
- -- 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 = "["
- for i = 1, 20 do
- if i <= (percent / 5) then
- bar = bar .. "="
- else
- bar = bar .. " "
- end
- end
- bar = bar .. "] " .. percent .. "%"
- term.setCursorPos(1, select(2, term.getCursorPos()))
- term.clearLine()
- term.write(bar)
- end
- -- Download helper function
- local function downloadFile(url, destPath)
- local response = http.get(url)
- if response then
- local file = fs.open(destPath, "w")
- file.write(response.readAll())
- file.close()
- response.close()
- return true
- else
- print("Failed to download from URL:", url)
- return false
- end
- end
- -- Install a package by Pastebin ID or URL
- local function installPackage(identifier, name, fromURL)
- local programPath = packageDir .. name
- if fs.exists(programPath) then
- print("Package already installed!")
- return
- end
- 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
- local success
- if fromURL then
- success = downloadFile(identifier, programPath)
- else
- success = shell.run("pastebin get " .. identifier .. " " .. programPath)
- end
- if success and fs.exists(programPath) then
- print("\nPackage installed: " .. name)
- local pkgData = loadPackages()
- pkgData[name] = { id = identifier, version = "1.0", fromURL = fromURL }
- savePackages(pkgData)
- else
- print("\nFailed to install the package.")
- end
- end
- -- Update a single package
- local function updatePackage(name)
- local pkgData = loadPackages()
- local packageInfo = pkgData[name]
- if not packageInfo then
- print("Package not found.")
- return
- end
- print("\nUpdating package: " .. name)
- removePackage(name)
- installPackage(packageInfo.id, name, packageInfo.fromURL)
- end
- -- Remove a package
- local function removePackage(name)
- local programPath = packageDir .. name
- if fs.exists(programPath) then
- print("\nRemoving package: " .. name)
- fs.delete(programPath)
- local pkgData = loadPackages()
- pkgData[name] = nil
- savePackages(pkgData)
- print("Package 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 .. " - ID: " .. data.id .. (data.fromURL and " (URL)" or " (Pastebin)"))
- end
- end
- -- Main menu
- local function displayMenu()
- term.clear()
- term.setCursorPos(1, 1)
- print("=== Package Manager ===")
- print("1. Install Package")
- print("2. Remove Package")
- print("3. List Installed Packages")
- print("4. Update Single Package")
- print("5. Update All Packages")
- print("6. Exit")
- end
- local function launchGUI()
- while true do
- displayMenu()
- write("Select an option (1-6): ")
- local choice = read()
- if choice == "1" then
- write("Enter Pastebin ID or URL: ")
- local identifier = read()
- write("Enter package name: ")
- local name = read()
- write("Is this a URL? (y/n): ")
- local isURL = read():lower() == "y"
- installPackage(identifier, name, isURL)
- elseif choice == "2" then
- write("Enter package name to remove: ")
- local name = read()
- removePackage(name)
- elseif choice == "3" then
- listPackages()
- elseif choice == "4" then
- write("Enter package name to update: ")
- local name = read()
- updatePackage(name)
- elseif choice == "5" then
- local pkgData = loadPackages()
- for name, _ in pairs(pkgData) do
- updatePackage(name)
- end
- print("All packages updated.")
- elseif choice == "6" then
- print("Exiting...")
- break
- else
- print("Invalid choice. Please select a valid option.")
- end
- sleep(2)
- end
- end
- -- Start GUI
- launchGUI()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement