From 2f24519015697eb8750620735b45137ac9659e8d Mon Sep 17 00:00:00 2001 From: Samuel Walker Date: Thu, 20 Jun 2024 08:48:58 -0600 Subject: [PATCH] Added failsafe to allow minecraft to launch if update server cant be contacted --- Backend.py | 79 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/Backend.py b/Backend.py index 008561c..fa2f888 100644 --- a/Backend.py +++ b/Backend.py @@ -9,49 +9,52 @@ import datetime def perform_installation(instance_name, prism_command, prism_instance_path): - ftp = FTP("gitea.piwalker.net") - ftp.login() - ftp.prot_p() + try: + ftp = FTP("gitea.piwalker.net") + ftp.login() + ftp.prot_p() - # Fetching versions.json from FTP - bio = io.BytesIO() - ftp.retrbinary("RETR versions.json", bio.write) - bio.seek(0) - versions = json.load(bio) - bio.close() + # Fetching versions.json from FTP + bio = io.BytesIO() + ftp.retrbinary("RETR versions.json", bio.write) + bio.seek(0) + versions = json.load(bio) + bio.close() - # Checking current version - version = "0.0.0" - version_file_path = os.path.join(prism_instance_path, instance_name, ".minecraft", "version.txt") - if os.path.exists(version_file_path): - with open(version_file_path, 'r') as fp: - version = fp.readline().rstrip() + # Checking current version + version = "0.0.0" + version_file_path = os.path.join(prism_instance_path, instance_name, ".minecraft", "version.txt") + if os.path.exists(version_file_path): + with open(version_file_path, 'r') as fp: + version = fp.readline().rstrip() - # Checking if update is needed - if version != versions[-1]["Version"]: - print(f"Current version: {version}") - print(f"Latest version: {versions[-1]['Version']}") + # Checking if update is needed + if version != versions[-1]["Version"]: + print(f"Current version: {version}") + print(f"Latest version: {versions[-1]['Version']}") - # Downloading modpack - with tempfile.TemporaryDirectory() as temp_dir: - modpack_file_path = os.path.join(temp_dir, instance_name + ".mrpack") - with open(modpack_file_path, 'wb') as modpack: - ftp.retrbinary("RETR " + versions[-1]["File"], modpack.write) + # Downloading modpack + with tempfile.TemporaryDirectory() as temp_dir: + modpack_file_path = os.path.join(temp_dir, instance_name + ".mrpack") + with open(modpack_file_path, 'wb') as modpack: + ftp.retrbinary("RETR " + versions[-1]["File"], modpack.write) + + # Running PrismLauncher with modpack + subprocess.Popen([prism_command, '-I', modpack_file_path]) - # Running PrismLauncher with modpack - subprocess.Popen([prism_command, '-I', modpack_file_path]) - - # Waiting for installation to complete - while True: - time.sleep(5) - if os.path.exists(version_file_path): - with open(version_file_path, 'r') as fp: - version = fp.readline().rstrip() - if version == versions[-1]["Version"]: - break - - # Launching PrismLauncher with the instance - subprocess.run([prism_command, '-l', instance_name]) + # Waiting for installation to complete + while True: + time.sleep(5) + if os.path.exists(version_file_path): + with open(version_file_path, 'r') as fp: + version = fp.readline().rstrip() + if version == versions[-1]["Version"]: + break + except: + print("Unable to check for updates. Modpack may be out of date") + finally: + # Launching PrismLauncher with the instance + subprocess.run([prism_command, '-l', instance_name]) def upload_pack(username, password, version_tag, fileName):