Added a install_forge function

This commit is contained in:
2024-02-20 21:31:53 -05:00
parent 0d3a88b62b
commit 99c4b502f0

35
main.py
View File

@@ -1,7 +1,9 @@
import os import os
import shutil import shutil
import requests import requests
import zipfile
from io import BytesIO
from pathlib import Path from pathlib import Path
# Minecraft installation folder. # Minecraft installation folder.
@@ -35,8 +37,33 @@ def check_minecraft_installation(minecraft_dir):
else: else:
print("Minecraft is already installed.") print("Minecraft is already installed.")
# Installs forge files and places them in the correct version folder.
def install_forge(forge_dir, forge_url):
print("Checking for existing Forge installation...")
if not Path(forge_dir).exists():
print("Forge not found, beginning installation...")
os.makedirs(forge_dir)
try:
print("Downloading Forge...")
response = requests.get(forge_url)
if response.status_code == 200:
with zipfile.ZipFile(BytesIO(response.content)) as z:
print("Extracting Forge...")
common_prefix = os.path.commonprefix(z.namelist())
for file_info in z.infolist():
target_path = os.path.join(forge_dir, file_info.filename[len(common_prefix):])
if file_info.filename.endswith('/'):
os.makedirs(target_path, exist_ok=True)
else:
with z.open(file_info) as source, open(target_path, 'wb') as target:
shutil.copyfileobj(source, target)
print("Forge installed successfully.")
else:
print("Error downloading Forge. Please check the URL or your internet connection.")
except Exception as e:
print(f"An error occurred during Forge installation: {e}")
else:
print("Forge is already installed.")
# Backups the mods from the mods folder to the backup folder. # Backups the mods from the mods folder to the backup folder.
@@ -54,7 +81,9 @@ def backup_mods(modpack_dir, mods_backup_dir):
# Entry point of the script. # Entry point of the script.
def main(): def main():
try: try:
print() check_minecraft_installation(minecraft_dir)
install_forge(forge_dir, forge_url)
backup_mods(modpack_dir, mods_backup_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: