From 72c06a258940696093f255fb1061beb58e1cdd0b Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Sat, 29 May 2010 07:25:38 +0000 Subject: copied refactor to trunk --- datasrc/compile.py | 245 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 159 insertions(+), 86 deletions(-) (limited to 'datasrc/compile.py') diff --git a/datasrc/compile.py b/datasrc/compile.py index 179020fd..4b7be441 100644 --- a/datasrc/compile.py +++ b/datasrc/compile.py @@ -76,7 +76,7 @@ if gen_client_content_header or gen_server_content_header: EmitTypeDeclaration(content.__dict__[name]) # the container pointer - print 'extern DATACONTAINER *data;'; + print 'extern DATACONTAINER *g_pData;'; # enums EmitEnum(["IMAGE_%s"%i.name.value.upper() for i in content.container.images.items], "NUM_IMAGES") @@ -85,17 +85,17 @@ if gen_client_content_header or gen_server_content_header: if gen_client_content_source or gen_server_content_source: if gen_client_content_source: - print '#include "gc_data.hpp"' + print '#include "client_data.h"' if gen_server_content_source: - print '#include "gs_data.hpp"' + print '#include "server_data.h"' EmitDefinition(content.container, "datacontainer") - print 'DATACONTAINER *data = &datacontainer;'; + print 'DATACONTAINER *g_pData = &datacontainer;'; # NETWORK if gen_network_header: - print "#ifndef FILE_G_PROTOCOL_H" - print "#define FILE_G_PROTOCOL_H" + print "#ifndef GAME_GENERATED_PROTOCOL_H" + print "#define GAME_GENERATED_PROTOCOL_H" print network.RawHeader for e in network.Enums: @@ -119,126 +119,199 @@ if gen_network_header: 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 "int netobj_validate(int type, void *data, int size);" - print "const char *netobj_get_name(int type);" - print "int netobj_get_size(int type);" - print "void *netmsg_secure_unpack(int type);" - print "const char *netmsg_get_name(int type);" - print "const char *netmsg_failed_on();" - print "int netobj_num_corrections();" - print "const char *netobj_corrected_on();" - print "#endif // FILE_G_PROTOCOL_H" + print """ + +class CNetObjHandler +{ + const char *m_pMsgFailedOn; + const char *m_pObjCorrectedOn; + char m_aMsgData[1024]; + int m_NumObjCorrections; + int ClampInt(const char *pErrorMsg, int Value, int Min, int Max); + + static const char *ms_apObjNames[]; + static int ms_aObjSizes[]; + static const char *ms_apMsgNames[]; + +public: + CNetObjHandler(); + + int ValidateObj(int Type, void *pData, int Size); + const char *GetObjName(int Type); + int GetObjSize(int Type); + int NumObjCorrections(); + const char *CorrectedObjOn(); + + const char *GetMsgName(int Type); + void *SecureUnpackMsg(int Type, CUnpacker *pUnpacker); + const char *FailedMsgOn(); +}; + +""" + + print "#endif // GAME_GENERATED_PROTOCOL_H" if gen_network_source: # create names lines = [] - lines += ['#include '] - lines += ['#include "g_protocol.hpp"'] + lines += ['#include '] + lines += ['#include '] + lines += ['#include "protocol.h"'] + + lines += ['CNetObjHandler::CNetObjHandler()'] + lines += ['{'] + lines += ['\tm_pMsgFailedOn = "";'] + lines += ['\tm_pObjCorrectedOn = "";'] + lines += ['\tm_NumObjCorrections = 0;'] + lines += ['}'] + lines += [''] + lines += ['int CNetObjHandler::NumObjCorrections() { return m_NumObjCorrections; }'] + lines += ['const char *CNetObjHandler::CorrectedObjOn() { return m_pObjCorrectedOn; }'] + lines += ['const char *CNetObjHandler::FailedMsgOn() { return m_pMsgFailedOn; }'] + lines += [''] + lines += [''] + lines += [''] + lines += [''] + lines += [''] - lines += ['const char *msg_failed_on = "";'] - lines += ['const char *obj_corrected_on = "";'] - lines += ['static int num_corrections = 0;'] - lines += ['int netobj_num_corrections() { return num_corrections; }'] - lines += ['const char *netobj_corrected_on() { return obj_corrected_on; }'] - lines += ['const char *netmsg_failed_on() { return msg_failed_on; }'] - lines += ['const int max_int = 0x7fffffff;'] + lines += ['static const int max_int = 0x7fffffff;'] - lines += ['static int netobj_clamp_int(const char *error_msg, int v, int min, int max)'] + lines += ['int CNetObjHandler::ClampInt(const char *pErrorMsg, int Value, int Min, int Max)'] lines += ['{'] - lines += ['\tif(vmax) { obj_corrected_on = error_msg; num_corrections++; return max; }'] - lines += ['\treturn v;'] + lines += ['\tif(Value < Min) { m_pObjCorrectedOn = pErrorMsg; m_NumObjCorrections++; return Min; }'] + lines += ['\tif(Value > Max) { m_pObjCorrectedOn = pErrorMsg; m_NumObjCorrections++; return Max; }'] + lines += ['\treturn Value;'] lines += ['}'] - lines += ["static const char *netobj_names[] = {"] + lines += ["const char *CNetObjHandler::ms_apObjNames[] = {"] lines += ['\t"invalid",'] lines += ['\t"%s",' % o.name for o in network.Objects] lines += ['\t""', "};", ""] - lines += ["static int netobj_sizes[] = {"] + lines += ["int CNetObjHandler::ms_aObjSizes[] = {"] lines += ['\t0,'] lines += ['\tsizeof(%s),' % o.struct_name for o in network.Objects] lines += ['\t0', "};", ""] - for l in lines: - print l - 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 += ['const char *netobj_get_name(int type)'] + 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)'] lines += ['{'] - lines += ['\tif(type < 0 || type >= NUM_NETOBJTYPES) return "(out of range)";'] - lines += ['\treturn netobj_names[type];'] + lines += ['\tif(Type < 0 || Type >= NUM_NETOBJTYPES) return "(out of range)";'] + lines += ['\treturn ms_apObjNames[Type];'] lines += ['};'] lines += [''] - lines += ['int netobj_get_size(int type)'] + lines += ['int CNetObjHandler::GetObjSize(int Type)'] lines += ['{'] - lines += ['\tif(type < 0 || type >= NUM_NETOBJTYPES) return 0;'] - lines += ['\treturn netobj_sizes[type];'] + lines += ['\tif(Type < 0 || Type >= NUM_NETOBJTYPES) return 0;'] + lines += ['\treturn ms_aObjSizes[Type];'] lines += ['};'] lines += [''] - for item in network.Messages: - for line in item.emit_unpack(): - print line - print "" - - lines += ['static void *secure_unpack_invalid() { return 0; }'] - lines += ['typedef void *(*SECUREUNPACKFUNC)();'] - 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 *netmsg_secure_unpack(int type)'] + lines += ['const char *CNetObjHandler::GetMsgName(int Type)'] lines += ['{'] - lines += ['\tvoid *msg;'] - lines += ['\tmsg_failed_on = "";'] - lines += ['\tif(type < 0 || type >= NUM_NETMSGTYPES) { msg_failed_on = "(type out of range)"; return 0; }'] - lines += ['\tmsg = secure_unpack_funcs[type]();'] - lines += ['\tif(msg_unpack_error()) { msg_failed_on = "(unpack error)"; return 0; }'] - lines += ['\treturn msg;'] + lines += ['\tif(Type < 0 || Type >= NUM_NETMSGTYPES) return "(out of range)";'] + lines += ['\treturn ms_apMsgNames[Type];'] lines += ['};'] lines += [''] + + + for l in lines: + print l - lines += ['static const char *message_names[] = {'] - lines += ['\t"invalid",'] - for msg in network.Messages: - lines += ['\t"%s",' % msg.name] - lines += ['\t""'] + 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, 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 += ['const char *netmsg_get_name(int type)'] + 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 += ['\tif(type < 0 || type >= NUM_NETMSGTYPES) return "(out of range)";'] - lines += ['\treturn message_names[type];'] + lines += ['\tm_pMsgFailedOn = 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)'] + lines += ['\t\treturn 0;'] + lines += ['\tm_pMsgFailedOn = "";'] + lines += ['\treturn m_aMsgData;'] lines += ['};'] lines += [''] + for l in lines: print l -- cgit 1.4.1