modpack/CLI tools/Curseforge to Packwiz-Modrinth.py

63 lines
2.5 KiB
Python
Raw Normal View History

2022-01-07 18:08:00 +00:00
import os
from zipfile import ZipFile
from pathlib import Path
import toml # pip install toml
2022-01-07 18:08:00 +00:00
user_path = os.path.expanduser("~")
git_path = user_path + "/Documents/GitHub/fabulously-optimized/"
2022-03-01 20:37:36 +00:00
minecraft_version = "1.18.2"
2022-01-19 16:12:02 +00:00
packwiz_path = git_path + "Packwiz/" + minecraft_version + "/"
packwiz_exe_path = "..\packwiz.exe"
2022-01-07 18:08:00 +00:00
mods_path = packwiz_path + "mods"
packwiz_manifest = "pack.toml"
2022-01-07 18:08:00 +00:00
2022-01-19 16:12:02 +00:00
refresh_only = False
is_legacy = False
2022-01-07 18:08:00 +00:00
def extract_file(from_zip, from_file, to_path, from_desc, to_desc):
with ZipFile(from_zip, 'r') as zip:
if from_file in zip.namelist():
zip.extract(from_file, to_path)
print("Copied " + from_desc + " to " + to_desc)
else:
print("Skipped " + from_desc + " copying to " + to_desc + ", didn't exist")
2022-01-19 16:12:02 +00:00
mod_files = os.listdir(mods_path)
if refresh_only == False:
for item in mod_files:
os.remove( os.path.join(mods_path, item))
2022-01-07 18:08:00 +00:00
os.chdir(packwiz_path)
2022-01-19 16:12:02 +00:00
if refresh_only == False:
cf_zip_path = input("Please drag the Curseforge zip file here: ")[3:][:-1] # Because dragging the file adds "& " and double quotes
# Update pack.toml first
pack_version = "-".join(str(Path(cf_zip_path).with_suffix("")).split("-")[1:])
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
2022-01-19 16:12:02 +00:00
os.system(packwiz_exe_path + " curseforge import \"" + cf_zip_path + "\"")
if is_legacy:
os.system(packwiz_exe_path + " remove hydrogen")
os.system(packwiz_exe_path + " mr install hydrogen")
os.system(packwiz_exe_path + " refresh")
2022-01-07 18:08:00 +00:00
# Copy fresh manifest/modlist to git
2022-01-19 16:12:02 +00:00
if is_legacy == False and refresh_only == False:
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")
2022-01-07 18:08:00 +00:00
# Export Modrinth pack and manifest
2022-01-31 15:21:19 +00:00
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")