/* * 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 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; }