import os import sys import hashlib import json from pprint import pprint from configparser import ConfigParser def calculate_md5(file_path): hash_md5 = hashlib.md5() with open(file_path, "rb") as f: for chunk in iter(lambda: f.read(4096), b""): hash_md5.update(chunk) return hash_md5.hexdigest() def read_config(config_path): config = ConfigParser() config.read(config_path) return config def temp_forge_json(): with open('temp_forge.json') as json_file: data = json.load(json_file) return data 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_version, 'id': "net.minecraftforge:forge:1.12.2-14.23.5.2847", 'name': 'Minecraft Forge', 'type': 'ForgeHosted', '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 def scan_mods_folder(minecraft_version): mods_files_dict = {} path = os.path.join(config.get('Basic', 'modules_' + minecraft_version), 'mods') mods_files = list_files_in_folder(path) for mod_file in mods_files: mod_file_path = os.path.join(path, mod_file) new_mod = { 'MD5': calculate_md5(mod_file_path), 'size': os.stat(mod_file_path).st_size } mods_files_dict[mod_file] = new_mod print(mods_files_dict) return mods_files_dict def temp_files_json(): with open('temp_files.json') as json_file: data = json.load(json_file) return [data] 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') forge_version = config.get(section, 'forge_version') forge_module = generate_forge_list(download_url, forge_version) mods_files_dict = scan_mods_folder(config.get(section, 'minecraftVersion')) config_share_mods = config.get(section, 'share_mods').split(' ') forge_share_modules = generate_mod_list(config_share_mods, download_url, submodules_dict, True, True, mods_files_dict) config_client_mods = config.get(section, 'client_mods').split(' ') forge_client_modules = generate_mod_list(config_client_mods, download_url, submodules_dict, True, True, mods_files_dict) config_client_optional_enabled_mods = config.get(section, 'client_optional_enabled_mods').split(' ') forge_client_optional_enabled_modules = generate_mod_list(config_client_optional_enabled_mods, download_url, submodules_dict, False, True, mods_files_dict) config_client_optional_disabled_mods = config.get(section, 'client_optional_disabled_mods').split(' ') forge_client_optional_disabled_modules = generate_mod_list(config_client_optional_disabled_mods, download_url, submodules_dict, False, False, mods_files_dict) forge_client_optional_modules = forge_client_optional_enabled_modules + forge_client_optional_disabled_modules temp_files = temp_files_json() new_server_modules = forge_module \ + forge_share_modules \ + forge_client_modules \ + forge_client_optional_modules \ + temp_files new_server = {'id': (config.get(section, 'id')), 'name': (config.get(section, 'name')), 'description': (config.get(section, 'description')), 'icon': (config.get(section, 'icon')), 'version': (config.get(section, 'version')), 'address': (config.get(section, 'address')), 'minecraftVersion': (config.get(section, 'minecraftVersion')), 'discord_shortId': (config.get(section, 'discord_shortId')), 'discord_largeImageText': (config.get(section, 'discord_largeImageText')), 'discord_largeImageKey': (config.get(section, 'discord_largeImageKey')), 'mainServer': (config.get(section, 'mainServer')), 'autoconnect': (config.get(section, 'autoconnect')), 'modules': new_server_modules } servers.append(new_server) return servers def scan_forge_folder(forge_path): forge_jar = list_files_in_folder(forge_path)[0] return forge_jar def list_files_in_folder(path): f = [] for (dirpath, dirnames, filenames) in os.walk(path): f.extend(filenames) break return f 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, is_enabled, mods_files_dict): # mods_file_list = list_files_in_folder(os.path.join(mods_path)) mod_list = [] for mod in mods_config_list: # path_to_mod = os.path.join(mods_path, mod) parsed_mod = mod.split(':') new_mod = { 'id': mod, 'name': get_name_in_dict(parsed_mod[1]), 'type': 'ForgeMod', 'artifact': {'size': mods_files_dict[mod + '.jar'].get('size'), 'MD5': mods_files_dict[mod + '.jar'].get('MD5'), 'url': download_url + 'mods/' + mod + '.jar' } } if is_required is not True: new_mod['required'] = { 'value': is_required, 'def': is_enabled } 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): lib_list = [] return lib_list def generate_json(config: ConfigParser): new_distribution = { 'version': (config.get('Basic', 'version')), 'discord': { 'clientId': (config.get('Basic', 'version')), 'smallImageText': (config.get('Basic', 'discord_smallImageText')), 'smallImageKey': (config.get('Basic', 'dicord_smallImageKey')) }, 'java': { 'oracle': (config.get('Basic', 'java')) }, 'rss': (config.get('Basic', 'rss')), 'servers': parse_servers_config(config) } return new_distribution if __name__ == '__main__': temp_forge_json() config = read_config(sys.argv[1]) distribution = generate_json(config) # pprint(distribution) with open('distribution.json', 'w', encoding='utf-8') as f: json.dump(distribution, f, ensure_ascii=False, indent=4)