From a932a9434a9ea49e3b1343d5e692bebfe767f833 Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Wed, 10 Aug 2022 04:48:48 +0300 Subject: [PATCH] create mods generation from config --- main.py | 139 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 82 insertions(+), 57 deletions(-) diff --git a/main.py b/main.py index f10e786..b78a696 100644 --- a/main.py +++ b/main.py @@ -26,35 +26,60 @@ def temp_forge_json(): return data -def generate_forge_list(forge_path, download_url): - forge_jar = scan_forge_folder(forge_path) - forge_submodules_list = generate_lib_list(os.path.join(forge_path, 'libs'), download_url) +def temp_forge_libs(): + with open('temp_forge_libs.json') as json_file: + data = json.load(json_file) + return data + + +def generate_forge_list(download_url, forge_version): + # forge_jar = scan_forge_folder(forge_path) + forge_submodules_list = generate_lib_list('none', download_url) forge_module = [{ - 'id': 'net.minecraftForge:forge:' + forge_jar.split('.')[0], + # 'id': 'net.minecraftForge:forge:' + forge_version, + 'id': "net.minecraftforge:forge:1.12.2-14.23.5.2847", 'name': 'Minecraft Forge', 'type': 'ForgeHosted', - 'subModules': forge_submodules_list + 'artifact': { + "size": 4884700, + "MD5": "90734a5a713e24902d24c45c15caa42c", + # 'url': download_url + 'forge/' + 'net.minecraftForge:forge:' + forge_version + '.jar', + 'url': "http://mc.westeroscraft.com/WesterosCraftLauncher/files/1.12.2/forge-1.12.2-14.23.5.2847-universal.jar", + }, + 'subModules': temp_forge_libs() }] - return (forge_module) - - + return forge_module def parse_servers_config(config: ConfigParser): servers = [] + submodules_dict = generate_submodules_dict() + for section in config.sections(): if section.split(' ')[0] == 'Server' or section.split(' ')[0] == 'server': download_url = config.get(section, 'download_url') - server_mods_path = os.path.join(config.get(section, 'path'), 'mods') - server_optional_mods_path = config.get(section, 'optional_mods_path') - forge_path = config.get(section, 'forge_reference_folder') + # server_mods_path = os.path.join(config.get(section, 'path'), 'mods') + # server_optional_mods_path = config.get(section, 'optional_mods_path') + # forge_path = config.get(section, 'forge_reference_folder') + forge_version = config.get(section, 'forge_version') + forge_module = generate_forge_list(download_url, forge_version) - forge_required_modules = generate_mod_list(server_mods_path, download_url, True) - forge_optionals_modules = generate_mod_list(server_optional_mods_path, download_url, False) - forge_libs = generate_forge_list(forge_path, download_url) - new_server_modules = forge_libs + forge_required_modules + forge_optionals_modules + config_share_mods = config.get(section, 'share_mods').split(' ') + forge_share_modules = generate_mod_list(config_share_mods, download_url, submodules_dict, True) + + config_client_mods = config.get(section, 'client_mods').split(' ') + forge_client_modules = generate_mod_list(config_client_mods, download_url, submodules_dict, True) + + config_client_optional_mods = config.get(section, 'client_optional_mods').split(' ') + forge_client_optional_modules = generate_mod_list(config_client_optional_mods, download_url, + submodules_dict, False) + + new_server_modules = forge_module \ + + forge_share_modules \ + # + forge_client_modules \ + # + forge_client_optional_modules new_server = {'id': (config.get(section, 'id')), 'name': (config.get(section, 'name')), @@ -68,8 +93,6 @@ def parse_servers_config(config: ConfigParser): 'discord_largeImageKey': (config.get(section, 'discord_largeImageKey')), 'mainServer': (config.get(section, 'mainServer')), 'autoconnect': (config.get(section, 'autoconnect')), - '_path': (config.get(section, 'path')), - '_download_url': download_url, 'modules': new_server_modules } servers.append(new_server) @@ -89,57 +112,59 @@ def list_files_in_folder(path): return f -def generate_mod_list(mods_path, download_url, isRequired): - mods_file_list = list_files_in_folder(os.path.join(mods_path)) +def get_name_in_dict(mod): + try: + human_mod_name = config.get('Mods Names', mod) + except: + print('Not find human name for: ' + mod) + return mod + return human_mod_name + + +def check_for_submodule(param): + return False + + +def generate_submodules_dict(): + mods_with_submodules = config.items('Mods Submodules') + submodules_dict = {} + for mod in mods_with_submodules: + for submodule in mod[1].split(' '): + submodules_dict[submodule] = mod[0] + return submodules_dict + + +def generate_mod_list(mods_config_list, download_url, submodules_dict, is_required): + # mods_file_list = list_files_in_folder(os.path.join(mods_path)) mod_list = [] - # mod_list.append(temp_forge_json()) - for mod in mods_file_list: - path_to_mod = os.path.join(mods_path, mod) - mod = mod[:-4] - parsed_mod = mod.split('_') - mod_id = parsed_mod[0] - parsed_mod[0] = parsed_mod[0].split(':') - parsed_mod[1] = parsed_mod[1].replace('-', ' ') - - if parsed_mod[-1] is not None: - if parsed_mod[-1] == 'ServerSide': - continue + for mod in mods_config_list: + # path_to_mod = os.path.join(mods_path, mod) + parsed_mod = mod.split(':') new_mod = { - 'id': mod_id, - 'name': parsed_mod[1], + 'id': mod, + 'name': get_name_in_dict(parsed_mod[1]), 'type': 'ForgeMod', - 'required': isRequired, - 'artifact': {'size': sys.getsizeof(path_to_mod), - 'MD5': calculate_md5(path_to_mod), - 'url': download_url + mod + '.jar' + 'artifact': {'size': 228, # sys.getsizeof(path_to_mod), + 'MD5': '228', # calculate_md5(path_to_mod), + 'url': download_url + 'mods/' + mod + '.jar' } } + if is_required is not True: + new_mod['required'] = {'value': is_required}, + mod_list.append(new_mod) + # try: + # master_mod = submodules_dict.get(parsed_mod[1]) + # kek = list(filter(lambda master_mod_dict: master_mod_dict['name'] == master_mod, mod_list)) + # pprint(kek) + # except: + # pass return mod_list def generate_lib_list(libs_path, download_url): - libs_file_list = list_files_in_folder(os.path.join(libs_path)) lib_list = [] - for lib in libs_file_list: - path_to_lib = os.path.join(libs_path, lib) - parsed_lib = lib.split(':') - lib_id = parsed_lib[1] - - if lib.split('.')[-1] == 'xz': - lib_id += "@jar.pack.xz" - - new_lib = { - 'id': lib_id, - 'name': parsed_lib[1], - 'type': 'Library', - 'artifact': {'size': sys.getsizeof(path_to_lib), - 'MD5': calculate_md5(path_to_lib), - 'url': download_url + lib - } - } - lib_list.append(new_lib) return lib_list @@ -165,7 +190,7 @@ if __name__ == '__main__': config = read_config(sys.argv[1]) distribution = generate_json(config) - pprint(distribution) + # pprint(distribution) with open('distribution.json', 'w', encoding='utf-8') as f: json.dump(distribution, f, ensure_ascii=False, indent=4)