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
- local logFile = "/disk/packages/log.txt" -- Log file for package actions
- -- 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
- -- Helper to log actions
- local function logAction(action)
- local file = fs.open(logFile, "a")
- file.write(os.date("%Y-%m-%d %H:%M:%S") .. " - " .. action .. "\n")
- 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 -- More steps for smoother progress
- local current = 0
- while current < totalSteps do
- -- Randomly decide to stutter
- if math.random(1, 10) <= 3 then -- 30% chance to stutter
- sleep(math.random(1, 3) / 10) -- Short random delay
- end
- -- Random increment and speed burst
- local increment = math.random(1, 5) -- Random increment between 1 and 5
- if math.random(1, 10) <= 2 then -- 20% chance for a speed burst
- increment = increment + math.random(5, 10) -- Random burst
- end
- current = math.min(current + increment, totalSteps) -- Prevent exceeding total
- progressBar(current, totalSteps)
- sleep(0.1) -- Simulate downloading time per step
- end
- -- Download from Pastebin
- local success, err = pcall(function()
- shell.run("pastebin get " .. pastebinId .. " " .. programPath)
- end)
- if success and fs.exists(programPath) then
- print("\nPackage installed: " .. name)
- logAction("Installed package: " .. name)
- -- Update package metadata
- local pkgData = loadPackages()
- pkgData[name] = { id = pastebinId, version = "1.0" } -- You can add versioning later
- savePackages(pkgData)
- else
- print("\nFailed to download the package. Error: " .. (err or "Unknown error"))
- 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 -- More steps for smoother progress
- local current = 0
- while current < totalSteps do
- -- Randomly decide to stutter
- if math.random(1, 10) <= 3 then -- 30% chance to stutter
- sleep(math.random(1, 3) / 10) -- Short random delay
- end
- -- Random increment and speed burst
- local increment = math.random(1, 5) -- Random increment between 1 and 5
- if math.random(1, 10) <= 2 then -- 20% chance for a speed burst
- increment = increment + math.random(5, 10) -- Random burst
- end
- current = math.min(current + increment, totalSteps) -- Prevent exceeding total
- progressBar(current, totalSteps)
- sleep(0.1) -- Simulate removal time per step
- end
- fs.delete(programPath)
- -- Update package metadata
- local pkgData = loadPackages()
- pkgData[name] = nil
- savePackages(pkgData)
- print("\nPackage removed: " .. name)
- logAction("Removed package: " .. 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
- -- Check for updates for installed packages
- local function checkForUpdates()
- local pkgData = loadPackages()
- if next(pkgData) == nil then
- print("No packages to check for updates.")
- return
- end
- print("\nChecking for updates...\n")
- for name, data in pairs(pkgData) do
- -- Simulate checking for updates (replace with actual check logic)
- print("Package: " .. name .. " - Latest version: 1.1 (you have " .. data.version .. ")")
- 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")
- -- Remove all installed packages
- print("Step 1: Removing old packages\n")
- for name, data in pairs(pkgData) do
- count = count + 1
- removePackage(name)
- sleep(0.2) -- Simulate process time
- end
- print("\nAll packages removed.\n")
- -- Reinstall all packages from Pastebin
- 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) -- Simulate process time
- end
- print("\nUpdate completed! All packages are reinstalled.")
- end
- -- Search for an installed package
- local function searchPackage(name)
- local pkgData = loadPackages()
- for pkgName, data in pairs(pkgData) do
- if string.find(pkgName:lower(), name:lower()) then
- print("Found package: " .. pkgName .. " - Version: " .. data.version .. " - Pastebin ID: " .. data.id)
- end
- end
- end
- -- Help command
- local function displayHelp()
- print("\nUsage: pkg <command> [args]")
- print("Commands:")
- print(" install <pastebin_id> <name> - Install a package from Pastebin.")
- print(" remove <name> - Remove an installed package.")
- print(" list - List all installed packages.")
- print(" check - Check for updates for installed packages.")
- print(" update - Update all installed packages.")
- print(" search <name> - Search for an installed package.")
- print(" help - Display this help message.")
- end
- -- Main command line interface
- local function main()
- while true do
- write("> ")
- local input = read()
- local args = {}
- for word in input:gmatch("%S+") do
- table.insert(args, word)
- end
- if #args == 0 then
- print("No command entered.")
- elseif args[1] == "install" and args[3] then
- installPackage(args[2], args[3])
- elseif args[1] == "remove" and args[2] then
- removePackage(args[2])
- elseif args[1] == "list" then
- listPackages()
- elseif args[1] == "check" then
- checkForUpdates()
- elseif args[1] == "update" then
- updatePackages()
- elseif args[1] == "search" and args[2] then
- searchPackage(args[2])
- elseif args[1] == "help" then
- displayHelp()
- else
- print("Unknown command. Type 'help' for a list of commands.")
- end
- end
- end
- -- Run the main function
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement