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
|
import sys
from datatypes import *
import content
import network
def create_enum_table(names, num):
lines = []
lines += ["enum", "{"]
lines += ["\t%s=0,"%names[0]]
for name in names[1:]:
lines += ["\t%s,"%name]
lines += ["\t%s" % num, "};"]
return lines
def create_flags_table(names):
lines = []
lines += ["enum", "{"]
i = 0
for name in names:
lines += ["\t%s = 1<<%d," % (name,i)]
i += 1
lines += ["};"]
return lines
def EmitEnum(names, num):
print("enum")
print("{")
print("\t%s=0," % names[0])
for name in names[1:]:
print("\t%s," % name)
print("\t%s" % num)
print("};")
def EmitFlags(names, num):
print("enum")
print("{")
i = 0
for name in names:
print("\t%s = 1<<%d," % (name,i))
i += 1
print("};")
gen_network_header = False
gen_network_source = False
gen_client_content_header = False
gen_client_content_source = False
gen_server_content_header = False
gen_server_content_source = False
if "network_header" in sys.argv: gen_network_header = True
if "network_source" in sys.argv: gen_network_source = True
if "client_content_header" in sys.argv: gen_client_content_header = True
if "client_content_source" in sys.argv: gen_client_content_source = True
if "server_content_header" in sys.argv: gen_server_content_header = True
if "server_content_source" in sys.argv: gen_server_content_source = True
if gen_client_content_header:
print("#ifndef CLIENT_CONTENT_HEADER")
print("#define CLIENT_CONTENT_HEADER")
if gen_server_content_header:
print("#ifndef SERVER_CONTENT_HEADER")
print("#define SERVER_CONTENT_HEADER")
if gen_client_content_header or gen_server_content_header:
# print some includes
print('#include <engine/graphics.h>')
print('#include <engine/sound.h>')
# emit the type declarations
contentlines = open("datasrc/content.py", "rb").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])
# the container pointer
print('extern CDataContainer *g_pData;')
# enums
EmitEnum(["IMAGE_%s"%i.name.value.upper() for i in content.container.images.items], "NUM_IMAGES")
EmitEnum(["ANIM_%s"%i.name.value.upper() for i in content.container.animations.items], "NUM_ANIMS")
EmitEnum(["SPRITE_%s"%i.name.value.upper() for i in content.container.sprites.items], "NUM_SPRITES")
if gen_client_content_source or gen_server_content_source:
if gen_client_content_source:
print('#include "client_data.h"')
if gen_server_content_source:
print('#include "server_data.h"')
EmitDefinition(content.container, "datacontainer")
print('CDataContainer *g_pData = &datacontainer;')
# NETWORK
if gen_network_header:
print("#ifndef GAME_GENERATED_PROTOCOL_H")
print("#define GAME_GENERATED_PROTOCOL_H")
print(network.RawHeader)
for e in network.Enums:
for l in create_enum_table(["%s_%s"%(e.name, v) for v in e.values], 'NUM_%sS'%e.name): print(l)
print("")
for e in network.Flags:
for l in create_flags_table(["%s_%s" % (e.name, v) for v in e.values]): print(l)
print("")
for l in create_enum_table(["NETOBJ_INVALID"]+[o.enum_name for o in network.Objects], "NUM_NETOBJTYPES"): print(l)
print("")
for l in create_enum_table(["NETMSG_INVALID"]+[o.enum_name for o in network.Messages], "NUM_NETMSGTYPES"): print(l)
print("")
for item in network.Objects + network.Messages:
for line in item.emit_declaration():
print(line)
print("")
EmitEnum(["SOUND_%s"%i.name.value.upper() for i in content.container.sounds.items], "NUM_SOUNDS")
EmitEnum(["WEAPON_%s"%i.name.value.upper() for i in content.container.weapons.id.items], "NUM_WEAPONS")
print("""
class CNetObjHandler
{
const char *m_pMsgFailedOn;
char m_aMsgData[1024];
const char *m_pObjFailedOn;
int m_NumObjFailures;
bool CheckInt(const char *pErrorMsg, int Value, int Min, int Max);
bool CheckFlag(const char *pErrorMsg, int Value, int Mask);
static const char *ms_apObjNames[];
static int ms_aObjSizes[];
static const char *ms_apMsgNames[];
public:
CNetObjHandler();
int ValidateObj(int Type, const void *pData, int Size);
const char *GetObjName(int Type) const;
int GetObjSize(int Type) const;
const char *FailedObjOn() const;
int NumObjFailures() const;
const char *GetMsgName(int Type) const;
void *SecureUnpackMsg(int Type, CUnpacker *pUnpacker);
const char *FailedMsgOn() const;
};
""")
print("#endif // GAME_GENERATED_PROTOCOL_H")
if gen_network_source:
# create names
lines = []
lines += ['#include <engine/shared/protocol.h>']
lines += ['#include <engine/message.h>']
lines += ['#include "protocol.h"']
lines += ['CNetObjHandler::CNetObjHandler()']
lines += ['{']
lines += ['\tm_pMsgFailedOn = "";']
lines += ['\tm_pObjFailedOn = "";']
lines += ['\tm_NumObjFailures = 0;']
lines += ['}']
lines += ['']
lines += ['const char *CNetObjHandler::FailedObjOn() const { return m_pObjFailedOn; }']
lines += ['int CNetObjHandler::NumObjFailures() const { return m_NumObjFailures; }']
lines += ['const char *CNetObjHandler::FailedMsgOn() const { return m_pMsgFailedOn; }']
lines += ['']
lines += ['']
lines += ['']
lines += ['']
lines += ['static const int max_int = 0x7fffffff;']
lines += ['']
lines += ['bool CNetObjHandler::CheckInt(const char *pErrorMsg, int Value, int Min, int Max)']
lines += ['{']
lines += ['\tif(Value < Min || Value > Max) { m_pObjFailedOn = pErrorMsg; m_NumObjFailures++; return false; }']
lines += ['\treturn true;']
lines += ['}']
lines += ['']
lines += ['bool CNetObjHandler::CheckFlag(const char *pErrorMsg, int Value, int Mask)']
lines += ['{']
lines += ['\tif((Value&Mask) != Value) { m_pObjFailedOn = pErrorMsg; m_NumObjFailures++; return false; }']
lines += ['\treturn true;']
lines += ['}']
lines += ['']
lines += ["const char *CNetObjHandler::ms_apObjNames[] = {"]
lines += ['\t"invalid",']
lines += ['\t"%s",' % o.name for o in network.Objects]
lines += ['\t""', "};", ""]
lines += ["int CNetObjHandler::ms_aObjSizes[] = {"]
lines += ['\t0,']
lines += ['\tsizeof(%s),' % o.struct_name for o in network.Objects]
lines += ['\t0', "};", ""]
lines += ['const char *CNetObjHandler::ms_apMsgNames[] = {']
lines += ['\t"invalid",']
for msg in network.Messages:
lines += ['\t"%s",' % msg.name]
lines += ['\t""']
lines += ['};']
lines += ['']
lines += ['const char *CNetObjHandler::GetObjName(int Type) const']
lines += ['{']
lines += ['\tif(Type < 0 || Type >= NUM_NETOBJTYPES) return "(out of range)";']
lines += ['\treturn ms_apObjNames[Type];']
lines += ['};']
lines += ['']
lines += ['int CNetObjHandler::GetObjSize(int Type) const']
lines += ['{']
lines += ['\tif(Type < 0 || Type >= NUM_NETOBJTYPES) return 0;']
lines += ['\treturn ms_aObjSizes[Type];']
lines += ['};']
lines += ['']
lines += ['const char *CNetObjHandler::GetMsgName(int Type) const']
lines += ['{']
lines += ['\tif(Type < 0 || Type >= NUM_NETMSGTYPES) return "(out of range)";']
lines += ['\treturn ms_apMsgNames[Type];']
lines += ['};']
lines += ['']
for l in lines:
print(l)
if 0:
for item in network.Objects:
for line in item.emit_validate():
print(line)
print("")
# create validate tables
lines = []
lines += ['static int validate_invalid(void *data, int size) { return -1; }']
lines += ["typedef int(*VALIDATEFUNC)(void *data, int size);"]
lines += ["static VALIDATEFUNC validate_funcs[] = {"]
lines += ['\tvalidate_invalid,']
lines += ['\tvalidate_%s,' % o.name for o in network.Objects]
lines += ["\t0x0", "};", ""]
lines += ["int netobj_validate(int type, void *data, int size)"]
lines += ["{"]
lines += ["\tif(type < 0 || type >= NUM_NETOBJTYPES) return -1;"]
lines += ["\treturn validate_funcs[type](data, size);"]
lines += ["};", ""]
lines = []
lines += ['int CNetObjHandler::ValidateObj(int Type, const void *pData, int Size)']
lines += ['{']
lines += ['\tswitch(Type)']
lines += ['\t{']
for item in network.Objects:
for line in item.emit_validate():
lines += ["\t" + line]
lines += ['\t']
lines += ['\t}']
lines += ['\treturn -1;']
lines += ['};']
lines += ['']
#int Validate(int Type, void *pData, int Size);
if 0:
for item in network.Messages:
for line in item.emit_unpack():
print(line)
print("")
lines += ['static void *secure_unpack_invalid(CUnpacker *pUnpacker) { return 0; }']
lines += ['typedef void *(*SECUREUNPACKFUNC)(CUnpacker *pUnpacker);']
lines += ['static SECUREUNPACKFUNC secure_unpack_funcs[] = {']
lines += ['\tsecure_unpack_invalid,']
for msg in network.Messages:
lines += ['\tsecure_unpack_%s,' % msg.name]
lines += ['\t0x0']
lines += ['};']
#
lines += ['void *CNetObjHandler::SecureUnpackMsg(int Type, CUnpacker *pUnpacker)']
lines += ['{']
lines += ['\tm_pMsgFailedOn = 0;']
lines += ['\tm_pObjFailedOn = 0;']
lines += ['\tswitch(Type)']
lines += ['\t{']
for item in network.Messages:
for line in item.emit_unpack():
lines += ["\t" + line]
lines += ['\t']
lines += ['\tdefault:']
lines += ['\t\tm_pMsgFailedOn = "(type out of range)";']
lines += ['\t\tbreak;']
lines += ['\t}']
lines += ['\t']
lines += ['\tif(pUnpacker->Error())']
lines += ['\t\tm_pMsgFailedOn = "(unpack error)";']
lines += ['\t']
lines += ['\tif(m_pMsgFailedOn || m_pObjFailedOn) {']
lines += ['\t\tif(!m_pMsgFailedOn)']
lines += ['\t\t\tm_pMsgFailedOn = "";']
lines += ['\t\tif(!m_pObjFailedOn)']
lines += ['\t\t\tm_pObjFailedOn = "";']
lines += ['\t\treturn 0;']
lines += ['\t}']
lines += ['\tm_pMsgFailedOn = "";']
lines += ['\tm_pObjFailedOn = "";']
lines += ['\treturn m_aMsgData;']
lines += ['};']
lines += ['']
for l in lines:
print(l)
if gen_client_content_header or gen_server_content_header:
print("#endif")
|