diff --git a/.github/workflows/build-win.yml b/.github/workflows/build-win.yml index 16577da36..a0fd13b98 100644 --- a/.github/workflows/build-win.yml +++ b/.github/workflows/build-win.yml @@ -43,10 +43,10 @@ jobs: run: pnpm run build - name: Build Python Backend 🛠️ - run: pyinstaller --noconfirm --onefile --name "PluginLoader" --add-data "./backend/static;/backend/static" --add-data "./backend/locales;/backend/locales" --add-data "./backend/legacy;/backend/legacy" --add-data "./plugin;/plugin" --hidden-import=sqlite3 ./main.py + run: pyinstaller --noconfirm --onefile --name "PluginLoader" --add-data "./backend/src/static;/backend/src/static" --add-data "./backend/src/locales;/backend/src/locales" --add-data "./backend/src/legacy;/backend/src/legacy" --add-data "./plugin;/plugin" --hidden-import=sqlite3 ./backend/main.py - name: Build Python Backend (noconsole) 🛠️ - run: pyinstaller --noconfirm --noconsole --onefile --name "PluginLoader_noconsole" --add-data "./backend/static;/backend/static" --add-data "./backend/locales;/backend/locales" --add-data "./backend/legacy;/backend/legacy" --add-data "./plugin;/plugin" --hidden-import=sqlite3 ./main.py + run: pyinstaller --noconfirm --noconsole --onefile --name "PluginLoader_noconsole" --add-data "./backend/src/static;/backend/src/static" --add-data "./backend/src/locales;/backend/src/locales" --add-data "./backend/src/legacy;/backend/src/legacy" --add-data "./plugin;/plugin" --hidden-import=sqlite3 ./backend/main.py - name: Upload package artifact ⬆️ uses: actions/upload-artifact@v3 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3c09beb94..70b68f964 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -86,7 +86,7 @@ jobs: run: pnpm run build - name: Build Python Backend 🛠️ - run: pyinstaller --noconfirm --onefile --name "PluginLoader" --add-data ./backend/static:/backend/static --add-data ./backend/locales:/backend/locales --add-data ./backend/legacy:/backend/legacy --add-data ./plugin:/plugin --hidden-import=sqlite3 ./main.py + run: pyinstaller --noconfirm --onefile --name "PluginLoader" --add-data ./backend/src/static:/backend/src/static --add-data ./backend/src/locales:/backend/src/locales --add-data ./backend/src/legacy:/backend/src/legacy --add-data ./plugin:/plugin --hidden-import=sqlite3 ./backend/main.py - name: Upload package artifact ⬆️ if: ${{ !env.ACT }} diff --git a/backend/main.py b/backend/main.py index 793d000c2..fa386d0c2 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,192 +1,4 @@ -# Change PyInstaller files permissions -import sys -from typing import Dict -from .localplatform import (chmod, chown, service_stop, service_start, - ON_WINDOWS, get_log_level, get_live_reload, - get_server_port, get_server_host, get_chown_plugin_path, - get_privileged_path) -if hasattr(sys, '_MEIPASS'): - chmod(sys._MEIPASS, 755) # type: ignore -# Full imports -from asyncio import AbstractEventLoop, new_event_loop, set_event_loop, sleep -from logging import basicConfig, getLogger -from os import path -from traceback import format_exc -import multiprocessing - -import aiohttp_cors # type: ignore -# Partial imports -from aiohttp import client_exceptions -from aiohttp.web import Application, Response, Request, get, run_app, static # type: ignore -from aiohttp_jinja2 import setup as jinja_setup - -# local modules -from .browser import PluginBrowser -from .helpers import (REMOTE_DEBUGGER_UNIT, csrf_middleware, get_csrf_token, - mkdir_as_user, get_system_pythonpaths, get_effective_user_id) - -from .injector import get_gamepadui_tab, Tab, close_old_tabs -from .loader import Loader -from .settings import SettingsManager -from .updater import Updater -from .utilities import Utilities -from .customtypes import UserType - - -basicConfig( - level=get_log_level(), - format="[%(module)s][%(levelname)s]: %(message)s" -) - -logger = getLogger("Main") -plugin_path = path.join(get_privileged_path(), "plugins") - -def chown_plugin_dir(): - if not path.exists(plugin_path): # For safety, create the folder before attempting to do anything with it - mkdir_as_user(plugin_path) - - if not chown(plugin_path, UserType.HOST_USER) or not chmod(plugin_path, 555): - logger.error(f"chown/chmod exited with a non-zero exit code") - -if get_chown_plugin_path() == True: - chown_plugin_dir() - -class PluginManager: - def __init__(self, loop: AbstractEventLoop) -> None: - self.loop = loop - self.web_app = Application() - self.web_app.middlewares.append(csrf_middleware) - self.cors = aiohttp_cors.setup(self.web_app, defaults={ - "https://steamloopback.host": aiohttp_cors.ResourceOptions( - expose_headers="*", - allow_headers="*", - allow_credentials=True - ) - }) - self.plugin_loader = Loader(self, plugin_path, self.loop, get_live_reload()) - self.settings = SettingsManager("loader", path.join(get_privileged_path(), "settings")) - self.plugin_browser = PluginBrowser(plugin_path, self.plugin_loader.plugins, self.plugin_loader, self.settings) - self.utilities = Utilities(self) - self.updater = Updater(self) - - jinja_setup(self.web_app) - - async def startup(_: Application): - if self.settings.getSetting("cef_forward", False): - self.loop.create_task(service_start(REMOTE_DEBUGGER_UNIT)) - else: - self.loop.create_task(service_stop(REMOTE_DEBUGGER_UNIT)) - self.loop.create_task(self.loader_reinjector()) - self.loop.create_task(self.load_plugins()) - - self.web_app.on_startup.append(startup) - - self.loop.set_exception_handler(self.exception_handler) - self.web_app.add_routes([get("/auth/token", self.get_auth_token)]) - - for route in list(self.web_app.router.routes()): - self.cors.add(route) # type: ignore - self.web_app.add_routes([static("/static", path.join(path.dirname(__file__), 'static'))]) - self.web_app.add_routes([static("/legacy", path.join(path.dirname(__file__), 'legacy'))]) - - def exception_handler(self, loop: AbstractEventLoop, context: Dict[str, str]): - if context["message"] == "Unclosed connection": - return - loop.default_exception_handler(context) - - async def get_auth_token(self, request: Request): - return Response(text=get_csrf_token()) - - async def load_plugins(self): - # await self.wait_for_server() - logger.debug("Loading plugins") - self.plugin_loader.import_plugins() - # await inject_to_tab("SP", "window.syncDeckyPlugins();") - if self.settings.getSetting("pluginOrder", None) == None: - self.settings.setSetting("pluginOrder", list(self.plugin_loader.plugins.keys())) - logger.debug("Did not find pluginOrder setting, set it to default") - - async def loader_reinjector(self): - while True: - tab = None - nf = False - dc = False - while not tab: - try: - tab = await get_gamepadui_tab() - except (client_exceptions.ClientConnectorError, client_exceptions.ServerDisconnectedError): - if not dc: - logger.debug("Couldn't connect to debugger, waiting...") - dc = True - pass - except ValueError: - if not nf: - logger.debug("Couldn't find GamepadUI tab, waiting...") - nf = True - pass - if not tab: - await sleep(5) - await tab.open_websocket() - await tab.enable() - await self.inject_javascript(tab, True) - try: - async for msg in tab.listen_for_message(): - # this gets spammed a lot - if msg.get("method", None) != "Page.navigatedWithinDocument": - logger.debug("Page event: " + str(msg.get("method", None))) - if msg.get("method", None) == "Page.domContentEventFired": - if not await tab.has_global_var("deckyHasLoaded", False): - await self.inject_javascript(tab) - if msg.get("method", None) == "Inspector.detached": - logger.info("CEF has requested that we detach.") - await tab.close_websocket() - break - # If this is a forceful disconnect the loop will just stop without any failure message. In this case, injector.py will handle this for us so we don't need to close the socket. - # This is because of https://github.com/aio-libs/aiohttp/blob/3ee7091b40a1bc58a8d7846e7878a77640e96996/aiohttp/client_ws.py#L321 - logger.info("CEF has disconnected...") - # At this point the loop starts again and we connect to the freshly started Steam client once it is ready. - except Exception: - logger.error("Exception while reading page events " + format_exc()) - await tab.close_websocket() - pass - # while True: - # await sleep(5) - # if not await tab.has_global_var("deckyHasLoaded", False): - # logger.info("Plugin loader isn't present in Steam anymore, reinjecting...") - # await self.inject_javascript(tab) - - async def inject_javascript(self, tab: Tab, first: bool=False, request: Request|None=None): - logger.info("Loading Decky frontend!") - try: - if first: - if await tab.has_global_var("deckyHasLoaded", False): - await close_old_tabs() - await tab.evaluate_js("try{if (window.deckyHasLoaded){setTimeout(() => location.reload(), 100)}else{window.deckyHasLoaded = true;(async()=>{try{while(!window.SP_REACT){await new Promise(r => setTimeout(r, 10))};await import('http://localhost:1337/frontend/index.js')}catch(e){console.error(e)};})();}}catch(e){console.error(e)}", False, False, False) - except: - logger.info("Failed to inject JavaScript into tab\n" + format_exc()) - pass - - def run(self): - return run_app(self.web_app, host=get_server_host(), port=get_server_port(), loop=self.loop, access_log=None) - -def main(): - if ON_WINDOWS: - # Fix windows/flask not recognising that .js means 'application/javascript' - import mimetypes - mimetypes.add_type('application/javascript', '.js') - - # Required for multiprocessing support in frozen files - multiprocessing.freeze_support() - else: - if get_effective_user_id() != 0: - logger.warning(f"decky is running as an unprivileged user, this is not officially supported and may cause issues") - - # Append the loader's plugin path to the recognized python paths - sys.path.append(path.join(path.dirname(__file__), "plugin")) - - # Append the system and user python paths - sys.path.extend(get_system_pythonpaths()) - - loop = new_event_loop() - set_event_loop(loop) - PluginManager(loop).run() +# This file is needed to make the relative imports in backend/ work properly. +if __name__ == "__main__": + from src.main import main + main() diff --git a/backend/browser.py b/backend/src/browser.py similarity index 100% rename from backend/browser.py rename to backend/src/browser.py diff --git a/backend/customtypes.py b/backend/src/customtypes.py similarity index 100% rename from backend/customtypes.py rename to backend/src/customtypes.py diff --git a/backend/helpers.py b/backend/src/helpers.py similarity index 100% rename from backend/helpers.py rename to backend/src/helpers.py diff --git a/backend/injector.py b/backend/src/injector.py similarity index 100% rename from backend/injector.py rename to backend/src/injector.py diff --git a/backend/legacy/library.js b/backend/src/legacy/library.js similarity index 100% rename from backend/legacy/library.js rename to backend/src/legacy/library.js diff --git a/backend/loader.py b/backend/src/loader.py similarity index 100% rename from backend/loader.py rename to backend/src/loader.py diff --git a/backend/locales/bg-BG.json b/backend/src/locales/bg-BG.json similarity index 100% rename from backend/locales/bg-BG.json rename to backend/src/locales/bg-BG.json diff --git a/backend/locales/cs-CZ.json b/backend/src/locales/cs-CZ.json similarity index 100% rename from backend/locales/cs-CZ.json rename to backend/src/locales/cs-CZ.json diff --git a/backend/locales/de-DE.json b/backend/src/locales/de-DE.json similarity index 100% rename from backend/locales/de-DE.json rename to backend/src/locales/de-DE.json diff --git a/backend/locales/el-GR.json b/backend/src/locales/el-GR.json similarity index 100% rename from backend/locales/el-GR.json rename to backend/src/locales/el-GR.json diff --git a/backend/locales/en-US.json b/backend/src/locales/en-US.json similarity index 100% rename from backend/locales/en-US.json rename to backend/src/locales/en-US.json diff --git a/backend/locales/es-ES.json b/backend/src/locales/es-ES.json similarity index 100% rename from backend/locales/es-ES.json rename to backend/src/locales/es-ES.json diff --git a/backend/locales/fi-FI.json b/backend/src/locales/fi-FI.json similarity index 100% rename from backend/locales/fi-FI.json rename to backend/src/locales/fi-FI.json diff --git a/backend/locales/fr-FR.json b/backend/src/locales/fr-FR.json similarity index 100% rename from backend/locales/fr-FR.json rename to backend/src/locales/fr-FR.json diff --git a/backend/locales/it-IT.json b/backend/src/locales/it-IT.json similarity index 100% rename from backend/locales/it-IT.json rename to backend/src/locales/it-IT.json diff --git a/backend/locales/ko-KR.json b/backend/src/locales/ko-KR.json similarity index 100% rename from backend/locales/ko-KR.json rename to backend/src/locales/ko-KR.json diff --git a/backend/locales/nl-NL.json b/backend/src/locales/nl-NL.json similarity index 100% rename from backend/locales/nl-NL.json rename to backend/src/locales/nl-NL.json diff --git a/backend/locales/pl-PL.json b/backend/src/locales/pl-PL.json similarity index 100% rename from backend/locales/pl-PL.json rename to backend/src/locales/pl-PL.json diff --git a/backend/locales/pt-BR.json b/backend/src/locales/pt-BR.json similarity index 100% rename from backend/locales/pt-BR.json rename to backend/src/locales/pt-BR.json diff --git a/backend/locales/pt-PT.json b/backend/src/locales/pt-PT.json similarity index 100% rename from backend/locales/pt-PT.json rename to backend/src/locales/pt-PT.json diff --git a/backend/locales/ru-RU.json b/backend/src/locales/ru-RU.json similarity index 100% rename from backend/locales/ru-RU.json rename to backend/src/locales/ru-RU.json diff --git a/backend/locales/sq-AL.json b/backend/src/locales/sq-AL.json similarity index 100% rename from backend/locales/sq-AL.json rename to backend/src/locales/sq-AL.json diff --git a/backend/locales/uk-UA.json b/backend/src/locales/uk-UA.json similarity index 100% rename from backend/locales/uk-UA.json rename to backend/src/locales/uk-UA.json diff --git a/backend/locales/zh-CN.json b/backend/src/locales/zh-CN.json similarity index 100% rename from backend/locales/zh-CN.json rename to backend/src/locales/zh-CN.json diff --git a/backend/locales/zh-TW.json b/backend/src/locales/zh-TW.json similarity index 100% rename from backend/locales/zh-TW.json rename to backend/src/locales/zh-TW.json diff --git a/backend/localplatform.py b/backend/src/localplatform.py similarity index 100% rename from backend/localplatform.py rename to backend/src/localplatform.py diff --git a/backend/localplatformlinux.py b/backend/src/localplatformlinux.py similarity index 100% rename from backend/localplatformlinux.py rename to backend/src/localplatformlinux.py diff --git a/backend/localplatformwin.py b/backend/src/localplatformwin.py similarity index 100% rename from backend/localplatformwin.py rename to backend/src/localplatformwin.py diff --git a/backend/localsocket.py b/backend/src/localsocket.py similarity index 100% rename from backend/localsocket.py rename to backend/src/localsocket.py diff --git a/backend/src/main.py b/backend/src/main.py new file mode 100644 index 000000000..793d000c2 --- /dev/null +++ b/backend/src/main.py @@ -0,0 +1,192 @@ +# Change PyInstaller files permissions +import sys +from typing import Dict +from .localplatform import (chmod, chown, service_stop, service_start, + ON_WINDOWS, get_log_level, get_live_reload, + get_server_port, get_server_host, get_chown_plugin_path, + get_privileged_path) +if hasattr(sys, '_MEIPASS'): + chmod(sys._MEIPASS, 755) # type: ignore +# Full imports +from asyncio import AbstractEventLoop, new_event_loop, set_event_loop, sleep +from logging import basicConfig, getLogger +from os import path +from traceback import format_exc +import multiprocessing + +import aiohttp_cors # type: ignore +# Partial imports +from aiohttp import client_exceptions +from aiohttp.web import Application, Response, Request, get, run_app, static # type: ignore +from aiohttp_jinja2 import setup as jinja_setup + +# local modules +from .browser import PluginBrowser +from .helpers import (REMOTE_DEBUGGER_UNIT, csrf_middleware, get_csrf_token, + mkdir_as_user, get_system_pythonpaths, get_effective_user_id) + +from .injector import get_gamepadui_tab, Tab, close_old_tabs +from .loader import Loader +from .settings import SettingsManager +from .updater import Updater +from .utilities import Utilities +from .customtypes import UserType + + +basicConfig( + level=get_log_level(), + format="[%(module)s][%(levelname)s]: %(message)s" +) + +logger = getLogger("Main") +plugin_path = path.join(get_privileged_path(), "plugins") + +def chown_plugin_dir(): + if not path.exists(plugin_path): # For safety, create the folder before attempting to do anything with it + mkdir_as_user(plugin_path) + + if not chown(plugin_path, UserType.HOST_USER) or not chmod(plugin_path, 555): + logger.error(f"chown/chmod exited with a non-zero exit code") + +if get_chown_plugin_path() == True: + chown_plugin_dir() + +class PluginManager: + def __init__(self, loop: AbstractEventLoop) -> None: + self.loop = loop + self.web_app = Application() + self.web_app.middlewares.append(csrf_middleware) + self.cors = aiohttp_cors.setup(self.web_app, defaults={ + "https://steamloopback.host": aiohttp_cors.ResourceOptions( + expose_headers="*", + allow_headers="*", + allow_credentials=True + ) + }) + self.plugin_loader = Loader(self, plugin_path, self.loop, get_live_reload()) + self.settings = SettingsManager("loader", path.join(get_privileged_path(), "settings")) + self.plugin_browser = PluginBrowser(plugin_path, self.plugin_loader.plugins, self.plugin_loader, self.settings) + self.utilities = Utilities(self) + self.updater = Updater(self) + + jinja_setup(self.web_app) + + async def startup(_: Application): + if self.settings.getSetting("cef_forward", False): + self.loop.create_task(service_start(REMOTE_DEBUGGER_UNIT)) + else: + self.loop.create_task(service_stop(REMOTE_DEBUGGER_UNIT)) + self.loop.create_task(self.loader_reinjector()) + self.loop.create_task(self.load_plugins()) + + self.web_app.on_startup.append(startup) + + self.loop.set_exception_handler(self.exception_handler) + self.web_app.add_routes([get("/auth/token", self.get_auth_token)]) + + for route in list(self.web_app.router.routes()): + self.cors.add(route) # type: ignore + self.web_app.add_routes([static("/static", path.join(path.dirname(__file__), 'static'))]) + self.web_app.add_routes([static("/legacy", path.join(path.dirname(__file__), 'legacy'))]) + + def exception_handler(self, loop: AbstractEventLoop, context: Dict[str, str]): + if context["message"] == "Unclosed connection": + return + loop.default_exception_handler(context) + + async def get_auth_token(self, request: Request): + return Response(text=get_csrf_token()) + + async def load_plugins(self): + # await self.wait_for_server() + logger.debug("Loading plugins") + self.plugin_loader.import_plugins() + # await inject_to_tab("SP", "window.syncDeckyPlugins();") + if self.settings.getSetting("pluginOrder", None) == None: + self.settings.setSetting("pluginOrder", list(self.plugin_loader.plugins.keys())) + logger.debug("Did not find pluginOrder setting, set it to default") + + async def loader_reinjector(self): + while True: + tab = None + nf = False + dc = False + while not tab: + try: + tab = await get_gamepadui_tab() + except (client_exceptions.ClientConnectorError, client_exceptions.ServerDisconnectedError): + if not dc: + logger.debug("Couldn't connect to debugger, waiting...") + dc = True + pass + except ValueError: + if not nf: + logger.debug("Couldn't find GamepadUI tab, waiting...") + nf = True + pass + if not tab: + await sleep(5) + await tab.open_websocket() + await tab.enable() + await self.inject_javascript(tab, True) + try: + async for msg in tab.listen_for_message(): + # this gets spammed a lot + if msg.get("method", None) != "Page.navigatedWithinDocument": + logger.debug("Page event: " + str(msg.get("method", None))) + if msg.get("method", None) == "Page.domContentEventFired": + if not await tab.has_global_var("deckyHasLoaded", False): + await self.inject_javascript(tab) + if msg.get("method", None) == "Inspector.detached": + logger.info("CEF has requested that we detach.") + await tab.close_websocket() + break + # If this is a forceful disconnect the loop will just stop without any failure message. In this case, injector.py will handle this for us so we don't need to close the socket. + # This is because of https://github.com/aio-libs/aiohttp/blob/3ee7091b40a1bc58a8d7846e7878a77640e96996/aiohttp/client_ws.py#L321 + logger.info("CEF has disconnected...") + # At this point the loop starts again and we connect to the freshly started Steam client once it is ready. + except Exception: + logger.error("Exception while reading page events " + format_exc()) + await tab.close_websocket() + pass + # while True: + # await sleep(5) + # if not await tab.has_global_var("deckyHasLoaded", False): + # logger.info("Plugin loader isn't present in Steam anymore, reinjecting...") + # await self.inject_javascript(tab) + + async def inject_javascript(self, tab: Tab, first: bool=False, request: Request|None=None): + logger.info("Loading Decky frontend!") + try: + if first: + if await tab.has_global_var("deckyHasLoaded", False): + await close_old_tabs() + await tab.evaluate_js("try{if (window.deckyHasLoaded){setTimeout(() => location.reload(), 100)}else{window.deckyHasLoaded = true;(async()=>{try{while(!window.SP_REACT){await new Promise(r => setTimeout(r, 10))};await import('http://localhost:1337/frontend/index.js')}catch(e){console.error(e)};})();}}catch(e){console.error(e)}", False, False, False) + except: + logger.info("Failed to inject JavaScript into tab\n" + format_exc()) + pass + + def run(self): + return run_app(self.web_app, host=get_server_host(), port=get_server_port(), loop=self.loop, access_log=None) + +def main(): + if ON_WINDOWS: + # Fix windows/flask not recognising that .js means 'application/javascript' + import mimetypes + mimetypes.add_type('application/javascript', '.js') + + # Required for multiprocessing support in frozen files + multiprocessing.freeze_support() + else: + if get_effective_user_id() != 0: + logger.warning(f"decky is running as an unprivileged user, this is not officially supported and may cause issues") + + # Append the loader's plugin path to the recognized python paths + sys.path.append(path.join(path.dirname(__file__), "plugin")) + + # Append the system and user python paths + sys.path.extend(get_system_pythonpaths()) + + loop = new_event_loop() + set_event_loop(loop) + PluginManager(loop).run() diff --git a/backend/plugin.py b/backend/src/plugin.py similarity index 100% rename from backend/plugin.py rename to backend/src/plugin.py diff --git a/backend/settings.py b/backend/src/settings.py similarity index 100% rename from backend/settings.py rename to backend/src/settings.py diff --git a/backend/updater.py b/backend/src/updater.py similarity index 100% rename from backend/updater.py rename to backend/src/updater.py diff --git a/backend/utilities.py b/backend/src/utilities.py similarity index 100% rename from backend/utilities.py rename to backend/src/utilities.py diff --git a/main.py b/main.py deleted file mode 100644 index c2b990893..000000000 --- a/main.py +++ /dev/null @@ -1,4 +0,0 @@ -# This file is needed to make the relative imports in backend/ work properly. -if __name__ == "__main__": - from backend.main import main - main()