diff options
Diffstat (limited to 'modpackman.py')
-rwxr-xr-x | modpackman.py | 81 |
1 files changed, 52 insertions, 29 deletions
diff --git a/modpackman.py b/modpackman.py index 503fbf3..a37730d 100755 --- a/modpackman.py +++ b/modpackman.py @@ -10,6 +10,7 @@ import shutil import requests import pathlib import hashlib +from configparser import RawConfigParser import util from util import config @@ -18,66 +19,88 @@ from util import config def install(): mods_location = os.path.join(config["pack"]["location"], "mods") whitelist = config["pack"]["whitelist"] - version_file = "version.txt" - pack_version = util.get_version_from_file(version_file) - print("Updating pack with version " + str(pack_version) + "...") - print() + + # Actual download links are stored in pack-lock.ini, so we need to read it in here + pack_lock = RawConfigParser() + pack_lock.read('pack-lock.ini') + + print(f"Updating pack with version {pack_lock['global']['pack_version']}...") # create the mods folder if it doesn't already exist pathlib.Path(mods_location).mkdir(parents=True, exist_ok=True) - # (fname, checksum, url) - mods = util.read_file(version_file) - names = [mod[0] for mod in mods] + names = set(f'{mod}.jar' for mod in pack_lock['mod_versions'].keys()) # whitelist client mods (e.g. optifine) - names += whitelist + names.update(whitelist) i = 0 - for mod in mods: - mod_path = os.path.join(mods_location, mod[0]) + for entry in pack_lock['mod_versions'].items(): + name = f'{entry[0]}.jar' + checksum, url = entry[1].split(',') + mod_path = os.path.join(mods_location, name) i += 1 if os.path.exists(mod_path) and os.path.isfile(mod_path) and \ - hashlib.sha1(open(mod_path, 'rb').read()).hexdigest() == mod[1]: - print("Skipping {mod[0]}, already up to date".format(mod=mod)) + hashlib.sha1(open(mod_path, 'rb').read()).hexdigest() == checksum: + print(f"Skipping {name}, already up to date") else: - print('Installing {mod[0]} from {mod[2]}...'.format(mod=mod)) - print(' ({i} of {x})'.format(i=i,x=len(mods)), end='\r') - download_obj = requests.get(mod[2], stream=True) + print(f'Installing {name} from {url}...') + print(f' ({i} of {len(pack_lock["mod_versions"])})', end='\r') + download_obj = requests.get(url, stream=True) with open(mod_path, "wb") as write_file: shutil.copyfileobj(download_obj.raw, write_file) print("Done!" + " " * 8) + # Remove any old mods that might be stuck in the mods folder print() print("Removing old mods...") for jar in os.listdir(mods_location): if jar not in names and os.path.splitext(jar)[1] == ".jar": os.remove(os.path.join(mods_location, jar)) - print("Removing '{jar}'".format(jar=jar)) + print(f"Removing '{jar}'") + + + print('\nInstalling configs...') + # For config files, we don't need to remove any extras, as they + # will simply be ignored by Forge + if not os.path.exists('config'): + raise RuntimeError("Error: config folder must exist!") + + mc_config_folder = os.path.join(config['pack']['location'], 'config') + if not os.path.exists(mc_config_folder): + os.mkdir(mc_config_folder) + for cfg in os.listdir('config'): + shutil.copyfile(cfg, os.path.join(mc_config_folder, cfg)) print() print("Finished installing mods!") -# Using the latest urls, update downloads.txt to match and have the correct sha1 -def apply_updates(mods, version_file, game_version=(2, 0, 0)): - pack_version = util.get_version_from_file(version_file) + +def apply_updates(): + """ + Using the URLs defined in pack.ini, update all mods to the latest + compatible version and write them out to pack-lock.ini + """ print("Populating version file...") print("Getting new versions of all mods...") - mod_urls = util.find_updated_urls([x for x in mods.values()], game_version, threads=3) + mod_urls = util.find_updated_urls([x for x in config['mods'].values()], config['pack']['game_version'], threads=16) print("Downloading and checksumming all mods...") checksums = util.find_checksums(mod_urls) - # Write information out to version.txt - with open(version_file, 'w') as f: - f.write('# Format: <jarname> <hex digested sha1> <direct download url>\n') - f.write("#VERSION " + str(pack_version + 1) + "\n") - for name, checksum, url in zip((k+'.jar' for k in mods.keys()), checksums, mod_urls): - f.write(f'{name} {checksum} {url}\n') + # Write the updated mods list out to pack-lock.ini + pack_lock = RawConfigParser() + pack_lock.read('pack-lock.ini') + pack_lock['global']['pack_version'] = str(int(pack_lock['global']['pack_version']) + 1) + # This is only needed for the self-update feature + pack_lock['global']['config_files'] = ','.join(os.listdir('config')) + pack_lock['mods'] = {name: f'{checksum},{url}' for name, checksum, url in zip(config['mods'].keys(), checksums, mod_urls)} + with open('pack-lock.ini', 'w') as f: + pack_lock.write(f) print() print("Done!") - print(f"Updates applied to {version_file}") - print("New pack version is " + str(pack_version + 1)) + print("Updates applied to pack-lock.ini") + print(f"New pack version is {pack_lock['global']['pack_version']}") print("[!] No mods were installed. To update your mods folder, run 'update.py install'") @@ -136,7 +159,7 @@ if __name__ == "__main__": if args.command == "install": install() elif args.command == "apply_updates": - apply_updates(config['mods'], "version.txt", config['pack']["game_version"]) + apply_updates() elif args.command == "check_updates": check_updates(config['mods'], "version.txt", config['pack']["game_version"]) else: |