/* * LocalMP multi-nation team country lists (`team1_countries` / `team2_countries`) and the * in-battle chat objects of the host. * * Chat (2.61.0.37): when the host sends a message, chat_on_send (0x154f540) hands it to the host * chat handler 0x133ba10 with Multiplayer+0x1f8 as its object instead of sending it to a server. * The client never creates that object (a dedicated server does); the handler reads exactly two * fields of it: [0] the Multiplayer and [1] an optional chat log writer, used only when non-null * (0x133c40f). The host here gets that two-field object with no log writer, and its MPChat * (Multiplayer+0x1f0) from the stock Multiplayer::initMPChat (0x5bd6a0). */ #include "hooks.h" #include "hook_config.h" #include "local_mp_internal.h" #include "local_mp_offsets.h" #include "MinHook.h" #include #define MAX_TEAM_COUNTRIES 16u typedef uint8_t (__fastcall *MpConstructLocalFn)(uint8_t *mp, void *mis, void *chat_settings); typedef void (__fastcall *MpInitChatFn)(uint8_t *mp, void *chat_settings); typedef void *(__fastcall *DagmemAllocFn)(size_t size); static MpConstructLocalFn orig_mp_construct_local; static MpInitChatFn fn_mp_init_chat; static DagmemAllocFn fn_dagmem_alloc; static void clear_team_country_tab(uint8_t *tab) { char **data = *(char ***)(tab + LMP_TAB_OFF_DATA); uint32_t count = *(uint32_t *)(tab + LMP_TAB_OFF_COUNT); uint32_t i; if (data != NULL) { for (i = 0; i < count; i++) local_mp_mem_free(data[i]); local_mp_mem_free(data); } *(char ***)(tab + LMP_TAB_OFF_DATA) = NULL; *(void **)(tab + LMP_TAB_OFF_ALLOC) = g_local_mp_stdmem ? *g_local_mp_stdmem : NULL; *(uint32_t *)(tab + LMP_TAB_OFF_COUNT) = 0; *(uint32_t *)(tab + LMP_TAB_OFF_CAPACITY) = 0; } static void populate_team_country_tab(uint8_t *tab, const char *csv) { char tokens[MAX_TEAM_COUNTRIES][32]; uint32_t count = 0; const char *p = csv; char **arr; uint32_t i; clear_team_country_tab(tab); if (p == NULL || *p == '\0' || g_local_mp_stdmem == NULL || *g_local_mp_stdmem == NULL) return; while (*p != '\0' && count < MAX_TEAM_COUNTRIES) { const char *sep = strchr(p, ';'); size_t len = sep ? (size_t)(sep - p) : strlen(p); if (len > 0 && len < sizeof(tokens[0])) { memcpy(tokens[count], p, len); tokens[count][len] = '\0'; count++; } if (sep == NULL) break; p = sep + 1; } if (count == 0) return; arr = (char **)local_mp_mem_alloc((size_t)count * sizeof(char *)); if (arr == NULL) return; for (i = 0; i < count; i++) arr[i] = local_mp_strdup(tokens[i]); *(char ***)(tab + LMP_TAB_OFF_DATA) = arr; *(void **)(tab + LMP_TAB_OFF_ALLOC) = *g_local_mp_stdmem; *(uint32_t *)(tab + LMP_TAB_OFF_COUNT) = count; *(uint32_t *)(tab + LMP_TAB_OFF_CAPACITY) = count; } static void apply_custom_bot_team_countries(uint8_t *mp) { const void *bots_blk; const char *team_a; const char *team_b; if (g_local_mp_mission_settings == NULL || g_lmp_datablock_get_str == NULL) return; bots_blk = g_local_mp_mission_settings + LMP_MIS_OFF_CUSTOM_BOTS; team_a = g_lmp_datablock_get_str(bots_blk, "wtoTeamACountries", ""); team_b = g_lmp_datablock_get_str(bots_blk, "wtoTeamBCountries", ""); if (team_a != NULL && team_a[0] != '\0') populate_team_country_tab(mp + LMP_MP_MGR_OFF_TEAM1_COUNTRIES, team_a); if (team_b != NULL && team_b[0] != '\0') populate_team_country_tab(mp + LMP_MP_MGR_OFF_TEAM2_COUNTRIES, team_b); } static uint8_t __fastcall hook_mp_construct_local(uint8_t *mp, void *mis, void *chat_settings) { uint8_t ok = orig_mp_construct_local(mp, mis, chat_settings); if (!ok || mp == NULL) return ok; apply_custom_bot_team_countries(mp); if (chat_settings != NULL && fn_mp_init_chat != NULL && *(void **)(mp + LMP_MP_MGR_OFF_MP_CHAT) == NULL) { fn_mp_init_chat(mp, chat_settings); } if (fn_dagmem_alloc != NULL && *(void **)(mp + LMP_MP_MGR_OFF_CHAT_HOST) == NULL) { void **chat_host = (void **)fn_dagmem_alloc(2u * sizeof(void *)); if (chat_host != NULL) { chat_host[0] = mp; chat_host[1] = NULL; *(void **)(mp + LMP_MP_MGR_OFF_CHAT_HOST) = chat_host; log_line("local_mp: created the host chat object for LocalMP"); } } return ok; } int install_local_mp_action_hooks(void) { 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; return local_mp_queue_hooks("local_mp_actions", hooks, sizeof(hooks) / sizeof(hooks[0])); }