From a219f226147b6b79c698a663d501edece92fdafb Mon Sep 17 00:00:00 2001 From: Leonard Excoffier Date: Tue, 20 Feb 2024 23:01:18 -0500 Subject: [PATCH] Added checks before downloading modpack --- main.py | 55 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/main.py b/main.py index 06cba56..dd800a4 100644 --- a/main.py +++ b/main.py @@ -120,26 +120,56 @@ def backup_mods(modpack_dir, mods_backup_dir): else: print("No mods directory found. No backup required.") -# Downloads the modpack. -def install_modpack(modpack_url, modpack_dir): - print("Downloading modpack...") +# Helper function to save the list of mod filenames as a manifest. +def save_modpack_manifest(modpack_dir, mod_filenames): + manifest_path = os.path.join(modpack_dir, 'modpack_manifest.txt') + with open(manifest_path, 'w') as manifest_file: + for filename in mod_filenames: + manifest_file.write(f"{filename}\n") + +# Function to check if modpack is already installed based on the manifest. +def is_modpack_installed(modpack_dir): + manifest_path = os.path.join(modpack_dir, 'modpack_manifest.txt') + if not os.path.exists(manifest_path): + return False # Manifest does not exist, modpack likely not installed. + with open(manifest_path, 'r') as manifest_file: + manifest_files = {line.strip() for line in manifest_file} + existing_files = {file for file in os.listdir(modpack_dir) if os.path.isfile(os.path.join(modpack_dir, file))} + return manifest_files.issubset(existing_files) and not manifest_files == set() + +# Updated install_modpack function. +def install_modpack(modpack_url, modpack_dir, mods_backup_dir): + if is_modpack_installed(modpack_dir): + print("Modpack is already installed according to the manifest. Skipping download and installation.") + return + + print("Modpack needs to be downloaded or updated. Proceeding with the installation.") try: + backup_mods(modpack_dir, mods_backup_dir) zip_path = os.path.join(modpack_dir, "modpack.zip") download_with_progress_bar(modpack_url, zip_path) print("Extracting modpack...") + + modpack_files = set() with zipfile.ZipFile(zip_path) as modpack_zip: for member in modpack_zip.namelist(): - filename = os.path.basename(member) - if not filename: - continue + if not member.endswith("/"): # Ignore directories + filename = os.path.basename(member) # Get base name to avoid directory structure + if filename: # Ignore any empty names + # Compute target path for extraction + target_path = os.path.join(modpack_dir, filename) + modpack_files.add(filename) - source = modpack_zip.open(member) - target = open(os.path.join(modpack_dir, filename), "wb") + # Extract each file to its correct location + source = modpack_zip.open(member) + target = open(target_path, "wb") + + with source, target: + shutil.copyfileobj(source, target) - with source, target: - shutil.copyfileobj(source, target) print("Modpack installed successfully.") - os.remove(zip_path) + os.remove(zip_path) # Clean up the downloaded ZIP file + save_modpack_manifest(modpack_dir, modpack_files) # Update the manifest except Exception as e: print(f"An error occurred during modpack installation: {e}") @@ -149,8 +179,7 @@ def main(): check_minecraft_installation(minecraft_dir) install_forge(forge_dir, forge_url) install_forge_libraries(forge_libraries_dir, forge_libraries_url) - backup_mods(modpack_dir, mods_backup_dir) - install_modpack(modpack_url, modpack_dir) + install_modpack(modpack_url, modpack_dir, mods_backup_dir) except KeyboardInterrupt: print("\033[91m\nSession ended by user.\033[0m") except Exception as e: