Files
WT-Offline/native/wto-net/src/hook_config.c
T
N0VAN0M4LY 85532cd930 feat(ground-ai)!: disable stock tankBot globally when AI script loads
Replace per-bot mid-battle movement handback to stock AI with a clean
binary startup gate:

- Locate Unit::setBehaviorTree (ground_ai_set_beh_tree: .37 0x4e89b0,
  .58 0x4e8f60) in patches/native/local_mp_ground_ai.jsonc and hook it
  in native/wto-net/src/local_mp_ground_ai.c.
- Announce readiness from [init] in wto_ai_runtime.das via
  wtoGroundAiScriptReady once the ground AI scripts finish compiling
  cleanly with custom movement enabled. When announced, hook_set_beh_tree
  suppresses attaching "tankBot" to LocalMP bot tanks and keeps their
  MoveSystem.inactive pinned to 1 for the life of the vehicle, ending
  all tug-of-war (external_inactive_reset) with stock AI.
- If scripts fail to compile, are missing, or mode is "stock" / "fire",
  ownMovement stays 0 and stock tankBot runs untouched.
- Remove per-bot handback, timeout release, and the now-obsolete
  navigation.returnToStockOnFailure config plumbing; on genuine script
  failure a bot halts in place and logs DRIVE_FAIL instead of silently
  switching to stock. Also initialize newly spawned bot ticks to run on
  frame 0 to eliminate spawn-frame invalid_drive_cmd noise.

Verified MSVC build of wto_ws.dll (0 warnings in project C sources), root
and launcher type checks, native module resolution on both 2.61.0.37 and
2.61.0.58 (0 failures), isolated 2.61.0.58 startup confirming 12/12
LocalMP modules installed, wto_ai_runtime.das loaded in 213 ms (0 compile
errors), and "ground AI script ready (ownMovement=1; stock tankBot
disabled)" in wto_ws.log.

BREAKING CHANGE: removes navigation.returnToStockOnFailure from
tuning/ground-ai.jsonc; stock tankBot is now controlled strictly by
whether the custom ground AI script loaded with custom movement enabled.
2026-12-04 01:06:04 +08:00

185 lines
6.2 KiB
C

