summaryrefslogtreecommitdiff
path: root/backend/src/plugin/plugin.py
diff options
context:
space:
mode:
authormarios8543 <marios8543@gmail.com>2023-10-17 23:52:18 +0300
committermarios8543 <marios8543@gmail.com>2023-10-31 23:18:23 +0200
commit321242b0d9bf737dff931be8cedd6a1590a3be5a (patch)
tree6e3fb7b7ecac29f3545875c8a69ac585bda9e472 /backend/src/plugin/plugin.py
parent949c5e73c496c3b467f7084ffffb466f98f906bc (diff)
downloaddecky-loader-321242b0d9bf737dff931be8cedd6a1590a3be5a.tar.gz
decky-loader-321242b0d9bf737dff931be8cedd6a1590a3be5a.zip
Experimental support for async method calls
Diffstat (limited to 'backend/src/plugin/plugin.py')
-rw-r--r--backend/src/plugin/plugin.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/backend/src/plugin/plugin.py b/backend/src/plugin/plugin.py
new file mode 100644
index 00000000..b7d0a44a
--- /dev/null
+++ b/backend/src/plugin/plugin.py
@@ -0,0 +1,55 @@
+from json import dumps, load, loads
+from logging import getLogger
+from os import path
+
+from .sandboxed_plugin import SandboxedPlugin
+from .method_call_request import MethodCallRequest
+from ..localplatform.localsocket import LocalSocket
+
+from typing import Any, Dict
+
+class PluginWrapper:
+ def __init__(self, file: str, plugin_directory: str, plugin_path: str) -> None:
+ self.file = file
+ self.plugin_path = plugin_path
+ self.plugin_directory = plugin_directory
+
+ self.version = None
+
+ json = load(open(path.join(plugin_path, plugin_directory, "plugin.json"), "r", encoding="utf-8"))
+ if path.isfile(path.join(plugin_path, plugin_directory, "package.json")):
+ package_json = load(open(path.join(plugin_path, plugin_directory, "package.json"), "r", encoding="utf-8"))
+ self.version = package_json["version"]
+
+ self.name = json["name"]
+ self.author = json["author"]
+ self.flags = json["flags"]
+ self.passive = not path.isfile(self.file)
+
+ self.log = getLogger("plugin")
+ self.method_call_requests: Dict[str, MethodCallRequest] = {}
+ self.sandboxed_plugin = SandboxedPlugin(self.name, self.passive, self.flags, self.file, self.plugin_directory, self.plugin_path, self.version, self.author)
+ #TODO: Maybe somehow make LocalSocket not require on_new_message to make this more clear
+ self.socket = LocalSocket(self.sandboxed_plugin.on_new_message)
+ self.sandboxed_plugin.start(self.socket)
+
+ def __str__(self) -> str:
+ return self.name
+
+ async def response_listener(self):
+ while True:
+ line = await self.socket.read_single_line()
+ if line != None:
+ res = loads(line)
+ self.method_call_requests.pop(res["id"]).set_result(res)
+
+ async def execute_method(self, method_name: str, kwargs: Dict[Any, Any]):
+ if self.passive:
+ raise RuntimeError("This plugin is passive (aka does not implement main.py)")
+
+ request = MethodCallRequest()
+ await self.socket.get_socket_connection()
+ await self.socket.write_single_line(dumps({ "method": method_name, "args": kwargs, "id": request.id }, ensure_ascii=False))
+ self.method_call_requests[request.id] = request
+
+ return await request.wait_for_result() \ No newline at end of file