ddnet/datasrc/compile.py
ChillerDragon 732798444c More detailed snapshot debug dump
The method CSnapShot::DebugDump() used to print just the raw snap items.
Without item names or field names:

```
2024-04-07 09:10:01 I snapshot: data_size=356 num_items=10
2024-04-07 09:10:01 I snapshot:         type=0 id=32767
2024-04-07 09:10:01 I snapshot:                   0  -1824658838        933dea6a
2024-04-07 09:10:01 I snapshot:                   1   -629591830        da7930ea
2024-04-07 09:10:01 I snapshot:                   2  -1450210576        a98f8af0
2024-04-07 09:10:01 I snapshot:                   3    914991429        3689a945
2024-04-07 09:10:01 I snapshot:         type=0 id=32766
2024-04-07 09:10:01 I snapshot:                   0    583701389        22ca938d
2024-04-07 09:10:01 I snapshot:                   1    327171627        13803e2b
2024-04-07 09:10:01 I snapshot:                   2  -1636052395        9e7bd255
2024-04-07 09:10:01 I snapshot:                   3  -1901674991        8ea6be11
2024-04-07 09:10:01 I snapshot:         type=0 id=32765
2024-04-07 09:10:01 I snapshot:                   0   1993229659        76ce455b
2024-04-07 09:10:01 I snapshot:                   1   -102024632        f9eb3a48
2024-04-07 09:10:01 I snapshot:                   2  -1378361269        add7e04b
2024-04-07 09:10:01 I snapshot:                   3  -1810037668        941d045c
2024-04-07 09:10:01 I snapshot:         type=6 id=0
2024-04-07 09:10:01 I snapshot:                   0            0        00000000
2024-04-07 09:10:01 I snapshot:                   1            0        00000000
2024-04-07 09:10:01 I snapshot:                   2            0        00000000
2024-04-07 09:10:01 I snapshot:                   3            0        00000000
2024-04-07 09:10:01 I snapshot:                   4            0        00000000
2024-04-07 09:10:01 I snapshot:                   5            0        00000000
2024-04-07 09:10:01 I snapshot:                   6            0        00000000
2024-04-07 09:10:01 I snapshot:                   7            1        00000001
2024-04-07 09:10:01 I snapshot:         type=32767 id=0
2024-04-07 09:10:01 I snapshot:                   0    247983155        0ec7ec33
2024-04-07 09:10:01 I snapshot:                   1            9        00000009
2024-04-07 09:10:01 I snapshot:                   2           64        00000040
2024-04-07 09:10:01 I snapshot:         type=11 id=0
2024-04-07 09:10:01 I snapshot:                   0  -1008145940        c3e8e9ec
2024-04-07 09:10:01 I snapshot:                   1   -320474428        ece5f2c4
2024-04-07 09:10:01 I snapshot:                   2   -220076049        f2e1e7ef
2024-04-07 09:10:01 I snapshot:                   3   -293568512        ee808000
2024-04-07 09:10:01 I snapshot:                   4  -1330727040        b0aeb780
2024-04-07 09:10:01 I snapshot:                   5  -2139062144        80808080
2024-04-07 09:10:01 I snapshot:                   6  -2139062272        80808000
2024-04-07 09:10:01 I snapshot:                   7           64        00000040
2024-04-07 09:10:01 I snapshot:                   8   -403511835        e7f2e5e5
2024-04-07 09:10:01 I snapshot:                   9   -286001183        eef3f7e1
2024-04-07 09:10:01 I snapshot:                  10   -219905920        f2e48080
2024-04-07 09:10:01 I snapshot:                  11  -2139062144        80808080
2024-04-07 09:10:01 I snapshot:                  12  -2139062144        80808080
2024-04-07 09:10:01 I snapshot:                  13  -2139062272        80808000
2024-04-07 09:10:01 I snapshot:                  14            0        00000000
2024-04-07 09:10:01 I snapshot:                  15      7199301        006dda45
2024-04-07 09:10:01 I snapshot:                  16      2345678        0023cace
2024-04-07 09:10:01 I snapshot:         type=10 id=0
2024-04-07 09:10:01 I snapshot:                   0            1        00000001
2024-04-07 09:10:01 I snapshot:                   1            0        00000000
2024-04-07 09:10:01 I snapshot:                   2            0        00000000
2024-04-07 09:10:01 I snapshot:                   3        -9999        ffffd8f1
2024-04-07 09:10:01 I snapshot:                   4            0        00000000
2024-04-07 09:10:01 I snapshot:         type=32766 id=0
2024-04-07 09:10:01 I snapshot:                   0            1        00000001
2024-04-07 09:10:01 I snapshot:                   1            0        00000000
2024-04-07 09:10:01 I snapshot:         type=9 id=0
2024-04-07 09:10:01 I snapshot:                   0          438        000001b6
2024-04-07 09:10:01 I snapshot:                   1         1968        000007b0
2024-04-07 09:10:01 I snapshot:                   2          913        00000391
2024-04-07 09:10:01 I snapshot:                   3            0        00000000
2024-04-07 09:10:01 I snapshot:                   4            0        00000000
2024-04-07 09:10:01 I snapshot:                   5            0        00000000
2024-04-07 09:10:01 I snapshot:                   6            0        00000000
2024-04-07 09:10:01 I snapshot:                   7            0        00000000
2024-04-07 09:10:01 I snapshot:                   8           -1        ffffffff
2024-04-07 09:10:01 I snapshot:                   9            0        00000000
2024-04-07 09:10:01 I snapshot:                  10            0        00000000
2024-04-07 09:10:01 I snapshot:                  11         1968        000007b0
2024-04-07 09:10:01 I snapshot:                  12          913        00000391
2024-04-07 09:10:01 I snapshot:                  13            0        00000000
2024-04-07 09:10:01 I snapshot:                  14            0        00000000
2024-04-07 09:10:01 I snapshot:                  15            1        00000001
2024-04-07 09:10:01 I snapshot:                  16           10        0000000a
2024-04-07 09:10:01 I snapshot:                  17           10        0000000a
2024-04-07 09:10:01 I snapshot:                  18            0        00000000
2024-04-07 09:10:01 I snapshot:                  19            1        00000001
2024-04-07 09:10:01 I snapshot:                  20            5        00000005
2024-04-07 09:10:01 I snapshot:                  21            0        00000000
2024-04-07 09:10:01 I snapshot:         type=32765 id=0
2024-04-07 09:10:01 I snapshot:                   0        49152        0000c000
2024-04-07 09:10:01 I snapshot:                   1            0        00000000
2024-04-07 09:10:01 I snapshot:                   2            2        00000002
2024-04-07 09:10:01 I snapshot:                   3            0        00000000
2024-04-07 09:10:01 I snapshot:                   4            0        00000000
2024-04-07 09:10:01 I snapshot:                   5            0        00000000
2024-04-07 09:10:01 I snapshot:                   6            0        00000000
2024-04-07 09:10:01 I snapshot:                   7            0        00000000
2024-04-07 09:10:01 I snapshot:                   8            1        00000001
2024-04-07 09:10:01 I snapshot:                   9            0        00000000
```

