refactor(native): batch MinHook enable calls and log status codes
Switch ws2_32 and LocalMP hook installers to MH_QueueEnableHook with two MH_ApplyQueued passes (mandatory network hooks first, then all LocalMP modules), reducing DllMain thread-freeze cycles from 26 to 2. Roll back partial module hooks on error and log MH_StatusToString diagnostics to wto_ws.log. Checked: native/wto-net/build.ps1 compiles cleanly with zero warnings in project sources, and PE inspection confirms WS2_32/KERNEL32 imports and 49 exports. In-game verification pending.
This commit is contained in:
1 parent
90bd5a315d
commit
96077153c6
11 files changed
+120
-105
No files matched your search
@@ -20,12 +20,20 @@ static HMODULE g_ws2;
|
||||
int hook_export(const char *name, void *replacement, void **original)
|
||||
{
|
||||
void *target = (void *)GetProcAddress(g_ws2, name);
|
||||
MH_STATUS status;
|
||||
if (target == NULL) {
|
||||
log_line("hook %s: not exported by ws2_32.dll", name);
|
||||
return 0;
|
||||
}
|
||||
if (MH_CreateHook(target, replacement, original) != MH_OK || MH_EnableHook(target) != MH_OK) {
|
||||
log_line("hook %s: failed", name);
|
||||
status = MH_CreateHook(target, replacement, original);
|
||||
if (status != MH_OK) {
|
||||
log_line("hook %s: MH_CreateHook failed (%s)", name, MH_StatusToString(status));
|
||||
return 0;
|
||||
}
|
||||
status = MH_QueueEnableHook(target);
|
||||
if (status != MH_OK) {
|
||||
log_line("hook %s: MH_QueueEnableHook failed (%s)", name, MH_StatusToString(status));
|
||||
MH_RemoveHook(target);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
@@ -33,12 +41,26 @@ int hook_export(const char *name, void *replacement, void **original)
|
||||
|
||||
static int install(void)
|
||||
{
|
||||
MH_STATUS status;
|
||||
/* ws2_32.dll is already loaded: this DLL links against it. */
|
||||
g_ws2 = GetModuleHandleW(L"ws2_32.dll");
|
||||
if (g_ws2 == NULL || MH_Initialize() != MH_OK)
|
||||
if (g_ws2 == NULL) {
|
||||
log_line("wto_ws: ws2_32.dll module handle unavailable");
|
||||
return 0;
|
||||
}
|
||||
status = MH_Initialize();
|
||||
if (status != MH_OK) {
|
||||
log_line("wto_ws: MH_Initialize failed (%s)", MH_StatusToString(status));
|
||||
return 0;
|
||||
}
|
||||
if (!install_resolver_hooks() || !install_connection_hooks())
|
||||
return 0;
|
||||
/* Enable all mandatory network hooks in one thread-freeze pass before touching LocalMP hooks. */
|
||||
status = MH_ApplyQueued();
|
||||
if (status != MH_OK) {
|
||||
log_line("wto_ws: MH_ApplyQueued for ws2_32 failed (%s)", MH_StatusToString(status));
|
||||
return 0;
|
||||
}
|
||||
install_local_mp_hooks();
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -125,15 +125,12 @@ static uint8_t __fastcall hook_mp_construct_local(uint8_t *mp, void *mis, void *
|
||||
|
||||
int install_local_mp_action_hooks(void)
|
||||
{
|
||||
uint8_t *p_construct = g_lmp.mp_construct_local;
|
||||
const LocalMpHookSpec hooks[] = {
|
||||
{ "mp_construct_local", g_lmp.mp_construct_local, (void *)&hook_mp_construct_local, (void **)&orig_mp_construct_local },
|
||||
};
|
||||
|
||||
fn_mp_init_chat = (MpInitChatFn)g_lmp.mp_init_chat;
|
||||
fn_dagmem_alloc = (DagmemAllocFn)g_lmp.dagmem_alloc;
|
||||
|
||||
if (MH_CreateHook(p_construct, (void *)&hook_mp_construct_local, (void **)&orig_mp_construct_local) != MH_OK ||
|
||||
MH_EnableHook(p_construct) != MH_OK) {
|
||||
log_line("local_mp: failed to install the LocalMP construction hook");
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
return local_mp_queue_hooks("local_mp_actions", hooks, sizeof(hooks) / sizeof(hooks[0]));
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "hook_config.h"
|
||||
#include "local_mp_internal.h"
|
||||
#include "local_mp_offsets.h"
|
||||
#include "MinHook.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
@@ -162,10 +163,38 @@ static void use_core_values(void)
|
||||
g_lmp_datablock_get_str = (LmpDataBlockGetStrFn)g_lmp.datablock_get_str;
|
||||
}
|
||||
|
||||
int local_mp_queue_hooks(const char *module, const LocalMpHookSpec *specs, size_t count)
|
||||
{
|
||||
size_t created = 0;
|
||||
|
||||
while (created < count) {
|
||||
MH_STATUS status = MH_CreateHook(specs[created].target, specs[created].detour, specs[created].original);
|
||||
if (status != MH_OK) {
|
||||
log_line("local_mp: %s hook %s MH_CreateHook failed (%s)",
|
||||
module, specs[created].name, MH_StatusToString(status));
|
||||
goto failed;
|
||||
}
|
||||
++created;
|
||||
status = MH_QueueEnableHook(specs[created - 1].target);
|
||||
if (status != MH_OK) {
|
||||
log_line("local_mp: %s hook %s MH_QueueEnableHook failed (%s)",
|
||||
module, specs[created - 1].name, MH_StatusToString(status));
|
||||
goto failed;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
|
||||
failed:
|
||||
while (created != 0)
|
||||
MH_RemoveHook(specs[--created].target);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void install_local_mp_hooks(void)
|
||||
{
|
||||
size_t i;
|
||||
unsigned installed = 0;
|
||||
MH_STATUS status;
|
||||
|
||||
if (!config_native_matches_executable()) {
|
||||
log_line("local_mp: wto_ws.cfg was built for another aces.exe, LocalMP bridge disabled");
|
||||
@@ -184,5 +213,12 @@ void install_local_mp_hooks(void)
|
||||
else
|
||||
log_line("local_mp: %s failed to install its hooks", MODULES[i].name);
|
||||
}
|
||||
if (installed > 0) {
|
||||
status = MH_ApplyQueued();
|
||||
if (status != MH_OK) {
|
||||
log_line("local_mp: MH_ApplyQueued failed (%s), LocalMP hooks not enabled", MH_StatusToString(status));
|
||||
return;
|
||||
}
|
||||
}
|
||||
log_line("local_mp: %u of %u LocalMP modules installed", installed, (unsigned)(sizeof(MODULES) / sizeof(MODULES[0])));
|
||||
}
|
||||
@@ -129,28 +129,16 @@ static void __fastcall hook_deliver_damage_hud(int32_t type, int32_t text_id, in
|
||||
|
||||
int install_local_mp_damage_notification_hooks(void)
|
||||
{
|
||||
const struct { void *target, *detour, **original; } hooks[] = {
|
||||
{ g_lmp.barrel_notice_deliver, (void *)&hook_deliver_hud, (void **)&original_deliver_hud },
|
||||
{ g_lmp.barrel_damage, (void *)&hook_barrel_damage, (void **)&original_barrel_damage },
|
||||
{ g_lmp.breech_fire_update, (void *)&hook_fire_update, (void **)&original_fire_update },
|
||||
{ g_lmp.hud_damage_deliver, (void *)&hook_deliver_damage_hud, (void **)&original_damage_deliver_hud },
|
||||
const LocalMpHookSpec hooks[] = {
|
||||
{ "barrel_notice_deliver", g_lmp.barrel_notice_deliver, (void *)&hook_deliver_hud, (void **)&original_deliver_hud },
|
||||
{ "barrel_damage", g_lmp.barrel_damage, (void *)&hook_barrel_damage, (void **)&original_barrel_damage },
|
||||
{ "breech_fire_update", g_lmp.breech_fire_update, (void *)&hook_fire_update, (void **)&original_fire_update },
|
||||
{ "hud_damage_deliver", g_lmp.hud_damage_deliver, (void *)&hook_deliver_damage_hud, (void **)&original_damage_deliver_hud },
|
||||
};
|
||||
size_t created = 0;
|
||||
|
||||
if (g_lmp.barrel_notice_deliver != g_lmp.breech_notice_deliver)
|
||||
if (g_lmp.barrel_notice_deliver != g_lmp.breech_notice_deliver) {
|
||||
log_line("local_mp: local_mp_damage_notifications skipped: barrel and breech deliver addresses differ");
|
||||
return 0;
|
||||
while (created < sizeof(hooks) / sizeof(hooks[0])) {
|
||||
if (MH_CreateHook(hooks[created].target, hooks[created].detour, hooks[created].original) != MH_OK)
|
||||
goto failed;
|
||||
++created;
|
||||
if (MH_QueueEnableHook(hooks[created - 1].target) != MH_OK)
|
||||
goto failed;
|
||||
}
|
||||
if (MH_ApplyQueued() == MH_OK)
|
||||
return 1;
|
||||
|
||||
failed:
|
||||
while (created != 0)
|
||||
MH_RemoveHook(hooks[--created].target);
|
||||
return 0;
|
||||
return local_mp_queue_hooks("local_mp_damage_notifications", hooks, sizeof(hooks) / sizeof(hooks[0]));
|
||||
}
|
||||
@@ -226,27 +226,14 @@ static void __fastcall hook_dispatch_naval_damage(int64_t uid, uint8_t *victim,
|
||||
|
||||
int install_local_mp_event_hooks(void)
|
||||
{
|
||||
uint8_t *p_igm_ass = g_lmp.iground_award_assists;
|
||||
uint8_t *p_assist = g_lmp.dispatch_assist;
|
||||
uint8_t *p_cap = g_lmp.dispatch_capture_zone;
|
||||
uint8_t *p_hit = g_lmp.dispatch_hit_crit;
|
||||
uint8_t *p_sev = g_lmp.dispatch_severe_damage;
|
||||
uint8_t *p_nav = g_lmp.dispatch_naval_damage;
|
||||
const LocalMpHookSpec hooks[] = {
|
||||
{ "iground_award_assists", g_lmp.iground_award_assists, (void *)&hook_iground_award_assists, (void **)&orig_iground_award_assists },
|
||||
{ "dispatch_assist", g_lmp.dispatch_assist, (void *)&hook_dispatch_assist, (void **)&orig_dispatch_assist },
|
||||
{ "dispatch_capture_zone", g_lmp.dispatch_capture_zone, (void *)&hook_dispatch_capture_zone, (void **)&orig_dispatch_capture_zone },
|
||||
{ "dispatch_hit_crit", g_lmp.dispatch_hit_crit, (void *)&hook_dispatch_hit_crit, (void **)&orig_dispatch_hit_crit },
|
||||
{ "dispatch_severe_damage", g_lmp.dispatch_severe_damage, (void *)&hook_dispatch_severe_damage, (void **)&orig_dispatch_severe_damage },
|
||||
{ "dispatch_naval_damage", g_lmp.dispatch_naval_damage, (void *)&hook_dispatch_naval_damage, (void **)&orig_dispatch_naval_damage },
|
||||
};
|
||||
|
||||
if (MH_CreateHook(p_igm_ass, (void *)&hook_iground_award_assists, (void **)&orig_iground_award_assists) != MH_OK ||
|
||||
MH_EnableHook(p_igm_ass) != MH_OK ||
|
||||
MH_CreateHook(p_assist, (void *)&hook_dispatch_assist, (void **)&orig_dispatch_assist) != MH_OK ||
|
||||
MH_EnableHook(p_assist) != MH_OK ||
|
||||
MH_CreateHook(p_cap, (void *)&hook_dispatch_capture_zone, (void **)&orig_dispatch_capture_zone) != MH_OK ||
|
||||
MH_EnableHook(p_cap) != MH_OK ||
|
||||
MH_CreateHook(p_hit, (void *)&hook_dispatch_hit_crit, (void **)&orig_dispatch_hit_crit) != MH_OK ||
|
||||
MH_EnableHook(p_hit) != MH_OK ||
|
||||
MH_CreateHook(p_sev, (void *)&hook_dispatch_severe_damage, (void **)&orig_dispatch_severe_damage) != MH_OK ||
|
||||
MH_EnableHook(p_sev) != MH_OK ||
|
||||
MH_CreateHook(p_nav, (void *)&hook_dispatch_naval_damage, (void **)&orig_dispatch_naval_damage) != MH_OK ||
|
||||
MH_EnableHook(p_nav) != MH_OK) {
|
||||
log_line("local_mp: failed to install combat event hooks");
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
return local_mp_queue_hooks("local_mp_events", hooks, sizeof(hooks) / sizeof(hooks[0]));
|
||||
}
|
||||
@@ -82,6 +82,16 @@ void local_mp_add_mplayer_score(uint8_t *mp, float delta, uint32_t event_id);
|
||||
void local_mp_mark_mplayer_dirty(uint8_t *mp);
|
||||
void local_mp_inc_mplayer_stat(uint8_t *mp, size_t off);
|
||||
|
||||
/* Hook descriptor queued by a LocalMP module and enabled in a single batch by local_mp_core.c. */
|
||||
typedef struct LocalMpHookSpec {
|
||||
const char *name;
|
||||
void *target;
|
||||
void *detour;
|
||||
void **original;
|
||||
} LocalMpHookSpec;
|
||||
|
||||
int local_mp_queue_hooks(const char *module, const LocalMpHookSpec *specs, size_t count);
|
||||
|
||||
/* Module hook installers, called by install_local_mp_hooks() once the module's values are loaded. */
|
||||
int install_local_mp_player_hooks(void);
|
||||
int install_local_mp_loadout_hooks(void);
|
||||
|
||||
@@ -192,18 +192,13 @@ static void __fastcall hook_on_unit_dead_once(uint8_t *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;
|
||||
const LocalMpHookSpec hooks[] = {
|
||||
{ "iground_on_dead", g_lmp.iground_on_dead, (void *)&hook_iground_on_became_dead, (void **)&orig_iground_on_dead },
|
||||
{ "on_unit_dead_once", g_lmp.on_unit_dead_once, (void *)&hook_on_unit_dead_once, (void **)&orig_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;
|
||||
return local_mp_queue_hooks("local_mp_kills", hooks, sizeof(hooks) / sizeof(hooks[0]));
|
||||
}
|
||||
@@ -202,7 +202,9 @@ static void __fastcall hook_sem_change_aircraft_send(
|
||||
|
||||
int install_local_mp_loadout_hooks(void)
|
||||
{
|
||||
uint8_t *p_sem_send = g_lmp.sem_change_aircraft_send;
|
||||
const LocalMpHookSpec hooks[] = {
|
||||
{ "sem_change_aircraft_send", g_lmp.sem_change_aircraft_send, (void *)&hook_sem_change_aircraft_send, (void **)&orig_sem_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) {
|
||||
@@ -222,10 +224,5 @@ int install_local_mp_loadout_hooks(void)
|
||||
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;
|
||||
return local_mp_queue_hooks("local_mp_loadout", hooks, sizeof(hooks) / sizeof(hooks[0]));
|
||||
}
|
||||
@@ -129,17 +129,11 @@ static uint64_t __fastcall hook_cta_logic(uint8_t *ctx, void *a2, void *a3, void
|
||||
|
||||
int install_local_mp_nuke_hooks(void)
|
||||
{
|
||||
uint8_t *p_launch = g_lmp.shell_launch;
|
||||
uint8_t *p_explode = g_lmp.shell_explode;
|
||||
uint8_t *p_cta = g_lmp.cta_logic_action;
|
||||
if (MH_CreateHook(p_launch, (void *)&hook_shell_launch, (void **)&orig_shell_launch) != MH_OK ||
|
||||
MH_CreateHook(p_explode, (void *)&hook_shell_explode, (void **)&orig_shell_explode) != MH_OK ||
|
||||
MH_CreateHook(p_cta, (void *)&hook_cta_logic, (void **)&orig_cta_logic) != MH_OK ||
|
||||
MH_EnableHook(p_launch) != MH_OK ||
|
||||
MH_EnableHook(p_explode) != MH_OK ||
|
||||
MH_EnableHook(p_cta) != MH_OK) {
|
||||
log_line("local_mp: failed to install the nuclear instant-win hooks");
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
const LocalMpHookSpec hooks[] = {
|
||||
{ "shell_launch", g_lmp.shell_launch, (void *)&hook_shell_launch, (void **)&orig_shell_launch },
|
||||
{ "shell_explode", g_lmp.shell_explode, (void *)&hook_shell_explode, (void **)&orig_shell_explode },
|
||||
{ "cta_logic_action", g_lmp.cta_logic_action, (void *)&hook_cta_logic, (void **)&orig_cta_logic },
|
||||
};
|
||||
|
||||
return local_mp_queue_hooks("local_mp_nuke", hooks, sizeof(hooks) / sizeof(hooks[0]));
|
||||
}
|
||||
@@ -208,21 +208,12 @@ static uint8_t __fastcall hook_is_slot_ready_at_host(int32_t slot_id, const char
|
||||
|
||||
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;
|
||||
const LocalMpHookSpec hooks[] = {
|
||||
{ "gui_state_update", g_lmp.gui_state_update, (void *)&hook_gui_state_update, (void **)&orig_gui_state_update },
|
||||
{ "mplayer_init_matching", g_lmp.mplayer_init_matching, (void *)&hook_mplayer_init_from_matching, (void **)&orig_mplayer_init_matching },
|
||||
{ "do_respawn_player", g_lmp.do_respawn_player, (void *)&hook_do_respawn_player, (void **)&orig_do_respawn },
|
||||
{ "is_slot_ready_at_host", g_lmp.is_slot_ready_at_host, (void *)&hook_is_slot_ready_at_host, (void **)&orig_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;
|
||||
return local_mp_queue_hooks("local_mp_players", hooks, sizeof(hooks) / sizeof(hooks[0]));
|
||||
}
|
||||
@@ -100,11 +100,9 @@ static void __fastcall hook_set_controlled_unit(uint8_t *mp, uint32_t eid)
|
||||
|
||||
int install_local_mp_support_hooks(void)
|
||||
{
|
||||
uint8_t *p_set = g_lmp.set_controlled_unit;
|
||||
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;
|
||||
const LocalMpHookSpec hooks[] = {
|
||||
{ "set_controlled_unit", g_lmp.set_controlled_unit, (void *)&hook_set_controlled_unit, (void **)&orig_set_controlled_unit },
|
||||
};
|
||||
|
||||
return local_mp_queue_hooks("local_mp_support", hooks, sizeof(hooks) / sizeof(hooks[0]));
|
||||
}
|
||||
Reference in new issue
Block a user