Remove Sodium from MMC after exporting to pacwiz and Modrinth (#635)

This commit is contained in:
Project D.D 2023-04-22 00:09:22 +07:00 committed by GitHub
parent 7806064a56
commit a1e6e2b764
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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,17 +34,54 @@ 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:
def remove_from_archive(file_path: str, archive_path: str) -> None:
files = []
with ZipFile(archive_path) as archive:
for zipinfo in archive.infolist():
if zipinfo.filename != file_path:
files.append((zipinfo.filename, archive.read(zipinfo.filename)))
with ZipFile(archive_path, "w") as archive:
for filename, content in files: archive.writestr(filename, content)
def read_mod_meta(file_handle: BytesIO) -> dict:
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:
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:
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)
@ -58,16 +98,16 @@ if not mmc_export_packwiz_export and not refresh_only:
os.system(packwiz_exe_path + " remove entityculling")
os.system(packwiz_exe_path + " mr install entityculling")
elif refresh_only:
elif refresh_only:
os.system(packwiz_exe_path + " refresh")
# Copy fresh manifest/modlist to git
if not is_legacy and not refresh_only:
# 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:
# 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"
@ -82,8 +122,8 @@ if mmc_export_packwiz_export and not refresh_only:
os.remove(packwiz_zip_path)
# Export Modrinth pack and manifest via mmc-export method
if mmc_export_modrinth_export and not refresh_only:
# 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"
@ -94,8 +134,8 @@ if mmc_export_modrinth_export and not refresh_only:
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:
# 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'):
@ -104,3 +144,15 @@ if packwiz_modrinth_export:
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"
remove_mod_from_archive("Sodium", mmc_zip_path)
return 0
if __name__ == "__main__":
try: main()
except KeyboardInterrupt:
print("Operation aborted by user.")
exit(-1)