Files
WT-Offline/native/wto-net/src/local_mp_loadout.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

232 lines
10 KiB
C

/*
* LocalMP vehicle loadout, modification map, decals, 3D attachables, and crew-skill bridge.
*
* The 2.61.0.37 client has no receiver for its own vehicle change request (0xb01d,
* SemChangeAircraftRequest) on a host; hook_sem_change_aircraft_send builds the
* ChangeUnitReflection from the request (weapon, modifications, bullets, skin, decals,
* attachables, crew skills, spawn base) and calls Unit::changeAircraft itself.
*/
#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 *SemChangeAircraftSendFn)(
void *self, const char *unit_name, const char *weapon_name, const void *modifications_blk,
const char *skin_name, const void *custom_preset_blk, uint8_t flags_byte,
const void *serialized_bullets, int32_t crew_id, int32_t resp_base_id, float fuel,
float gun_targeting_dist, uint8_t vertical_targeting, float bomb_activation_time,
uint16_t extra_u16, uint8_t extra_b1, uint8_t extra_b2, const char *extra_str,
const char *custom_skin, int32_t slot_id, const void *spare_data, int32_t spare_type
);
typedef void *(__fastcall *ReflCtorFn)(void *refl);
typedef void (__fastcall *ReflDtorFn)(void *refl);
typedef uint8_t (__fastcall *UnitChangeAircraftFn)(void *unit, void *refl, uint8_t async);
typedef void *(__fastcall *DataBlockSetFromFn)(void *dst_blk, const void *src_blk, const char *src_fname);
typedef void (__fastcall *DataBlockDtorFn)(void *blk);
typedef void (__fastcall *BulletSetVecAssignFn)(void *dst_vec, const void *first, const void *last);
typedef void *(__fastcall *GetDecalsBlkFn)(
void *econ, void *out_blk, const char *unit, const char *skin, const char *def_key, const char *cskin
);
typedef const void *(__fastcall *GetAttachablesBlkFn)(
void *econ, const char *unit, const char *skin, const char *cskin
);
typedef uint64_t (__fastcall *StrHashFnv1Fn)(const char *str, size_t len);
typedef int32_t (__fastcall *NameMapAddNameIdFn)(void *map, const char *str, size_t len, uint64_t hash);
typedef void (__fastcall *CrewSkillsInitFn)(void *crew_skills, void *econ_state, const char *unit_name, float mult);
static SemChangeAircraftSendFn orig_sem_send;
static ReflCtorFn fn_refl_ctor;
static ReflDtorFn fn_refl_dtor;
static UnitChangeAircraftFn fn_unit_change_aircraft;
static DataBlockSetFromFn fn_datablock_set_from;
static DataBlockDtorFn fn_datablock_dtor;
static BulletSetVecAssignFn fn_bullet_vec_assign;
static GetDecalsBlkFn fn_get_decals_blk;
static GetAttachablesBlkFn fn_get_attachables_blk;
static StrHashFnv1Fn fn_str_hash;
static NameMapAddNameIdFn fn_namemap_add;
static CrewSkillsInitFn fn_crew_skills_init;
static void populate_namemap_csv(void *map, const char *csv)
{
const char *p = csv;
char tok[96];
if (map == NULL || p == NULL || *p == '\0')
return;
while (*p != '\0') {
const char *sep = strchr(p, ';');
size_t len = sep ? (size_t)(sep - p) : strlen(p);
if (len > 0 && len < sizeof(tok)) {
memcpy(tok, p, len);
tok[len] = '\0';
fn_namemap_add(map, tok, len, fn_str_hash(tok, len));
}
if (sep == NULL)
break;
p = sep + 1;
}
}
static int parse_spawn_metadata_blk(const void *mods_blk, uint8_t *refl)
{
const char *enabled_csv = "";
const char *disabled_csv = "";
const char *silent_str = "0";
if (mods_blk != NULL && g_lmp_datablock_get_str != NULL) {
enabled_csv = g_lmp_datablock_get_str(mods_blk, "enabledMods", "");
disabled_csv = g_lmp_datablock_get_str(mods_blk, "disabledMods", "");
silent_str = g_lmp_datablock_get_str(mods_blk, "isSilentPreview", "0");
}
if (refl != NULL) {
populate_namemap_csv(refl + LMP_REFL_OFF_ENABLED_MODS, enabled_csv);
populate_namemap_csv(refl + LMP_REFL_OFF_DISABLED_MODS, disabled_csv);
}
return silent_str != NULL && strcmp(silent_str, "1") == 0;
}
static void populate_cosmetics(
uint8_t *refl, uint8_t *econ_state, const char *unit_name, const char *skin_name, const char *custom_skin
)
{
__declspec(align(16)) uint8_t decals_tmp[64];
const char *u = unit_name ? unit_name : "";
const char *s = skin_name ? skin_name : "";
const char *c = custom_skin ? custom_skin : "";
const void *att_blk;
memset(decals_tmp, 0, sizeof(decals_tmp));
if (fn_get_decals_blk(econ_state, decals_tmp, u, s, "defaultDecals", c) != NULL) {
fn_datablock_set_from(refl + LMP_REFL_OFF_DECALS_BLK, decals_tmp, NULL);
fn_datablock_dtor(decals_tmp);
}
att_blk = fn_get_attachables_blk(econ_state, u, s, c);
if (att_blk != NULL)
fn_datablock_set_from(refl + LMP_REFL_OFF_ATTACHABLES_BLK, att_blk, NULL);
}
static void apply_mplayer_spawn_base(uint8_t *mplayer, int32_t req_base_id)
{
*(int32_t *)(mplayer + LMP_MP_OFF_RESP_BASE_ID) = req_base_id;
*(int32_t *)(mplayer + LMP_MP_OFF_SPAWN_DELAY_A) = -1;
*(int32_t *)(mplayer + LMP_MP_OFF_SPAWN_DELAY_B) = -1;
*(float *)(mplayer + LMP_MP_OFF_SPAWN_POS_X) = 0.0f;
*(float *)(mplayer + LMP_MP_OFF_SPAWN_POS_Y) = 0.0f;
*(float *)(mplayer + LMP_MP_OFF_SPAWN_POS_Z) = 0.0f;
}
static void __fastcall hook_sem_change_aircraft_send(
void *self, const char *unit_name, const char *weapon_name, const void *modifications_blk,
const char *skin_name, const void *custom_preset_blk, uint8_t flags_byte,
const void *serialized_bullets, int32_t crew_id, int32_t resp_base_id, float fuel,
float gun_targeting_dist, uint8_t vertical_targeting, float bomb_activation_time,
uint16_t extra_u16, uint8_t extra_b1, uint8_t extra_b2, const char *extra_str,
const char *custom_skin, int32_t slot_id, const void *spare_data, int32_t spare_type
)
{
uint8_t *hero, *mplayer, *econ_state, *req;
const void *bullets_begin, *bullets_end;
void *crew_skills;
int32_t req_slot_id, req_base_id, req_spare_type;
int is_silent;
__declspec(align(16)) uint8_t refl[LMP_REFL_SIZE];
if (!local_mp_is_host()) {
orig_sem_send(self, unit_name, weapon_name, modifications_blk, skin_name, custom_preset_blk,
flags_byte, serialized_bullets, crew_id, resp_base_id, fuel, gun_targeting_dist,
vertical_targeting, bomb_activation_time, extra_u16, extra_b1, extra_b2,
extra_str, custom_skin, slot_id, spare_data, spare_type);
return;
}
hero = g_local_mp_hero ? *g_local_mp_hero : NULL;
mplayer = g_local_mp_mplayer ? *g_local_mp_mplayer : (hero ? *(uint8_t **)(hero + LMP_UNIT_OFF_MPLAYER) : NULL);
econ_state = local_mp_econ_state();
if (hero == NULL || mplayer == NULL || econ_state == NULL)
return;
if (mplayer[LMP_MP_OFF_GUI_STATE] == LMP_GUI_STATE_PILOT_CONTROL &&
parse_spawn_metadata_blk(modifications_blk, NULL)) {
g_lmp_aircraft_change_result(econ_state, 0);
return;
}
req = econ_state + LMP_ECON_OFF_CHANGE_REQ;
bullets_begin = *(const void **)(req + LMP_REQ_OFF_BULLETS_BEGIN);
bullets_end = *(const void **)(req + LMP_REQ_OFF_BULLETS_END);
req_slot_id = *(int32_t *)(req + LMP_REQ_OFF_SLOT_ID);
req_base_id = *(int32_t *)(req + LMP_REQ_OFF_RESP_BASE_ID);
req_spare_type = *(int32_t *)(req + LMP_REQ_OFF_SPARE_TYPE);
apply_mplayer_spawn_base(mplayer, req_base_id);
memset(refl, 0, sizeof(refl));
fn_refl_ctor(refl);
*(char **)(refl + LMP_REFL_OFF_UNIT_NAME) = local_mp_strdup(unit_name);
*(char **)(refl + LMP_REFL_OFF_WEAPON_NAME) = local_mp_strdup(weapon_name);
*(char **)(refl + LMP_REFL_OFF_SKIN_NAME) = local_mp_strdup(skin_name);
*(char **)(refl + LMP_REFL_OFF_CUSTOM_SKIN) = local_mp_strdup(custom_skin);
populate_cosmetics(refl, econ_state, unit_name, skin_name, custom_skin);
is_silent = parse_spawn_metadata_blk(modifications_blk, refl);
if (custom_preset_blk != NULL)
fn_datablock_set_from(refl + LMP_REFL_OFF_CUSTOM_PRESET_BLK, custom_preset_blk, NULL);
if (bullets_begin != NULL && bullets_end != NULL && bullets_end > bullets_begin)
fn_bullet_vec_assign(refl + LMP_REFL_OFF_BULLETS_VEC, bullets_begin, bullets_end);
*(uint32_t *)(refl + LMP_REFL_OFF_FLAGS) |= 0x50u;
*(float *)(refl + LMP_REFL_OFF_FUEL) = fuel;
*(float *)(refl + LMP_REFL_OFF_GUN_CONVERGENCE) = gun_targeting_dist;
refl[LMP_REFL_OFF_VERT_TARGETING] = vertical_targeting ? 1u : 0u;
*(float *)(refl + LMP_REFL_OFF_BOMB_FUSE) = bomb_activation_time;
fn_unit_change_aircraft(hero, refl, 1);
fn_refl_dtor(refl);
crew_skills = *(void **)(hero + LMP_UNIT_OFF_CREW_SKILLS);
if (crew_skills != NULL && unit_name != NULL && unit_name[0] != '\0')
fn_crew_skills_init(crew_skills, econ_state, unit_name, 1.0f);
if (mplayer[LMP_MP_OFF_GUI_STATE] != LMP_GUI_STATE_PILOT_CONTROL &&
mplayer[LMP_MP_OFF_GUI_STATE] != LMP_GUI_STATE_CA_READY)
g_lmp_set_player_gui_state((int32_t)mplayer[LMP_MP_OFF_PID], LMP_GUI_STATE_CA_READY);
local_mp_players_queue_spawn(req_slot_id, req_spare_type, is_silent);
log_line("local_mp: configured unit='%s' weapon='%s' slot=%d silent=%d",
unit_name ? unit_name : "", weapon_name ? weapon_name : "", (int)req_slot_id, is_silent);
}
int install_local_mp_loadout_hooks(void)
{
uint8_t *p_sem_send = g_lmp.sem_change_aircraft_send;
/* The reflection is built in a fixed stack buffer; its bullet sets vector is its last member. */
if (LMP_REFL_OFF_BULLETS_VEC + 0x20u > LMP_REFL_SIZE) {
log_line("local_mp: ChangeUnitReflection members reach +0x%zx, past the 0x%x-byte buffer",
LMP_REFL_OFF_BULLETS_VEC + 0x20u, LMP_REFL_SIZE);
return 0;
}
fn_refl_ctor = (ReflCtorFn)g_lmp.refl_ctor;
fn_refl_dtor = (ReflDtorFn)g_lmp.refl_dtor;
fn_unit_change_aircraft = (UnitChangeAircraftFn)g_lmp.unit_change_aircraft;
fn_datablock_set_from = (DataBlockSetFromFn)g_lmp.datablock_set_from;
fn_datablock_dtor = (DataBlockDtorFn)g_lmp.datablock_dtor;
fn_bullet_vec_assign = (BulletSetVecAssignFn)g_lmp.bullet_vec_assign;
fn_get_decals_blk = (GetDecalsBlkFn)g_lmp.get_decals_blk;
fn_get_attachables_blk = (GetAttachablesBlkFn)g_lmp.get_attachables_blk;
fn_str_hash = (StrHashFnv1Fn)g_lmp.str_hash_fnv1;
fn_namemap_add = (NameMapAddNameIdFn)g_lmp.namemap_add_name_id;
fn_crew_skills_init = (CrewSkillsInitFn)g_lmp.crew_skills_init;
if (MH_CreateHook(p_sem_send, (void *)&hook_sem_change_aircraft_send, (void **)&orig_sem_send) != MH_OK ||
MH_EnableHook(p_sem_send) != MH_OK) {
log_line("local_mp: failed to install SemChangeAircraftRequest::send hook");
return 0;
}
return 1;
}