86 lines
3.6 KiB
Python
86 lines
3.6 KiB
Python
import sys, json, subprocess, configparser, shutil, os, urllib.request
|
|
from pathlib import Path
|
|
|
|
# User Variables
|
|
REPO_URL = ""
|
|
BRANCH = "main"
|
|
|
|
InstanceConfig = configparser.ConfigParser()
|
|
|
|
if Path("../instance.cfg").is_file():
|
|
# Launched by Prism/MultiMC - read the instance's own metadata
|
|
InstanceConfig.read("../instance.cfg")
|
|
with open("../mmc-pack.json", "r") as file:
|
|
Components = json.load(file)["components"]
|
|
# ponytail: match on uid, not list index - component order isn't guaranteed
|
|
Minecraft = next(c for c in Components if c["uid"] == "net.minecraft")
|
|
Modloader = next(c for c in Components
|
|
if c["uid"] != "net.minecraft" and not c.get("dependencyOnly"))
|
|
MinecraftVersion = Minecraft["version"]
|
|
ModloaderName = Modloader["cachedName"]
|
|
ModloaderVersion = Modloader["version"]
|
|
elif Path("env.txt").is_file():
|
|
# Standalone - same INI format, plus the versions mmc-pack.json would have given us
|
|
InstanceConfig.read("env.txt")
|
|
MinecraftVersion = InstanceConfig['General']['MinecraftVersion']
|
|
ModloaderName = InstanceConfig['General']['ModloaderName']
|
|
ModloaderVersion = InstanceConfig['General']['ModloaderVersion']
|
|
else:
|
|
sys.exit("* Found neither ../instance.cfg nor env.txt - cannot identify this instance.")
|
|
|
|
PackName = InstanceConfig['General']['name']
|
|
|
|
# Program Variables DO NOT TOUCH
|
|
MINGIT_URL = "https://github.com/git-for-windows/git/releases/download/v2.45.2.windows.1/MinGit-2.45.2-64-bit.zip"
|
|
MINGIT = Path(".mingit/cmd/git.exe")
|
|
|
|
print("****************************************")
|
|
print(f"* Launching {PackName}")
|
|
print(f"* {MinecraftVersion} / {ModloaderName} {ModloaderVersion}")
|
|
|
|
# ponytail: check the local MinGit before downloading - which() never sees it, it's not on PATH
|
|
GIT = shutil.which("git") or (str(MINGIT.resolve()) if MINGIT.is_file() else None)
|
|
if GIT is None:
|
|
if os.name != "nt":
|
|
sys.exit("* Git not found, please install Git!")
|
|
print("* Git not found, downloading and installing Git.")
|
|
urllib.request.urlretrieve(MINGIT_URL, "MinGit.zip")
|
|
shutil.unpack_archive("MinGit.zip", ".mingit")
|
|
Path("MinGit.zip").unlink()
|
|
GIT = str(MINGIT.resolve())
|
|
|
|
def git(*args):
|
|
with open("launchClient.log", "a") as log:
|
|
subprocess.run([GIT, *args], stdout=log, stderr=subprocess.STDOUT, check=True)
|
|
|
|
if Path(".git").is_dir():
|
|
print("* Detected git repo. Updating Pack!")
|
|
|
|
print("* * Fetching changes from upstream")
|
|
git("fetch", "origin")
|
|
|
|
# ponytail: reset straight to the remote - pull-down-only means local history never matters,
|
|
# and this can't produce a merge conflict the way fetch + merge could
|
|
print("* * Resetting local to match upstream")
|
|
git("reset", "--hard", f"origin/{BRANCH}")
|
|
|
|
# ponytail: clears leftovers that would block the reset. Respects .gitignore - no -x flag,
|
|
# so anything the game generates stays put as long as the repo ignores it
|
|
print("* * Clearing stale untracked pack files")
|
|
git("clean", "-fd")
|
|
|
|
print("* Pack Update Complete!")
|
|
else:
|
|
print("* No .git folder was found. Installing Pack!")
|
|
|
|
print("* * Cloning repo into temp folder")
|
|
tmp = Path("repo.tmp")
|
|
git("clone", "--branch", BRANCH, REPO_URL, str(tmp))
|
|
|
|
print("* * Copying repo out of temp and into current folder")
|
|
shutil.copytree(tmp, ".", dirs_exist_ok=True) # ponytail: merges like robocopy /E; move refuses existing dirs
|
|
shutil.rmtree(tmp)
|
|
|
|
print("* Pack Installation Complete!")
|
|
|
|
print("****************************************") |