mirror of
https://github.com/SteamDeckHomebrew/decky-loader.git
synced 2026-06-17 00:37:49 +00:00
First draft of backend independent plugins
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import os
|
||||
from asyncio import get_event_loop, sleep, subprocess
|
||||
from posixpath import join
|
||||
from tempfile import mkdtemp
|
||||
|
||||
from plugin_protocol import PluginProtocolServer
|
||||
|
||||
|
||||
class BinaryPlugin:
|
||||
def __init__(self, plugin_directory, file_name, flags, logger) -> None:
|
||||
self.server = PluginProtocolServer(self)
|
||||
self.connection = None
|
||||
self.flags = flags
|
||||
self.logger = logger
|
||||
|
||||
self.plugin_directory = plugin_directory
|
||||
self.file_name = file_name
|
||||
|
||||
|
||||
async def start(self):
|
||||
if self.connection:
|
||||
self.connection.close()
|
||||
|
||||
self.unix_socket_path = BinaryPlugin.generate_socket_path()
|
||||
self.logger.debug(f"starting unix server on {self.unix_socket_path}")
|
||||
self.connection = await get_event_loop().create_unix_server(lambda: self.server, path=self.unix_socket_path)
|
||||
|
||||
env = dict(DECKY_PLUGIN_SOCKET = self.unix_socket_path)
|
||||
self.process = await subprocess.create_subprocess_exec(join(self.plugin_directory, self.file_name), env=env)
|
||||
get_event_loop().create_task(self.process_loop())
|
||||
|
||||
async def process_loop(self):
|
||||
await self.process.wait()
|
||||
self.logger.info("backend process was killed - restarting in 10 seconds")
|
||||
await sleep(10)
|
||||
await self.start()
|
||||
|
||||
def generate_socket_path():
|
||||
tmp_dir = mkdtemp("decky-plugin")
|
||||
os.chown(tmp_dir, 1000, 1000)
|
||||
return join(tmp_dir, "socket")
|
||||
|
||||
# called on the server/loader process
|
||||
async def call_method(self, method_name, method_args):
|
||||
if self.process.returncode == None:
|
||||
return dict(success = False, result = "Process not alive")
|
||||
|
||||
return await self.server.call_method(method_name, method_args)
|
||||
Reference in New Issue
Block a user