Files
AVA2/tools/generate_login_oracle.py
T

43 lines
2.4 KiB
Python

#!/usr/bin/env python3
"""Compile original AVA2 declarations using explicit Win32-width types as an oracle."""
from pathlib import Path
import re,sys
root=Path(__file__).resolve().parents[1]
client=(root/'upstream/ava2-client/UserInterface/Packet.h').read_text(errors='replace')
server=(root/'upstream/ava2-server/common/tables.h').read_text(errors='replace')
packet=(root/'upstream/ava2-server/game/src/packet.h').read_text(errors='replace')
tea=(root/'upstream/ava2-client/EterBase/tea.cpp').read_text(errors='replace')
def declaration(source, start, end):
text=source[source.index(start):]
text=text[:text.index(end)+len(end)]
return re.sub(r'\blong\b','std::int32_t',text)
# Both baseline macro headers enable sash, conqueror and five-character accounts.
for path,names in [
('upstream/ava2-client/UserInterface/Locale_inc.h',['ENABLE_SASH_SYSTEM','ENABLE_CONQUEROR_LEVEL','ENABLE_PLAYER_PER_ACCOUNT5']),
('upstream/ava2-server/common/CommonDefines.h',['__SASH_SYSTEM__','ENABLE_CONQUEROR_LEVEL','ENABLE_PLAYER_PER_ACCOUNT5'])]:
text=(root/path).read_text(errors='replace')
for name in names:
assert re.search(r'^\s*#\s*define\s+'+name+r'\b',text,re.M), name+' not active in baseline'
header='''#pragma once
#include <cstdint>
namespace ava2_oracle {
using BYTE=std::uint8_t; using WORD=std::uint16_t; using DWORD=std::uint32_t; using LONG=std::int32_t;
constexpr int CHARACTER_NAME_MAX_LEN=24, GUILD_NAME_MAX_LEN=12, PLAYER_PER_ACCOUNT4=5, PLAYER_PER_ACCOUNT=5;
#define ENABLE_SASH_SYSTEM
#define __SASH_SYSTEM__
#define ENABLE_CONQUEROR_LEVEL
#pragma pack(push,1)
namespace client {
'''+declaration(client,'typedef struct SSimplePlayerInformation','} TSimplePlayerInformation;')+'\n'+declaration(client,'typedef struct packet_login_success4','} TPacketGCLoginSuccess4;')+'\n}\nnamespace server {\n'+declaration(server,'typedef struct SSimplePlayer','} TSimplePlayer;')+'\n'+declaration(packet,'typedef struct packet_login_success','} TPacketGCLoginSuccess;')+'''\n}
#pragma pack(pop)
namespace original_cipher {
'''
start=tea.index('#define TEA_ROUND')
end=tea.index('int tea_encrypt(' , start)
text=tea[start:end].replace('unsigned long','std::uint32_t').replace('register ','')
header+=text+'\n}\n#undef ENABLE_SASH_SYSTEM\n#undef __SASH_SYSTEM__\n#undef ENABLE_CONQUEROR_LEVEL\n}\n'
out=Path(sys.argv[1]);out.parent.mkdir(parents=True,exist_ok=True);out.write_text(header)