Skip to content

Commit

Permalink
Integrate new mapping system
Browse files Browse the repository at this point in the history
  • Loading branch information
suchmememanyskill committed Sep 15, 2024
1 parent 2a9af59 commit 9ff5194
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 60 deletions.
27 changes: 1 addition & 26 deletions css_inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down
13 changes: 1 addition & 12 deletions css_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from os import listdir, path, mkdir
import json

LOADER_INSTANCE = None

class Loader:
def __init__(self):
self.busy = False
Expand Down Expand Up @@ -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
return Result(True)
33 changes: 19 additions & 14 deletions css_mappings.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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)}")
Expand All @@ -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))
asyncio.get_event_loop().create_task(__every(60, __fetch_class_mappings, css_translations_path, loader))
1 change: 0 additions & 1 deletion css_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
14 changes: 7 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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()):
Expand All @@ -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__':
Expand Down

0 comments on commit 9ff5194

Please sign in to comment.