Files
WT-Offline/native/wto-net/src/hook_config.c
T
N0VAN0M4LY e0a344708d feat(ground-ai): load baked navigation packs in battle
Statically link upstream Detour into wto_ws.dll with a navigation engine
(native/wto-nav) that loads one .wtonav pack per battle, reads the level
file's eVER fingerprint and answers path queries through a cost filter
that multiplies chokepoint polygons. Start and goal points that do not
sit on the ground are retried over the mesh's height range.

The ground AI script talks to it through call_mission_action("wtoNav")
with load_map, unload_map, find_path and get_stats. Requests are string
parameters; replies use the DataBlock setters local_mp_awards already
resolves, with coordinates as centimetre ints, so no new native site is
needed. The local server writes the battle level into customBots
wtoLevel, and the build writes the pack folder as nav.dir in wto_ws.cfg
(build-game-copy.ts --map-nav).

The first load_map of a battle decides it once: loaded, fallback to the
script's own navigation, or refused when navEngine.allowFallback is off,
in which case bots do not plan or drive. A fingerprint mismatch is
logged and the pack is still used. The script proves the round trip
with one path per team at battle start and logs query statistics.

Add the navEngine, navCostsTracked and navCostsWheeled tuning sections
through the server reader, generated daScript constants and the
launcher's bot tuning page in both languages.

Verified with root and launcher type checks, MSVC builds without
warnings in project code, check-patches 14/14 on 2.61.0.58 and
2.61.0.60, native module resolution 14/14 on 2.61.0.37, .58, .60 and
.64, a scratch game copy build packing 29 ground AI modules, and an
offline run of the engine: all three maps load with matching
fingerprints and every spawn-to-capture path on every tier succeeds.
2026-12-05 16:39:26 +08:00

200 lines
6.6 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)
* nav.dir=<folder> optional, UTF-8: where the ground AI finds <level>.wtonav packs
*
* 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 wchar_t g_nav_dir[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_nav_dir(const char *utf8)
{
if (MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8, -1, g_nav_dir, MAX_PATH) == 0)
g_nav_dir[0] = L'\0';
}
static void read_settings(void)
{
wchar_t path[MAX_PATH];
char line[1024];
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);
else if (strncmp(line, "nav.dir=", 8) == 0)
read_nav_dir(line + 8);
}
fclose(file);
}
const wchar_t *config_nav_dir(void)
{
return g_nav_dir;
}
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);
}