Files
WT-Offline/native/wto-net/src/local_mp_support.c
T
N0VAN0M4LY 96077153c6 refactor(native): batch MinHook enable calls and log status codes
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.
2026-12-02 07:24:27 +08:00

109 lines
4.4 KiB
C

/*
* Mission support unit (the nuclear carrier) after it is destroyed, on the LocalMP host.
*
* The host gives a player a support unit when the player's spawn score reaches the threshold and
* the player has none: MPlayer per-frame update 0x59d270 at 0x59d9c8..0x59da47 (2.61.0.37) checks
* missionSupportUnitRef (MPlayer +0xb50 cached pointer, +0xb58 entity id) == 0, creates the unit
* with async_create_support_unit (0xcb55d0) and stores its entity id there. The action bar button
* launches the unit the reference names; the host then hands it control (setControlledUnit
* 0x598a70, via 0xfd7160) and respawns it (0xe4fdf0, which picks an airfield only for a unit with
* an owner). When the support unit is gone, the unit update (0xfd1450 at 0xfd2f01 / 0xfd25a7)
* recognises it by that reference (unit +0x88 == MPlayer +0xb58) and gives the player back the
* vehicle (setControlledUnit with eid 0).
*
* The client never clears the reference while a battle runs (only MPlayer::initMPlayer /
* clearBeforeLobby reset it, 0x595a30 / 0x599680), so after the first support unit was destroyed
* the next launch reused the dead unit: control was refused ("changed controlled unit to already
* dead unit"), the unit respawned in the air where it died and nobody flew it. Online the battle
* server presumably drops the reference (not observed). Here the host does it once the player has
* been given back control after the unit died (clearing it at the death itself kept the player
* from getting the vehicle back), so the stock update creates a new support unit the next time
* the threshold is reached.
*/
#include "hooks.h"
#include "hook_config.h"
#include "local_mp_internal.h"
#include "local_mp_offsets.h"
#include "MinHook.h"
typedef void (__fastcall *SetControlledUnitFn)(uint8_t *mp, uint32_t eid);
static SetControlledUnitFn orig_set_controlled_unit;
/* Players whose support unit died, with that unit's entity id; cleared after control moves on. */
#define LMP_SUPPORT_PENDING_MAX 32
static struct {
uint8_t *mp;
uint32_t eid;
} g_pending[LMP_SUPPORT_PENDING_MAX];
static void remember_dead_support_unit(uint8_t *mp, uint32_t eid)
{
size_t i, free_slot = LMP_SUPPORT_PENDING_MAX;
for (i = 0; i < LMP_SUPPORT_PENDING_MAX; i++) {
if (g_pending[i].mp == mp) {
g_pending[i].eid = eid;
return;
}
if (g_pending[i].mp == NULL && free_slot == LMP_SUPPORT_PENDING_MAX)
free_slot = i;
}
if (free_slot < LMP_SUPPORT_PENDING_MAX) {
g_pending[free_slot].mp = mp;
g_pending[free_slot].eid = eid;
}
}
void local_mp_support_unit_dead(const uint8_t *victim)
{
uint8_t **table = g_local_mp_player_table ? *g_local_mp_player_table : NULL;
uint32_t count = g_local_mp_player_count ? *g_local_mp_player_count : 0;
uint32_t i;
if (victim == NULL || table == NULL || g_lmp_get_unit_by_eid == NULL || !local_mp_is_host())
return;
for (i = 0; i < count; i++) {
uint8_t *mp = table[i];
uint32_t eid;
if (mp == NULL)
continue;
eid = *(uint32_t *)(mp + LMP_MP_OFF_SUPPORT_UNIT_EID);
if (eid == 0 || (const uint8_t *)g_lmp_get_unit_by_eid(eid) != victim)
continue;
log_line("local_mp: support unit eid %08x of pid %u destroyed", (unsigned)eid, (unsigned)mp[LMP_MP_OFF_PID]);
remember_dead_support_unit(mp, eid);
}
}
static void __fastcall hook_set_controlled_unit(uint8_t *mp, uint32_t eid)
{
size_t i;
orig_set_controlled_unit(mp, eid);
if (mp == NULL || !local_mp_is_host())
return;
for (i = 0; i < LMP_SUPPORT_PENDING_MAX; i++) {
if (g_pending[i].mp != mp)
continue;
if (*(uint32_t *)(mp + LMP_MP_OFF_SUPPORT_UNIT_EID) == g_pending[i].eid) {
log_line("local_mp: support unit reference of pid %u cleared", (unsigned)mp[LMP_MP_OFF_PID]);
*(uint64_t *)(mp + LMP_MP_OFF_SUPPORT_UNIT_PTR) = 0;
*(uint32_t *)(mp + LMP_MP_OFF_SUPPORT_UNIT_EID) = 0;
}
g_pending[i].mp = NULL;
g_pending[i].eid = 0;
}
}
int install_local_mp_support_hooks(void)
{
const LocalMpHookSpec hooks[] = {
{ "set_controlled_unit", g_lmp.set_controlled_unit, (void *)&hook_set_controlled_unit, (void **)&orig_set_controlled_unit },
};
return local_mp_queue_hooks("local_mp_support", hooks, sizeof(hooks) / sizeof(hooks[0]));
}