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 Date: Thu, 13 Feb 2025 22:14:31 +0000 Subject: Updated Aircraft.py Updated to do the prev_life/life swap in the aircraft add_state function. --- lib/Aircraft.py | 4 ++++ plugins/smoke_on_damage/Plugin.py | 3 ++- proxy.py | 4 ---- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/Aircraft.py b/lib/Aircraft.py index dd2bbed..5e6e24d 100644 --- a/lib/Aircraft.py +++ b/lib/Aircraft.py @@ -76,7 +76,11 @@ class Aircraft: if packet.player_id != self.id: return None + if self.life == -1: + self.life=packet.life + self.prev_life = self.life + self.life = packet.life self.set_position(packet.position) self.set_attitude(packet.atti) diff --git a/plugins/smoke_on_damage/Plugin.py b/plugins/smoke_on_damage/Plugin.py index b3b4ccd..e6c57e2 100644 --- a/plugins/smoke_on_damage/Plugin.py +++ b/plugins/smoke_on_damage/Plugin.py @@ -1,5 +1,6 @@ """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.""" +It is also an example of a plugin as a folder/module. +This can be used as a basis for more complex plugins that may need multiple files.""" from lib.PacketManager.packets import FSNETCMD_AIRCMD, FSNETCMD_AIRPLANESTATE, FSNETCMD_TEXTMESSAGE from logging import debug from config import SMOKE_LIFE, SMOKE_PLANE diff --git a/proxy.py b/proxy.py index 58d241f..3ca6394 100644 --- a/proxy.py +++ b/proxy.py @@ -137,15 +137,11 @@ async def handle_client(client_reader, client_writer): if not keep_message: data = None - if player.aircraft.life == -1: #Uninitialised - player.aircraft.prev_life = player.aircraft.life - elif player.aircraft.prev_life < player.aircraft.life and player.aircraft.prev_life != -1 and not player.aircraft.just_repaired: cheatingMsg = YSchat.message(f"{HEALTH_HACK_MESSAGE} by {player.username}") warning(f"Health hack detected for {player.username}, Connected from {player.ip}") message_to_server.append(cheatingMsg) - player.aircraft.prev_life = player.aircraft.life player.aircraft.just_repaired = False elif packet_type == "FSNETCMD_UNJOIN": -- cgit v1.2.3 From 7e74ceff2b7d32c934fa2e42fefaee45cfab4ff9 Mon Sep 17 00:00:00 2001 From: Skipper Date: Thu, 13 Feb 2025 22:22:13 +0000 Subject: Started to wrap FSNETCMD round YSchat --- lib/YSchat.py | 6 +----- proxy.py | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/YSchat.py b/lib/YSchat.py index d3db201..980a11b 100644 --- a/lib/YSchat.py +++ b/lib/YSchat.py @@ -1,6 +1,5 @@ from struct import pack, unpack from lib.PacketManager.packets import FSNETCMD_TEXTMESSAGE -#Re-write this to wrap the FSNETCMD_TEXTMESSAGE class def send(buffer: bytes): """ @@ -18,7 +17,4 @@ def message(msg: str): """ Generate packets for sending messages """ - decode = "l" + str(len(msg) + 2) + "s" - msg_buffer = bytes(msg, 'utf-8') - buffer = pack(decode, 0, msg_buffer) - return reply(32, buffer) + return FSNETCMD_TEXTMESSAGE.encode(msg,True) diff --git a/proxy.py b/proxy.py index 3ca6394..82fe90b 100644 --- a/proxy.py +++ b/proxy.py @@ -6,7 +6,7 @@ Lisenced under GPLv3 import asyncio from struct import unpack, pack from lib.parseFlightData import parseFlightData -from lib import YSchat, YSplayer, YSendFlight, YSundead, YSviaversion, Player, Aircraft +from lib import YSchat, YSviaversion, Player, Aircraft from lib.plugin_manager import PluginManager from lib.PacketManager.PacketManager import PacketManager from lib.PacketManager.packets import * -- cgit v1.2.3