1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
|
import argparse
import content
import network
from datatypes import EmitDefinition, EmitTypeDeclaration
def create_enum_table(names, num, start = 0):
lines = []
lines += ["enum", "{"]
if len(names) > 0 and start != 0:
lines += [f"\t{names[0]} = {start},"]
names = names[1:]
for name in names:
lines += [f"\t{name},"]
lines += [f"\t{num}", "};"]
return lines
def create_flags_table(names):
lines = []
lines += ["enum", "{"]
for i, name in enumerate(names):
lines += [f"\t{name} = 1<<{int(i)},"]
lines += ["};"]
return lines
def EmitEnum(names, num):
print("enum")
print("{")
print(f"\t{names[0]}=0,")
for name in names[1:]:
print(f"\t{name},")
print(f"\t{num}")
print("};")
def EmitFlags(names):
print("enum")
print("{")
for i, name in enumerate(names):
print(f"\t{name} = 1<<{int(i)},")
print("};")
def gen_network_header():
print("#ifndef GAME_GENERATED_PROTOCOL_H")
print("#define GAME_GENERATED_PROTOCOL_H")
print("class CUnpacker;")
print("#include <engine/message.h>")
print(network.RawHeader)
for e in network.Enums:
for line in create_enum_table([f"{e.name}_{v}" for v in e.values], f'NUM_{e.name}S', e.start): # pylint: disable=no-member
print(line)
print("")
for e in network.Flags:
for line in create_flags_table([f"{e.name}_{v}" for v in e.values]):
print(line)
print("")
non_extended = [o for o in network.Objects if o.ex is None]
extended = [o for o in network.Objects if o.ex is not None]
for line in create_enum_table(["NETOBJTYPE_EX"]+[o.enum_name for o in non_extended], "NUM_NETOBJTYPES"):
print(line)
for line in create_enum_table(["__NETOBJTYPE_UUID_HELPER=OFFSET_GAME_UUID-1"]+[o.enum_name for o in extended], "OFFSET_NETMSGTYPE_UUID"):
print(line)
print("")
non_extended = [o for o in network.Messages if o.ex is None]
extended = [o for o in network.Messages if o.ex is not None]
for line in create_enum_table(["NETMSGTYPE_EX"]+[o.enum_name for o in non_extended], "NUM_NETMSGTYPES"):
print(line)
print("")
for line in create_enum_table(["__NETMSGTYPE_UUID_HELPER=OFFSET_NETMSGTYPE_UUID-1"]+[o.enum_name for o in extended], "OFFSET_MAPITEMTYPE_UUID"):
print(line)
print("")
for item in network.Objects + network.Messages:
for line in item.emit_declaration():
print(line)
print("")
EmitEnum([f"SOUND_{i.name.value.upper()}" for i in content.container.sounds.items], "NUM_SOUNDS")
EmitEnum([f"WEAPON_{i.name.value.upper()}" for i in content.container.weapons.id.items], "NUM_WEAPONS")
print("""
enum
{
WEAPON_GAME = -3, // team switching etc
WEAPON_SELF = -2, // console kill command
WEAPON_WORLD = -1, // death tiles etc
};
class CNetObjHandler
{
const char *m_pMsgFailedOn;
const char *m_pObjFailedOn;
const char *m_pObjCorrectedOn;
char m_aUnpackedData[1024 * 2];
int m_NumObjCorrections;
int ClampInt(const char *pErrorMsg, int Value, int Min, int Max);
static const char *ms_apObjNames[];
static const char *ms_apExObjNames[];
static int ms_aObjSizes[];
static int ms_aUnpackedObjSizes[];
static int ms_aUnpackedExObjSizes[];
static const char *ms_apMsgNames[];
static const char *ms_apExMsgNames[];
public:
CNetObjHandler();
void *SecureUnpackObj(int Type, CUnpacker *pUnpacker);
const char *GetObjName(int Type) const;
int GetObjSize(int Type) const;
int GetUnpackedObjSize(int Type) const;
int NumObjCorrections() const;
const char *CorrectedObjOn() const;
const char *FailedObjOn() const;
const char *GetMsgName(int Type) const;
void *SecureUnpackMsg(int Type, CUnpacker *pUnpacker);
bool TeeHistorianRecordMsg(int Type);
const char *FailedMsgOn() const;
};
""")
print("#endif // GAME_GENERATED_PROTOCOL_H")
def gen_network_source():
print("""\
#include "protocol.h"
#include <base/system.h>
#include <engine/shared/packer.h>
#include <engine/shared/protocol.h>
#include <engine/shared/uuid_manager.h>
#include <game/mapitems_ex.h>
CNetObjHandler::CNetObjHandler()
{
m_pMsgFailedOn = "";
m_pObjFailedOn = "";
m_pObjCorrectedOn = "";
m_NumObjCorrections = 0;
}
int CNetObjHandler::NumObjCorrections() const { return m_NumObjCorrections; }
const char *CNetObjHandler::CorrectedObjOn() const { return m_pObjCorrectedOn; }
const char *CNetObjHandler::FailedObjOn() const { return m_pObjFailedOn; }
const char *CNetObjHandler::FailedMsgOn() const { return m_pMsgFailedOn; }
static const int max_int = 0x7fffffff;
static const int min_int = 0x80000000;
int CNetObjHandler::ClampInt(const char *pErrorMsg, int Value, int Min, int Max)
{
if(Value < Min) { m_pObjCorrectedOn = pErrorMsg; m_NumObjCorrections++; return Min; }
if(Value > Max) { m_pObjCorrectedOn = pErrorMsg; m_NumObjCorrections++; return Max; }
return Value;
}
""")
lines = []
lines += ["const char *CNetObjHandler::ms_apObjNames[] = {"]
lines += ['\t"EX/UUID",']
lines += [f'\t"{o.name}",' for o in network.Objects if o.ex is None]
lines += ['\t""', "};", ""]
lines += ["const char *CNetObjHandler::ms_apExObjNames[] = {"]
lines += ['\t"invalid",']
lines += [f'\t"{o.name}",' for o in network.Objects if o.ex is not None]
lines += ['\t""', "};", ""]
lines += ["int CNetObjHandler::ms_aObjSizes[] = {"]
lines += ['\t0,']
lines += [f'\tsizeof({o.struct_name}),' for o in network.Objects if o.ex is None]
lines += ['\t0', "};", ""]
lines += ["int CNetObjHandler::ms_aUnpackedObjSizes[] = {"]
lines += ['\t16,']
lines += [f'\tsizeof({o.struct_name}),' for o in network.Objects if o.ex is None]
lines += ["};", ""]
lines += ["int CNetObjHandler::ms_aUnpackedExObjSizes[] = {"]
lines += ['\t0,']
lines += [f'\tsizeof({o.struct_name}),' for o in network.Objects if o.ex is not None]
lines += ["};", ""]
lines += ['const char *CNetObjHandler::ms_apMsgNames[] = {']
lines += ['\t"invalid",']
lines += [f'\t"{msg.name}",' for msg in network.Messages if msg.ex is None]
lines += ['\t""', "};", ""]
lines += ['const char *CNetObjHandler::ms_apExMsgNames[] = {']
lines += ['\t"invalid",']
lines += [f'\t"{msg.name}",' for msg in network.Messages if msg.ex is not None]
lines += ['\t""', "};", ""]
for line in lines:
print(line)
print("""\
const char *CNetObjHandler::GetObjName(int Type) const
{
if(Type >= 0 && Type < NUM_NETOBJTYPES)
{
return ms_apObjNames[Type];
}
else if(Type > __NETOBJTYPE_UUID_HELPER && Type < OFFSET_NETMSGTYPE_UUID)
{
return ms_apExObjNames[Type - __NETOBJTYPE_UUID_HELPER];
}
return "(out of range)";
}
int CNetObjHandler::GetObjSize(int Type) const
{
if(Type < 0 || Type >= NUM_NETOBJTYPES) return 0;
return ms_aObjSizes[Type];
}
int CNetObjHandler::GetUnpackedObjSize(int Type) const
{
if(Type >= 0 && Type < NUM_NETOBJTYPES)
{
return ms_aUnpackedObjSizes[Type];
}
else if(Type > __NETOBJTYPE_UUID_HELPER && Type < OFFSET_NETMSGTYPE_UUID)
{
return ms_aUnpackedExObjSizes[Type - __NETOBJTYPE_UUID_HELPER];
}
return 0;
}
const char *CNetObjHandler::GetMsgName(int Type) const
{
if(Type >= 0 && Type < NUM_NETMSGTYPES)
{
return ms_apMsgNames[Type];
}
else if(Type > __NETMSGTYPE_UUID_HELPER && Type < OFFSET_MAPITEMTYPE_UUID)
{
return ms_apExMsgNames[Type - __NETMSGTYPE_UUID_HELPER];
}
return "(out of range)";
}
""")
lines = []
lines += ["""\
void *CNetObjHandler::SecureUnpackObj(int Type, CUnpacker *pUnpacker)
{
m_pObjFailedOn = 0;
switch(Type)
{
case NETOBJTYPE_EX:
{
const unsigned char *pPtr = pUnpacker->GetRaw(sizeof(CUuid));
if(pPtr != 0)
{
mem_copy(m_aUnpackedData, pPtr, sizeof(CUuid));
}
break;
}
"""]
for item in network.Objects:
for line in item.emit_uncompressed_unpack_and_validate(network.Objects):
lines += ["\t" + line]
lines += ['\t']
lines += ["""\
default:
m_pObjFailedOn = "(type out of range)";
break;
}
if(pUnpacker->Error())
m_pObjFailedOn = "(unpack error)";
if(m_pObjFailedOn)
return 0;
m_pObjFailedOn = "";
return m_aUnpackedData;
}
"""]
for line in lines:
print(line)
lines = []
lines += ["""\
void *CNetObjHandler::SecureUnpackMsg(int Type, CUnpacker *pUnpacker)
{
m_pMsgFailedOn = 0;
switch(Type)
{
"""]
for item in network.Messages:
for line in item.emit_unpack_msg():
lines += ["\t" + line]
lines += ['\t']
lines += ["""\
default:
m_pMsgFailedOn = "(type out of range)";
break;
}
if(pUnpacker->Error())
m_pMsgFailedOn = "(unpack error)";
if(m_pMsgFailedOn)
return 0;
m_pMsgFailedOn = "";
return m_aUnpackedData;
}
"""]
for line in lines:
print(line)
lines = []
lines += ["""\
bool CNetObjHandler::TeeHistorianRecordMsg(int Type)
{
switch(Type)
{
"""]
empty = True
for msg in network.Messages:
if not msg.teehistorian:
lines += [f'\tcase {msg.enum_name}:']
empty = False
if not empty:
lines += ['\t\treturn false;']
lines += ["""\
default:
return true;
}
}
"""]
for line in lines:
print(line)
lines = []
lines += ["""\
void RegisterGameUuids(CUuidManager *pManager)
{
"""]
for item in network.Objects + network.Messages:
if item.ex is not None:
lines += [f'\tpManager->RegisterName({item.enum_name}, "{item.ex}");']
lines += ["""
RegisterMapItemTypeUuids(pManager);
}
"""]
for line in lines:
print(line)
def gen_common_content_types_header():
# print some includes
print('#include <engine/graphics.h>')
# emit the type declarations
with open("datasrc/content.py", "rb") as content_file:
contentlines = content_file.readlines()
order = []
for line in contentlines:
line = line.strip()
if line[:6] == "class ".encode() and "(Struct)".encode() in line:
order += [line.split()[1].split("(".encode())[0].decode("ascii")]
for name in order:
EmitTypeDeclaration(content.__dict__[name])
def gen_common_content_header():
# print some includes
print('#include "data_types.h"')
# the container pointer
print('extern CDataContainer *g_pData;')
# enums
EmitEnum([f"IMAGE_{i.name.value.upper()}" for i in content.container.images.items], "NUM_IMAGES")
EmitEnum([f"ANIM_{i.name.value.upper()}" for i in content.container.animations.items], "NUM_ANIMS")
EmitEnum([f"SPRITE_{i.name.value.upper()}" for i in content.container.sprites.items], "NUM_SPRITES")
def gen_common_content_source():
EmitDefinition(content.container, "datacontainer")
print('CDataContainer *g_pData = &datacontainer;')
def gen_content_types_header():
print("#ifndef CONTENT_TYPES_HEADER")
print("#define CONTENT_TYPES_HEADER")
gen_common_content_types_header()
print("#endif")
def gen_client_content_header():
print("#ifndef CLIENT_CONTENT_HEADER")
print("#define CLIENT_CONTENT_HEADER")
gen_common_content_header()
print("#endif")
def gen_client_content_source():
print('#include "client_data.h"')
gen_common_content_source()
def gen_server_content_header():
print("#ifndef SERVER_CONTENT_HEADER")
print("#define SERVER_CONTENT_HEADER")
gen_common_content_header()
print("#endif")
def gen_server_content_source():
print('#include "server_data.h"')
gen_common_content_source()
def main():
parser = argparse.ArgumentParser(
description=('Generate C++ Source Files for the Teeworlds Network Protocol')
)
FUNCTION_MAP = {
'network_header': gen_network_header,
'network_source': gen_network_source,
'content_types_header': gen_content_types_header,
'client_content_header': gen_client_content_header,
'client_content_source': gen_client_content_source,
'server_content_header': gen_server_content_header,
'server_content_source': gen_server_content_source,
}
parser.add_argument('file_to_generate', choices=FUNCTION_MAP.keys())
args = parser.parse_args()
FUNCTION_MAP[args.file_to_generate]()
if __name__ == '__main__':
main()
|