Added checks before downloading modpack
This commit is contained in:
49
main.py
49
main.py
@@ -120,26 +120,56 @@ def backup_mods(modpack_dir, mods_backup_dir):
|
|||||||
else:
|
else:
|
||||||
print("No mods directory found. No backup required.")
|
print("No mods directory found. No backup required.")
|
||||||
|
|
||||||
# Downloads the modpack.
|
# Helper function to save the list of mod filenames as a manifest.
|
||||||
def install_modpack(modpack_url, modpack_dir):
|
def save_modpack_manifest(modpack_dir, mod_filenames):
|
||||||
print("Downloading modpack...")
|
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:
|
try:
|
||||||
|
backup_mods(modpack_dir, mods_backup_dir)
|
||||||
zip_path = os.path.join(modpack_dir, "modpack.zip")
|
zip_path = os.path.join(modpack_dir, "modpack.zip")
|
||||||
download_with_progress_bar(modpack_url, zip_path)
|
download_with_progress_bar(modpack_url, zip_path)
|
||||||
print("Extracting modpack...")
|
print("Extracting modpack...")
|
||||||
|
|
||||||
|
modpack_files = set()
|
||||||
with zipfile.ZipFile(zip_path) as modpack_zip:
|
with zipfile.ZipFile(zip_path) as modpack_zip:
|
||||||
for member in modpack_zip.namelist():
|
for member in modpack_zip.namelist():
|
||||||
filename = os.path.basename(member)
|
if not member.endswith("/"): # Ignore directories
|
||||||
if not filename:
|
filename = os.path.basename(member) # Get base name to avoid directory structure
|
||||||
continue
|
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)
|
source = modpack_zip.open(member)
|
||||||
target = open(os.path.join(modpack_dir, filename), "wb")
|
target = open(target_path, "wb")
|
||||||
|
|
||||||
with source, target:
|
with source, target:
|
||||||
shutil.copyfileobj(source, target)
|
shutil.copyfileobj(source, target)
|
||||||
|
|
||||||
print("Modpack installed successfully.")
|
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:
|
except Exception as e:
|
||||||
print(f"An error occurred during modpack installation: {e}")
|
print(f"An error occurred during modpack installation: {e}")
|
||||||
|
|
||||||
@@ -149,8 +179,7 @@ def main():
|
|||||||
check_minecraft_installation(minecraft_dir)
|
check_minecraft_installation(minecraft_dir)
|
||||||
install_forge(forge_dir, forge_url)
|
install_forge(forge_dir, forge_url)
|
||||||
install_forge_libraries(forge_libraries_dir, forge_libraries_url)
|
install_forge_libraries(forge_libraries_dir, forge_libraries_url)
|
||||||
backup_mods(modpack_dir, mods_backup_dir)
|
install_modpack(modpack_url, modpack_dir, mods_backup_dir)
|
||||||
install_modpack(modpack_url, modpack_dir)
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("\033[91m\nSession ended by user.\033[0m")
|
print("\033[91m\nSession ended by user.\033[0m")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
Reference in New Issue
Block a user