Skip to content

Commit

Permalink
PoC fix for beta
Browse files Browse the repository at this point in the history
  • Loading branch information
suchmememanyskill committed Mar 10, 2024
1 parent 0097f83 commit d0e151b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
50 changes: 46 additions & 4 deletions css_inject.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,51 @@
import json
import re
import os
import requests
from typing import List
from css_utils import Result, Log
from css_utils import Result, Log, store_read, get_theme_path
from css_browserhook import BrowserTabHook as CssTab, inject, remove
import uuid

CLASS_MAPPINGS = {}

def initialize_class_mappings():
css_translations_path = os.path.join(get_theme_path(), "css_translations.json")
setting = store_read("beta_translations")
css_translations_url = "https://api.deckthemes.com/beta.json" if (setting == "1" or setting == "true") else "https://api.deckthemes.com/stable.json"

timeout = 8
while timeout > 0:
try:
response = requests.get(css_translations_url, timeout=2)
if response.status_code == 200:
with open(css_translations_path, "w", encoding="utf-8") as fp:
json.dump(response.json(), fp)
break
except:
pass
finally:
timeout -= 1

if not os.path.exists(css_translations_path):
Log("Failed to get css translations from server or from local file")
return

with open(css_translations_path, "r", encoding="utf-8") as fp:
data : dict = json.load(fp)

# 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

ALL_INJECTS = []

def helper_get_tab_from_list(tab_list : List[str], cssTab : CssTab) -> str|None:
for x in tab_list:
if cssTab.compare(x):
return x

return None

class Inject:
Expand All @@ -28,8 +64,14 @@ async def load(self) -> Result:
with open(self.cssPath, "r") as fp:
self.css = fp.read()

split_css = re.split(r"(\.[_a-zA-Z]+[_a-zA-Z0-9-]*)", self.css)

for x in range(len(split_css)):
if split_css[x].startswith(".") and split_css[x][1:] in CLASS_MAPPINGS:
split_css[x] = "." + CLASS_MAPPINGS[split_css[x][1:]]

self.css = ("".join(split_css)).replace("\\", "\\\\").replace("`", "\\`")
Log(f"Loaded css at {self.cssPath}")
self.css = self.css.replace("\\", "\\\\").replace("`", "\\`")

return Result(True)
except Exception as e:
Expand Down
3 changes: 2 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
sys.path.append(os.path.dirname(__file__))

from css_utils import Log, create_steam_symlink, Result, get_theme_path, store_read as util_store_read, store_write as util_store_write, store_or_file_config
from css_inject import ALL_INJECTS
from css_inject import ALL_INJECTS, initialize_class_mappings
from css_theme import CSS_LOADER_VER
from css_remoteinstall import install

Expand Down Expand Up @@ -158,6 +158,7 @@ async def _main(self):
self.server_loaded = False

Log("Initializing css loader...")
initialize_class_mappings()
Log(f"Max supported manifest version: {CSS_LOADER_VER}")

create_steam_symlink()
Expand Down

0 comments on commit d0e151b

Please sign in to comment.