From 2a9af59319ff51df514d31f7afde53d6e7f7898d Mon Sep 17 00:00:00 2001 From: Sims <38142618+suchmememanyskill@users.noreply.github.com> Date: Sun, 15 Sep 2024 16:26:48 +0200 Subject: [PATCH] Start implementation of new mappings --- css_mappings.py | 80 +++++++++++++++++++++++++++++++++++++++++++++++-- css_utils.py | 21 +++++++++++-- 2 files changed, 97 insertions(+), 4 deletions(-) diff --git a/css_mappings.py b/css_mappings.py index f4a6e89..a0cf69c 100644 --- a/css_mappings.py +++ b/css_mappings.py @@ -1,6 +1,6 @@ -import os, asyncio, aiohttp +import os, asyncio, aiohttp, json, time from css_loader import get_loader_instance -from css_utils import Log, get_theme_path +from css_utils import Log, get_theme_path, get_steam_version from css_settings import setting_beta_mappings from css_inject import initialize_class_mappings from css_browserhook import ON_WEBSOCKET_CONNECT @@ -8,6 +8,82 @@ STARTED_FETCHING_TRANSLATIONS = False SUCCESSFUL_FETCH_THIS_RUN = False +def __get_target_steam_version(data : dict) -> str|None: + local_steam_version = get_steam_version() + is_beta = setting_beta_mappings() + target_branch = ("beta" if is_beta else "stable") + + if local_steam_version and local_steam_version in data['versions'] and data['versions'][local_steam_version] == target_branch: + target_steam_version = local_steam_version + else: + target_steam_version = None + prev = "999999999999" + for i, (k, v) in list(enumerate(data['versions'].items()))[::-1]: + if v == local_steam_version: + if int(prev) > int(target_steam_version) and int(k) < int(target_steam_version): + target_steam_version = k + break + + prev = k + + if target_steam_version not in data['versions']: + Log("Cannot find suitable version for translation") + return None + + return target_steam_version + +def generate_translations_from_local_file() -> dict[str, str]: + translations = {} + timer = time.time() + + path = os.path.join(get_theme_path(), "css_translations.json") + + if not os.path.exists(path): + Log("Translations file does not exist.") + return translations + + try: + with open(path, 'r', encoding="utf-8") as fp: + data = json.load(fp) + except Exception as e: + Log(f"Error while loading translations: {str(e)}.") + return translations + + target_steam_version = __get_target_steam_version(data) + if target_steam_version == None: + return translations + + Log(f"Using steam version {target_steam_version} for translations") + + for _, (module_id, module_data) in enumerate(data['module_mappings'].items()): + module_name = (str(module_id) if module_data['name'] is None else module_data['name']) + for _, (class_name, class_mappings) in enumerate(module_data['classname_mappings'].items()): + class_target = None + if target_steam_version in class_mappings: + class_target = class_mappings[target_steam_version] + else: + prev = "9999999999999" + for _, (class_mapping_name, class_mapping_value) in list(enumerate(class_mappings.items()))[::-1]: + if int(prev) > int(target_steam_version) and int(class_mapping_name) < int(target_steam_version): + class_target = class_mapping_value + break + + prev = class_mapping_name + + if class_target == None: + Log(f"No suitable version found for mapping {module_id}_{class_name}. Using last") + class_target = class_mappings[list(class_mappings)[-1]] + + for class_mapping_value in class_mappings.values(): + if class_mapping_value == class_target: + continue + + translations[class_mapping_value] = class_target + + translations[f"{module_name}_{class_name}"] = class_target + + return translations + async def __fetch_class_mappings(css_translations_path : str): global SUCCESSFUL_FETCH_THIS_RUN diff --git a/css_utils.py b/css_utils.py index b41b441..109131d 100644 --- a/css_utils.py +++ b/css_utils.py @@ -1,5 +1,5 @@ from logging import getLogger -import os, platform, traceback +import os, platform, traceback, re HOME = os.getenv("HOME") @@ -29,6 +29,7 @@ def Log(text : str): Logger.info(f"[CSS_Loader] {text}") + print(text) class Result: def __init__(self, success : bool, message : str = "Success", log : bool = True): @@ -176,4 +177,20 @@ def save_mappings(val: str, version: str): file_location = os.path.join(path, f"{version}.{branch_str}.json") with open(file_location, 'w') as fp: - fp.write(val) \ No newline at end of file + fp.write(val) + +def get_steam_version() -> None|str: + path = os.path.join(get_steam_path(), "logs", "console_log.txt") + + if not os.path.exists(path): + return None + + with open(path, 'r') as fp: + for line in reversed(fp.readlines()): + match = re.match(r"^\[.*?\] Client version: (.*?)$", line) + if match: + version = match.group(1) + if version != None and len(version) > 0: + return version + + return None \ No newline at end of file