120 lines
4.1 KiB
Python
120 lines
4.1 KiB
Python
import os
|
||
from sys import platform
|
||
import shutil
|
||
import zipfile
|
||
import json
|
||
|
||
|
||
class MCVersion:
|
||
def __init__(self, version_number, is_modified=False, modloader=None):
|
||
self.version_number = version_number
|
||
self.is_modified = is_modified
|
||
self.modloader = modloader
|
||
|
||
def __lt__(self, other):
|
||
own_nums = map(int, self.version_number.split("."))
|
||
other_nums = map(int, other.version_number.split("."))
|
||
for own_num, other_num in zip(own_nums, other_nums):
|
||
if own_num < other_num:
|
||
return True
|
||
if own_num > other_num:
|
||
return False
|
||
return False
|
||
|
||
|
||
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 __version_sort(mc_ver: MCVersion) -> str:
|
||
return mc_ver.version_number
|
||
|
||
|
||
mc_dir = __get_mc_dir()
|
||
cache_dir = __get_cache_dir()
|
||
|
||
|
||
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 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)
|
||
|
||
|
||
def get_installed_mc_versions():
|
||
mc_vers_dir = os.path.join(mc_dir, "versions")
|
||
if not is_path_exist(mc_vers_dir):
|
||
return # TODO: Выброс ошибки
|
||
versions_dirs = next(os.walk(mc_vers_dir))[1]
|
||
versions = []
|
||
for version_dir in versions_dirs:
|
||
version_json_file = os.path.join(mc_vers_dir, version_dir, version_dir + ".json")
|
||
if not is_path_exist(version_json_file):
|
||
continue
|
||
with open(version_json_file) as json_file:
|
||
version_json = json.load(json_file)
|
||
mc_ver = None
|
||
match version_json.get("type", None):
|
||
case "modified":
|
||
version_number = version_json["jar"] # TODO: ИСПОЛЬЗОВАТЬ get ВМЕСТО []
|
||
is_modified = True
|
||
modloader = version_json["id"].split()[0].lower()
|
||
mc_ver = MCVersion(version_number, is_modified, modloader)
|
||
case "release": # TODO: Добавить поддержку других значений
|
||
version_number = version_json["id"]
|
||
mc_ver = MCVersion(version_number)
|
||
case None:
|
||
pass
|
||
case _: # TODO: Throw some errors
|
||
pass
|
||
versions.append(mc_ver)
|
||
return sorted(versions, reverse=True)
|