wto_ws.dll no longer scans aces.exe or carries structure offsets of a game build. It reads the values the patcher resolved (wto_ws.cfg native.* lines) into g_lmp through one table that names, per module, which value fills which field (local_mp_values.c), and installs each module whose values are all present, in dependency order, instead of giving up on the whole bridge when one subsystem is missing. When the settings were resolved for another executable (PE time stamp and image size differ), the bridge stays off. local_mp_offsets.h keeps the existing names so the module code reads as before; only Dagor library layouts (dag::Vector, DataBlock), protocol enums, the reflection buffer size and the two change request members with no reader in the client remain constants. A few fields are derived where the client has no instruction of its own for them (local_mp_values.c derive_values). The signatures moved to patches/native, and the header-based check-offsets command is gone: record-game-version and the launcher's patch check resolve the native modules now. Shared sites moved to local_mp_core (mission customBots, the player's unit eid) so modules do not depend on each other for them. Checked: DLL build (no warnings outside MinHook); root and launcher type checks; check-patches on 2.61.0.37; version records for 2.61.0.37 and 2.61.0.43 (10 native modules each); a lab run of the 2.61.0.37 copy logs 239 native values and "9 of 9 LocalMP modules installed". Battles were not played in this run. BREAKING CHANGE: Rebuild game copies to generate the new native values in wto_ws.cfg. Use record-game-version.ts or the launcher patch check in place of wtotools check-offsets.
210 lines
8.6 KiB
C
210 lines
8.6 KiB
C
/*
|
|
* LocalMP authoritative unit-death and kill-attribution hooks across all vehicle branches.
|
|
*
|
|
* Hooks BOTH `IGroundModel::onBecameDead` (tanks, IFVs, SPAA, boats, ships, infantry)
|
|
* and `onUnitDeadOnce` (aircraft & helicopters). All 31 bound MPlayer bots receive 1:1
|
|
* PvP kill/death counters and battle score alongside the local player; the scores come from
|
|
* the mission settings (local_mp_scores.c). Kills made by the local player are also reported
|
|
* as facts through local_mp_report_award_event().
|
|
*/
|
|
#include "hooks.h"
|
|
#include "hook_config.h"
|
|
#include "local_mp_internal.h"
|
|
#include "local_mp_offsets.h"
|
|
|
|
#include "MinHook.h"
|
|
|
|
#include <string.h>
|
|
|
|
typedef void (__fastcall *MPlayerAddScoreFn)(uint8_t *mp, float delta, uint32_t event_id, uint8_t add_team);
|
|
typedef void (__fastcall *NetMarkDirtyFn)(void *dirty_field, void *owner_obj, void *payload_ptr);
|
|
typedef void (__fastcall *ChooseOffenderVfn)(uint8_t *victim, uint8_t *out_offender, uint8_t clear_flag);
|
|
typedef void (__fastcall *IGroundOnDeadFn)(uint8_t *victim, int32_t p2);
|
|
typedef void (__fastcall *OnUnitDeadOnceFn)(uint8_t *victim);
|
|
|
|
static MPlayerAddScoreFn fn_mplayer_add_score;
|
|
static NetMarkDirtyFn fn_net_mark_dirty;
|
|
static IGroundOnDeadFn orig_iground_on_dead;
|
|
static OnUnitDeadOnceFn orig_on_unit_dead_once;
|
|
|
|
void local_mp_mark_mplayer_dirty(uint8_t *mp)
|
|
{
|
|
if (mp != NULL && fn_net_mark_dirty != NULL)
|
|
fn_net_mark_dirty(mp + LMP_MP_OFF_STAT_DIRTY, mp, mp + LMP_MP_OFF_STAT_PAYLOAD);
|
|
}
|
|
|
|
void local_mp_inc_mplayer_stat(uint8_t *mp, size_t off)
|
|
{
|
|
int16_t v = *(int16_t *)(mp + off);
|
|
if (v < 32767)
|
|
*(int16_t *)(mp + off) = (int16_t)(v + 1);
|
|
}
|
|
|
|
static void dec_mplayer_stat_pos(uint8_t *mp, size_t off)
|
|
{
|
|
int16_t v = *(int16_t *)(mp + off);
|
|
if (v > 0)
|
|
*(int16_t *)(mp + off) = (int16_t)(v - 1);
|
|
}
|
|
|
|
void local_mp_add_mplayer_score(uint8_t *mp, float delta, uint32_t event_id)
|
|
{
|
|
uint8_t *app = g_local_mp_app ? *g_local_mp_app : NULL;
|
|
int32_t total;
|
|
if (mp == NULL || delta <= 0.0f || event_id >= LMP_EXP_EVENT_COUNT || !fn_mplayer_add_score)
|
|
return;
|
|
if (app == NULL || *(void **)(app + LMP_APP_OFF_SCORE_RULES) == NULL)
|
|
return;
|
|
fn_mplayer_add_score(mp, delta, event_id, 1);
|
|
total = *(int32_t *)(mp + LMP_MP_OFF_SCORE);
|
|
*(int16_t *)(mp + LMP_MP_OFF_FALLBACK_SCORE) = (total > 32767) ? (int16_t)32767 : (int16_t)total;
|
|
}
|
|
|
|
static int is_same_severe_offender(const uint8_t *victim, const uint8_t *killer_mp)
|
|
{
|
|
if (victim == NULL || killer_mp == NULL || *(const int16_t *)(victim + LMP_UNIT_OFF_SEVERE_IDX) == -1)
|
|
return 0;
|
|
return local_mp_find_mplayer(*(const int64_t *)(victim + LMP_UNIT_OFF_SEVERE_UID),
|
|
victim[LMP_UNIT_OFF_SEVERE_PID]) == killer_mp;
|
|
}
|
|
|
|
static void record_enemy_kill(uint8_t *killer_mp, uint8_t *victim, int is_pvp)
|
|
{
|
|
uint16_t utype = *(uint16_t *)(victim + LMP_UNIT_OFF_TYPE);
|
|
int is_air = (utype == 0u || utype == 0xdu);
|
|
int is_ship = (utype == 5u);
|
|
int is_human = (utype == 0xeu);
|
|
int own_sev = is_pvp && (is_air || is_ship) && is_same_severe_offender(victim, killer_mp);
|
|
uint32_t ev = is_air ? LMP_EXP_EVENT_KILL : (is_human ? LMP_EXP_EVENT_KILL_HUMAN : LMP_EXP_EVENT_KILL_GROUND);
|
|
/* Finishing a unit the killer had already severely damaged adds what the kill is worth over
|
|
* the severe damage score the killer got then. */
|
|
float pts = local_mp_event_score("kill", victim, is_pvp) -
|
|
(own_sev ? local_mp_event_score("severeDamage", victim, is_pvp) : 0.0f);
|
|
|
|
if (is_pvp) {
|
|
local_mp_inc_mplayer_stat(killer_mp, is_air ? LMP_MP_OFF_KILLS : (is_ship ? LMP_MP_OFF_NAVAL_KILLS :
|
|
(is_human ? LMP_MP_OFF_HUMAN_KILLS : LMP_MP_OFF_GROUND_KILLS)));
|
|
if (own_sev)
|
|
dec_mplayer_stat_pos(killer_mp, is_air ? LMP_MP_OFF_AIR_SEVERE_DMG : LMP_MP_OFF_SHIP_SEVERE_DMG);
|
|
} else {
|
|
local_mp_inc_mplayer_stat(killer_mp, is_air ? LMP_MP_OFF_AI_KILLS :
|
|
(is_ship ? LMP_MP_OFF_AI_NAVAL_KILLS : LMP_MP_OFF_AI_GROUND_KILLS));
|
|
}
|
|
local_mp_add_mplayer_score(killer_mp, pts, ev);
|
|
local_mp_mark_mplayer_dirty(killer_mp);
|
|
|
|
if (local_mp_is_local_mplayer(killer_mp)) {
|
|
LocalMpAwardEvent award = { ev, victim, is_pvp, 0, own_sev ? "from_severe_damage" : NULL, 0 };
|
|
local_mp_report_award_event(&award);
|
|
}
|
|
}
|
|
|
|
static void record_team_kill(uint8_t *killer_mp, uint8_t *victim, int is_pvp)
|
|
{
|
|
uint16_t utype = *(uint16_t *)(victim + LMP_UNIT_OFF_TYPE);
|
|
int is_air = (utype == 0u || utype == 0xdu);
|
|
|
|
local_mp_inc_mplayer_stat(killer_mp, LMP_MP_OFF_TEAM_KILLS);
|
|
local_mp_mark_mplayer_dirty(killer_mp);
|
|
if (local_mp_is_local_mplayer(killer_mp)) {
|
|
LocalMpAwardEvent award = {
|
|
is_air ? LMP_EXP_EVENT_KILL : (utype == 0xeu ? LMP_EXP_EVENT_KILL_HUMAN : LMP_EXP_EVENT_KILL_GROUND),
|
|
victim, is_pvp, 1, NULL, 0
|
|
};
|
|
local_mp_report_award_event(&award);
|
|
}
|
|
}
|
|
|
|
static void process_unit_death(uint8_t *victim, uint8_t *killer_mp, int32_t killer_army, int allow_kill_credit)
|
|
{
|
|
uint8_t *victim_mp = *(uint8_t **)(victim + LMP_UNIT_OFF_MPLAYER);
|
|
uint8_t victim_army = victim[LMP_UNIT_OFF_ARMY];
|
|
|
|
if (victim_mp != NULL) {
|
|
local_mp_inc_mplayer_stat(victim_mp, LMP_MP_OFF_DEATHS);
|
|
local_mp_mark_mplayer_dirty(victim_mp);
|
|
}
|
|
if (!allow_kill_credit || killer_mp == NULL || killer_mp == victim_mp)
|
|
return;
|
|
if (killer_army <= 0)
|
|
killer_army = (int32_t)killer_mp[LMP_MP_OFF_TEAM];
|
|
if (victim_army != 0 && killer_army == (int32_t)victim_army) {
|
|
record_team_kill(killer_mp, victim, victim_mp != NULL);
|
|
} else {
|
|
record_enemy_kill(killer_mp, victim, victim_mp != NULL);
|
|
}
|
|
}
|
|
|
|
static uint8_t *resolve_offender_killer(uint8_t *victim, int32_t *out_army)
|
|
{
|
|
__declspec(align(16)) uint8_t offender[0x180];
|
|
uintptr_t vtbl = victim ? *(uintptr_t *)victim : 0;
|
|
ChooseOffenderVfn choose_fn;
|
|
|
|
if (vtbl == 0)
|
|
return NULL;
|
|
memset(offender, 0, sizeof(offender));
|
|
*(int16_t *)offender = -1;
|
|
offender[LMP_OFFENDER_OFF_PID] = 0xffu;
|
|
*(int64_t *)(offender + LMP_OFFENDER_OFF_UID) = -1;
|
|
choose_fn = *(ChooseOffenderVfn *)(vtbl + LMP_UNIT_VTF_CHOOSE_OFFENDER);
|
|
if (choose_fn != NULL)
|
|
choose_fn(victim, offender, 0);
|
|
if (*(int16_t *)offender == -1)
|
|
return NULL;
|
|
*out_army = *(int32_t *)(offender + LMP_OFFENDER_OFF_ARMY);
|
|
return local_mp_find_mplayer(*(int64_t *)(offender + LMP_OFFENDER_OFF_UID), offender[LMP_OFFENDER_OFF_PID]);
|
|
}
|
|
|
|
static void __fastcall hook_iground_on_became_dead(uint8_t *victim, int32_t p2)
|
|
{
|
|
int is_host = local_mp_is_host();
|
|
if (is_host && victim != NULL &&
|
|
victim[LMP_IGM_OFF_ALREADY_DEAD] == 0 &&
|
|
victim[LMP_IGM_OFF_DEAD_SILENT] == 0) {
|
|
int allow_kill = (victim[LMP_IGM_OFF_BAILOUT_DEAD] == 0);
|
|
int32_t killer_army = *(int32_t *)(victim + LMP_IGM_OFF_KILLER_ARMY);
|
|
uint8_t *killer_mp = allow_kill ? local_mp_find_mplayer(
|
|
*(int64_t *)(victim + LMP_IGM_OFF_KILLER_UID), victim[LMP_IGM_OFF_KILLER_PID]) : NULL;
|
|
local_mp_events_begin_death(victim);
|
|
if (allow_kill && killer_mp == NULL)
|
|
killer_mp = resolve_offender_killer(victim, &killer_army);
|
|
if (allow_kill && killer_mp != NULL && victim[LMP_IGM_OFF_KILLER_PID] == 0xffu) {
|
|
victim[LMP_IGM_OFF_KILLER_PID] = killer_mp[LMP_MP_OFF_PID];
|
|
*(int64_t *)(victim + LMP_IGM_OFF_KILLER_UID) = *(int64_t *)(killer_mp + LMP_MP_OFF_USER_ID);
|
|
}
|
|
process_unit_death(victim, killer_mp, killer_army, allow_kill);
|
|
}
|
|
orig_iground_on_dead(victim, p2);
|
|
}
|
|
|
|
static void __fastcall hook_on_unit_dead_once(uint8_t *victim)
|
|
{
|
|
local_mp_support_unit_dead(victim);
|
|
if (local_mp_is_host() && victim != NULL && victim[LMP_UNIT_OFF_SKIP_KILL] == 0) {
|
|
int32_t killer_army = 0;
|
|
uint8_t *killer_mp = resolve_offender_killer(victim, &killer_army);
|
|
local_mp_events_begin_death(victim);
|
|
process_unit_death(victim, killer_mp, killer_army, 1);
|
|
}
|
|
orig_on_unit_dead_once(victim);
|
|
}
|
|
|
|
int install_local_mp_kill_hooks(void)
|
|
{
|
|
uint8_t *p_ground_dead = g_lmp.iground_on_dead;
|
|
uint8_t *p_dead = g_lmp.on_unit_dead_once;
|
|
|
|
fn_mplayer_add_score = (MPlayerAddScoreFn)g_lmp.mplayer_add_score;
|
|
fn_net_mark_dirty = (NetMarkDirtyFn)g_lmp.net_mark_dirty;
|
|
|
|
if (MH_CreateHook(p_ground_dead, (void *)&hook_iground_on_became_dead, (void **)&orig_iground_on_dead) != MH_OK ||
|
|
MH_EnableHook(p_ground_dead) != MH_OK ||
|
|
MH_CreateHook(p_dead, (void *)&hook_on_unit_dead_once, (void **)&orig_on_unit_dead_once) != MH_OK ||
|
|
MH_EnableHook(p_dead) != MH_OK) {
|
|
log_line("local_mp: failed to install unit-death scoring hooks");
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|