diff --git a/native/wto-net/build.ps1 b/native/wto-net/build.ps1 index 4305ece..3003884 100644 --- a/native/wto-net/build.ps1 +++ b/native/wto-net/build.ps1 @@ -23,7 +23,7 @@ New-Item -ItemType Directory -Force $obj | Out-Null $sources = @( "$src\dll_main.c", "$src\hook_config.c", "$src\redirect_rules.c", "$src\resolver_hooks.c", "$src\connection_hooks.c", "$src\local_mp_core.c", "$src\local_mp_values.c", "$src\local_mp_players.c", "$src\local_mp_loadout.c", "$src\local_mp_actions.c", "$src\local_mp_awards.c", "$src\local_mp_kills.c", "$src\local_mp_events.c", "$src\local_mp_scores.c", "$src\local_mp_nuke.c", "$src\local_mp_support.c", - "$src\local_mp_damage_notifications.c", "$src\local_mp_hit_feedback.c", "$src\local_mp_air_events.c", "$src\local_mp_ground_ai.c", "$src\local_mp_zones.c", "$src\local_mp_captures.c", "$src\local_mp_air_ai_sync.c", + "$src\local_mp_damage_notifications.c", "$src\local_mp_ground_part_feedback.c", "$src\local_mp_hit_feedback.c", "$src\local_mp_air_events.c", "$src\local_mp_ground_ai.c", "$src\local_mp_zones.c", "$src\local_mp_captures.c", "$src\local_mp_air_ai_sync.c", "$mh\src\hook.c", "$mh\src\buffer.c", "$mh\src\trampoline.c", "$mh\src\hde\hde64.c" ) | ForEach-Object { '"' + (Resolve-Path $_) + '"' } diff --git a/native/wto-net/src/local_mp_core.c b/native/wto-net/src/local_mp_core.c index 93d3b9e..8c55f16 100644 --- a/native/wto-net/src/local_mp_core.c +++ b/native/wto-net/src/local_mp_core.c @@ -144,6 +144,7 @@ static const LocalMpModule MODULES[] = { { "local_mp_nuke", install_local_mp_nuke_hooks }, { "local_mp_support", install_local_mp_support_hooks }, { "local_mp_damage_notifications", install_local_mp_damage_notification_hooks }, + { "local_mp_ground_part_feedback", install_local_mp_ground_part_feedback_hooks }, { "local_mp_hit_feedback", install_local_mp_hit_feedback_hooks }, { "local_mp_air_events", install_local_mp_air_event_hooks }, { "local_mp_ground_ai", install_local_mp_ground_ai_hooks }, diff --git a/native/wto-net/src/local_mp_damage_notifications.c b/native/wto-net/src/local_mp_damage_notifications.c index 0302a4d..a1e87d6 100644 --- a/native/wto-net/src/local_mp_damage_notifications.c +++ b/native/wto-net/src/local_mp_damage_notifications.c @@ -7,6 +7,8 @@ #include "local_mp_values.h" #include "hook_config.h" +#include + typedef uint8_t (__fastcall *BarrelDamageFn)(void *damage, const uint8_t *impact); typedef void (__fastcall *FireUpdateFn)( const uint8_t *weapon, void *arg2, void *arg3, void *arg4, void *arg5, void *arg6, @@ -20,7 +22,6 @@ typedef uint64_t (__fastcall *ExitOpticViewFn)(void *camera, int32_t arg2, uint8 typedef uint64_t (__fastcall *UnitOnEventFn)(void *unit, const uint8_t *event, void *arg3, void *arg4); typedef void (__fastcall *HitPartsFeedbackFn)(void *hud, const uint8_t *victim, const void *info); typedef void (__fastcall *LocalMpiDispatchFn)(void *proxy, const uint8_t *msg, uint32_t recipient_pid); -typedef const uint8_t *(__fastcall *ControlledUnitFn)(const uint8_t *mplayer); /* HUD message types, registered in the stock hudMessages module. */ enum { HUD_MSG_DAMAGE = 4, HUD_MSG_EVENT = 9 }; @@ -55,27 +56,10 @@ static LocalMpiDispatchFn original_local_mpi_dispatch; static int suppressed_avionics_logged; static int suppressed_optic_logged; static int suppressed_parts_logged; -static int suppressed_enemy_mpi_logged; +static LONG suppressed_enemy_mpi_logged; +static LONG suppressed_ground_mpi_logged; static int damage_hooks_queued; -static const uint8_t *local_mplayer(void) -{ - return g_local_mp_mplayer != NULL ? *g_local_mp_mplayer : NULL; -} - -static const uint8_t *controlled_unit(const uint8_t *mplayer) -{ - return mplayer != NULL ? ((ControlledUnitFn)g_lmp.mplayer_controlled_unit)(mplayer) : NULL; -} - -static int is_local_controlled_unit(const uint8_t *unit) -{ - const uint8_t *controlled = controlled_unit(local_mplayer()); - if (controlled == NULL && g_local_mp_hero != NULL) - controlled = *g_local_mp_hero; - return unit != NULL && unit == controlled; -} - static int is_controlled_damage(const DamageNotice *notice) { uintptr_t unit; @@ -83,34 +67,7 @@ static int is_controlled_damage(const DamageNotice *notice) if (notice->context == NULL) return 0; unit = *(const uintptr_t *)(notice->context + notice->unit_offset) & ~notice->unit_tag; - return is_local_controlled_unit((const uint8_t *)unit); -} - -static const uint8_t *owning_mplayer(const uint8_t *unit) -{ - return unit != NULL ? *(const uint8_t *const *)(unit + LMP_UNIT_OFF_MPLAYER) : NULL; -} - -static const uint8_t *watched_unit(const uint8_t *mplayer) -{ - if (mplayer == NULL || mplayer[LMP_MP_OFF_GUI_STATE] == LMP_GUI_STATE_PILOT_CONTROL - || !*g_lmp.respawn_spectator_active) - return NULL; - return *(const uint8_t *const *)g_lmp.watched_hero; -} - -/* The owner check also covers projectiles fired before switching to a support vehicle. */ -int local_mp_is_view_hit(const uint8_t *attacker, const uint8_t *victim) -{ - const uint8_t *mplayer = local_mplayer(); - if (attacker == NULL || attacker == victim || mplayer == NULL) - return 0; - - const uint8_t *owner = owning_mplayer(attacker); - if (owner != NULL && owner == owning_mplayer(victim)) - return 0; - return owner == mplayer || attacker == controlled_unit(mplayer) - || attacker == watched_unit(mplayer); + return local_mp_is_controlled_unit((const uint8_t *)unit); } int local_mp_has_enemy_hit_feedback(void) @@ -182,7 +139,7 @@ static void __fastcall hook_deliver_damage_hud(int32_t type, int32_t text_id, in { const uint8_t *unit = active_avionics_unit; - if (unit != NULL && type == HUD_MSG_DAMAGE && local_mp_is_host() && !is_local_controlled_unit(unit)) { + if (unit != NULL && type == HUD_MSG_DAMAGE && local_mp_is_host() && !local_mp_is_controlled_unit(unit)) { if (suppressed_avionics_logged < SUPPRESSED_LOG_LIMIT) { ++suppressed_avionics_logged; log_line("local_mp: dropped avionics damage notice %d of a non-local unit", (int)text_id); @@ -196,7 +153,7 @@ static uint64_t __fastcall hook_exit_optic_view(void *camera, int32_t arg2, uint { const uint8_t *unit = active_avionics_unit; - if (unit != NULL && local_mp_is_host() && !is_local_controlled_unit(unit)) { + if (unit != NULL && local_mp_is_host() && !local_mp_is_controlled_unit(unit)) { if (suppressed_optic_logged < 4) { ++suppressed_optic_logged; log_line("local_mp: prevented optic view exit triggered by a non-local unit"); @@ -257,27 +214,21 @@ static void __fastcall hook_naval_hit_state(void *hud, const uint8_t *victim, co original_naval_hit_state(hud, victim, info); } -/* - * AcesApp's local MPI proxy ignores recipient_pid and delivers every local message to - * AcesApp::onEvent, leaking AI-vs-AI aircraft part damage (0xf104 / 0xf118) into the local - * player's bottom-right kill feed. - */ +/* The local proxy discards the recipient before handing the message to the HUD producers. */ static void __fastcall hook_local_mpi_dispatch(void *proxy, const uint8_t *msg, uint32_t recipient_pid) { if (msg != NULL && local_mp_is_host()) { - uint16_t msg_id = *(const uint16_t *)(msg + g_lmp.mpi_msg_id_off); - if (msg_id == (uint16_t)g_lmp.enemy_part_dmg_mpi_id || msg_id == (uint16_t)g_lmp.enemy_part_hit_mpi_id) { - uint8_t pid = (uint8_t)recipient_pid; - const uint8_t *recipient_mp = local_mp_find_mplayer(-1, pid); - const uint8_t *mplayer = local_mplayer(); - if (recipient_mp == NULL || (recipient_mp != mplayer && recipient_mp != owning_mplayer(watched_unit(mplayer)))) { - if (suppressed_enemy_mpi_logged < SUPPRESSED_LOG_LIMIT) { - ++suppressed_enemy_mpi_logged; - log_line("local_mp: dropped enemy part damage MPI 0x%04x for non-local pid %u", - (unsigned)msg_id, (unsigned)pid); - } - return; + const uint16_t msg_id = *(const uint16_t *)(msg + g_lmp.mpi_msg_id_off); + const int ground = g_lmp.ground_part_damage_mpi_id != 0 && msg_id == g_lmp.ground_part_damage_mpi_id; + if ((ground || msg_id == g_lmp.enemy_part_dmg_mpi_id || msg_id == g_lmp.enemy_part_hit_mpi_id) && + !local_mp_is_view_recipient((uint8_t)recipient_pid)) { + LONG *logged = ground ? &suppressed_ground_mpi_logged : &suppressed_enemy_mpi_logged; + if (InterlockedCompareExchange(logged, 0, 0) < SUPPRESSED_LOG_LIMIT && + InterlockedIncrement(logged) <= SUPPRESSED_LOG_LIMIT) { + log_line("local_mp: dropped enemy part damage MPI 0x%04x for non-local pid %u", + (unsigned)msg_id, (unsigned)recipient_pid); } + return; } } original_local_mpi_dispatch(proxy, msg, recipient_pid); diff --git a/native/wto-net/src/local_mp_ground_part_feedback.c b/native/wto-net/src/local_mp_ground_part_feedback.c new file mode 100644 index 0000000..7d6b29b --- /dev/null +++ b/native/wto-net/src/local_mp_ground_part_feedback.c @@ -0,0 +1,66 @@ +/* Keep the source player of ground part damage until the stock HUD callback runs. */ +#include "local_mp_internal.h" +#include "local_mp_values.h" +#include "hook_config.h" + +#include + +typedef void (__fastcall *ApplyPartDamageFn)( + const uint8_t *victim, void *damage, const uint8_t *source, uint32_t flags); +typedef void (__fastcall *ShowPartDamageFn)( + const uint8_t *victim, int32_t part, float damage, float health, uint16_t damage_type); + +typedef struct PartDamageSource { + const uint8_t *victim; + uint8_t pid; +} PartDamageSource; + +static __declspec(thread) const PartDamageSource *active_source; +static ApplyPartDamageFn original_apply_part_damage; +static ShowPartDamageFn original_show_part_damage; +static LONG rejected_direct_feedback_logged; + +static void __fastcall hook_apply_part_damage( + const uint8_t *victim, void *damage, const uint8_t *source, uint32_t flags) +{ + const PartDamageSource *previous = active_source; + PartDamageSource current; + + active_source = NULL; + if (source != NULL && local_mp_is_host()) { + current.victim = victim; + current.pid = source[g_lmp.ground_part_damage_source_pid]; + active_source = ¤t; + } + original_apply_part_damage(victim, damage, source, flags); + active_source = previous; +} + +static void __fastcall hook_show_part_damage( + const uint8_t *victim, int32_t part, float damage, float health, uint16_t damage_type) +{ + const PartDamageSource *source = active_source; + if (local_mp_is_host() && + (source == NULL || source->victim != victim || !local_mp_is_view_recipient(source->pid))) { + if (InterlockedExchange(&rejected_direct_feedback_logged, 1) == 0) { + if (source != NULL && source->victim == victim) + log_line("local_mp: dropped direct ground part feedback for non-local pid %u", (unsigned)source->pid); + else + log_line("local_mp: dropped direct ground part feedback without matching damage source"); + } + return; + } + original_show_part_damage(victim, part, damage, health, damage_type); +} + +int install_local_mp_ground_part_feedback_hooks(void) +{ + if (!local_mp_damage_notifications_ready()) + return 0; + + const LocalMpHookSpec hooks[] = { + { "apply_part_damage", g_lmp.apply_part_damage, (void *)&hook_apply_part_damage, (void **)&original_apply_part_damage }, + { "show_ground_part_damage", g_lmp.show_ground_part_damage, (void *)&hook_show_part_damage, (void **)&original_show_part_damage }, + }; + return local_mp_queue_hooks("local_mp_ground_part_feedback", hooks, sizeof(hooks) / sizeof(hooks[0])); +} diff --git a/native/wto-net/src/local_mp_hit_feedback.c b/native/wto-net/src/local_mp_hit_feedback.c index d215f6d..575fdaa 100644 --- a/native/wto-net/src/local_mp_hit_feedback.c +++ b/native/wto-net/src/local_mp_hit_feedback.c @@ -1,4 +1,4 @@ -/* Validate recipient ownership at the two outgoing hit-feedback dispatch points. */ +/* Resolve the active view for damage feedback and validate outgoing hit markers and shell hints. */ #include "local_mp_internal.h" #include "local_mp_offsets.h" @@ -7,6 +7,7 @@ typedef void (__fastcall *SendScriptNetEventFn)(void *manager, uint8_t broadcast, uint32_t destination, const void *event, const char *event_name, const void *recipient_args, const void *recipient_filter); typedef void (__fastcall *NotifyHudScriptFn)(void *hud, int32_t event_id, const void *data); +typedef const uint8_t *(__fastcall *ControlledUnitFn)(const uint8_t *mplayer); /* The CmdShowHitMark unicast constructor writes 0x0001001c6c52bfea followed by its victim EID. * The stock sender handles event destruction when its flags require it. This event is plain data. */ @@ -23,6 +24,58 @@ enum { HUD_HINT_CHANGE_SHELL_TYPE = 0xc5 }; static SendScriptNetEventFn original_send_script_event; static NotifyHudScriptFn original_notify_hud_script; +static const uint8_t *local_mplayer(void) +{ + return g_local_mp_mplayer != NULL ? *g_local_mp_mplayer : NULL; +} + +static const uint8_t *controlled_unit(const uint8_t *mplayer) +{ + return mplayer != NULL ? ((ControlledUnitFn)g_lmp.mplayer_controlled_unit)(mplayer) : NULL; +} + +static const uint8_t *owning_mplayer(const uint8_t *unit) +{ + return unit != NULL ? *(const uint8_t *const *)(unit + LMP_UNIT_OFF_MPLAYER) : NULL; +} + +static const uint8_t *watched_unit(const uint8_t *mplayer) +{ + if (mplayer == NULL || mplayer[LMP_MP_OFF_GUI_STATE] == LMP_GUI_STATE_PILOT_CONTROL + || !*g_lmp.respawn_spectator_active) + return NULL; + return *(const uint8_t *const *)g_lmp.watched_hero; +} + +int local_mp_is_controlled_unit(const uint8_t *unit) +{ + const uint8_t *controlled = controlled_unit(local_mplayer()); + if (controlled == NULL && g_local_mp_hero != NULL) + controlled = *g_local_mp_hero; + return unit != NULL && unit == controlled; +} + +/* A player can land an earlier shot after switching to a support vehicle. */ +int local_mp_is_view_hit(const uint8_t *attacker, const uint8_t *victim) +{ + const uint8_t *mplayer = local_mplayer(); + if (attacker == NULL || attacker == victim || mplayer == NULL) + return 0; + + const uint8_t *owner = owning_mplayer(attacker); + if (owner != NULL && owner == owning_mplayer(victim)) + return 0; + return owner == mplayer || attacker == controlled_unit(mplayer) + || attacker == watched_unit(mplayer); +} + +int local_mp_is_view_recipient(uint8_t pid) +{ + const uint8_t *recipient = local_mp_find_mplayer(-1, pid); + const uint8_t *mplayer = local_mplayer(); + return recipient != NULL && (recipient == mplayer || recipient == owning_mplayer(watched_unit(mplayer))); +} + static int should_drop_hit_mark(uint8_t broadcast, uint32_t destination, const HitMarkEvent *event) { if (event == NULL || event->type != HIT_MARK_EVENT_TYPE || event->size != HIT_MARK_EVENT_SIZE || diff --git a/native/wto-net/src/local_mp_internal.h b/native/wto-net/src/local_mp_internal.h index 8ae9ebd..15b586f 100644 --- a/native/wto-net/src/local_mp_internal.h +++ b/native/wto-net/src/local_mp_internal.h @@ -110,8 +110,11 @@ int install_local_mp_nuke_hooks(void); int install_local_mp_support_hooks(void); int install_local_mp_damage_notification_hooks(void); int install_local_mp_hit_feedback_hooks(void); +int install_local_mp_ground_part_feedback_hooks(void); int local_mp_damage_notifications_ready(void); +int local_mp_is_controlled_unit(const uint8_t *unit); int local_mp_is_view_hit(const uint8_t *attacker, const uint8_t *victim); +int local_mp_is_view_recipient(uint8_t pid); int local_mp_has_enemy_hit_feedback(void); int install_local_mp_zone_hooks(void); void local_mp_zones_reset(void); diff --git a/native/wto-net/src/local_mp_values.c b/native/wto-net/src/local_mp_values.c index 15afa69..9835a8c 100644 --- a/native/wto-net/src/local_mp_values.c +++ b/native/wto-net/src/local_mp_values.c @@ -42,6 +42,7 @@ typedef struct LmpValueSpec { #define NUKE "local_mp_nuke" #define SUPPORT "local_mp_support" #define DAMAGE_NOTICES "local_mp_damage_notifications" +#define GROUND_PART_FEEDBACK "local_mp_ground_part_feedback" #define HIT_FEEDBACK "local_mp_hit_feedback" #define AIR_EVENTS "local_mp_air_events" #define GROUND_AI "local_mp_ground_ai" @@ -180,6 +181,11 @@ static const LmpValueSpec SPECS[] = { O(DAMAGE_NOTICES, "enemy_part_damage_mpi.msg_id", enemy_part_dmg_mpi_id), O(DAMAGE_NOTICES, "enemy_part_hit_mpi.msg_id", enemy_part_hit_mpi_id), + A(GROUND_PART_FEEDBACK, "apply_part_damage", apply_part_damage), + A(GROUND_PART_FEEDBACK, "show_ground_part_damage", show_ground_part_damage), + O(GROUND_PART_FEEDBACK, "ground_part_damage_source.pid", ground_part_damage_source_pid), + O(GROUND_PART_FEEDBACK, "ground_part_damage_mpi.msg_id", ground_part_damage_mpi_id), + A(HIT_FEEDBACK, "hit_mark_send.send", hit_mark_send), A(HIT_FEEDBACK, "hit_shell_hint_call", hit_shell_hint_call), A(HIT_FEEDBACK, "hit_shell_hint_call.notify", hit_shell_hint_notify), diff --git a/native/wto-net/src/local_mp_values.h b/native/wto-net/src/local_mp_values.h index f6a0bf6..aab4621 100644 --- a/native/wto-net/src/local_mp_values.h +++ b/native/wto-net/src/local_mp_values.h @@ -87,6 +87,10 @@ typedef struct LmpValues { size_t barrel_unit_ref, breech_unit, barrel_notice_id, breech_notice_id, avionics_unit, hit_event_offender; size_t mpi_msg_id_off, enemy_part_dmg_mpi_id, enemy_part_hit_mpi_id; + /* local_mp_ground_part_feedback */ + uint8_t *apply_part_damage, *show_ground_part_damage; + size_t ground_part_damage_source_pid, ground_part_damage_mpi_id; + /* local_mp_hit_feedback */ uint8_t *hit_mark_send, *hit_shell_hint_call, *hit_shell_hint_notify; diff --git a/patches/native/local_mp_ground_part_feedback.jsonc b/patches/native/local_mp_ground_part_feedback.jsonc new file mode 100644 index 0000000..e8eda5e --- /dev/null +++ b/patches/native/local_mp_ground_part_feedback.jsonc @@ -0,0 +1,71 @@ +// Keep the ground module damage list and damage preview tied to the firing player. +// LocalMP damage is simulated in the same process as the HUD, beyond the local server's reach. +// Other players' unicast messages and direct damage callbacks can otherwise reach the local HUD. +{ + "module": "local_mp_ground_part_feedback", + "features": ["K-Player damage notices"], + "requires": ["local_mp_damage_notifications"], + "sites": { + // Stock damage application. Both profiler and validation strings reference this function. + // 2.61.0.37: 0xc116d0; 2.61.0.64: 0xc154c0. + "apply_part_damage": { + "variants": [{ + "written_for": "2.61.0.37", + "locate": { "method": "string_ref", "string": "do_unit_part_damage_impl", "target": "function" } + }] + }, + // The virtual ground model callback only writes the HUD part list and damage preview. + // 2.61.0.37: 0x1651140; 2.61.0.64: 0x1657cf0. + "show_ground_part_damage": { + "variants": [{ + "written_for": "2.61.0.37", + "locate": { "method": "string_ref", "string": "HeavyVehicleModel__onDamagedByPlayer", "target": "function" } + }] + }, + // IGroundModel::sendPartDamage routes this same damage source to its player slot. + // 2.61.0.37: 0x42c240; 2.61.0.64: 0x42c840. + "send_ground_part_damage": { + "variants": [{ + "written_for": "2.61.0.37", + "locate": { "method": "string_ref", "string": "IGroundModel__sendPartDamage", "target": "function" } + }] + }, + // The source player slot is read before checking the stock no-player sentinel. + // 2.61.0.37: 0x42c2a9; 2.61.0.64: 0x42c8a9. + "ground_part_damage_source": { + "variants": [{ + "written_for": "2.61.0.37", + "locate": { + "method": "within", + "in": "send_ground_part_damage", + "pattern": "41 0f b6 46 ?? 3d ff 00 00 00 0f 84 ?? ?? ?? ??", + "captures": { "pid": { "offset": 4, "type": "u8" } } + } + }] + }, + // Unicast constructor for the ground part index and health. The TLS address varies by build. + // 2.61.0.37: 0x42c450; 2.61.0.64: 0x42ca50. + "ground_part_damage_packet": { + "variants": [{ + "written_for": "2.61.0.37", + "locate": { + "method": "signature", + "pattern": "41 56 56 57 53 48 81 ec f8 00 00 00 44 89 ce 44 89 c7 49 89 d6 89 cb 8b 05 ?? ?? ?? ?? 65 48 8b 0c 25 58 00 00 00" + } + }] + }, + // The constructor writes the MPI message type to the packet header. + // 2.61.0.37: 0x42c4b4 (0xf13a); 2.61.0.64: 0x42cab4 (0xf13a). + "ground_part_damage_mpi": { + "variants": [{ + "written_for": "2.61.0.37", + "locate": { + "method": "within", + "in": "ground_part_damage_packet", + "pattern": "66 c7 44 24 40 ?? ?? 48 c7 44 24 48 00 00 00 00 c7 44 24 50 00 00 00 00", + "captures": { "msg_id": { "offset": 5, "type": "u16" } } + } + }] + } + } +}