Доработка взаимодействия с API, новая процедура установки, определение установленных версий игры и начальная реализация БД

This commit is contained in:
Евгений Титаренко 2023-07-21 18:05:44 +03:00
parent 983d0f9e21
commit 5beb835d1e
4 changed files with 217 additions and 51 deletions

59
mcfs.py
View file

@ -5,6 +5,23 @@ 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':
@ -35,6 +52,14 @@ def __get_cache_dir():
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:
@ -58,10 +83,6 @@ 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)
@ -69,5 +90,31 @@ def install(filename, subdir: str):
shutil.copy2(_from, _to)
mc_dir = __get_mc_dir()
cache_dir = __get_cache_dir()
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)