Switch ws2_32 and LocalMP hook installers to MH_QueueEnableHook with two MH_ApplyQueued passes (mandatory network hooks first, then all LocalMP modules), reducing DllMain thread-freeze cycles from 26 to 2. Roll back partial module hooks on error and log MH_StatusToString diagnostics to wto_ws.log. Checked: native/wto-net/build.ps1 compiles cleanly with zero warnings in project sources, and PE inspection confirms WS2_32/KERNEL32 imports and 49 exports. In-game verification pending.
240 lines
11 KiB
C
240 lines
11 KiB
C
/*
|
|
* LocalMP non-fatal combat event dispatchers: deduplicated assists across air/ground/naval,
|
|
* zone captures, hits/criticals, severe damage, and naval damage awards.
|
|
*
|
|
* `IGroundModel::onBecameDead` calls both `IGroundModel::awardAssists` (`0x465540`) and
|
|
* `Unit::awardAssists` (`0xe5b200` -> `dispatchAssist` `0x35beb0`); deduplicating `(victim, mp)`
|
|
* per unit death ensures every assist across air, ground, and naval is credited strictly once.
|
|
* Scores come from the mission settings (local_mp_scores.c).
|
|
*/
|
|
#include "hooks.h"
|
|
#include "hook_config.h"
|
|
#include "local_mp_internal.h"
|
|
#include "local_mp_offsets.h"
|
|
|
|
#include "MinHook.h"
|
|
|
|
#include <string.h>
|
|
|
|
#define MAX_OFFENDER_SNAP 64u
|
|
#define MAX_ASSIST_DEDUP 64u
|
|
|
|
|
|
typedef struct AssistDedupRecord {
|
|
const uint8_t *victim;
|
|
const uint8_t *offender_mp;
|
|
} AssistDedupRecord;
|
|
|
|
typedef void (__fastcall *IGroundAwardAssistsFn)(uint8_t *victim);
|
|
typedef void (__fastcall *DispatchAssistFn)(int64_t uid, uint8_t *victim, uint8_t *offender, float share, int32_t extra);
|
|
typedef void (__fastcall *DispatchCaptureZoneFn)(int64_t uid, uint8_t *capturer_unit);
|
|
typedef void (__fastcall *DispatchHitCritFn)(int64_t uid, uint8_t *victim, uint8_t *offender, float dmg, uint8_t is_kill, uint8_t is_crit, uint8_t extra);
|
|
typedef void (__fastcall *DispatchSevereDamageFn)(int64_t uid, uint8_t *victim, uint8_t *offender);
|
|
typedef void (__fastcall *DispatchNavalDamageFn)(int64_t uid, uint8_t *victim, uint8_t *offender, float award_dmg);
|
|
|
|
static IGroundAwardAssistsFn orig_iground_award_assists;
|
|
static DispatchAssistFn orig_dispatch_assist;
|
|
static DispatchCaptureZoneFn orig_dispatch_capture_zone;
|
|
static DispatchHitCritFn orig_dispatch_hit_crit;
|
|
static DispatchSevereDamageFn orig_dispatch_severe_damage;
|
|
static DispatchNavalDamageFn orig_dispatch_naval_damage;
|
|
|
|
static AssistDedupRecord g_assist_dedup[MAX_ASSIST_DEDUP];
|
|
static size_t g_assist_dedup_cursor = 0;
|
|
|
|
void local_mp_events_reset(void)
|
|
{
|
|
memset(g_assist_dedup, 0, sizeof(g_assist_dedup));
|
|
g_assist_dedup_cursor = 0;
|
|
}
|
|
|
|
void local_mp_events_begin_death(const uint8_t *victim)
|
|
{
|
|
size_t i;
|
|
if (victim == NULL)
|
|
return;
|
|
for (i = 0; i < MAX_ASSIST_DEDUP; i++) {
|
|
if (g_assist_dedup[i].victim == victim) {
|
|
g_assist_dedup[i].victim = NULL;
|
|
g_assist_dedup[i].offender_mp = NULL;
|
|
}
|
|
}
|
|
}
|
|
|
|
static int mark_assist_credited_once(const uint8_t *victim, const uint8_t *mp)
|
|
{
|
|
size_t i;
|
|
for (i = 0; i < MAX_ASSIST_DEDUP; i++) {
|
|
if (g_assist_dedup[i].victim == victim && g_assist_dedup[i].offender_mp == mp)
|
|
return 0;
|
|
}
|
|
g_assist_dedup[g_assist_dedup_cursor].victim = victim;
|
|
g_assist_dedup[g_assist_dedup_cursor].offender_mp = mp;
|
|
g_assist_dedup_cursor = (g_assist_dedup_cursor + 1u) % MAX_ASSIST_DEDUP;
|
|
return 1;
|
|
}
|
|
|
|
static void credit_assist_to_mplayer(uint8_t *mp, uint8_t *victim)
|
|
{
|
|
uint8_t *victim_mp = *(uint8_t **)(victim + LMP_UNIT_OFF_MPLAYER);
|
|
float pts;
|
|
|
|
if (mp == NULL || mp == victim_mp || !mark_assist_credited_once(victim, mp))
|
|
return;
|
|
pts = local_mp_event_score("assist", victim, victim_mp != NULL);
|
|
local_mp_inc_mplayer_stat(mp, LMP_MP_OFF_ASSISTS);
|
|
local_mp_add_mplayer_score(mp, pts, LMP_EXP_EVENT_ASSIST);
|
|
local_mp_mark_mplayer_dirty(mp);
|
|
if (local_mp_is_local_mplayer(mp)) {
|
|
LocalMpAwardEvent award = { LMP_EXP_EVENT_ASSIST, victim, victim_mp != NULL, 0, NULL, 0 };
|
|
local_mp_report_award_event(&award);
|
|
}
|
|
}
|
|
|
|
static void __fastcall hook_iground_award_assists(uint8_t *victim)
|
|
{
|
|
uint8_t prev_sent[MAX_OFFENDER_SNAP];
|
|
uint32_t count = 0;
|
|
uint8_t *arr = NULL;
|
|
uint32_t i;
|
|
|
|
if (local_mp_is_host() && victim != NULL) {
|
|
arr = *(uint8_t **)(victim + LMP_IGM_OFF_OFFENDERS_PTR);
|
|
count = *(uint32_t *)(victim + LMP_IGM_OFF_OFFENDERS_CNT);
|
|
if (count > MAX_OFFENDER_SNAP)
|
|
count = MAX_OFFENDER_SNAP;
|
|
for (i = 0; arr != NULL && i < count; i++)
|
|
prev_sent[i] = arr[i * LMP_OFFENDER_STRIDE + LMP_OFFENDER_OFF_ASSIST_SENT];
|
|
}
|
|
|
|
orig_iground_award_assists(victim);
|
|
|
|
if (local_mp_is_host() && victim != NULL && arr != NULL && count > 0) {
|
|
arr = *(uint8_t **)(victim + LMP_IGM_OFF_OFFENDERS_PTR);
|
|
for (i = 0; arr != NULL && i < count; i++) {
|
|
uint8_t *off = arr + i * LMP_OFFENDER_STRIDE;
|
|
if (prev_sent[i] == 0 && off[LMP_OFFENDER_OFF_ASSIST_SENT] != 0) {
|
|
uint8_t *mp = local_mp_find_mplayer(
|
|
*(int64_t *)(off + LMP_OFFENDER_OFF_UID), off[LMP_OFFENDER_OFF_PID]);
|
|
credit_assist_to_mplayer(mp, victim);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
static void __fastcall hook_dispatch_assist(int64_t uid, uint8_t *victim, uint8_t *offender, float share, int32_t extra)
|
|
{
|
|
if (local_mp_is_host() && victim != NULL) {
|
|
uint8_t *mp = local_mp_find_mplayer(uid, offender ? offender[LMP_OFFENDER_OFF_PID] : 0xffu);
|
|
credit_assist_to_mplayer(mp, victim);
|
|
}
|
|
orig_dispatch_assist(uid, victim, offender, share, extra);
|
|
}
|
|
|
|
static void __fastcall hook_dispatch_capture_zone(int64_t uid, uint8_t *capturer_unit)
|
|
{
|
|
if (local_mp_is_host()) {
|
|
uint8_t *mp = local_mp_find_mplayer(uid, 0xffu);
|
|
if (mp == NULL && capturer_unit != NULL)
|
|
mp = *(uint8_t **)(capturer_unit + LMP_UNIT_OFF_MPLAYER);
|
|
if (mp != NULL) {
|
|
const uint8_t *own_unit = capturer_unit != NULL ? capturer_unit
|
|
: (g_lmp_get_unit_by_eid ? (const uint8_t *)g_lmp_get_unit_by_eid(*(uint32_t *)(mp + LMP_MP_OFF_UNIT_EID)) : NULL);
|
|
local_mp_inc_mplayer_stat(mp, LMP_MP_OFF_CAPTURE_ZONE);
|
|
local_mp_add_mplayer_score(mp, local_mp_event_score("capture", own_unit, 1), LMP_EXP_EVENT_CAPTURE_ZONE);
|
|
local_mp_mark_mplayer_dirty(mp);
|
|
if (local_mp_is_local_mplayer(mp)) {
|
|
LocalMpAwardEvent award = { LMP_EXP_EVENT_CAPTURE_ZONE, NULL, 0, 0, NULL, 0 };
|
|
local_mp_report_award_event(&award);
|
|
}
|
|
}
|
|
}
|
|
orig_dispatch_capture_zone(uid, capturer_unit);
|
|
}
|
|
|
|
static void __fastcall hook_dispatch_hit_crit(
|
|
int64_t uid, uint8_t *victim, uint8_t *offender, float dmg, uint8_t is_kill, uint8_t is_crit, uint8_t extra
|
|
)
|
|
{
|
|
if (local_mp_is_host() && !is_kill && victim != NULL) {
|
|
uint8_t *mp = local_mp_find_mplayer(uid, offender ? offender[LMP_OFFENDER_OFF_PID] : 0xffu);
|
|
if (mp != NULL && mp != *(uint8_t **)(victim + LMP_UNIT_OFF_MPLAYER)) {
|
|
float pts = local_mp_event_score(is_crit ? "criticalHit" : "hit", victim,
|
|
*(uint8_t **)(victim + LMP_UNIT_OFF_MPLAYER) != NULL);
|
|
uint32_t ev = is_crit ? LMP_EXP_EVENT_CRITICAL_HIT : LMP_EXP_EVENT_HIT;
|
|
local_mp_add_mplayer_score(mp, pts, ev);
|
|
local_mp_mark_mplayer_dirty(mp);
|
|
if (local_mp_is_local_mplayer(mp)) {
|
|
LocalMpAwardEvent award = {
|
|
ev, victim, *(uint8_t **)(victim + LMP_UNIT_OFF_MPLAYER) != NULL, 0, NULL, 0
|
|
};
|
|
local_mp_report_award_event(&award);
|
|
}
|
|
}
|
|
}
|
|
orig_dispatch_hit_crit(uid, victim, offender, dmg, is_kill, is_crit, extra);
|
|
}
|
|
|
|
static void __fastcall hook_dispatch_severe_damage(int64_t uid, uint8_t *victim, uint8_t *offender)
|
|
{
|
|
if (local_mp_is_host() && victim != NULL && offender != NULL) {
|
|
uint8_t victim_army = victim[LMP_UNIT_OFF_ARMY];
|
|
if (victim_army == 0 || *(int32_t *)(offender + LMP_OFFENDER_OFF_ARMY) != (int32_t)victim_army) {
|
|
uint8_t *mp = local_mp_find_mplayer(uid, offender[LMP_OFFENDER_OFF_PID]);
|
|
uint8_t *victim_mp = *(uint8_t **)(victim + LMP_UNIT_OFF_MPLAYER);
|
|
if (mp != NULL && mp != victim_mp && victim_mp != NULL) {
|
|
uint16_t utype = *(uint16_t *)(victim + LMP_UNIT_OFF_TYPE);
|
|
int is_air = (utype == 0u || utype == 0xdu);
|
|
if (is_air) local_mp_inc_mplayer_stat(mp, LMP_MP_OFF_AIR_SEVERE_DMG);
|
|
else if (utype == 5u) local_mp_inc_mplayer_stat(mp, LMP_MP_OFF_SHIP_SEVERE_DMG);
|
|
local_mp_add_mplayer_score(mp, local_mp_event_score("severeDamage", victim, 1), LMP_EXP_EVENT_SEVERE_DAMAGE);
|
|
local_mp_mark_mplayer_dirty(mp);
|
|
if (local_mp_is_local_mplayer(mp)) {
|
|
LocalMpAwardEvent award = { LMP_EXP_EVENT_SEVERE_DAMAGE, victim, 1, 0, NULL, 0 };
|
|
local_mp_report_award_event(&award);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
orig_dispatch_severe_damage(uid, victim, offender);
|
|
}
|
|
|
|
static void __fastcall hook_dispatch_naval_damage(int64_t uid, uint8_t *victim, uint8_t *offender, float award_dmg)
|
|
{
|
|
if (local_mp_is_host() && victim != NULL && offender != NULL && award_dmg > 0.0f) {
|
|
uint8_t victim_army = victim[LMP_UNIT_OFF_ARMY];
|
|
if (victim_army == 0 || *(int32_t *)(offender + LMP_OFFENDER_OFF_ARMY) != (int32_t)victim_army) {
|
|
uint8_t *mp = local_mp_find_mplayer(uid, offender[LMP_OFFENDER_OFF_PID]);
|
|
if (mp != NULL) {
|
|
int32_t add_dmg = (int32_t)(award_dmg + 0.5f);
|
|
float pts = award_dmg * local_mp_score_param("wtoScore_damagePerPoint");
|
|
if (add_dmg > 0) *(int32_t *)(mp + LMP_MP_OFF_AWARD_DAMAGE) += add_dmg;
|
|
if (pts >= 1.0f) local_mp_add_mplayer_score(mp, pts, LMP_EXP_EVENT_UNIT_DAMAGE);
|
|
local_mp_mark_mplayer_dirty(mp);
|
|
if (local_mp_is_local_mplayer(mp) && add_dmg > 0) {
|
|
LocalMpAwardEvent award = {
|
|
LMP_EXP_EVENT_UNIT_DAMAGE, victim, *(uint8_t **)(victim + LMP_UNIT_OFF_MPLAYER) != NULL,
|
|
0, NULL, add_dmg
|
|
};
|
|
local_mp_report_award_event(&award);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
orig_dispatch_naval_damage(uid, victim, offender, award_dmg);
|
|
}
|
|
|
|
int install_local_mp_event_hooks(void)
|
|
{
|
|
const LocalMpHookSpec hooks[] = {
|
|
{ "iground_award_assists", g_lmp.iground_award_assists, (void *)&hook_iground_award_assists, (void **)&orig_iground_award_assists },
|
|
{ "dispatch_assist", g_lmp.dispatch_assist, (void *)&hook_dispatch_assist, (void **)&orig_dispatch_assist },
|
|
{ "dispatch_capture_zone", g_lmp.dispatch_capture_zone, (void *)&hook_dispatch_capture_zone, (void **)&orig_dispatch_capture_zone },
|
|
{ "dispatch_hit_crit", g_lmp.dispatch_hit_crit, (void *)&hook_dispatch_hit_crit, (void **)&orig_dispatch_hit_crit },
|
|
{ "dispatch_severe_damage", g_lmp.dispatch_severe_damage, (void *)&hook_dispatch_severe_damage, (void **)&orig_dispatch_severe_damage },
|
|
{ "dispatch_naval_damage", g_lmp.dispatch_naval_damage, (void *)&hook_dispatch_naval_damage, (void **)&orig_dispatch_naval_damage },
|
|
};
|
|
|
|
return local_mp_queue_hooks("local_mp_events", hooks, sizeof(hooks) / sizeof(hooks[0]));
|
|
}
|