/*
* Settings and log next to the DLL.
*
* wto_ws.cfg, written by the build into the game copy (@wto/patcher network-hook.ts):
* port=<local server port> without a valid port the hooks still answer every host name
* locally but refuse every connection that would leave the machine
* log=0|1 optional, defaults to 1; when 0, wto_ws.log is not opened
* exe.time_date_stamp=0x... the executable the native values were resolved for
* exe.size_of_image=0x...
* native.<name>=<value> what the LocalMP modules need from the executable: RVAs of
* functions and globals, structure offsets (local_mp_values.c)
*
* wto_ws.log: which host names were answered locally and which connections were redirected or
* refused, so a run shows every address the game tried (the audit handover 6.2 asks for).
*/
#include "hook_config.h"
#include "redirect_rules.h"
#include <share.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NATIVE_PREFIX "native."
#define NATIVE_NAME_MAX 80
#define NATIVE_VALUES_MAX 512
typedef struct NativeValue {
char name[NATIVE_NAME_MAX];
int64_t value;
} NativeValue;
static wchar_t g_folder[MAX_PATH];
static CRITICAL_SECTION g_log_lock;
static FILE *g_log;
static int g_log_enabled = 1;
static NativeValue g_native[NATIVE_VALUES_MAX];
static size_t g_native_count;
static int64_t g_exe_time_date_stamp = -1;
static int64_t g_exe_size_of_image = -1;
static void find_folder(HMODULE self)
{
DWORD length = GetModuleFileNameW(self, g_folder, MAX_PATH);
if (length == 0 || length >= MAX_PATH) {
g_folder[0] = L'\0';
return;
}
wchar_t *slash = wcsrchr(g_folder, L'\\');
if (slash)
slash[1] = L'\0';
}
static void read_port(const char *text)
{
char *end;
unsigned long port = strtoul(text, &end, 10);
if (end != text && port > 0 && port < 65536)
g_service_port = (unsigned short)port;
}
/* "<value>" as written by the build: hexadecimal 0x..., negative as -0x... */
static int parse_value(const char *text, int64_t *value)
{
char *end;
long long parsed = strtoll(text, &end, 0);
if (end == text)
return 0;
*value = (int64_t)parsed;
return 1;
}
static void read_native_value(const char *line)
{
const char *name = line + sizeof(NATIVE_PREFIX) - 1;
const char *equals = strchr(name, '=');
size_t length = equals ? (size_t)(equals - name) : 0;
NativeValue *slot;
if (length == 0 || length >= NATIVE_NAME_MAX || g_native_count >= NATIVE_VALUES_MAX)
return;
slot = &g_native[g_native_count];
memcpy(slot->name, name, length);
slot->name[length] = '\0';
if (parse_value(equals + 1, &slot->value))
g_native_count++;
}
static void read_settings(void)
{
wchar_t path[MAX_PATH];
char line[160];
FILE *file;
if (_snwprintf_s(path, MAX_PATH, _TRUNCATE, L"%swto_ws.cfg", g_folder) < 0)
return;
if (_wfopen_s(&file, path, L"r") != 0 || file == NULL)
return;
while (fgets(line, sizeof(line), file)) {
line[strcspn(line, "\r\n")] = '\0';
if (strncmp(line, "port=", 5) == 0)
read_port(line + 5);
else if (strncmp(line, "log=", 4) == 0)
g_log_enabled = atoi(line + 4) != 0;
else if (strncmp(line, "exe.time_date_stamp=", 20) == 0)
parse_value(line + 20, &g_exe_time_date_stamp);
else if (strncmp(line, "exe.size_of_image=", 18) == 0)
parse_value(line + 18, &g_exe_size_of_image);
else if (strncmp(line, NATIVE_PREFIX, sizeof(NATIVE_PREFIX) - 1) == 0)
read_native_value(line);
}
fclose(file);
}
int config_native_value(const char *name, int64_t *value)
{
size_t i;
for (i = 0; i < g_native_count; i++) {
if (strcmp(g_native[i].name, name) == 0) {
*value = g_native[i].value;
return 1;
}
}
return 0;
}
int config_native_matches_executable(void)
{
const uint8_t *module = (const uint8_t *)GetModuleHandleW(NULL);
const IMAGE_DOS_HEADER *dos = (const IMAGE_DOS_HEADER *)module;
const IMAGE_NT_HEADERS64 *nt;
if (module == NULL || dos->e_magic != IMAGE_DOS_SIGNATURE)
return 0;
nt = (const IMAGE_NT_HEADERS64 *)(module + dos->e_lfanew);
return nt->Signature == IMAGE_NT_SIGNATURE &&
(int64_t)nt->FileHeader.TimeDateStamp == g_exe_time_date_stamp &&
(int64_t)nt->OptionalHeader.SizeOfImage == g_exe_size_of_image;
}
void config_load(HMODULE self)
{
wchar_t path[MAX_PATH];
InitializeCriticalSection(&g_log_lock);
find_folder(self);
read_settings();
if (g_log_enabled && _snwprintf_s(path, MAX_PATH, _TRUNCATE, L"%swto_ws.log", g_folder) >= 0)
g_log = _wfsopen(path, L"w", _SH_DENYWR);
log_line("wto_ws: local server port %u%s", g_service_port, g_service_port ? "" : " (none: outside connections are refused)");
log_line("wto_ws: %u native value(s) for the LocalMP modules", (unsigned)g_native_count);
}
void log_line(const char *format, ...)
{
va_list args;
SYSTEMTIME now;
if (g_log == NULL)
return;
GetLocalTime(&now);
EnterCriticalSection(&g_log_lock);
fprintf(g_log, "%02u:%02u:%02u.%03u ", now.wHour, now.wMinute, now.wSecond, now.wMilliseconds);
va_start(args, format);
vfprintf(g_log, format, args);
va_end(args);
fputc('\n', g_log);
fflush(g_log);
LeaveCriticalSection(&g_log_lock);
}
void describe_address(const struct sockaddr *name, int length, char *text, size_t size)
{
char host[INET6_ADDRSTRLEN] = "?";
unsigned short port = 0;
if (name && name->sa_family == AF_INET && length >= (int)sizeof(struct sockaddr_in)) {
const struct sockaddr_in *v4 = (const struct sockaddr_in *)name;
inet_ntop(AF_INET, &v4->sin_addr, host, sizeof(host));
port = ntohs(v4->sin_port);
} else if (name && name->sa_family == AF_INET6 && length >= (int)sizeof(struct sockaddr_in6)) {
const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)name;
inet_ntop(AF_INET6, &v6->sin6_addr, host, sizeof(host));
port = ntohs(v6->sin6_port);
}
_snprintf_s(text, size, _TRUNCATE, "%s:%u", host, port);
}