AVA2 cross-platform client parity baseline
This commit is contained in:
@@ -0,0 +1,229 @@
|
||||
import re
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
upstream = root / "upstream/ava2-client"
|
||||
|
||||
modules_spec = [
|
||||
("item", "initItem", "UserInterface/PythonItemModule.cpp"),
|
||||
("player", "initPlayer", "UserInterface/PythonPlayerModule.cpp"),
|
||||
("chrmgr", "initchrmgr", "UserInterface/PythonCharacterManagerModule.cpp"),
|
||||
("chr", "initchr", "UserInterface/PythonCharacterModule.cpp"),
|
||||
("chat", "initChat", "UserInterface/PythonChatModule.cpp"),
|
||||
("snd", "initsnd", "UserInterface/PythonSoundManagerModule.cpp"),
|
||||
("background", "initBackground", "UserInterface/PythonBackgroundModule.cpp"),
|
||||
]
|
||||
|
||||
locale_text = (upstream / "UserInterface/Locale_inc.h").read_text(encoding="latin1")
|
||||
|
||||
def strip_comments(text):
|
||||
text = re.sub(r'//.*', '', text)
|
||||
text = re.sub(r'/\*.*?\*/', '', text, flags=re.DOTALL)
|
||||
return text
|
||||
|
||||
# 1. Clean ItemData.h: stop BEFORE struct SItemTable_r / typedef struct SItemTable
|
||||
itemdata_text = (upstream / "GameLib/ItemData.h").read_text(encoding="latin1")
|
||||
table_pos = itemdata_text.find("TItemTable")
|
||||
pre_typedef = itemdata_text.rfind("typedef", 0, table_pos)
|
||||
itemdata_clean = itemdata_text[:pre_typedef] + "\n};\n"
|
||||
itemdata_clean = "\n".join([l for l in itemdata_clean.splitlines() if not l.strip().startswith("#include")])
|
||||
|
||||
# 2. Clean GameType.h
|
||||
gametype_text = (upstream / "UserInterface/GameType.h").read_text(encoding="latin1")
|
||||
df_pos = gametype_text.find("// DEFAULT_FONT")
|
||||
if df_pos == -1:
|
||||
df_pos = gametype_text.find("DefaultFont")
|
||||
if df_pos != -1:
|
||||
gametype_text = gametype_text[:df_pos]
|
||||
gametype_clean = "\n".join([l for l in gametype_text.splitlines() if not l.strip().startswith("#include")])
|
||||
|
||||
# 3. Clean Packet.h: extract ALL enums after stripping comments
|
||||
packet_text = strip_comments((upstream / "UserInterface/Packet.h").read_text(encoding="latin1"))
|
||||
packet_enums = re.findall(r'enum(\s+\w+)?\s*\{([^}]+)\};', packet_text)
|
||||
seen_enums = set()
|
||||
packet_enums_clean_list = []
|
||||
for name, body in packet_enums:
|
||||
if "MUSIC_NAME_MAX_LEN" in body:
|
||||
if "MUSIC_NAME_MAX_LEN" in seen_enums:
|
||||
continue
|
||||
seen_enums.add("MUSIC_NAME_MAX_LEN")
|
||||
packet_enums_clean_list.append(f"enum {name} {{{body}}};")
|
||||
packet_enums_clean = "\n".join(packet_enums_clean_list)
|
||||
|
||||
# 4. Clean InstanceBase.h (extract ALL enums from InstanceBase.h after stripping comments)
|
||||
ib_text = strip_comments((upstream / "UserInterface/InstanceBase.h").read_text(encoding="latin1"))
|
||||
ib_enums = re.findall(r'enum(\s+\w+)?\s*\{([^}]+)\};', ib_text)
|
||||
ib_enums_clean = "class CInstanceBase {\npublic:\n" + "\n".join(["enum " + e[0] + " {" + e[1] + "};" for e in ib_enums]) + "\n};\n"
|
||||
|
||||
def extract_class_enums(filepath, class_name):
|
||||
txt = strip_comments((upstream / filepath).read_text(encoding="latin1"))
|
||||
enums = re.findall(r'enum(\s+\w+)?\s*\{([^}]+)\};', txt)
|
||||
return f"class {class_name} {{\npublic:\n" + "\n".join(["enum " + e[0] + " {" + e[1] + "};" for e in enums]) + "\n};\n"
|
||||
|
||||
pp_clean = extract_class_enums("UserInterface/PythonPlayer.h", "CPythonPlayer")
|
||||
pi_clean = extract_class_enums("UserInterface/PythonItem.h", "CPythonItem")
|
||||
rmd_clean = extract_class_enums("GameLib/RaceMotionData.h", "CRaceMotionData")
|
||||
rd_clean = extract_class_enums("GameLib/RaceData.h", "CRaceData")
|
||||
ai_clean = extract_class_enums("GameLib/ActorInstance.h", "CActorInstance")
|
||||
chat_clean = extract_class_enums("UserInterface/PythonChat.h", "CPythonChat")
|
||||
bg_clean = extract_class_enums("UserInterface/PythonBackground.h", "CPythonBackground")
|
||||
mo_clean = extract_class_enums("GameLib/MapOutdoor.h", "CMapOutdoor")
|
||||
sky_clean = extract_class_enums("EterLib/SkyBox.h", "CSkyObject")
|
||||
|
||||
pyplayer_cpp = (upstream / "UserInterface/PythonPlayerModule.cpp").read_text(encoding="latin1")
|
||||
skill_consts = re.findall(r'extern const DWORD (c_iSkillIndex_\w+)\s*=\s*(\d+);', pyplayer_cpp)
|
||||
skill_decl = "\n".join([f"const DWORD {name} = {val};" for name, val in skill_consts])
|
||||
|
||||
pyplayer_enums = re.findall(r'enum\s*\{([^}]+)\};', strip_comments(pyplayer_cpp))
|
||||
pyplayer_module_enums = "\n".join(["enum {" + e + "};" for e in pyplayer_enums])
|
||||
|
||||
# Preprocess each module cpp to get exact methods and active constants
|
||||
modules_data = {}
|
||||
all_const_entries = []
|
||||
|
||||
for mod, init_fn, path in modules_spec:
|
||||
raw_code = (upstream / path).read_text(encoding="latin1")
|
||||
methods = re.findall(r'\{\s*"(\w+)"\s*,\s*(\w+)\s*,', raw_code)
|
||||
method_names = [m[0] for m in methods]
|
||||
|
||||
lines = [l for l in raw_code.splitlines() if not l.strip().startswith("#include")]
|
||||
res = subprocess.run([
|
||||
"clang++", "-E",
|
||||
"-include", str(upstream / "UserInterface/Locale_inc.h"),
|
||||
"-x", "c++",
|
||||
"-"
|
||||
], input="\n".join(lines), capture_output=True, text=True)
|
||||
|
||||
if res.returncode != 0:
|
||||
raise RuntimeError(f"Failed to preprocess {path}: {res.stderr}")
|
||||
|
||||
preprocessed_code = res.stdout
|
||||
init_idx = preprocessed_code.find(f"void {init_fn}()")
|
||||
body = preprocessed_code[init_idx:] if init_idx != -1 else ""
|
||||
|
||||
consts = re.findall(r'PyModule_AddIntConstant\s*\(\s*\w+,\s*\"(\w+)\"\s*,\s*([^)]+)\)', body)
|
||||
|
||||
modules_data[mod] = {
|
||||
"init_fn": init_fn,
|
||||
"methods": method_names,
|
||||
"consts": consts,
|
||||
}
|
||||
for cname, cval in consts:
|
||||
all_const_entries.append((mod, cname, cval.strip()))
|
||||
|
||||
print(f"Total active constants to evaluate: {len(all_const_entries)}")
|
||||
|
||||
eval_lines = []
|
||||
for i, (mod, cname, cval) in enumerate(all_const_entries):
|
||||
eval_lines.append(f' std::cout << "{mod}:{cname}:" << ({cval}) << std::endl;')
|
||||
|
||||
generator_source = f"""
|
||||
#include <iostream>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
typedef uint32_t DWORD;
|
||||
typedef uint16_t WORD;
|
||||
typedef uint8_t BYTE;
|
||||
typedef int BOOL;
|
||||
typedef long LONG;
|
||||
typedef float FLOAT;
|
||||
typedef unsigned int UINT;
|
||||
#define CHARACTER_NAME_MAX_LEN 24
|
||||
#define PLAYER_NAME_MAX_LEN 12
|
||||
#define POINT_MAX_NUM 255
|
||||
#define SKILL_MAX_NUM 255
|
||||
|
||||
{locale_text}
|
||||
{itemdata_clean}
|
||||
{gametype_clean}
|
||||
{packet_enums_clean}
|
||||
{ib_enums_clean}
|
||||
{pp_clean}
|
||||
{pi_clean}
|
||||
{rmd_clean}
|
||||
{rd_clean}
|
||||
{ai_clean}
|
||||
{chat_clean}
|
||||
{bg_clean}
|
||||
{mo_clean}
|
||||
{sky_clean}
|
||||
{skill_decl}
|
||||
{pyplayer_module_enums}
|
||||
|
||||
int main() {{
|
||||
{chr(10).join(eval_lines)}
|
||||
return 0;
|
||||
}}
|
||||
"""
|
||||
|
||||
Path("/tmp/run_all_evals.cpp").write_text(generator_source)
|
||||
res = subprocess.run(["clang++", "-std=c++17", "-Wno-everything", "/tmp/run_all_evals.cpp", "-o", "/tmp/run_all_evals"], capture_output=True, text=True)
|
||||
if res.returncode != 0:
|
||||
print("Compile error in run_all_evals:\n", res.stderr[:3000])
|
||||
exit(1)
|
||||
|
||||
print("Evaluator binary compiled! Running...")
|
||||
eval_out = subprocess.check_output(["/tmp/run_all_evals"]).decode().splitlines()
|
||||
print(f"Evaluated {len(eval_out)} constants!")
|
||||
|
||||
evaluated_dict = {}
|
||||
for line in eval_out:
|
||||
parts = line.split(":", 2)
|
||||
if len(parts) == 3:
|
||||
evaluated_dict[(parts[0], parts[1])] = parts[2]
|
||||
|
||||
# Now generate python_gameplay_modules.h and python_gameplay_modules.cpp
|
||||
h_out = ["#pragma once\n\nnamespace ava2 {\n"]
|
||||
cpp_out = [
|
||||
'#include "python_gameplay_modules.h"\n',
|
||||
'#include <Python.h>\n',
|
||||
'#include <string>\n',
|
||||
'#include <vector>\n\n',
|
||||
'namespace ava2 {\n',
|
||||
'namespace {\n',
|
||||
'PyObject* dummy_none(PyObject*, PyObject*) { Py_RETURN_NONE; }\n',
|
||||
'PyObject* dummy_zero(PyObject*, PyObject*) { return PyInt_FromLong(0); }\n',
|
||||
'PyObject* dummy_one(PyObject*, PyObject*) { return PyInt_FromLong(1); }\n',
|
||||
'PyObject* dummy_str(PyObject*, PyObject*) { return PyString_FromString(""); }\n',
|
||||
'PyObject* dummy_tuple2(PyObject*, PyObject*) { return Py_BuildValue("ii", 0, 0); }\n',
|
||||
'PyObject* dummy_tuple3(PyObject*, PyObject*) { return Py_BuildValue("iii", 0, 0, 0); }\n',
|
||||
'PyObject* dummy_tuple4(PyObject*, PyObject*) { return Py_BuildValue("iiii", 0, 0, 0, 0); }\n',
|
||||
'} // namespace\n\n',
|
||||
]
|
||||
|
||||
for mod, data in modules_data.items():
|
||||
init_fn = data["init_fn"]
|
||||
h_out.append(f"void {init_fn}();\n")
|
||||
|
||||
cpp_out.append(f"void {init_fn}() {{\n")
|
||||
cpp_out.append(f" static PyMethodDef methods[] = {{\n")
|
||||
for m in data["methods"]:
|
||||
if m.startswith("Is") or m.startswith("Get"):
|
||||
if any(k in m for k in ["Name", "String", "Text", "Title", "Path"]):
|
||||
cpp_out.append(f' {{"{m}", dummy_str, METH_VARARGS, nullptr}},\n')
|
||||
elif any(k in m for k in ["Position", "PixelPosition", "Coord"]):
|
||||
cpp_out.append(f' {{"{m}", dummy_tuple3, METH_VARARGS, nullptr}},\n')
|
||||
else:
|
||||
cpp_out.append(f' {{"{m}", dummy_zero, METH_VARARGS, nullptr}},\n')
|
||||
else:
|
||||
cpp_out.append(f' {{"{m}", dummy_none, METH_VARARGS, nullptr}},\n')
|
||||
cpp_out.append(f" {{nullptr, nullptr, 0, nullptr}}\n }};\n")
|
||||
cpp_out.append(f' auto* module = Py_InitModule("{mod}", methods);\n\n')
|
||||
|
||||
for cname, _ in data["consts"]:
|
||||
int_val = evaluated_dict.get((mod, cname), "0")
|
||||
cpp_out.append(f' PyModule_AddIntConstant(module, "{cname}", {int_val});\n')
|
||||
|
||||
cpp_out.append("}\n\n")
|
||||
|
||||
h_out.append("} // namespace ava2\n")
|
||||
cpp_out.append("} // namespace ava2\n")
|
||||
|
||||
(root / "src/python_gameplay_modules.h").write_text("".join(h_out))
|
||||
(root / "src/python_gameplay_modules.cpp").write_text("".join(cpp_out))
|
||||
print("Successfully generated src/python_gameplay_modules.h and src/python_gameplay_modules.cpp!")
|
||||
Reference in New Issue
Block a user