From 68ab3d65247c02d469d77e5702f57e46f690965b Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 27 May 2022 01:11:42 -0500 Subject: [PATCH] Fix codegen more --- codegen/lib/code/version.py | 3 ++- codegen/lib/download.py | 40 ++++++++++++++++++------------------- codegen/lib/utils.py | 5 +++++ 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/codegen/lib/code/version.py b/codegen/lib/code/version.py index e131a598..4c8500be 100644 --- a/codegen/lib/code/version.py +++ b/codegen/lib/code/version.py @@ -1,7 +1,8 @@ import re import os +from lib.utils import get_dir_location -README_DIR = os.path.join(os.path.dirname(__file__), '../../../README.md') +README_DIR = get_dir_location('../README.md') VERSION_REGEX = r'\*Currently supported Minecraft version: `(.*)`.\*' diff --git a/codegen/lib/download.py b/codegen/lib/download.py index 5030f8f3..0d6668bd 100644 --- a/codegen/lib/download.py +++ b/codegen/lib/download.py @@ -1,42 +1,40 @@ +from lib.utils import get_dir_location from .mappings import Mappings import requests import json import os # make sure the downloads directory exists -if not os.path.exists('downloads'): - os.mkdir('downloads') +if not os.path.exists(get_dir_location('downloads')): + os.mkdir(get_dir_location('downloads')) def get_burger(): - if not os.path.exists('downloads/Burger'): - with open('burger.json', 'w') as f: - json.dump(requests.get( - 'https://api.github.com/repos/Burger/Burger/releases/latest').json(), f) + if not os.path.exists(get_dir_location('downloads/Burger')): print('\033[92mDownloading Burger...\033[m') os.system( - 'cd downloads && git clone https://github.com/pokechu22/Burger && cd Burger && git pull') + f'cd {get_dir_location("downloads")} && git clone https://github.com/pokechu22/Burger && cd Burger && git pull') print('\033[92mInstalling dependencies...\033[m') os.system('cd downloads/Burger && pip install six jawa') def get_version_manifest(): - if not os.path.exists(f'downloads/version_manifest.json'): + if not os.path.exists(get_dir_location(f'downloads/version_manifest.json')): print( f'\033[92mDownloading version manifest...\033[m') version_manifest_data = requests.get( 'https://launchermeta.mojang.com/mc/game/version_manifest.json').json() - with open(f'downloads/version_manifest.json', 'w') as f: + with open(get_dir_location(f'downloads/version_manifest.json'), 'w') as f: json.dump(version_manifest_data, f) else: - with open(f'downloads/version_manifest.json', 'r') as f: + with open(get_dir_location(f'downloads/version_manifest.json'), 'r') as f: version_manifest_data = json.load(f) return version_manifest_data def get_version_data(version_id: str): - if not os.path.exists(f'downloads/{version_id}.json'): + if not os.path.exists(get_dir_location(f'downloads/{version_id}.json')): version_manifest_data = get_version_manifest() print( @@ -48,46 +46,46 @@ def get_version_data(version_id: str): raise ValueError( f'No version with id {version_id} found. Maybe delete downloads/version_manifest.json and try again?') package_data = requests.get(package_url).json() - with open(f'downloads/{version_id}.json', 'w') as f: + with open(get_dir_location(f'downloads/{version_id}.json'), 'w') as f: json.dump(package_data, f) else: - with open(f'downloads/{version_id}.json', 'r') as f: + with open(get_dir_location(f'downloads/{version_id}.json'), 'r') as f: package_data = json.load(f) return package_data def get_client_jar(version_id: str): - if not os.path.exists(f'downloads/client-{version_id}.jar'): + if not os.path.exists(get_dir_location(f'downloads/client-{version_id}.jar')): package_data = get_version_data(version_id) print('\033[92mDownloading client jar...\033[m') client_jar_url = package_data['downloads']['client']['url'] - with open(f'downloads/client-{version_id}.jar', 'wb') as f: + with open(get_dir_location(f'downloads/client-{version_id}.jar'), 'wb') as f: f.write(requests.get(client_jar_url).content) def get_burger_data_for_version(version_id: str): - if not os.path.exists(f'downloads/burger-{version_id}.json'): + if not os.path.exists(get_dir_location(f'downloads/burger-{version_id}.json')): get_burger() get_client_jar(version_id) os.system( - f'cd downloads/Burger && python munch.py ../client-{version_id}.jar --output ../burger-{version_id}.json' + f'cd {get_dir_location("downloads/Burger")} && python munch.py ../client-{version_id}.jar --output ../burger-{version_id}.json' ) - with open(f'downloads/burger-{version_id}.json', 'r') as f: + with open(get_dir_location(f'downloads/burger-{version_id}.json'), 'r') as f: return json.load(f) def get_mappings_for_version(version_id: str): - if not os.path.exists(f'downloads/mappings-{version_id}.txt'): + if not os.path.exists(get_dir_location(f'downloads/mappings-{version_id}.txt')): package_data = get_version_data(version_id) client_mappings_url = package_data['downloads']['client_mappings']['url'] mappings_text = requests.get(client_mappings_url).text - with open(f'downloads/mappings-{version_id}.txt', 'w') as f: + with open(get_dir_location(f'downloads/mappings-{version_id}.txt'), 'w') as f: f.write(mappings_text) else: - with open(f'downloads/mappings-{version_id}.txt', 'r') as f: + with open(get_dir_location(f'downloads/mappings-{version_id}.txt'), 'r') as f: mappings_text = f.read() return Mappings.parse(mappings_text) diff --git a/codegen/lib/utils.py b/codegen/lib/utils.py index c185c0e5..77a7b2d4 100644 --- a/codegen/lib/utils.py +++ b/codegen/lib/utils.py @@ -1,4 +1,5 @@ import re +import os # utilities that could be used for things other than codegen @@ -44,3 +45,7 @@ def group_packets(packets: list[PacketIdentifier]): packet_groups[key] = [] packet_groups[key].append(packet.packet_id) return packet_groups + + +def get_dir_location(name: str): + return os.path.join(os.path.dirname(__file__), '..', name)