184 lines
5.6 KiB
Python
184 lines
5.6 KiB
Python
"""@author Jose Stovall | github.com/oitsjustjose"""
|
|
|
|
import json
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
from typing import Any
|
|
from zipfile import ZipFile
|
|
import toml
|
|
|
|
THIS_DIR = Path(__file__).parent.resolve()
|
|
CONFIG_PATH = THIS_DIR.joinpath("../configuration.json").resolve()
|
|
|
|
PACK_UNZIP_PATH = THIS_DIR.joinpath("activepack").resolve()
|
|
PACK_ZIP_PATH = PACK_UNZIP_PATH.with_suffix(".zip").resolve()
|
|
|
|
TROUBLEMAKERS_PATH = THIS_DIR.joinpath("troublemakers").resolve()
|
|
RESOURCE_PACKS_DIR = PACK_UNZIP_PATH.joinpath("minecraft").joinpath("resourcepacks").resolve()
|
|
MMC_EXPORT_CONFIG_PATH = THIS_DIR.joinpath("config.toml").resolve()
|
|
|
|
|
|
def print(obj: Any):
|
|
"""override"""
|
|
log_nm_base = sys.argv[1].split("\\")[-1]
|
|
with open(f"./{log_nm_base}.log", "a+", encoding="utf-8") as log_handle:
|
|
log_handle.write(f"{str(obj)}\n")
|
|
|
|
|
|
def _clean_pack():
|
|
"""
|
|
Removes client-only configs/files
|
|
"""
|
|
with open(CONFIG_PATH, "r", encoding="utf-8") as handle:
|
|
config = json.loads(handle.read())
|
|
|
|
for to_del in config["remove"]:
|
|
fpath = PACK_UNZIP_PATH.joinpath("minecraft").joinpath(to_del).resolve()
|
|
|
|
if not fpath.exists():
|
|
continue
|
|
if fpath.is_dir():
|
|
shutil.rmtree(fpath)
|
|
print(f"Removed directory {fpath}")
|
|
else:
|
|
fpath.unlink()
|
|
print(f"Removed file {fpath}")
|
|
|
|
|
|
def _get_modpack_outnames():
|
|
"""
|
|
Gets the MR_{packname}.mrpack and CF_{packname}.zip names based off of the config.toml contents
|
|
Args: join (bool) [False]: Whether to join them to the current dir or not
|
|
Returns: Tuple[str, str]: The MR_ and CF_ paths (in that order)
|
|
"""
|
|
with open(MMC_EXPORT_CONFIG_PATH, "r", encoding="utf-8") as handle:
|
|
name = toml.loads(handle.read())["name"]
|
|
return [
|
|
THIS_DIR.joinpath(f"MR_{name}.mrpack"),
|
|
THIS_DIR.joinpath(f"CF_{name}.zip"),
|
|
]
|
|
|
|
|
|
def __cleanup() -> None:
|
|
"""Cleans up output files"""
|
|
MMC_EXPORT_CONFIG_PATH.unlink()
|
|
PACK_ZIP_PATH.unlink()
|
|
shutil.rmtree(PACK_UNZIP_PATH)
|
|
shutil.rmtree(TROUBLEMAKERS_PATH)
|
|
for file in THIS_DIR.iterdir():
|
|
if file.name.startswith("CF_") or file.name.startswith("MR_"):
|
|
file.unlink()
|
|
|
|
|
|
def main() -> None:
|
|
"""
|
|
The main run function.
|
|
Args: None
|
|
Returns: None
|
|
Process Flow:
|
|
1. Move file to this dir
|
|
2. Unzip it, then:
|
|
1. Clean up the client mods
|
|
2. Grab the config.toml
|
|
3. Find any exceptive files that are going to be a PITA and remove them (a. la. Witchery in the RP's dir)
|
|
3. Re-zip
|
|
4. mmc-export
|
|
5. Move files from step 3 into output files
|
|
6. Move output folders back to originating dir
|
|
"""
|
|
if len(sys.argv) != 2:
|
|
print("Missing 2nd parameter File")
|
|
return
|
|
|
|
if not TROUBLEMAKERS_PATH.exists():
|
|
TROUBLEMAKERS_PATH.mkdir()
|
|
|
|
# STEP 1: copy the modpack locally
|
|
shutil.copy(sys.argv[1], PACK_ZIP_PATH)
|
|
print("STEP 1: SUCCESS")
|
|
|
|
# STEP 2: unzip the pack
|
|
shutil.unpack_archive(PACK_ZIP_PATH, PACK_UNZIP_PATH, "zip")
|
|
print("STEP 2: SUCCESS")
|
|
|
|
# STEP 2.1: clean up client mods
|
|
_clean_pack()
|
|
print("STEP 2.1: SUCCESS")
|
|
|
|
# STEP 2.2: grab the config.toml file
|
|
config_toml_path = PACK_UNZIP_PATH.joinpath("config.toml").resolve()
|
|
|
|
if not config_toml_path.exists():
|
|
raise Exception("Failed to find a valid config.toml inside the modpack. Please see this example: https://github.com/RozeFound/mmc-export/blob/master/example_config.toml")
|
|
|
|
shutil.move(config_toml_path, MMC_EXPORT_CONFIG_PATH)
|
|
print("STEP 2.2: SUCCESS")
|
|
|
|
# STEP 2.3: grab PITA resourcepacks files like Witchery's 1.7.10 jar
|
|
if RESOURCE_PACKS_DIR.exists():
|
|
for res_pack in RESOURCE_PACKS_DIR.iterdir():
|
|
if res_pack.is_dir() or res_pack.suffix == ".zip":
|
|
continue
|
|
shutil.move(res_pack, TROUBLEMAKERS_PATH.joinpath(res_pack.name))
|
|
print(f"STEP 2.3: Potential Troublemaker Found & Relocated: {res_pack.name}")
|
|
else:
|
|
print("No resourcepacks dir - skipping")
|
|
|
|
# STEP 3: re-zip & cleanup
|
|
shutil.make_archive(str(PACK_ZIP_PATH.with_suffix("")), "zip", PACK_UNZIP_PATH)
|
|
shutil.rmtree(PACK_UNZIP_PATH)
|
|
print("STEP 3: SUCCESS")
|
|
|
|
# ---- ANY FURTHER REFERENCES TO PACK_UNZIPPED_PATH ARE BAD!! ---- #
|
|
|
|
# STEP 4: MMC-EXPORT
|
|
print("MMC-Exporter is running, please wait")
|
|
status = subprocess.Popen(
|
|
[
|
|
"mmc-export.exe",
|
|
"-i",
|
|
PACK_ZIP_PATH,
|
|
"-f",
|
|
"Modrinth",
|
|
"CurseForge",
|
|
"-c",
|
|
MMC_EXPORT_CONFIG_PATH,
|
|
"-o",
|
|
THIS_DIR,
|
|
],
|
|
shell=True,
|
|
).wait()
|
|
|
|
if status != 0:
|
|
print("Failed to convert MMC pack to Curse and/or Modrinth. See above log.")
|
|
return
|
|
|
|
print("STEP 4: SUCCESS")
|
|
|
|
# STEP 5: Move Troublemakers back to inside each zip
|
|
output_files = _get_modpack_outnames()
|
|
for file in output_files:
|
|
with ZipFile(THIS_DIR.joinpath(file), "a") as zipf:
|
|
for file in TROUBLEMAKERS_PATH.iterdir():
|
|
zipf.write(file, f"overrides/resourcepacks/{file.name}")
|
|
print("STEP 5: SUCCESS")
|
|
|
|
# STEP 6: Move finished pack files back to OG dir
|
|
back = Path(sys.argv[1]).parent.resolve()
|
|
for file in output_files + [THIS_DIR.joinpath("bundled_links.md").resolve()]:
|
|
shutil.move(file, back.joinpath(file.name))
|
|
|
|
print("STEP 6: SUCCESS")
|
|
|
|
__cleanup()
|
|
print("DONE")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as e:
|
|
__cleanup()
|
|
print(e)
|