diff --git a/css_inject.py b/css_inject.py index 69be90d..103c04b 100644 --- a/css_inject.py +++ b/css_inject.py @@ -6,32 +6,7 @@ from typing import List from css_utils import Result, Log, store_read, get_theme_path from css_browserhook import BrowserTabHook as CssTab, inject, remove - -CLASS_MAPPINGS = {} - -def initialize_class_mappings(): - css_translations_path = os.path.join(get_theme_path(), "css_translations.json") - - if not os.path.exists(css_translations_path): - Log("Failed to get css translations from local file") - return - - try: - with open(css_translations_path, "r", encoding="utf-8") as fp: - data : dict = json.load(fp) - except Exception as e: - Log(f"Failed to load css translations from local file: {str(e)}") - return - - CLASS_MAPPINGS.clear() - - # Data is in the format of { "uid": ["ver1", "ver2", "ver3"]} - for uid in data: - latest_value = data[uid][-1] - for y in data[uid][:-1]: - CLASS_MAPPINGS[y] = latest_value - - Log(f"Loaded {len(CLASS_MAPPINGS)} css translations from local file") +from css_mappings import CLASS_MAPPINGS ALL_INJECTS = [] diff --git a/css_loader.py b/css_loader.py index bf34243..5c2a71e 100644 --- a/css_loader.py +++ b/css_loader.py @@ -9,8 +9,6 @@ from os import listdir, path, mkdir import json -LOADER_INSTANCE = None - class Loader: def __init__(self): self.busy = False @@ -376,13 +374,4 @@ async def _generate_preset_theme_internal(self, name : str, deps : dict) -> Resu x.dependencies = deps break - return Result(True) - - -def get_loader_instance() -> Loader: - global LOADER_INSTANCE - - if LOADER_INSTANCE == None: - LOADER_INSTANCE = Loader() - - return LOADER_INSTANCE \ No newline at end of file + return Result(True) \ No newline at end of file diff --git a/css_mappings.py b/css_mappings.py index a0cf69c..bc6a2ff 100644 --- a/css_mappings.py +++ b/css_mappings.py @@ -1,12 +1,11 @@ import os, asyncio, aiohttp, json, time -from css_loader import get_loader_instance 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 STARTED_FETCHING_TRANSLATIONS = False SUCCESSFUL_FETCH_THIS_RUN = False +CLASS_MAPPINGS = {} def __get_target_steam_version(data : dict) -> str|None: local_steam_version = get_steam_version() @@ -82,19 +81,25 @@ def generate_translations_from_local_file() -> dict[str, str]: translations[f"{module_name}_{class_name}"] = class_target + Log(f"Loaded {len(translations)} css translations from local file in {time.time() - timer:.0f}s") return translations -async def __fetch_class_mappings(css_translations_path : str): +def load_global_translations(): + CLASS_MAPPINGS.clear() + + try: + for _, (k, v) in enumerate(generate_translations_from_local_file().items()): + CLASS_MAPPINGS[k] = v + except Exception as e: + Log(f"Error while loading global translations: {str(e)}") + +async def __fetch_class_mappings(css_translations_path : str, loader): global SUCCESSFUL_FETCH_THIS_RUN if SUCCESSFUL_FETCH_THIS_RUN: return - if setting_beta_mappings(): - css_translations_url = "https://api.deckthemes.com/beta.json" - else: - css_translations_url = "https://api.deckthemes.com/stable.json" - + css_translations_url = "https://api.deckthemes.com/mappings.json" Log(f"Fetching CSS mappings from {css_translations_url}") try: @@ -111,8 +116,8 @@ async def __fetch_class_mappings(css_translations_path : str): SUCCESSFUL_FETCH_THIS_RUN = True Log(f"Fetched css translations from server") - initialize_class_mappings() - asyncio.get_running_loop().create_task(get_loader_instance().reset(silent=True)) + load_global_translations() + asyncio.get_running_loop().create_task(loader.reset(silent=True)) except Exception as ex: Log(f"Failed to fetch css translations from server [{type(ex).__name__}]: {str(ex)}") @@ -126,13 +131,13 @@ async def __every(__seconds: float, func, *args, **kwargs): await func(*args, **kwargs) await asyncio.sleep(__seconds) -async def force_fetch_translations(): +async def force_fetch_translations(loader): global SUCCESSFUL_FETCH_THIS_RUN SUCCESSFUL_FETCH_THIS_RUN = False css_translations_path = os.path.join(get_theme_path(), "css_translations.json") - await __fetch_class_mappings(css_translations_path) + await __fetch_class_mappings(css_translations_path, loader) -def start_fetch_translations(): +def start_fetch_translations(loader): css_translations_path = os.path.join(get_theme_path(), "css_translations.json") - asyncio.get_event_loop().create_task(__every(60, __fetch_class_mappings, css_translations_path)) \ No newline at end of file + asyncio.get_event_loop().create_task(__every(60, __fetch_class_mappings, css_translations_path, loader)) \ No newline at end of file diff --git a/css_utils.py b/css_utils.py index 109131d..b9cfb21 100644 --- a/css_utils.py +++ b/css_utils.py @@ -29,7 +29,6 @@ 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): diff --git a/main.py b/main.py index 7f98264..e2eddf9 100644 --- a/main.py +++ b/main.py @@ -6,15 +6,15 @@ sys.path.append(os.path.dirname(__file__)) from css_utils import Log, create_steam_symlink, Result, get_theme_path, save_mappings as util_save_mappings, store_read as util_store_read, store_write as util_store_write, is_steam_beta_active -from css_inject import ALL_INJECTS, initialize_class_mappings +from css_inject import ALL_INJECTS from css_theme import CSS_LOADER_VER from css_remoteinstall import install from css_settings import setting_redirect_logs, setting_watch_files, set_setting_watch_files, setting_run_server from css_server import start_server from css_browserhook import initialize -from css_loader import Loader, get_loader_instance -from css_mappings import force_fetch_translations, start_fetch_translations +from css_loader import Loader +from css_mappings import force_fetch_translations, start_fetch_translations, load_global_translations ALWAYS_RUN_SERVER = False @@ -157,7 +157,7 @@ async def upload_theme(self, name : str, base_url : str, bearer_token : str) -> return (await self.loader.upload_theme(name, base_url, bearer_token)).to_dict() async def fetch_class_mappings(self): - await force_fetch_translations() + await force_fetch_translations(self.loader) return Result(True).to_dict() async def _main(self): @@ -170,12 +170,12 @@ async def _main(self): self.server_loaded = False Log("Initializing css loader...") - initialize_class_mappings() + load_global_translations() Log(f"Max supported manifest version: {CSS_LOADER_VER}") create_steam_symlink() - self.loader = get_loader_instance() + self.loader = Loader() await self.loader.load(False) if (setting_watch_files()): @@ -188,7 +188,7 @@ async def _main(self): if (ALWAYS_RUN_SERVER or setting_run_server()): await self.enable_server(self) - start_fetch_translations() + start_fetch_translations(self.loader) await initialize() if __name__ == '__main__':