From a1e6e2b764a353798aae40a14239f90c594b9f17 Mon Sep 17 00:00:00 2001 From: "Project D.D" Date: Sat, 22 Apr 2023 00:09:22 +0700 Subject: [PATCH] Remove Sodium from MMC after exporting to pacwiz and Modrinth (#635) --- CLI tools/CurseForge to Packwiz-Modrinth.py | 182 +++++++++++++------- 1 file changed, 117 insertions(+), 65 deletions(-) diff --git a/CLI tools/CurseForge to Packwiz-Modrinth.py b/CLI tools/CurseForge to Packwiz-Modrinth.py index 390cc99..4b656aa 100644 --- a/CLI tools/CurseForge to Packwiz-Modrinth.py +++ b/CLI tools/CurseForge to Packwiz-Modrinth.py @@ -1,5 +1,8 @@ -import os +from json import loads as parse_json from zipfile import ZipFile +from io import BytesIO + +import os from shutil import unpack_archive from pathlib import Path import toml # pip install toml @@ -31,76 +34,125 @@ def extract_file(from_zip, from_file, to_path, from_desc, to_desc): else: print("Skipped " + from_desc + " copying to " + to_desc + ", didn't exist") -mod_files = os.listdir(mods_path) -if not refresh_only: - for item in mod_files: - os.remove( os.path.join(mods_path, item)) +def remove_from_archive(file_path: str, archive_path: str) -> None: -os.chdir(packwiz_path) -if not refresh_only: - cf_zip_path = input("Please drag the CurseForge zip file here: ")[3:][:-1] # Because dragging the file adds "& " and double quotes - pack_version = "-".join(str(Path(cf_zip_path).with_suffix("")).split("-")[1:]) + files = [] -if not mmc_export_packwiz_export and not refresh_only: - # Update pack.toml first - with open(packwiz_manifest, "r") as f: - pack_toml = toml.load(f) - pack_toml["version"] = pack_version - with open(packwiz_manifest, "w") as f: - toml.dump(pack_toml, f) + with ZipFile(archive_path) as archive: + for zipinfo in archive.infolist(): + if zipinfo.filename != file_path: + files.append((zipinfo.filename, archive.read(zipinfo.filename))) - # Packwiz import - os.system(packwiz_exe_path + " curseforge import \"" + cf_zip_path + "\"") - if hydrogen: - os.system(packwiz_exe_path + " remove hydrogen") - os.system(packwiz_exe_path + " mr install hydrogen") - if modrinth_overrides: - os.system(packwiz_exe_path + " remove entityculling") - os.system(packwiz_exe_path + " mr install entityculling") - -elif refresh_only: - os.system(packwiz_exe_path + " refresh") + with ZipFile(archive_path, "w") as archive: + for filename, content in files: archive.writestr(filename, content) -# Copy fresh manifest/modlist to git -if not is_legacy and not refresh_only: - extract_file(cf_zip_path, "manifest.json", git_path + "CurseForge", "CurseForge manifest.json", "Git") - extract_file(cf_zip_path, "modlist.html", git_path + "CurseForge", "CurseForge modlist.html", "Git") +def read_mod_meta(file_handle: BytesIO) -> dict: -# Export packwiz pack via mmc-export method -if mmc_export_packwiz_export and not refresh_only: + with ZipFile(file_handle) as archive: + data = archive.read("fabric.mod.json") + return parse_json(data, strict=False) + +def remove_mod_from_archive(mod_name: str, archive_path: str) -> None: + + with ZipFile(archive_path) as archive: + + for zipinfo in archive.infolist(): + + name = zipinfo.filename + + if not name.endswith(".jar"): continue + + with archive.open(name) as mod_file: + mod_meta = read_mod_meta(mod_file) + + if mod_meta["name"] == mod_name: + remove_from_archive(name, archive_path) + return + +def main() -> int: + + mod_files = os.listdir(mods_path) + if not refresh_only: + for item in mod_files: + os.remove( os.path.join(mods_path, item)) + + os.chdir(packwiz_path) + if not refresh_only: + cf_zip_path = input("Please drag the CurseForge zip file here: ")[3:][:-1] # Because dragging the file adds "& " and double quotes + pack_version = "-".join(str(Path(cf_zip_path).with_suffix("")).split("-")[1:]) + + if not mmc_export_packwiz_export and not refresh_only: + # Update pack.toml first + with open(packwiz_manifest, "r") as f: + pack_toml = toml.load(f) + pack_toml["version"] = pack_version + with open(packwiz_manifest, "w") as f: + toml.dump(pack_toml, f) + + # Packwiz import + os.system(packwiz_exe_path + " curseforge import \"" + cf_zip_path + "\"") + if hydrogen: + os.system(packwiz_exe_path + " remove hydrogen") + os.system(packwiz_exe_path + " mr install hydrogen") + if modrinth_overrides: + os.system(packwiz_exe_path + " remove entityculling") + os.system(packwiz_exe_path + " mr install entityculling") + + elif refresh_only: + os.system(packwiz_exe_path + " refresh") + + # Copy fresh manifest/modlist to git + if not is_legacy and not refresh_only: + extract_file(cf_zip_path, "manifest.json", git_path + "CurseForge", "CurseForge manifest.json", "Git") + extract_file(cf_zip_path, "modlist.html", git_path + "CurseForge", "CurseForge modlist.html", "Git") + + # Export packwiz pack via mmc-export method + if mmc_export_packwiz_export and not refresh_only: + mmc_zip_root = str(Path(cf_zip_path).parents[0]) + mmc_zip_path = mmc_zip_root + "\\Fabulously Optimized " + pack_version + ".zip" + packwiz_config = git_path + "Packwiz\\mmc-export.toml" + + cmd = f'mmc-export -i "{mmc_zip_path}" -f packwiz --modrinth-search loose -o "{mmc_zip_root}" -c "{packwiz_config}" -v {pack_version} --provider-priority Modrinth CurseForge Other --scheme mmc_export_packwiz_output' + os.system(cmd) + + packwiz_zip_path = Path(mmc_zip_root) / "mmc_export_packwiz_output.zip" + packwiz_out_path = Path(git_path) / "Packwiz" / minecraft_version + packwiz_out_path.mkdir(parents=True, exist_ok=True) + unpack_archive(packwiz_zip_path, packwiz_out_path) + + os.remove(packwiz_zip_path) + + # Export Modrinth pack and manifest via mmc-export method + if mmc_export_modrinth_export and not refresh_only: + mmc_zip_root = str(Path(cf_zip_path).parents[0]) + mmc_zip_path = mmc_zip_root + "\\Fabulously Optimized " + pack_version + ".zip" + modrinth_config = git_path + "Modrinth\\mmc-export.toml" + + cmd = f'mmc-export -i "{mmc_zip_path}" -f Modrinth --modrinth-search loose -o "{mmc_zip_root}" -c "{modrinth_config}" -v {pack_version} --scheme {"{name}-{version}"}' + os.system(cmd) + + if is_legacy == False: + extract_file(mmc_zip_root + "\\Fabulously Optimized-" + pack_version + ".mrpack", "modrinth.index.json", git_path + "\\" + "Modrinth", "Modrinth manifest", "Git") + + # Export Modrinth pack and manifest via packwiz method + if packwiz_modrinth_export: + os.system(packwiz_exe_path + " modrinth export") + for pack in os.listdir(packwiz_path): + if pack.endswith('.mrpack'): + if is_legacy == False: + extract_file(packwiz_path + "\\" + pack, "modrinth.index.json", git_path + "\\" + "Modrinth", "Modrinth manifest", "Git") + os.replace(packwiz_path + "\\" + pack, os.path.expanduser("~/Desktop") + "\\" + pack) + print("Moved " + pack + " to desktop") + os.system(packwiz_exe_path + " refresh") + mmc_zip_root = str(Path(cf_zip_path).parents[0]) mmc_zip_path = mmc_zip_root + "\\Fabulously Optimized " + pack_version + ".zip" - packwiz_config = git_path + "Packwiz\\mmc-export.toml" + remove_mod_from_archive("Sodium", mmc_zip_path) - cmd = f'mmc-export -i "{mmc_zip_path}" -f packwiz --modrinth-search loose -o "{mmc_zip_root}" -c "{packwiz_config}" -v {pack_version} --provider-priority Modrinth CurseForge Other --scheme mmc_export_packwiz_output' - os.system(cmd) + return 0 - packwiz_zip_path = Path(mmc_zip_root) / "mmc_export_packwiz_output.zip" - packwiz_out_path = Path(git_path) / "Packwiz" / minecraft_version - packwiz_out_path.mkdir(parents=True, exist_ok=True) - unpack_archive(packwiz_zip_path, packwiz_out_path) - - os.remove(packwiz_zip_path) - -# Export Modrinth pack and manifest via mmc-export method -if mmc_export_modrinth_export and not refresh_only: - mmc_zip_root = str(Path(cf_zip_path).parents[0]) - mmc_zip_path = mmc_zip_root + "\\Fabulously Optimized " + pack_version + ".zip" - modrinth_config = git_path + "Modrinth\\mmc-export.toml" - - cmd = f'mmc-export -i "{mmc_zip_path}" -f Modrinth --modrinth-search loose -o "{mmc_zip_root}" -c "{modrinth_config}" -v {pack_version} --scheme {"{name}-{version}"}' - os.system(cmd) - - if is_legacy == False: - extract_file(mmc_zip_root + "\\Fabulously Optimized-" + pack_version + ".mrpack", "modrinth.index.json", git_path + "\\" + "Modrinth", "Modrinth manifest", "Git") - -# Export Modrinth pack and manifest via packwiz method -if packwiz_modrinth_export: - os.system(packwiz_exe_path + " modrinth export") - for pack in os.listdir(packwiz_path): - if pack.endswith('.mrpack'): - if is_legacy == False: - extract_file(packwiz_path + "\\" + pack, "modrinth.index.json", git_path + "\\" + "Modrinth", "Modrinth manifest", "Git") - os.replace(packwiz_path + "\\" + pack, os.path.expanduser("~/Desktop") + "\\" + pack) - print("Moved " + pack + " to desktop") - os.system(packwiz_exe_path + " refresh") +if __name__ == "__main__": + try: main() + except KeyboardInterrupt: + print("Operation aborted by user.") + exit(-1)