Now there is also `CNetObjHandler::DebugDumpSnapshot(const CSnapshot *pSnap)`
which prints the name of the snap item and also the names of all fields:

```
2024-04-07 09:21:42 I snapshot: data_size=356 num_items=10
2024-04-07 09:21:42 I snapshot:         EX/UUID type=0 id=32767 size=16
2024-04-07 09:21:42 I snapshot:                   0  -1824658838        933dea6a
2024-04-07 09:21:42 I snapshot:                   1   -629591830        da7930ea
2024-04-07 09:21:42 I snapshot:                   2  -1450210576        a98f8af0
2024-04-07 09:21:42 I snapshot:                   3    914991429        3689a945
2024-04-07 09:21:42 I snapshot:         EX/UUID type=0 id=32766 size=16
2024-04-07 09:21:42 I snapshot:                   0    583701389        22ca938d
2024-04-07 09:21:42 I snapshot:                   1    327171627        13803e2b
2024-04-07 09:21:42 I snapshot:                   2  -1636052395        9e7bd255
2024-04-07 09:21:42 I snapshot:                   3  -1901674991        8ea6be11
2024-04-07 09:21:42 I snapshot:         EX/UUID type=0 id=32765 size=16
2024-04-07 09:21:42 I snapshot:                   0   1993229659        76ce455b
2024-04-07 09:21:42 I snapshot:                   1   -102024632        f9eb3a48
2024-04-07 09:21:42 I snapshot:                   2  -1378361269        add7e04b
2024-04-07 09:21:42 I snapshot:                   3  -1810037668        941d045c
2024-04-07 09:21:42 I snapshot:         GameInfo type=6 id=0 size=32
2024-04-07 09:21:42 I snapshot:                   0            0        00000000        m_GameFlags=0 (min=0 max=256)
2024-04-07 09:21:42 I snapshot:                   1            0        00000000        m_GameStateFlags=0 (min=0 max=256)
2024-04-07 09:21:42 I snapshot:                   2            0        00000000        m_RoundStartTick=0 (NetTick)
2024-04-07 09:21:42 I snapshot:                   3            0        00000000        m_WarmupTimer=0 (min=min_int(-2147483648) max=max_int(2147483647))
2024-04-07 09:21:42 I snapshot:                   4            0        00000000        m_ScoreLimit=0 (min=0 max=max_int(2147483647))
2024-04-07 09:21:42 I snapshot:                   5            0        00000000        m_TimeLimit=0 (min=0 max=max_int(2147483647))
2024-04-07 09:21:42 I snapshot:                   6            0        00000000        m_RoundNum=0 (min=0 max=max_int(2147483647))
2024-04-07 09:21:42 I snapshot:                   7            1        00000001        m_RoundCurrent=1 (min=0 max=max_int(2147483647))
2024-04-07 09:21:42 I snapshot:         (out of range) type=32767 id=0 size=12
2024-04-07 09:21:42 I snapshot:                   0    247983155        0ec7ec33
2024-04-07 09:21:42 I snapshot:                   1            9        00000009
2024-04-07 09:21:42 I snapshot:                   2           64        00000040
2024-04-07 09:21:42 I snapshot:         ClientInfo type=11 id=0 size=68
2024-04-07 09:21:42 I snapshot:                   0  -1008145940        c3e8e9ec        m_Name0=-1008145940     IntsToStr: Chil
2024-04-07 09:21:42 I snapshot:                   1   -320474428        ece5f2c4        m_Name1=-320474428      IntsToStr: lerD
2024-04-07 09:21:42 I snapshot:                   2   -220076049        f2e1e7ef        m_Name2=-220076049      IntsToStr: rago
2024-04-07 09:21:42 I snapshot:                   3   -293568512        ee808000        m_Name3=-293568512      IntsToStr: n
2024-04-07 09:21:42 I snapshot:                   4  -1330727040        b0aeb780        m_Clan0=-1330727040     IntsToStr: 0.7
2024-04-07 09:21:42 I snapshot:                   5  -2139062144        80808080        m_Clan1=-2139062144     IntsToStr:
2024-04-07 09:21:42 I snapshot:                   6  -2139062272        80808000        m_Clan2=-2139062272     IntsToStr:
2024-04-07 09:21:42 I snapshot:                   7           64        00000040        m_Country=64
2024-04-07 09:21:42 I snapshot:                   8   -403511835        e7f2e5e5        m_Skin0=-403511835      IntsToStr: gree
2024-04-07 09:21:42 I snapshot:                   9   -286001183        eef3f7e1        m_Skin1=-286001183      IntsToStr: nswa
2024-04-07 09:21:42 I snapshot:                  10   -219905920        f2e48080        m_Skin2=-219905920      IntsToStr: rd
2024-04-07 09:21:42 I snapshot:                  11  -2139062144        80808080        m_Skin3=-2139062144     IntsToStr:
2024-04-07 09:21:42 I snapshot:                  12  -2139062144        80808080        m_Skin4=-2139062144     IntsToStr:
2024-04-07 09:21:42 I snapshot:                  13  -2139062272        80808000        m_Skin5=-2139062272     IntsToStr:
2024-04-07 09:21:42 I snapshot:                  14            0        00000000        m_UseCustomColor=0 (min=0 max=1)
2024-04-07 09:21:42 I snapshot:                  15      7199301        006dda45        m_ColorBody=7199301
2024-04-07 09:21:42 I snapshot:                  16      2345678        0023cace        m_ColorFeet=2345678
2024-04-07 09:21:42 I snapshot:         PlayerInfo type=10 id=0 size=20
2024-04-07 09:21:42 I snapshot:                   0            1        00000001        m_Local=1 (min=0 max=1)
2024-04-07 09:21:42 I snapshot:                   1            0        00000000        m_ClientId=0 (min=0 max=MAX_CLIENTS-1(63))
2024-04-07 09:21:42 I snapshot:                   2            0        00000000        m_Team=0 (min=TEAM_SPECTATORS(-1) max=TEAM_BLUE(1))
2024-04-07 09:21:42 I snapshot:                   3        -9999        ffffd8f1        m_Score=-9999
2024-04-07 09:21:42 I snapshot:                   4            0        00000000        m_Latency=0
2024-04-07 09:21:42 I snapshot:         (out of range) type=32766 id=0 size=8
2024-04-07 09:21:42 I snapshot:                   0            1        00000001
2024-04-07 09:21:42 I snapshot:                   1            0        00000000
2024-04-07 09:21:42 I snapshot:         Character type=9 id=0 size=88
2024-04-07 09:21:42 I snapshot:                   0          176        000000b0        m_Tick=176
2024-04-07 09:21:42 I snapshot:                   1         1968        000007b0        m_X=1968
2024-04-07 09:21:42 I snapshot:                   2          913        00000391        m_Y=913
2024-04-07 09:21:42 I snapshot:                   3            0        00000000        m_VelX=0
2024-04-07 09:21:42 I snapshot:                   4            0        00000000        m_VelY=0
2024-04-07 09:21:42 I snapshot:                   5            0        00000000        m_Angle=0
2024-04-07 09:21:42 I snapshot:                   6            0        00000000        m_Direction=0 (min=-1 max=1)
2024-04-07 09:21:42 I snapshot:                   7            0        00000000        m_Jumped=0 (min=0 max=3)
2024-04-07 09:21:42 I snapshot:                   8           -1        ffffffff        m_HookedPlayer=-1 (min=-1 max=MAX_CLIENTS-1(63))
2024-04-07 09:21:42 I snapshot:                   9            0        00000000        m_HookState=0 (min=-1 max=5)
2024-04-07 09:21:42 I snapshot:                  10            0        00000000        m_HookTick=0 (NetTick)
2024-04-07 09:21:42 I snapshot:                  11         1968        000007b0        m_HookX=1968
2024-04-07 09:21:42 I snapshot:                  12          913        00000391        m_HookY=913
2024-04-07 09:21:42 I snapshot:                  13            0        00000000        m_HookDx=0
2024-04-07 09:21:42 I snapshot:                  14            0        00000000        m_HookDy=0
2024-04-07 09:21:42 I snapshot:                  15            1        00000001        m_PlayerFlags=1 (min=0 max=256)
2024-04-07 09:21:42 I snapshot:                  16           10        0000000a        m_Health=10 (min=0 max=10)
2024-04-07 09:21:42 I snapshot:                  17           10        0000000a        m_Armor=10 (min=0 max=10)
2024-04-07 09:21:42 I snapshot:                  18            0        00000000        m_AmmoCount=0 (min=0 max=10)
2024-04-07 09:21:42 I snapshot:                  19            1        00000001        m_Weapon=1 (min=0 max=NUM_WEAPONS-1(5))
2024-04-07 09:21:42 I snapshot:                  20            5        00000005        m_Emote=5 (min=0 max=6)
2024-04-07 09:21:42 I snapshot:                  21            0        00000000        m_AttackTick=0 (min=0 max=max_int(2147483647))
2024-04-07 09:21:42 I snapshot:         (out of range) type=32765 id=0 size=40
2024-04-07 09:21:42 I snapshot:                   0        49152        0000c000
2024-04-07 09:21:42 I snapshot:                   1            0        00000000
2024-04-07 09:21:42 I snapshot:                   2            2        00000002
2024-04-07 09:21:42 I snapshot:                   3            0        00000000
2024-04-07 09:21:42 I snapshot:                   4            0        00000000
2024-04-07 09:21:42 I snapshot:                   5            0        00000000
2024-04-07 09:21:42 I snapshot:                   6            0        00000000
2024-04-07 09:21:42 I snapshot:                   7            0        00000000
2024-04-07 09:21:42 I snapshot:                   8            1        00000001
2024-04-07 09:21:42 I snapshot:                   9            0        00000000
```
2024-09-07 15:28:25 +08:00

