#!/usr/bin/env python3 """Generate src/eter_net/Ava2GcPacketTable.inc from the AVA2 Windows reference. Source of truth: * UserInterface/PythonNetworkStream.cpp CMainPacketHeaderMap (header -> size, static/dynamic) * UserInterface/PythonNetworkStreamPhaseGame.cpp GamePhase() switch (headers with a handler) * UserInterface/Packet.h + Locale_inc.h struct layouts and feature defines sizeof() is evaluated by clang for i686-pc-windows-msvc (the Win32 client ABI) with _USE_32BIT_TIME_T semantics (StdAfx.h), so the table is the Win32 wire size, not a guess. Only constants used by Packet.h are stubbed; they are copied from the reference. """ import json import re import subprocess import sys import tempfile from pathlib import Path root = Path(__file__).resolve().parents[1] ui = root / "upstream/ava2-client/UserInterface" out = root / "src/eter_net/Ava2GcPacketTable.inc" def ascii_text(path): return re.sub(r"[^\t\n\r\x20-\x7e]", "", path.read_bytes().decode("latin1")) PRELUDE = """ #include typedef uint8_t BYTE; typedef uint16_t WORD; typedef uint32_t DWORD; typedef int32_t LONG; typedef int BOOL; typedef uint32_t UINT; typedef int INT; typedef int64_t LONGLONG; typedef uint64_t ULONGLONG; typedef int16_t SHORT; typedef char CHAR; typedef float FLOAT; #define NULL 0 typedef int time_t; /* StdAfx.h: _USE_32BIT_TIME_T */ typedef unsigned int size_t; extern "C" void* memset(void*, int, size_t); extern "C" float sqrtf(float); class CResource {}; struct TPixelPosition { float x, y, z; }; namespace std { template struct vector { T* a; T* b; T* c; }; struct string { char b[24]; }; template struct map {}; template struct pair { A first; B second; }; } /* UserInterface/StdAfx.h */ enum { POINT_MAX_NUM = 255, CHARACTER_NAME_MAX_LEN = 24, PLAYER_NAME_MAX_LEN = 12 }; """ # GameLib/ItemData.h CItemData enum values referenced by Packet.h / GameType.h ITEMDATA = """#pragma once class CItemData { public: enum { ITEM_NAME_MAX_LEN = 24, ITEM_LIMIT_MAX_NUM = 2, ITEM_VALUES_MAX_NUM = 6, ITEM_APPLY_MAX_NUM = 3, ITEM_SOCKET_MAX_NUM = 8, WEAR_MAX_NUM = 32, DS_SLOT_NUM_TYPES = 6 }; }; """ def header_map_body(): text = ascii_text(ui / "PythonNetworkStream.cpp") start = text.index("CMainPacketHeaderMap()") body_start = text.index("{", start) + 1 end = text.index("\n};", body_start) body = text[body_start:end].rstrip() assert body.endswith("}") return body[:-1] def game_switch_body(): text = ascii_text(ui / "PythonNetworkStreamPhaseGame.cpp") start = text.index("void CPythonNetworkStream::GamePhase()") end = text.index("RecvDefaultPacket(header)", start) lines = [] body = re.sub(r"/\*.*?\*/", "", text[start:end], flags=re.S) for line in body.splitlines(): s = line.split("//", 1)[0].strip() if re.match(r"#\s*(if|ifdef|ifndef|else|elif|endif)\b", s): lines.append(s) else: for m in re.finditer(r"case\s+(HEADER_GC_\w+)\s*:", s): lines.append(f"handled[{m.group(1)}] = 1;") return "\n".join(lines) GEN = """#include "Locale_inc.h" #include "GameType.h" #include "Packet.h" struct CNetworkPacketHeaderMap { struct TPacketType { int s, d; constexpr TPacketType(int a = 0, int b = 0) : s(a), d(b) {} }; }; struct Table { enum { STATIC_SIZE_PACKET = 0, DYNAMIC_SIZE_PACKET = 1 }; int size[256] = {}; int dyn[256] = {}; int set[256] = {}; int handled[256] = {}; constexpr void Set(int h, CNetworkPacketHeaderMap::TPacketType t) { size[h] = t.s; dyn[h] = t.d; set[h] = 1; } constexpr Table() { #include "map.inc" #include "switch.inc" } }; extern "C" const Table exported_table = Table(); """ def main(): with tempfile.TemporaryDirectory() as tmp: tmp = Path(tmp) (tmp / "x").mkdir() (tmp / "GameLib").mkdir() for name in ("Packet.h", "GameType.h", "Locale_inc.h"): (tmp / "x" / name).write_bytes((ui / name).read_bytes()) (tmp / "x" / "Locale.h").write_text("") (tmp / "GameLib" / "ItemData.h").write_text(ITEMDATA) (tmp / "GameLib" / "RaceData.h").write_text("") (tmp / "prelude.h").write_text(PRELUDE) (tmp / "map.inc").write_text(header_map_body()) (tmp / "switch.inc").write_text(game_switch_body()) (tmp / "gen.cpp").write_text(GEN) cmd = ["clang++", "-target", "i686-pc-windows-msvc", "-fms-extensions", "-fms-compatibility", "-std=c++17", "-S", "-emit-llvm", "-o", str(tmp / "gen.ll"), "-I", str(tmp / "x"), "-I", str(tmp), "-include", str(tmp / "prelude.h"), str(tmp / "gen.cpp")] subprocess.run(cmd, check=True) ir = (tmp / "gen.ll").read_text() line = next(l for l in ir.splitlines() if l.startswith("@exported_table")) values = [] for m in re.finditer(r"\[(\d+) x i32\] zeroinitializer|(? {out.relative_to(root)}") if __name__ == "__main__": main()