Files
WT-Offline/native/wto-net/src/local_mp_players.c
T
N0VAN0M4LY fdac56a036 refactor(native)!: load LocalMP values from the build configuration
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.
2026-11-29 12:26:11 +08:00

229 lines
8.3 KiB
C

/*
* LocalMP player team alignment, AI MPlayer <-> Unit ownership binding,
* asynchronous aircraftChangeResultFromHost handshake, and spawn-slot bookkeeping.
*/
#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 *GuiStateUpdateFn)(uint8_t *gui_state_obj, float dt);
typedef void (__fastcall *MPlayerInitFromMatchingFn)(uint8_t *mplayer, uint8_t *info, uint64_t uid);
typedef int64_t (__fastcall *DoRespawnPlayerFn)(void *app);
typedef uint8_t (__fastcall *IsSlotReadyAtHostFn)(int32_t slot_id, const char *unit_name, uint8_t debug);
static GuiStateUpdateFn orig_gui_state_update;
static MPlayerInitFromMatchingFn orig_mplayer_init_matching;
static DoRespawnPlayerFn orig_do_respawn;
static IsSlotReadyAtHostFn orig_is_slot_ready_at_host;
static int g_accept_pending = 0;
static int g_respawn_dispatched = 0;
static int32_t g_pending_slot_id = -1;
static int32_t g_pending_spare_type = -1;
void local_mp_players_queue_spawn(int32_t slot_id, int32_t spare_type, int is_silent)
{
if (!is_silent || g_pending_slot_id < 0) {
g_pending_slot_id = slot_id;
g_pending_spare_type = spare_type;
}
if (!is_silent)
g_respawn_dispatched = 0;
g_accept_pending = 1;
}
static void bind_ai_mplayer_units(void)
{
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 (table == NULL || count <= 1 || !g_lmp_get_unit_by_eid || !g_lmp_set_owning_mplayer)
return;
for (i = 1; i < count; i++) {
uint8_t *mp = table[i];
uint32_t eid;
uint8_t *unit;
if (mp == NULL)
continue;
eid = *(uint32_t *)(mp + LMP_MP_OFF_UNIT_EID);
if (eid == 0)
continue;
unit = (uint8_t *)g_lmp_get_unit_by_eid(eid);
if (unit != NULL && *(uint8_t **)(unit + LMP_UNIT_OFF_MPLAYER) != mp)
g_lmp_set_owning_mplayer(unit, mp, 1, 1);
}
}
static int32_t resolve_configured_player_team(void)
{
const char *team_str;
char country_buf[64];
uint8_t saved_arcade;
const char *short_country;
const char *axis_country;
const char *allies_country;
if (g_lmp_datablock_get_str != NULL) {
team_str = g_lmp_datablock_get_str(
g_local_mp_mission_settings + LMP_MIS_OFF_CUSTOM_BOTS, "wtoPlayerTeam", "");
if (team_str != NULL && strcmp(team_str, "1") == 0)
return 1;
if (team_str != NULL && strcmp(team_str, "2") == 0)
return 2;
}
memset(country_buf, 0, sizeof(country_buf));
saved_arcade = g_local_mp_mission_settings[LMP_MIS_OFF_ARCADE_COUNTRY];
g_local_mp_mission_settings[LMP_MIS_OFF_ARCADE_COUNTRY] = 1;
g_lmp_get_player_country(g_local_mp_mission_settings, 0, country_buf, (int32_t)sizeof(country_buf));
g_local_mp_mission_settings[LMP_MIS_OFF_ARCADE_COUNTRY] = saved_arcade;
short_country = strncmp(country_buf, "country_", 8) == 0 ? country_buf + 8 : country_buf;
axis_country = *(const char **)(g_local_mp_mission_settings + LMP_MIS_OFF_AXIS_COUNTRY);
allies_country = *(const char **)(g_local_mp_mission_settings + LMP_MIS_OFF_ALLIES_COUNTRY);
if (short_country[0] != '\0') {
if (axis_country != NULL && strcmp(short_country, axis_country) == 0)
return 2;
if (allies_country != NULL && strcmp(short_country, allies_country) == 0)
return 1;
}
return 0;
}
static void __fastcall hook_mplayer_init_from_matching(uint8_t *mplayer, uint8_t *info, uint64_t uid)
{
if (local_mp_is_host() && mplayer != NULL && info != NULL &&
g_local_mp_mission_settings != NULL && g_lmp_get_player_country != NULL &&
local_mp_is_local_mplayer(mplayer)) {
int32_t desired_team = resolve_configured_player_team();
if (desired_team == 1 || desired_team == 2)
*(int32_t *)(info + LMP_MATCH_INFO_OFF_TEAM) = desired_team;
local_mp_events_reset();
log_line("local_mp: initialized local player team=%d",
(int)(*(int32_t *)(info + LMP_MATCH_INFO_OFF_TEAM)));
}
orig_mplayer_init_matching(mplayer, info, uid);
}
static void consume_pending_slot_if_spawned(uint8_t *mplayer)
{
const char *hero_name;
if (!g_respawn_dispatched || mplayer == NULL)
return;
if (mplayer[LMP_MP_OFF_GUI_STATE] == LMP_GUI_STATE_CA_SELECT) {
g_respawn_dispatched = 0;
return;
}
if (mplayer[LMP_MP_OFF_GUI_STATE] != LMP_GUI_STATE_PILOT_CONTROL)
return;
g_respawn_dispatched = 0;
hero_name = g_local_mp_hero ? local_mp_unit_name(*g_local_mp_hero) : NULL;
if (g_pending_slot_id >= 0 && g_pending_slot_id < 32) {
uint32_t bit = 1u << (uint32_t)g_pending_slot_id;
if (g_pending_spare_type < 0)
*(uint32_t *)(mplayer + LMP_MP_OFF_BROKEN_SLOTS_MASK) |= bit;
else
*(uint32_t *)(mplayer + LMP_MP_OFF_USED_SPARES_MASK) |= bit;
log_line("local_mp: spawned unit='%s' slot=%d spare=%d brokenMask=0x%x spareMask=0x%x",
hero_name ? hero_name : "", (int)g_pending_slot_id, (int)g_pending_spare_type,
*(uint32_t *)(mplayer + LMP_MP_OFF_BROKEN_SLOTS_MASK),
*(uint32_t *)(mplayer + LMP_MP_OFF_USED_SPARES_MASK));
g_pending_slot_id = -1;
}
}
static void __fastcall hook_gui_state_update(uint8_t *gui_state_obj, float dt)
{
uint8_t *mplayer;
orig_gui_state_update(gui_state_obj, dt);
if (!local_mp_is_host()) {
g_accept_pending = 0;
g_respawn_dispatched = 0;
g_pending_slot_id = -1;
return;
}
bind_ai_mplayer_units();
mplayer = g_local_mp_mplayer ? *g_local_mp_mplayer : NULL;
consume_pending_slot_if_spawned(mplayer);
if (g_accept_pending && gui_state_obj != NULL) {
uint8_t *econ_state = local_mp_econ_state();
if (mplayer != NULL && econ_state != NULL &&
mplayer[LMP_MP_OFF_GUI_STATE] == LMP_GUI_STATE_CA_READY &&
*(int32_t *)gui_state_obj == LMP_GUI_STATE_CA_READY) {
g_accept_pending = 0;
*(float *)(gui_state_obj + 0x10) = 0.0f;
g_lmp_aircraft_change_result(econ_state, 0);
}
}
}
static int64_t __fastcall hook_do_respawn_player(void *app)
{
uint8_t *mplayer;
int64_t result;
if (!local_mp_is_host())
return orig_do_respawn(app);
mplayer = g_local_mp_mplayer ? *g_local_mp_mplayer : NULL;
if (mplayer != NULL && mplayer[LMP_MP_OFF_GUI_STATE] != LMP_GUI_STATE_PILOT_CONTROL)
g_respawn_dispatched = 1;
result = orig_do_respawn(app);
consume_pending_slot_if_spawned(mplayer);
bind_ai_mplayer_units();
return result;
}
static uint8_t __fastcall hook_is_slot_ready_at_host(int32_t slot_id, const char *unit_name, uint8_t debug)
{
uint8_t *mplayer;
if (!local_mp_is_host())
return orig_is_slot_ready_at_host(slot_id, unit_name, debug);
if (orig_is_slot_ready_at_host(slot_id, unit_name, debug))
return 1;
if (slot_id < 0 || slot_id >= 32 || unit_name == NULL || unit_name[0] == '\0')
return 0;
mplayer = g_local_mp_mplayer ? *g_local_mp_mplayer : NULL;
if (mplayer == NULL)
return 1;
return (*(uint32_t *)(mplayer + LMP_MP_OFF_BROKEN_SLOTS_MASK) & (1u << (uint32_t)slot_id)) == 0 ? 1 : 0;
}
int install_local_mp_player_hooks(void)
{
uint8_t *p_gui_update = g_lmp.gui_state_update;
uint8_t *p_mp_init = g_lmp.mplayer_init_matching;
uint8_t *p_do_respawn = g_lmp.do_respawn_player;
uint8_t *p_slot_ready = g_lmp.is_slot_ready_at_host;
if (MH_CreateHook(p_gui_update, (void *)&hook_gui_state_update, (void **)&orig_gui_state_update) != MH_OK ||
MH_EnableHook(p_gui_update) != MH_OK ||
MH_CreateHook(p_mp_init, (void *)&hook_mplayer_init_from_matching, (void **)&orig_mplayer_init_matching) != MH_OK ||
MH_EnableHook(p_mp_init) != MH_OK ||
MH_CreateHook(p_do_respawn, (void *)&hook_do_respawn_player, (void **)&orig_do_respawn) != MH_OK ||
MH_EnableHook(p_do_respawn) != MH_OK ||
MH_CreateHook(p_slot_ready, (void *)&hook_is_slot_ready_at_host, (void **)&orig_is_slot_ready_at_host) != MH_OK ||
MH_EnableHook(p_slot_ready) != MH_OK) {
log_line("local_mp: failed to install player state hooks");
return 0;
}
return 1;
}