Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Plugin.uninstall callback support #555

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/src/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ async def uninstall_plugin(self, name: str):
# logger.debug("current plugins: %s", snapshot_string)
if name in self.plugins:
logger.debug("Plugin %s was found", name)
self.plugins[name].stop()
self.plugins[name].stop(uninstall=True)
logger.debug("Plugin %s was stopped", name)
del self.plugins[name]
logger.debug("Plugin %s was removed from the dictionary", name)
Expand Down
25 changes: 21 additions & 4 deletions backend/src/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _init(self):

# append the plugin's `py_modules` to the recognized python paths
syspath.append(path.join(environ["DECKY_PLUGIN_DIR"], "py_modules"))

#TODO: FIX IN A LESS CURSED WAY
keys = [key.replace("src.", "") for key in sysmodules if key.startswith("src.")]
for key in keys:
Expand Down Expand Up @@ -113,12 +113,29 @@ async def _unload(self):
self.log.error("Failed to unload " + self.name + "!\n" + format_exc())
exit(0)

async def _uninstall(self):
try:
self.log.info("Attempting to uninstall with plugin " + self.name + "'s \"_uninstall\" function.\n")
if hasattr(self.Plugin, "_uninstall"):
await self.Plugin._uninstall(self.Plugin)
self.log.info("Uninstalled " + self.name + "\n")
else:
self.log.info("Could not find \"_uninstall\" in " + self.name + "'s main.py" + "\n")
except:
self.log.error("Failed to uninstall " + self.name + "!\n" + format_exc())
exit(0)

async def _on_new_message(self, message : str) -> str|None:
data = loads(message)

if "stop" in data:
self.log.info("Calling Loader unload function.")
await self._unload()

if data.get('uninstall'):
self.log.info("Calling Loader uninstall function.")
await self._uninstall()

get_event_loop().stop()
while get_event_loop().is_running():
await sleep(0)
Expand All @@ -141,12 +158,12 @@ def start(self):
multiprocessing.Process(target=self._init).start()
return self

def stop(self):
def stop(self, uninstall: bool = False):
if self.passive:
return

async def _(self: PluginWrapper):
await self.socket.write_single_line(dumps({ "stop": True }, ensure_ascii=False))
await self.socket.write_single_line(dumps({ "stop": True, "uninstall": uninstall }, ensure_ascii=False))
await self.socket.close_socket_connection()

get_event_loop().create_task(_(self))
Expand All @@ -155,7 +172,7 @@ 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)")
async with self.method_call_lock:
# reader, writer =
# reader, writer =
await self.socket.get_socket_connection()

await self.socket.write_single_line(dumps({ "method": method_name, "args": kwargs }, ensure_ascii=False))
Expand Down
Loading