Added a install_forge function
This commit is contained in:
35
main.py
35
main.py
@@ -1,7 +1,9 @@
|
||||
import os
|
||||
import shutil
|
||||
import requests
|
||||
import zipfile
|
||||
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
|
||||
# Minecraft installation folder.
|
||||
@@ -35,8 +37,33 @@ def check_minecraft_installation(minecraft_dir):
|
||||
else:
|
||||
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.
|
||||
@@ -54,7 +81,9 @@ def backup_mods(modpack_dir, mods_backup_dir):
|
||||
# Entry point of the script.
|
||||
def main():
|
||||
try:
|
||||
print()
|
||||
check_minecraft_installation(minecraft_dir)
|
||||
install_forge(forge_dir, forge_url)
|
||||
backup_mods(modpack_dir, mods_backup_dir)
|
||||
except KeyboardInterrupt:
|
||||
print("\033[91m\nSession ended by user.\033[0m")
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user