496 lines
13 KiB
Python

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("""
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 DebugDumpSnapshot(const class CSnapshot *pSnap) const;
int DumpObj(int Type, const void *pData, int Size) 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 <engine/shared/snapshot.h>
#include <game/gamecore.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)";
}
void CNetObjHandler::DebugDumpSnapshot(const CSnapshot *pSnap) const
{
dbg_msg("snapshot", "data_size=%d num_items=%d", pSnap->DataSize(), pSnap->NumItems());
for(int i = 0; i < pSnap->NumItems(); i++)
{
const CSnapshotItem *pItem = pSnap->GetItem(i);
int Size = pSnap->GetItemSize(i);
int Type = pSnap->GetItemType(i);
const char *pName = GetObjName(pItem->Type());
if(Type > OFFSET_UUID && Type < g_UuidManager.NumUuids() + OFFSET_UUID)
pName = g_UuidManager.GetName(Type);
dbg_msg("snapshot", "\\t%s type=%d id=%d size=%d", pName, pItem->Type(), pItem->Id(), Size);
if(!DumpObj(Type, pItem->Data(), Size))
continue;
for(size_t b = 0; b < Size / sizeof(int32_t); b++)
dbg_msg("snapshot", "\\t\\t%3d %12d\\t%08x", (int)b, pItem->Data()[b], pItem->Data()[b]);
}
}
""")
lines = []
lines += ['int CNetObjHandler::DumpObj(int Type, const void *pData, int Size) const']
lines += ['{']
lines += ["\tchar aRawData[512];"]
lines += ['\tswitch(Type)']
lines += ['\t{']
for item in network.Objects:
for line in item.emit_dump(network.Objects):
lines += ["\t" + line]
lines += ['\t']
lines += ['\t}']
lines += ['\treturn -1;']
lines += ['};']
lines += ['']
for line in lines:
print(line)
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()