diff options
| author | marios8543 <marios8543@gmail.com> | 2023-10-18 14:45:36 +0300 |
|---|---|---|
| committer | marios8543 <marios8543@gmail.com> | 2023-10-31 23:18:31 +0200 |
| commit | 268311c4821e2df93c99a257a6992e50d49fc788 (patch) | |
| tree | 20f3206ddac2a793e4be5b07f557d651a759d0f6 /backend/src/plugin/sandboxed_plugin.py | |
| parent | ed0f851d4d5c81b642bc30b7d609ec2bbb9003f7 (diff) | |
| download | decky-loader-268311c4821e2df93c99a257a6992e50d49fc788.tar.gz decky-loader-268311c4821e2df93c99a257a6992e50d49fc788.zip | |
Add message emit mechanism
Diffstat (limited to 'backend/src/plugin/sandboxed_plugin.py')
| -rw-r--r-- | backend/src/plugin/sandboxed_plugin.py | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/backend/src/plugin/sandboxed_plugin.py b/backend/src/plugin/sandboxed_plugin.py index fea02ac2..e4b5f7df 100644 --- a/backend/src/plugin/sandboxed_plugin.py +++ b/backend/src/plugin/sandboxed_plugin.py @@ -3,7 +3,6 @@ from signal import SIGINT, signal from importlib.util import module_from_spec, spec_from_file_location from json import dumps, loads from logging import getLogger -import multiprocessing from sys import exit, path as syspath from traceback import format_exc from asyncio import (get_event_loop, new_event_loop, @@ -15,7 +14,7 @@ from ..localplatform.localplatform import setgid, setuid, get_username, get_home from ..customtypes import UserType from .. import helpers -from typing import List +from typing import Any, Dict, List class SandboxedPlugin: def __init__(self, @@ -38,7 +37,9 @@ class SandboxedPlugin: self.log = getLogger("plugin") - def _init(self, socket: LocalSocket): + def initialize(self, socket: LocalSocket): + self._socket = socket + try: signal(SIGINT, lambda s, f: exit(0)) @@ -84,6 +85,9 @@ class SandboxedPlugin: spec.loader.exec_module(module) self.Plugin = module.Plugin + setattr(self.Plugin, "emit_message", self.emit_message) + #TODO: Find how to put emit_message on global namespace so it doesn't pollute Plugin + if hasattr(self.Plugin, "_migration"): get_event_loop().run_until_complete(self.Plugin._migration(self.Plugin)) if hasattr(self.Plugin, "_main"): @@ -118,7 +122,6 @@ class SandboxedPlugin: get_event_loop().close() raise Exception("Closing message listener") - # TODO there is definitely a better way to type this d: SocketResponseDict = {"res": None, "success": True, "id": data["id"]} try: d["res"] = await getattr(self.Plugin, data["method"])(self.Plugin, **data["args"]) @@ -128,9 +131,8 @@ class SandboxedPlugin: finally: return dumps(d, ensure_ascii=False) - - def start(self, socket: LocalSocket): - if self.passive: - return self - multiprocessing.Process(target=self._init, args=[socket]).start() - return self
\ No newline at end of file + async def emit_message(self, message: Dict[Any, Any]): + await self._socket.write_single_line(dumps({ + "id": 0, + "payload": message + }))
\ No newline at end of file |
