Clearing missionSupportUnitRef as soon as the support unit died kept the player from getting the vehicle back: the unit update (2.61.0.37 0xfd1450, 0xfd2f01 / 0xfd25a7) recognises the player's dead support unit by that reference (unit +0x88 == MPlayer +0xb58) and only then calls setControlledUnit(mp, 0) (0x598a70) to return control. In game the camera stayed on the wreck until the player respawned. Remember the player and the dead unit's entity id at the death, and clear the reference after setControlledUnit has run for that player (new hook, LMP_SIG_SET_CONTROLLED_UNIT, 2.61.0.37 hit 0x598a70), as long as it still names that unit. Checked with the native build and check-offsets; not yet verified in game.
114 lines
4.5 KiB
C
114 lines
4.5 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(const uint8_t *text, size_t text_size)
|
|
{
|
|
uint8_t *p_set = local_mp_scan_unique(text, text_size, "setControlledUnit", LMP_SIG_SET_CONTROLLED_UNIT);
|
|
|
|
if (!p_set)
|
|
return 0;
|
|
if (MH_CreateHook(p_set, (void *)&hook_set_controlled_unit, (void **)&orig_set_controlled_unit) != MH_OK ||
|
|
MH_EnableHook(p_set) != MH_OK) {
|
|
log_line("local_mp: failed to install the support unit hook");
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|