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,6 +34,43 @@ 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")
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:
@ -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)