summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSkipper <[email protected]>2025-02-12 21:46:42 +0000
committerSkipper <[email protected]>2025-02-12 21:46:42 +0000
commitab9065592f811dcb78c6f1f7fe927d3deadf3695 (patch)
tree35c85d9099f255be12aa1a2daa40215b41a9bcb0 /lib
parentacca268ffafd26bf87fa6b14f228bdd54cf47233 (diff)
Initial setup of plugin manager
Diffstat (limited to 'lib')
-rw-r--r--lib/Aircraft.py1
-rw-r--r--lib/plugin_manager.py46
2 files changed, 47 insertions, 0 deletions
diff --git a/lib/Aircraft.py b/lib/Aircraft.py
index ecbecff..dd2bbed 100644
--- a/lib/Aircraft.py
+++ b/lib/Aircraft.py
@@ -17,6 +17,7 @@ class Aircraft:
self.id = -1
self.last_packet = None
self.damage_engine_warn_sent = False
+ self.last_over_g_message = 0
self.just_repaired = False
def reset(self):
diff --git a/lib/plugin_manager.py b/lib/plugin_manager.py
new file mode 100644
index 0000000..113c47a
--- /dev/null
+++ b/lib/plugin_manager.py
@@ -0,0 +1,46 @@
+import importlib
+import os
+import sys
+from logging import info
+
+PLUGIN_DIR = os.path.join(os.path.dirname(__file__), '../plugins')
+sys.path.append(PLUGIN_DIR)
+
+class PluginManager:
+ def __init__(self):
+ self.plugins = {}
+ self.hooks= {}
+ self.load_plugins()
+
+ def load_plugins(self):
+ for plugin in os.listdir(PLUGIN_DIR):
+ if plugin.endswith('.py') and not plugin.startswith('__'):
+ plugin_name = plugin[:-3]
+ plugin_module = importlib.import_module(plugin_name)
+ if hasattr(plugin_module, 'Plugin'):
+ if plugin_module.ENABLED:
+ plugin_instance = plugin_module.Plugin()
+ self.register_plugin(plugin_instance)
+ info(f"Loaded plugin {plugin_name}")
+ self.plugins[plugin_name] = plugin_instance
+
+ def register_plugin(self, plugin):
+ """Registers the plugin with the plugin manager"""
+ plugin.register(self)
+
+ def register_hook(self, hook_name, callback):
+ """Registers the hook with the plugin manager"""
+ if hook_name not in self.hooks:
+ self.hooks[hook_name] = []
+ self.hooks[hook_name].append(callback)
+
+ def triggar_hook(self, hook_name, data, *args, **kwargs):
+ """Triggars the callbacks for the hook.
+ If a callback returns False, then the original data is set to None
+ and not forwarded to the destination. This is useful for modifying
+ packets."""
+ keep_orignal = True
+ if hook_name in self.hooks:
+ for callback in self.hooks[hook_name]:
+ keep_orignal = callback(data, *args, **kwargs)
+ return keep_orignal