Added checks before downloading modpack

This commit is contained in:
2024-02-20 23:01:18 -05:00
parent 2557c6cd7d
commit a219f22614

49
main.py
View File

@@ -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)
# Extract each file to its correct location
source = modpack_zip.open(member)
target = open(os.path.join(modpack_dir, filename), "wb")
target = open(target_path, "wb")
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: