diff --git a/native/wto-net/build.ps1 b/native/wto-net/build.ps1 index aa6d954..2c7c683 100644 --- a/native/wto-net/build.ps1 +++ b/native/wto-net/build.ps1 @@ -22,7 +22,7 @@ New-Item -ItemType Directory -Force $obj | Out-Null $sources = @( "$src\dll_main.c", "$src\hook_config.c", "$src\redirect_rules.c", "$src\resolver_hooks.c", "$src\connection_hooks.c", - "$src\local_mp_core.c", "$src\local_mp_players.c", "$src\local_mp_loadout.c", "$src\local_mp_actions.c", "$src\local_mp_awards.c", "$src\local_mp_kills.c", "$src\local_mp_events.c", "$src\local_mp_scores.c", + "$src\local_mp_core.c", "$src\local_mp_players.c", "$src\local_mp_loadout.c", "$src\local_mp_actions.c", "$src\local_mp_awards.c", "$src\local_mp_kills.c", "$src\local_mp_events.c", "$src\local_mp_scores.c", "$src\local_mp_nuke.c", "$mh\src\hook.c", "$mh\src\buffer.c", "$mh\src\trampoline.c", "$mh\src\hde\hde64.c" ) | ForEach-Object { '"' + (Resolve-Path $_) + '"' } diff --git a/native/wto-net/src/local_mp_core.c b/native/wto-net/src/local_mp_core.c index bc415da..fe68b7c 100644 --- a/native/wto-net/src/local_mp_core.c +++ b/native/wto-net/src/local_mp_core.c @@ -283,7 +283,8 @@ void install_local_mp_hooks(void) !install_local_mp_award_hooks(text, text_size, p_ground_dead) || !install_local_mp_kill_hooks(text, text_size, p_ground_dead) || !install_local_mp_event_hooks(text, text_size) || - !install_local_mp_score_reader(text, text_size)) { + !install_local_mp_score_reader(text, text_size) || + !install_local_mp_nuke_hooks(text, text_size)) { log_line("local_mp: one or more LocalMP subsystem hooks failed to initialize"); return; } diff --git a/native/wto-net/src/local_mp_internal.h b/native/wto-net/src/local_mp_internal.h index ee6876c..39f2b0c 100644 --- a/native/wto-net/src/local_mp_internal.h +++ b/native/wto-net/src/local_mp_internal.h @@ -85,3 +85,4 @@ int install_local_mp_award_hooks(const uint8_t *text, size_t text_size, uint8_t int install_local_mp_kill_hooks(const uint8_t *text, size_t text_size, uint8_t *p_ground_dead); int install_local_mp_event_hooks(const uint8_t *text, size_t text_size); int install_local_mp_score_reader(const uint8_t *text, size_t text_size); +int install_local_mp_nuke_hooks(const uint8_t *text, size_t text_size); diff --git a/native/wto-net/src/local_mp_nuke.c b/native/wto-net/src/local_mp_nuke.c new file mode 100644 index 0000000..ce938df --- /dev/null +++ b/native/wto-net/src/local_mp_nuke.c @@ -0,0 +1,137 @@ +/* + * Instant win of the nuclear bomb in a LocalMP battle. + * + * Online the battle server ends the battle when a bomb with isInstantWinOnExplode explodes + * (gamedata/weapons/bombguns/us_b61.blk and the other killstreak nuclear bombs); the 2.61.0.37 + * client only marks the bomb and plays the explosion. The LocalMP host has no battle server, so + * the battle went on after the explosion. Here the host takes the same decision with the stock + * end of battle: + * + * 1. Bomb launch 0x1431c10: at its end the client sets the launch record's instant-win flag + * (record +0x70, written at 0x143330d / 0x143337d) from the bomb property + * isInstantWinOnExplode (+0x6a6, read at 0x13cb5a5). After it returns, a flagged record of a + * unit with an MPlayer remembers that player's team. + * 2. Nuclear explosion: the ECS system create_nuke_destr_area_es (0x63caf0, registered at + * 0x641d7c) runs for the nuclear explosion entity. The remembered team becomes the winner. + * 3. Domination logic cta_logic_action (0x11bc400, the mp_update_trig action registered at + * 0x11ba558): before it runs, the loser's mission variable mpTi1 / mpTi2 (tickets of team 1 / 2, + * looked up at 0x11bd1c7) is set to 0. The stock logic then ends the battle as on tickets: + * mp_game_over with the result for the local player's team (0x11bf661 / 0x11bf9c1). + */ +#include "hooks.h" +#include "hook_config.h" +#include "local_mp_internal.h" +#include "local_mp_offsets.h" + +#include "MinHook.h" + +#include + +typedef void (__fastcall *ShellLaunchFn)(void **launch, void *a2, uint8_t *owner, void *a4, uint64_t a5, uint64_t a6); +typedef void (__fastcall *EcsSystemFn)(void *a1, void *query, void *a3, void *a4); +typedef uint64_t (__fastcall *LogicActionFn)(uint8_t *ctx, void *a2, void *a3, void *a4); + +static ShellLaunchFn orig_shell_launch; +static EcsSystemFn orig_nuke_destr_area; +static LogicActionFn orig_cta_logic; + +/* Team of the last launched instant-win bomb, and the team that won with it (0 = none). */ +static volatile uint8_t g_bomb_team; +static volatile uint8_t g_winner_team; +static uint8_t *g_logic_ctx; + +static uint8_t owner_team(const uint8_t *unit) +{ + const uint8_t *mp; + + if (unit == NULL) + return 0; + mp = *(uint8_t *const *)(unit + LMP_UNIT_OFF_MPLAYER); + return mp != NULL ? mp[LMP_MP_OFF_TEAM] : 0; +} + +static void __fastcall hook_shell_launch(void **launch, void *a2, uint8_t *owner, void *a4, uint64_t a5, uint64_t a6) +{ + const uint8_t *record; + uint8_t team; + + orig_shell_launch(launch, a2, owner, a4, a5, a6); + if (launch == NULL || !local_mp_is_host()) + return; + record = (const uint8_t *)launch[LMP_SHELL_LAUNCH_IDX_RECORD]; + if (record == NULL || record[LMP_SHELL_REC_OFF_INSTANT_WIN] != 1) + return; + team = owner_team(owner); + if (team != 1 && team != 2) + return; + if (g_bomb_team != team) + log_line("local_mp: instant-win bomb launched by team %u", (unsigned)team); + g_bomb_team = team; +} + +static void __fastcall hook_nuke_destr_area(void *a1, void *query, void *a3, void *a4) +{ + orig_nuke_destr_area(a1, query, a3, a4); + if (!local_mp_is_host() || g_bomb_team == 0 || g_winner_team != 0) + return; + g_winner_team = g_bomb_team; + g_bomb_team = 0; + log_line("local_mp: nuclear explosion, team %u wins", (unsigned)g_winner_team); +} + +static int *mission_int_var(uint8_t *ctx, const char *name) +{ + uint8_t *vars = *(uint8_t **)(ctx + LMP_MISSION_VARS_OFF_DATA); + uint32_t count = *(uint32_t *)(ctx + LMP_MISSION_VARS_OFF_COUNT); + uint32_t i; + + if (vars == NULL) + return NULL; + for (i = 0; i < count; i++) { + uint8_t *var = vars + (size_t)i * LMP_MISSION_VAR_STRIDE; + const char *var_name = *(const char **)(var + LMP_MISSION_VAR_OFF_NAME); + if (var_name != NULL && strcmp(var_name, name) == 0) + return (int *)(var + LMP_MISSION_VAR_OFF_VALUE); + } + return NULL; +} + +static uint64_t __fastcall hook_cta_logic(uint8_t *ctx, void *a2, void *a3, void *a4) +{ + if (ctx != g_logic_ctx) { + /* A new mission: nothing launched or won in the previous one carries over. */ + g_logic_ctx = ctx; + g_bomb_team = 0; + g_winner_team = 0; + } + if (ctx != NULL && g_winner_team != 0 && local_mp_is_host()) { + int *loser_tickets = mission_int_var(ctx, g_winner_team == 1 ? "mpTi2" : "mpTi1"); + if (loser_tickets != NULL && *loser_tickets > 0) { + log_line("local_mp: nuclear win, tickets of team %u set to 0 (were %d)", + (unsigned)(3u - g_winner_team), *loser_tickets); + *loser_tickets = 0; + } + g_winner_team = 0; + } + return orig_cta_logic(ctx, a2, a3, a4); +} + +int install_local_mp_nuke_hooks(const uint8_t *text, size_t text_size) +{ + uint8_t *p_launch = local_mp_scan_unique(text, text_size, "shell launch (instant-win flag)", LMP_SIG_SHELL_LAUNCH); + uint8_t *p_destr = local_mp_scan_unique(text, text_size, "create_nuke_destr_area_es", LMP_SIG_NUKE_DESTR_AREA_ES); + uint8_t *p_cta = local_mp_scan_unique(text, text_size, "cta_logic_action", LMP_SIG_CTA_LOGIC_ACTION); + + if (!p_launch || !p_destr || !p_cta) + return 0; + if (MH_CreateHook(p_launch, (void *)&hook_shell_launch, (void **)&orig_shell_launch) != MH_OK || + MH_CreateHook(p_destr, (void *)&hook_nuke_destr_area, (void **)&orig_nuke_destr_area) != 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_destr) != MH_OK || + MH_EnableHook(p_cta) != MH_OK) { + log_line("local_mp: failed to install the nuclear instant-win hooks"); + return 0; + } + return 1; +} diff --git a/native/wto-net/src/local_mp_offsets.h b/native/wto-net/src/local_mp_offsets.h index 1a80f69..9b1ac2f 100644 --- a/native/wto-net/src/local_mp_offsets.h +++ b/native/wto-net/src/local_mp_offsets.h @@ -267,4 +267,27 @@ #define LMP_SIG_CALLER_HIT_CRIT "8b 0d ?? ?? ?? ?? f7 c1 00 00 08 00 75 ?? f7 c1 00 00 00 01 75 ?? 0f b6 8f 10 10 00 00 39 4e 1c 74 ?? 41 20 c0 0f b6 86 49 01 00 00 0f 57 db f3 0f 5f 9e 30 01 00 00 48 8b 4e 58 85 ed f3 0f 5d 1d ?? ?? ?? ?? 88 44 24 30 0f 9f 44 24 28 44 88 44 24 20 48 89 fa 49 89 f0 e8 ?? ?? ?? ??" #define LMP_SIG_CALLER_SEVERE_DMG "66 83 be b0 06 00 00 ff 0f 85 ?? ?? ?? ?? 48 89 f1 e8 ?? ?? ?? ?? 84 c0 0f 84 ?? ?? ?? ?? 48 8d 9e b0 06 00 00 48 8b 4f 58 48 89 f2 49 89 f8 e8 ?? ?? ?? ?? 48 89 f1 48 89 fa e8 ?? ?? ?? ?? 0f b6 ae d4 06 00 00" #define LMP_SIG_CALLER_NAVAL_DMG "66 83 b9 90 0f 00 00 01 77 ?? 66 83 ff ff 74 ?? 0f b7 c7 0f 57 d2 f3 0f 2a d0 f3 0f 59 15 ?? ?? ?? ?? eb ?? f3 0f 10 15 ?? ?? ?? ?? 48 8b 94 24 c0 03 00 00 0f b6 7c 24 5b 41 89 f9 e8 ?? ?? ?? ?? 0f 57 c0 0f 2e f0 0f 96 c0 40 08 f8 a8 01 75 ?? 48 8b 96 e8 0a 00 00 4c 8b 84 24 c0 03 00 00 49 8b 48 58 0f 28 de e8 ?? ?? ?? ??" +/* + * Nuclear instant win (local_mp_nuke.c, 2.61.0.37): + * bomb launch 0x1431c10: launch[3] is the launch record, record+0x70 its instant-win flag + * (set at 0x143330d / 0x143337d from the bomb property isInstantWinOnExplode); + * create_nuke_destr_area_es 0x63caf0 (registered at 0x641d7c); + * cta_logic_action 0x11bc400: its context holds the mission variables at +0x40 (data) and + * +0x50 (count), 0x20 bytes each, name pointer at +0 and int value at +8 (0x11bd150). + * + * @anchor "isInstantWinOnExplode" + * @anchor "create_nuke_destr_area_es" + * @anchor "cta_logic_action" + * @anchor "mpTi1" + */ +#define LMP_SHELL_LAUNCH_IDX_RECORD 3u +#define LMP_SHELL_REC_OFF_INSTANT_WIN 0x0070u +#define LMP_MISSION_VARS_OFF_DATA 0x0040u +#define LMP_MISSION_VARS_OFF_COUNT 0x0050u +#define LMP_MISSION_VAR_STRIDE 0x0020u +#define LMP_MISSION_VAR_OFF_NAME 0x0000u +#define LMP_MISSION_VAR_OFF_VALUE 0x0008u +#define LMP_SIG_SHELL_LAUNCH "41 57 41 56 41 54 56 57 55 53 48 81 ec 30 02 00 00 44 0f 29 bc 24 20 02 00 00" +#define LMP_SIG_NUKE_DESTR_AREA_ES "41 57 41 56 41 55 41 54 56 57 55 53 48 81 ec 88 01 00 00 44 0f 29 94 24 70 01 00 00 44 0f 29 8c 24 60 01 00 00 44 0f 29 84 24 50 01 00 00 0f 29 bc 24 40 01 00 00 0f 29 b4 24 30 01 00 00 0f b7 6a 02" +#define LMP_SIG_CTA_LOGIC_ACTION "41 57 41 56 41 55 41 54 56 57 55 53 48 81 ec a8 02 00 00 44 0f 29 bc 24 90 02 00 00 44 0f 29 b4 24 80 02 00 00 44 0f 29 ac 24 70 02 00 00 44 0f 29 a4 24 60 02 00 00 44 0f 29 9c 24 50 02 00 00 44 0f 29 94 24 40 02 00 00 44 0f 29 8c 24 30 02 00 00 44 0f 29 84 24 20 02 00 00 0f 29 bc 24 10 02 00 00 0f 29 b4 24 00 02 00 00 48 89 cf" #define LMP_SIG_IS_SLOT_READY_AT_HOST "41 56 56 57 55 53 48 81 ec a0 00 00 00 0f 29 b4 24 90 00 00 00 44 89 c3 48 89 d6 48 83 3d"