Created
April 14, 2024 14:33
-
-
Save ThePython10110/32a7f5b04e02a3877207bbf9bbac86c5 to your computer and use it in GitHub Desktop.
Creates symlinks so Minecraft instances use the same data. Requires changes on non-Windows platforms (changing paths)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from shutil import rmtree | |
symlink_path = r"C:\Portable\MCSymlink" | |
class LauncherData: | |
def __init__(self, **kwargs): | |
if not kwargs["path"]: | |
raise KeyError("Path must be included") | |
self.path = kwargs["path"] | |
self.no_instances = kwargs.get("no_instances", False) | |
self.ignore = kwargs.get("ignore", []) | |
if not self.no_instances: | |
if not kwargs.get("get_instance_data"): | |
def get_instance_data(self, instance_name): | |
path = os.path.join(self.path, instance_name) | |
if not os.path.isdir(path): | |
raise NotADirectoryError(path + " is not a directory.") | |
return path | |
self.get_instance_data = get_instance_data | |
else: | |
self.get_instance_data = kwargs["get_instance_data"] | |
def prism_get_data(self, instance_name): | |
path = os.path.join(self.path, instance_name) | |
if not os.path.isdir(path): | |
return False | |
if os.path.exists(os.path.join(path, "flame")): | |
return os.path.join(path, "minecraft") | |
else: | |
return os.path.join(path, ".minecraft") | |
launchers = { | |
"atlauncher": LauncherData( | |
path = os.path.expandvars("%appdata%\\atlauncher\\instances") | |
), | |
"prismlauncher": LauncherData( | |
path = os.path.expandvars("%appdata%\\prismlauncher\\instances"), | |
ignore = [".LAUNCHER_TEMP"], | |
get_instance_data = prism_get_data | |
), | |
"minecraft": LauncherData( | |
path = os.path.expandvars("%appdata%\\.minecraft"), | |
no_instances = True | |
) | |
} | |
launchers["at"] = launchers["atlauncher"] | |
launchers["prism"] = launchers["prismlauncher"] | |
launchers[".minecraft"] = launchers["minecraft"] | |
launchers["mc"] = launchers["minecraft"] | |
# name: is directory? | |
mc_symlinks = { | |
"resourcepacks": True, | |
"saves": True, | |
"shaderpacks": True, | |
"screenshots": True, | |
"options.txt": False | |
} | |
def get_instance_path(): | |
instance = None | |
launcher = None | |
while launcher not in launchers.keys(): | |
launcher = input("Launcher (? to list, ! to quit): ").lower() | |
if launcher == "!": | |
return | |
elif launcher == "?": | |
for thing in launchers.keys(): | |
print(thing) | |
continue | |
launcher_path = launchers[launcher].path | |
if launchers[launcher].no_instances: | |
return launcher_path | |
else: | |
while True: | |
instance = input("Instance (? to list, ! to quit): ") | |
if instance == "!": | |
return | |
elif instance == "?": | |
for d in os.listdir(launcher_path): | |
if os.path.isdir(os.path.join(launcher_path, d)) and d not in launchers[launcher].ignore: | |
print(d) | |
else: | |
data = launchers[launcher] | |
result = data.get_instance_data(data, instance) | |
if result and os.path.isdir(result): | |
return result | |
else: | |
print(f"Invalid instance {result}.") | |
def create_symlinks(path): | |
for thing, is_dir in mc_symlinks.items(): | |
src = os.path.join(symlink_path, thing) | |
dst = os.path.join(path, thing) | |
if not os.path.exists(src): | |
print(f"Source does not have {thing}, skipping.") | |
continue | |
elif os.path.exists(dst): | |
if input(f"Dst already has {thing}. Delete? ('y' to delete, any other value to skip) ") == "y": | |
if os.path.islink(dst): | |
if input(f"{thing} is a symlink. Still remove? ") == "y": | |
os.unlink(dst) | |
else: | |
continue | |
if input("Really delete? ") == "y": | |
if is_dir: | |
rmtree(dst, ignore_errors=True) | |
if os.path.exists(dst): | |
os.rmdir(dst) | |
else: | |
os.remove(dst) | |
os.symlink(src, dst, is_dir) | |
else: | |
continue | |
else: | |
os.symlink(src, dst, is_dir) | |
print(f"Created symlink for {thing}") | |
if __name__ == "__main__": | |
while True: | |
instance_path = get_instance_path() | |
if instance_path: | |
create_symlinks(instance_path) | |
else: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment