diff options
| author | AAGaming <aa@mail.catvibers.me> | 2022-09-18 14:49:32 -0400 |
|---|---|---|
| committer | AAGaming <aa@mail.catvibers.me> | 2022-09-18 14:49:32 -0400 |
| commit | 7716c73014a645d3742c24eaa7c7b1d54e081363 (patch) | |
| tree | 89120cabfb6b1dca621fa7e319c278a499169acf /backend | |
| parent | 8829adc5b6361cc7ab972ea2f43ab07db206dc93 (diff) | |
| download | decky-loader-7716c73014a645d3742c24eaa7c7b1d54e081363.tar.gz decky-loader-7716c73014a645d3742c24eaa7c7b1d54e081363.zip | |
fix plugin loading after install, move updater reloads to loader
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/browser.py | 15 | ||||
| -rw-r--r-- | backend/loader.py | 7 | ||||
| -rw-r--r-- | backend/main.py | 7 |
3 files changed, 23 insertions, 6 deletions
diff --git a/backend/browser.py b/backend/browser.py index 83c68d47..79df4625 100644 --- a/backend/browser.py +++ b/backend/browser.py @@ -28,9 +28,10 @@ class PluginInstallContext: self.hash = hash class PluginBrowser: - def __init__(self, plugin_path, plugins) -> None: + def __init__(self, plugin_path, plugins, loader) -> None: self.plugin_path = plugin_path self.plugins = plugins + self.loader = loader self.install_requests = {} def _unzip_to_plugin_dir(self, zip, name, hash): @@ -58,6 +59,8 @@ class PluginBrowser: logger.debug(f"skipping {folder}") async def uninstall_plugin(self, name): + if self.loader.watcher: + self.loader.watcher.disabled = True tab = await get_tab("SP") try: logger.info("uninstalling " + name) @@ -74,8 +77,12 @@ class PluginBrowser: except Exception as e: logger.error(f"Plugin {name} in {self.find_plugin_folder(name)} was not uninstalled") logger.error(f"Error at %s", exc_info=e) + if self.loader.watcher: + self.loader.watcher.disabled = False async def _install(self, artifact, name, version, hash): + if self.loader.watcher: + self.loader.watcher.disabled = True try: await self.uninstall_plugin(name) except: @@ -93,11 +100,15 @@ class PluginBrowser: ret = self._unzip_to_plugin_dir(res_zip, name, hash) if ret: logger.info(f"Installed {name} (Version: {version})") - await inject_to_tab("SP", "window.syncDeckyPlugins()") + plugin_dir = self.find_plugin_folder(name) + self.loader.import_plugin(path.join(plugin_dir, "main.py"), plugin_dir) + # await inject_to_tab("SP", "window.syncDeckyPlugins()") else: self.log.fatal(f"SHA-256 Mismatch!!!! {name} (Version: {version})") else: logger.fatal(f"Could not fetch from URL. {await res.text()}") + if self.loader.watcher: + self.loader.watcher.disabled = False async def request_plugin_install(self, artifact, name, version, hash): request_id = str(time()) diff --git a/backend/loader.py b/backend/loader.py index 9c2b33f4..b4d81368 100644 --- a/backend/loader.py +++ b/backend/loader.py @@ -25,8 +25,11 @@ class FileChangeHandler(RegexMatchingEventHandler): self.logger = getLogger("file-watcher") self.plugin_path = plugin_path self.queue = queue + self.disabled = False def maybe_reload(self, src_path): + if self.disabled: + return plugin_dir = Path(path.relpath(src_path, self.plugin_path)).parts[0] if exists(path.join(self.plugin_path, plugin_dir, "plugin.json")): self.queue.put_nowait((path.join(self.plugin_path, plugin_dir, "main.py"), plugin_dir, True)) @@ -66,11 +69,13 @@ class Loader: self.plugin_path = plugin_path self.logger.info(f"plugin_path: {self.plugin_path}") self.plugins = {} + self.watcher = None if live_reload: self.reload_queue = Queue() self.observer = Observer() - self.observer.schedule(FileChangeHandler(self.reload_queue, plugin_path), self.plugin_path, recursive=True) + self.watcher = FileChangeHandler(self.reload_queue, plugin_path) + self.observer.schedule(self.watcher, self.plugin_path, recursive=True) self.observer.start() self.loop.create_task(self.handle_reloads()) diff --git a/backend/main.py b/backend/main.py index 83032be3..aa38d8ac 100644 --- a/backend/main.py +++ b/backend/main.py @@ -4,6 +4,7 @@ from json import dumps, loads from logging import DEBUG, INFO, basicConfig, getLogger from os import getenv, path from subprocess import call +from traceback import format_exc import aiohttp_cors # Partial imports @@ -70,7 +71,7 @@ class PluginManager: ) }) self.plugin_loader = Loader(self.web_app, CONFIG["plugin_path"], self.loop, CONFIG["live_reload"]) - self.plugin_browser = PluginBrowser(CONFIG["plugin_path"], self.plugin_loader.plugins) + self.plugin_browser = PluginBrowser(CONFIG["plugin_path"], self.plugin_loader.plugins, self.plugin_loader) self.settings = SettingsManager("loader", path.join(HOMEBREW_PATH, "settings")) self.utilities = Utilities(self) self.updater = Updater(self) @@ -123,9 +124,9 @@ class PluginManager: async def inject_javascript(self, request=None): try: - await inject_to_tab("SP", "try{window.deckyHasLoaded = true;(async()=>{while(!window.SP_REACT){await new Promise(r => setTimeout(r, 10))};await import('http://localhost:1337/frontend/index.js')})();}catch(e){console.error(e)}", True) + await inject_to_tab("SP", "try{if (window.deckyHasLoaded) location.reload();window.deckyHasLoaded = true;(async()=>{while(!window.SP_REACT){await new Promise(r => setTimeout(r, 10))};await import('http://localhost:1337/frontend/index.js')})();}catch(e){console.error(e)}", True) except: - logger.info("Failed to inject JavaScript into tab") + logger.info("Failed to inject JavaScript into tab\n" + format_exc()) pass def run(self): |
