HeliosDistributionGenerator/main.py

197 lines
6.8 KiB
Python
Raw Normal View History

2022-08-02 03:10:24 +00:00
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
2022-08-10 01:48:48 +00:00
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 = [{
2022-08-10 01:48:48 +00:00
# 'id': 'net.minecraftForge:forge:' + forge_version,
'id': "net.minecraftforge:forge:1.12.2-14.23.5.2847",
'name': 'Minecraft Forge',
'type': 'ForgeHosted',
2022-08-10 01:48:48 +00:00
'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()
}]
2022-08-10 01:48:48 +00:00
return forge_module
2022-08-02 03:10:24 +00:00
def parse_servers_config(config: ConfigParser):
servers = []
2022-08-10 01:48:48 +00:00
submodules_dict = generate_submodules_dict()
2022-08-02 03:10:24 +00:00
for section in config.sections():
if section.split(' ')[0] == 'Server' or section.split(' ')[0] == 'server':
download_url = config.get(section, 'download_url')
2022-08-10 01:48:48 +00:00
# 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)
config_share_mods = config.get(section, 'share_mods').split(' ')
forge_share_modules = generate_mod_list(config_share_mods, download_url, submodules_dict, True)
2022-08-10 01:48:48 +00:00
config_client_mods = config.get(section, 'client_mods').split(' ')
forge_client_modules = generate_mod_list(config_client_mods, download_url, submodules_dict, True)
2022-08-10 01:48:48 +00:00
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
2022-08-02 03:10:24 +00:00
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
}
2022-08-02 03:10:24 +00:00
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):
2022-08-02 03:10:24 +00:00
f = []
for (dirpath, dirnames, filenames) in os.walk(path):
f.extend(filenames)
break
return f
2022-08-10 01:48:48 +00:00
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))
2022-08-02 03:10:24 +00:00
mod_list = []
2022-08-10 01:48:48 +00:00
for mod in mods_config_list:
# path_to_mod = os.path.join(mods_path, mod)
parsed_mod = mod.split(':')
2022-08-02 03:10:24 +00:00
new_mod = {
2022-08-10 01:48:48 +00:00
'id': mod,
'name': get_name_in_dict(parsed_mod[1]),
2022-08-02 03:10:24 +00:00
'type': 'ForgeMod',
2022-08-10 01:48:48 +00:00
'artifact': {'size': 228, # sys.getsizeof(path_to_mod),
'MD5': '228', # calculate_md5(path_to_mod),
'url': download_url + 'mods/' + mod + '.jar'
2022-08-02 03:10:24 +00:00
}
}
2022-08-10 01:48:48 +00:00
if is_required is not True:
new_mod['required'] = {'value': is_required},
2022-08-02 03:10:24 +00:00
mod_list.append(new_mod)
2022-08-10 01:48:48 +00:00
# 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
2022-08-02 03:10:24 +00:00
return mod_list
def generate_lib_list(libs_path, download_url):
lib_list = []
return lib_list
2022-08-02 03:10:24 +00:00
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)
2022-08-10 01:48:48 +00:00
# pprint(distribution)
2022-08-02 03:10:24 +00:00
with open('distribution.json', 'w', encoding='utf-8') as f:
json.dump(distribution, f, ensure_ascii=False, indent=4)