mc-get/mcfs.py

73 lines
2.2 KiB
Python

import os
from sys import platform
import shutil
import zipfile
import json
def __get_mc_dir():
directory = ""
if platform == 'linux':
home = os.getenv("HOME", "")
directory = home + "/.minecraft"
elif platform == 'win32':
appdata = os.getenv('APPDATA')
directory = appdata + r"\.minecraft"
elif platform == "darwin":
directory = "~/Library/Application Support/minecraft" # unsure
directory = os.getenv("MC_DIR", directory)
return directory
def __get_cache_dir():
directory = ""
if platform == 'win32':
appdata_local = os.getenv("LOCALAPPDATA")
directory = appdata_local + r"\mc-get"
elif platform == 'darwin':
directory = '~/Library/Caches/mc-get' # unsure
elif platform == 'linux':
cache = os.getenv("HOME") + "/.cache"
directory = cache + "/mc-get"
else:
cache = os.getenv("XDG_CACHE_HOME", "")
directory = cache + "/mc-get"
return directory
def get_modpack_info(filename: str):
with zipfile.ZipFile(os.path.join(cache_dir, filename)) as modpack:
with modpack.open("modrinth.index.json") as index:
return type("modpack_index", (object,), json.load(index))
def install_modpacks_override(filename: str):
with zipfile.ZipFile(os.path.join(cache_dir, filename)) as modpack:
files = filter(lambda x: x.filename.startswith("overrides/"), modpack.infolist())
for file in files:
if file.is_dir():
continue
_to_path = os.path.join(mc_dir, file.filename[len("overrides/"):])
os.makedirs(os.path.dirname(_to_path), exist_ok=True)
print(file.filename[len("overrides/"):])
with modpack.open(file) as _from, open(_to_path, 'wb') as _to:
shutil.copyfileobj(_from, _to)
def is_path_exist(path: str):
return os.path.exists(os.path.join(path))
def is_standard_dir_structure():
return not os.path.exists(os.path.join(mc_dir, "home"))
def install(filename, subdir: str):
_from = os.path.join(cache_dir, filename)
_to = os.path.join(mc_dir, subdir, filename)
os.makedirs(os.path.dirname(_to), exist_ok=True)
shutil.copy2(_from, _to)
mc_dir = __get_mc_dir()
cache_dir = __get_cache_dir()