From 1b4f3db2f32ffdef3acb7657f37bca3a51ffe467 Mon Sep 17 00:00:00 2001 From: Skipper Date: Thu, 13 Feb 2025 22:09:59 +0000 Subject: Updated plugins and manager Added smoke on damage as a plugin And added in ability for plugin manager to use folder plugins (multi file if necessary) --- lib/plugin_manager.py | 14 ++++++++++- plugins/smoke_on_damage/Plugin.py | 47 +++++++++++++++++++++++++++++++++++++ plugins/smoke_on_damage/__init__.py | 2 ++ proxy.py | 19 ++++----------- 4 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 plugins/smoke_on_damage/Plugin.py create mode 100644 plugins/smoke_on_damage/__init__.py diff --git a/lib/plugin_manager.py b/lib/plugin_manager.py index 113c47a..73c2ac1 100644 --- a/lib/plugin_manager.py +++ b/lib/plugin_manager.py @@ -14,6 +14,7 @@ class PluginManager: def load_plugins(self): for plugin in os.listdir(PLUGIN_DIR): + plugin_path = os.path.join(PLUGIN_DIR, plugin) if plugin.endswith('.py') and not plugin.startswith('__'): plugin_name = plugin[:-3] plugin_module = importlib.import_module(plugin_name) @@ -23,6 +24,15 @@ class PluginManager: self.register_plugin(plugin_instance) info(f"Loaded plugin {plugin_name}") self.plugins[plugin_name] = plugin_instance + elif os.path.isdir(plugin_path) and os.path.exists(os.path.join(plugin_path, '__init__.py')): + plugin_module = importlib.import_module(plugin) + if hasattr(plugin_module, 'Plugin'): + if plugin_module.ENABLED: + plugin_instance = plugin_module.Plugin() + self.register_plugin(plugin_instance) + info(f"Loaded plugin {plugin}") + self.plugins[plugin] = plugin_instance + def register_plugin(self, plugin): """Registers the plugin with the plugin manager""" @@ -42,5 +52,7 @@ class PluginManager: keep_orignal = True if hook_name in self.hooks: for callback in self.hooks[hook_name]: - keep_orignal = callback(data, *args, **kwargs) + keep = callback(data, *args, **kwargs) + if keep == False: + keep_orignal = False return keep_orignal diff --git a/plugins/smoke_on_damage/Plugin.py b/plugins/smoke_on_damage/Plugin.py new file mode 100644 index 0000000..b3b4ccd --- /dev/null +++ b/plugins/smoke_on_damage/Plugin.py @@ -0,0 +1,47 @@ +"""TThis plugin will cause the aircraft to emit smoke if it's health is below a certain threshold. +It is also an example of a plugin as a folder/module.""" +from lib.PacketManager.packets import FSNETCMD_AIRCMD, FSNETCMD_AIRPLANESTATE, FSNETCMD_TEXTMESSAGE +from logging import debug +from config import SMOKE_LIFE, SMOKE_PLANE +ENABLED = True + +class Plugin: + def __init__(self): + self.plugin_manager = None + + def register(self, plugin_manager): + self.plugin_manager = plugin_manager + self.plugin_manager.register_hook('on_flight_data', self.on_flight_data) + self.plugin_manager.register_hook('on_weapon_config', self.on_weapon_config) + + def on_flight_data(self, data, player,message_to_client, message_to_server): + """After the initial incoming flight packet, check whether + the health is below the threshold, if so add smoke""" + if ENABLED and SMOKE_PLANE and player.aircraft.life