diff --git a/cpp/dynmodule.cc b/cpp/dynmodule.cc index f6eaf1b..9703242 100644 --- a/cpp/dynmodule.cc +++ b/cpp/dynmodule.cc @@ -1,41 +1,45 @@ #include "framework/cpp/dynmodule.h" -DynModule::DynModule() +namespace f8 { -} + DynModule::DynModule() + { + } -DynModule::~DynModule() -{ + DynModule::~DynModule() + { + + } + + bool DynModule::IsDataMsg(int msgid) + { + return false; + } + + bool DynModule::IsPending() + { + return module_state_.pending; + } + + bool DynModule::DataIsValid() + { + return module_state_.data_is_valid; + } + + void DynModule::MarkDataValid() + { + module_state_.data_is_valid = true; + } + + void DynModule::MarkPending() + { + module_state_.pending = true; + } + + void DynModule::CancelPending() + { + module_state_.pending = false; + } } -bool DynModule::IsDataMsg(int msgid) -{ - return false; -} - -bool DynModule::IsPending() -{ - return module_state_.pending; -} - -bool DynModule::DataIsValid() -{ - return module_state_.data_is_valid; -} - -void DynModule::MarkDataValid() -{ - module_state_.data_is_valid = true; -} - -void DynModule::MarkPending() -{ - module_state_.pending = true; -} - -void DynModule::CancelPending() -{ - module_state_.pending = false; -} - diff --git a/cpp/dynmodule.h b/cpp/dynmodule.h index c64e313..8593fdf 100644 --- a/cpp/dynmodule.h +++ b/cpp/dynmodule.h @@ -1,24 +1,28 @@ #pragma once -struct DynModuleState +namespace f8 { - bool pending = false; - bool data_is_valid = false;; -}; + struct DynModuleState + { + bool pending = false; + bool data_is_valid = false;; + }; -class DynModule -{ - public: - DynModule(); - ~DynModule(); - virtual bool IsDataMsg(int msgid); - bool IsPending(); - bool DataIsValid(); - void MarkDataValid(); - void MarkPending(); - void CancelPending(); + class DynModule + { + public: + DynModule(); + ~DynModule(); + virtual bool IsDataMsg(int msgid); + bool IsPending(); + bool DataIsValid(); + void MarkDataValid(); + void MarkPending(); + void CancelPending(); - private: - DynModuleState module_state_; -}; + private: + DynModuleState module_state_; + }; + +} diff --git a/cpp/netmsghandler.cc b/cpp/netmsghandler.cc index 91104e9..f124e87 100644 --- a/cpp/netmsghandler.cc +++ b/cpp/netmsghandler.cc @@ -6,8 +6,11 @@ #include "framework/cpp/protoutils.h" #include "framework/cpp/netmsghandler.h" -NetMsgHandler* GetNetMsgHandler(NetMsgHandlerObject* handlers, - unsigned short msgid) +namespace f8 { - return msgid < MAX_MSG_ID ? handlers->handlers[msgid] : nullptr; + NetMsgHandler* GetNetMsgHandler(NetMsgHandlerObject* handlers, + unsigned short msgid) + { + return msgid < MAX_MSG_ID ? handlers->handlers[msgid] : nullptr; + } } diff --git a/cpp/netmsghandler.h b/cpp/netmsghandler.h index 7bfc2a0..35d53a0 100644 --- a/cpp/netmsghandler.h +++ b/cpp/netmsghandler.h @@ -7,83 +7,85 @@ #include #include -const unsigned short MAX_MSG_ID = 2000; - -struct MsgHdr; - -struct NetMsgHandler +namespace f8 { - int handlerid; - const ::google::protobuf::Message* prototype = nullptr; -}; + const unsigned short MAX_MSG_ID = 2000; -struct NetMsgHandlerObject -{ - NetMsgHandler* handlers[MAX_MSG_ID] = { nullptr }; -}; + struct MsgHdr; -template -static void NetMsgHandlerWrapper(InstanceType* instance, MsgHdr& hdr, NetMsgHandler* handler) -{ - MsgType msg; - bool ok = msg.ParseFromArray(hdr.buf + hdr.offset, hdr.buflen - hdr.offset); - assert(ok); - if (ok) { + struct NetMsgHandler + { + int handlerid; + const ::google::protobuf::Message* prototype = nullptr; + }; + + struct NetMsgHandlerObject + { + NetMsgHandler* handlers[MAX_MSG_ID] = { nullptr }; + }; + + template + static void NetMsgHandlerWrapper(InstanceType* instance, MsgHdr& hdr, NetMsgHandler* handler) + { + MsgType msg; + bool ok = msg.ParseFromArray(hdr.buf + hdr.offset, hdr.buflen - hdr.offset); + assert(ok); + if (ok) { + struct Invoker: public NetMsgHandler + { + void (*wrapper)(InstanceType*, MsgHdr&, NetMsgHandler*); + void (InstanceType::*func)(MsgHdr&, const MsgType&); + }; + Invoker* invoker = (Invoker*)handler; + auto ptr = invoker->func; + (instance->*ptr)(hdr, msg); + } + } + + template + static void RegisterNetMsgHandler(NetMsgHandlerObject* msghandler, + void (InstanceType::*ptr)(MsgHdr&, const MsgType&) + ) + { + MsgType dumy_msg; + static int msgid = f8::Net_GetMessageId(dumy_msg); + assert(msgid >= 0 || msgid < MAX_MSG_ID); + if (msgid < 0 || msgid >= MAX_MSG_ID) { + abort(); + } struct Invoker: public NetMsgHandler { void (*wrapper)(InstanceType*, MsgHdr&, NetMsgHandler*); void (InstanceType::*func)(MsgHdr&, const MsgType&); }; - Invoker* invoker = (Invoker*)handler; - auto ptr = invoker->func; - (instance->*ptr)(hdr, msg); + Invoker *p = new Invoker(); + p->wrapper = &NetMsgHandlerWrapper; + p->func = ptr; + p->handlerid = InstanceType::HID; + p->prototype = ::google::protobuf::MessageFactory::generated_factory()->GetPrototype(MsgType::descriptor()); + msghandler->handlers[msgid] = p; } -} -template -static void RegisterNetMsgHandler(NetMsgHandlerObject* msghandler, - void (InstanceType::*ptr)(MsgHdr&, const MsgType&) - ) -{ - MsgType dumy_msg; - static int msgid = ::Net_GetMessageId(dumy_msg); - assert(msgid >= 0 || msgid < MAX_MSG_ID); - if (msgid < 0 || msgid >= MAX_MSG_ID) { - abort(); - } - struct Invoker: public NetMsgHandler + template + static bool ProcessNetMsg(NetMsgHandler* handler, + InstanceType* instance, + MsgHdr& hdr) { - void (*wrapper)(InstanceType*, MsgHdr&, NetMsgHandler*); - void (InstanceType::*func)(MsgHdr&, const MsgType&); - }; - Invoker *p = new Invoker(); - p->wrapper = &NetMsgHandlerWrapper; - p->func = ptr; - p->handlerid = InstanceType::HID; - p->prototype = ::google::protobuf::MessageFactory::generated_factory()->GetPrototype(MsgType::descriptor()); - msghandler->handlers[msgid] = p; -} - -template -static bool ProcessNetMsg(NetMsgHandler* handler, - InstanceType* instance, - MsgHdr& hdr) -{ - if(handler){ - struct Invoker: public NetMsgHandler - { - void (*wrapper)(InstanceType*, MsgHdr&, NetMsgHandler*); - void* func; - }; - Invoker *p = (Invoker*)handler; - p->wrapper(instance, hdr, p); - return true; - }else{ - return false; + if(handler){ + struct Invoker: public NetMsgHandler + { + void (*wrapper)(InstanceType*, MsgHdr&, NetMsgHandler*); + void* func; + }; + Invoker *p = (Invoker*)handler; + p->wrapper(instance, hdr, p); + return true; + }else{ + return false; + } } + + NetMsgHandler* GetNetMsgHandler(NetMsgHandlerObject* handlers, + unsigned short msgid); } - -NetMsgHandler* GetNetMsgHandler(NetMsgHandlerObject* handlers, - unsigned short msgid); - #endif diff --git a/cpp/protoutils.cc b/cpp/protoutils.cc index 129d14c..e3b50e6 100644 --- a/cpp/protoutils.cc +++ b/cpp/protoutils.cc @@ -8,168 +8,171 @@ #include #include -int Net_GetMessageId(::google::protobuf::Message& msg) +namespace f8 { - std::string msgname; + int Net_GetMessageId(::google::protobuf::Message& msg) { - std::string full_msgname = msg.GetTypeName(); - std::vector strings; - a8::Split(full_msgname, strings, '.'); - msgname = strings[strings.size() - 1]; - } - assert(msgname.size() > 2); - if (msgname.size() < 2) { - abort(); - return 0; - } - std::string msgid_enum_name; - if (msgname[0] == 'C' && msgname[1] == 'M') { - msgid_enum_name = "cs.CMMessageId_e"; - } else if (msgname[0] == 'S' && msgname[1] == 'M') { - msgid_enum_name = "cs.SMMessageId_e"; - } else if (msgname[0] == 'S' && msgname[1] == 'S') { - msgid_enum_name = "ss.SSMessageId_e"; - } else { - assert(false); - abort(); - return 0; + std::string msgname; + { + std::string full_msgname = msg.GetTypeName(); + std::vector strings; + a8::Split(full_msgname, strings, '.'); + msgname = strings[strings.size() - 1]; + } + assert(msgname.size() > 2); + if (msgname.size() < 2) { + abort(); + return 0; + } + std::string msgid_enum_name; + if (msgname[0] == 'C' && msgname[1] == 'M') { + msgid_enum_name = "cs.CMMessageId_e"; + } else if (msgname[0] == 'S' && msgname[1] == 'M') { + msgid_enum_name = "cs.SMMessageId_e"; + } else if (msgname[0] == 'S' && msgname[1] == 'S') { + msgid_enum_name = "ss.SSMessageId_e"; + } else { + assert(false); + abort(); + return 0; + } + + auto msgid_enum = ::google::protobuf::DescriptorPool::generated_pool()->FindEnumTypeByName(msgid_enum_name); + assert(msgid_enum); + if (!msgid_enum) { + abort(); + return 0; + } + auto msgid_value = msgid_enum->FindValueByName("_" + msgname); + assert(msgid_value); + if (!msgid_value) { + abort(); + return 0; + } + return msgid_value->number(); } - auto msgid_enum = ::google::protobuf::DescriptorPool::generated_pool()->FindEnumTypeByName(msgid_enum_name); - assert(msgid_enum); - if (!msgid_enum) { - abort(); - return 0; + void Net_PackMsg(unsigned short msgid, ::google::protobuf::Message& msg, std::string& out) + { + int packlen = msg.ByteSize(); + + out.resize(sizeof(PackHead) + packlen); + char* buff = (char*)out.data(); + PackHead* head = (PackHead*)buff; + head->packlen = packlen; + head->msgid = msgid; + head->magic_code = MAGIC_CODE; + msg.SerializeToArray(buff + sizeof(PackHead), packlen); } - auto msgid_value = msgid_enum->FindValueByName("_" + msgname); - assert(msgid_value); - if (!msgid_value) { - abort(); - return 0; + + int Net_SendMsg(a8::TcpClient* tcp_client, unsigned seqid, unsigned short msgid, ::google::protobuf::Message& msg) + { + int packlen = msg.ByteSize(); + assert(packlen < 1024 * 60); + + char* buff = (char*)malloc(sizeof(PackHead) + packlen); + PackHead* head = (PackHead*)buff; + head->packlen = packlen; + head->msgid = msgid; + head->seqid = seqid; + head->magic_code = MAGIC_CODE; + head->rpc_error_code = 0; + msg.SerializeToArray(buff + sizeof(PackHead), packlen); + tcp_client->SendBuff(buff, sizeof(PackHead) + packlen); + free(buff); + return sizeof(PackHead) + packlen; + } + + int Net_SendMsg(a8::TcpListener* tcp_listener, unsigned short socket_handle, unsigned int seqid, + unsigned short msgid, ::google::protobuf::Message& msg) + { + int packlen = msg.ByteSize(); + assert(packlen < 1024 * 60); + + char* buff = (char*)malloc(sizeof(PackHead) + packlen); + PackHead* head = (PackHead*)buff; + head->packlen = packlen; + head->msgid = msgid; + head->seqid = seqid; + head->magic_code = MAGIC_CODE; + head->rpc_error_code = 0; + msg.SerializeToArray(buff + sizeof(PackHead), packlen); + tcp_listener->SendClientMsg(socket_handle, buff, sizeof(PackHead) + packlen); + free(buff); + return sizeof(PackHead) + packlen; + } + + int Net_BroadcastMsg(a8::TcpListener* tcp_listener, unsigned int seqid, + unsigned short msgid, ::google::protobuf::Message& msg) + { + int packlen = msg.ByteSize(); + assert(packlen < 1024 * 60); + + char* buff = (char*)malloc(sizeof(PackHead) + packlen); + PackHead* head = (PackHead*)buff; + head->packlen = packlen; + head->msgid = msgid; + head->seqid = seqid; + head->magic_code = MAGIC_CODE; + head->rpc_error_code = 0; + msg.SerializeToArray(buff + sizeof(PackHead), packlen); + tcp_listener->BroadcastMsg(buff, sizeof(PackHead) + packlen); + free(buff); + return sizeof(PackHead) + packlen; + } + + int Net_SendProxyCMsg(a8::TcpClient* tcp_client, unsigned short msgid, ::google::protobuf::Message& msg) + { + int packlen = msg.ByteSize(); + assert(packlen < 1024 * 60); + + char* buff = (char*)malloc(sizeof(WSProxyPackHead_S) + packlen); + WSProxyPackHead_C* head = (WSProxyPackHead_C*)buff; + head->packlen = packlen; + head->msgid = msgid; +#if 1 + head->seqid = 0; +#else + head->seqid = seqid; +#endif + head->magic_code = MAGIC_CODE; +#if 0 + head->rpc_error_code = error_code; +#endif +#if 1 + head->socket_handle = 0; +#else + head->socket_handle = child_socket_handle; +#endif + + msg.SerializeToArray(buff + sizeof(WSProxyPackHead_C), packlen); + tcp_client->SendBuff(buff, sizeof(WSProxyPackHead_C) + packlen); + free(buff); + return sizeof(WSProxyPackHead_C) + packlen; + } + + int Net_SendProxyMsg(a8::TcpListener* tcp_listener, int socket_handle, unsigned int seqid, unsigned short error_code, + unsigned short msgid, ::google::protobuf::Message& msg) + { + unsigned short parent_socket_handle = (socket_handle >> 16) & 0xFFFF; + unsigned short child_socket_handle = socket_handle & 0xFFFF; + + int packlen = msg.ByteSize(); + assert(packlen < 1024 * 60); + + char* buff = (char*)malloc(sizeof(WSProxyPackHead_S) + packlen); + WSProxyPackHead_S* head = (WSProxyPackHead_S*)buff; + head->packlen = packlen; + head->msgid = msgid; + head->seqid = seqid; + head->magic_code = MAGIC_CODE; + head->rpc_error_code = error_code; + head->socket_handle = child_socket_handle; + head->reserved = 0; + + msg.SerializeToArray(buff + sizeof(WSProxyPackHead_S), packlen); + tcp_listener->SendClientMsg(parent_socket_handle, buff, sizeof(WSProxyPackHead_S) + packlen); + free(buff); + return sizeof(WSProxyPackHead_S) + packlen; } - return msgid_value->number(); -} - -void Net_PackMsg(unsigned short msgid, ::google::protobuf::Message& msg, std::string& out) -{ - int packlen = msg.ByteSize(); - - out.resize(sizeof(PackHead) + packlen); - char* buff = (char*)out.data(); - PackHead* head = (PackHead*)buff; - head->packlen = packlen; - head->msgid = msgid; - head->magic_code = MAGIC_CODE; - msg.SerializeToArray(buff + sizeof(PackHead), packlen); -} - -int Net_SendMsg(a8::TcpClient* tcp_client, unsigned seqid, unsigned short msgid, ::google::protobuf::Message& msg) -{ - int packlen = msg.ByteSize(); - assert(packlen < 1024 * 60); - - char* buff = (char*)malloc(sizeof(PackHead) + packlen); - PackHead* head = (PackHead*)buff; - head->packlen = packlen; - head->msgid = msgid; - head->seqid = seqid; - head->magic_code = MAGIC_CODE; - head->rpc_error_code = 0; - msg.SerializeToArray(buff + sizeof(PackHead), packlen); - tcp_client->SendBuff(buff, sizeof(PackHead) + packlen); - free(buff); - return sizeof(PackHead) + packlen; -} - -int Net_SendMsg(a8::TcpListener* tcp_listener, unsigned short socket_handle, unsigned int seqid, - unsigned short msgid, ::google::protobuf::Message& msg) -{ - int packlen = msg.ByteSize(); - assert(packlen < 1024 * 60); - - char* buff = (char*)malloc(sizeof(PackHead) + packlen); - PackHead* head = (PackHead*)buff; - head->packlen = packlen; - head->msgid = msgid; - head->seqid = seqid; - head->magic_code = MAGIC_CODE; - head->rpc_error_code = 0; - msg.SerializeToArray(buff + sizeof(PackHead), packlen); - tcp_listener->SendClientMsg(socket_handle, buff, sizeof(PackHead) + packlen); - free(buff); - return sizeof(PackHead) + packlen; -} - -int Net_BroadcastMsg(a8::TcpListener* tcp_listener, unsigned int seqid, - unsigned short msgid, ::google::protobuf::Message& msg) -{ - int packlen = msg.ByteSize(); - assert(packlen < 1024 * 60); - - char* buff = (char*)malloc(sizeof(PackHead) + packlen); - PackHead* head = (PackHead*)buff; - head->packlen = packlen; - head->msgid = msgid; - head->seqid = seqid; - head->magic_code = MAGIC_CODE; - head->rpc_error_code = 0; - msg.SerializeToArray(buff + sizeof(PackHead), packlen); - tcp_listener->BroadcastMsg(buff, sizeof(PackHead) + packlen); - free(buff); - return sizeof(PackHead) + packlen; -} - -int Net_SendProxyCMsg(a8::TcpClient* tcp_client, unsigned short msgid, ::google::protobuf::Message& msg) -{ - int packlen = msg.ByteSize(); - assert(packlen < 1024 * 60); - - char* buff = (char*)malloc(sizeof(WSProxyPackHead_S) + packlen); - WSProxyPackHead_C* head = (WSProxyPackHead_C*)buff; - head->packlen = packlen; - head->msgid = msgid; - #if 1 - head->seqid = 0; - #else - head->seqid = seqid; - #endif - head->magic_code = MAGIC_CODE; - #if 0 - head->rpc_error_code = error_code; - #endif - #if 1 - head->socket_handle = 0; - #else - head->socket_handle = child_socket_handle; - #endif - - msg.SerializeToArray(buff + sizeof(WSProxyPackHead_C), packlen); - tcp_client->SendBuff(buff, sizeof(WSProxyPackHead_C) + packlen); - free(buff); - return sizeof(WSProxyPackHead_C) + packlen; -} - -int Net_SendProxyMsg(a8::TcpListener* tcp_listener, int socket_handle, unsigned int seqid, unsigned short error_code, - unsigned short msgid, ::google::protobuf::Message& msg) -{ - unsigned short parent_socket_handle = (socket_handle >> 16) & 0xFFFF; - unsigned short child_socket_handle = socket_handle & 0xFFFF; - - int packlen = msg.ByteSize(); - assert(packlen < 1024 * 60); - - char* buff = (char*)malloc(sizeof(WSProxyPackHead_S) + packlen); - WSProxyPackHead_S* head = (WSProxyPackHead_S*)buff; - head->packlen = packlen; - head->msgid = msgid; - head->seqid = seqid; - head->magic_code = MAGIC_CODE; - head->rpc_error_code = error_code; - head->socket_handle = child_socket_handle; - head->reserved = 0; - - msg.SerializeToArray(buff + sizeof(WSProxyPackHead_S), packlen); - tcp_listener->SendClientMsg(parent_socket_handle, buff, sizeof(WSProxyPackHead_S) + packlen); - free(buff); - return sizeof(WSProxyPackHead_S) + packlen; } diff --git a/cpp/protoutils.h b/cpp/protoutils.h index 0edd3a4..3781bdc 100644 --- a/cpp/protoutils.h +++ b/cpp/protoutils.h @@ -1,66 +1,6 @@ #pragma once -//普通消息头部 -struct PackHead -{ - unsigned short packlen; - unsigned short msgid; - unsigned int seqid; - unsigned short magic_code; - unsigned short rpc_error_code; -}; - -//转发类消息头部 -struct ForwardPackHead -{ - unsigned short packlen; - unsigned short sockethandle; - unsigned short msgid; - unsigned short src_msgid; - unsigned int src_seqid; - unsigned long ip_saddr; - unsigned int sign; -}; - -struct WSProxyPackHead_C -{ - unsigned short packlen; - unsigned short msgid; - unsigned int seqid; - unsigned short magic_code; - - unsigned short socket_handle; - unsigned long ip_saddr; -}; - -struct WSProxyPackHead_S -{ - unsigned short packlen; - unsigned short msgid; - unsigned int seqid; - unsigned short magic_code; - unsigned short rpc_error_code; - - unsigned short socket_handle; - unsigned short reserved; -}; - class Player; -struct MsgHdr -{ - unsigned int seqid; - unsigned short msgid; - int socket_handle; - unsigned long ip_saddr; - const char* buf; - int buflen; - int offset; - Player *hum = nullptr; - const void* user_data = nullptr; -}; - -const unsigned short MAGIC_CODE = (((unsigned short)'S') << 8) | ((unsigned short)'K'); - namespace google { namespace protobuf @@ -75,19 +15,82 @@ namespace a8 class TcpListener; } -int Net_GetMessageId(::google::protobuf::Message& msg); +namespace f8 +{ + //普通消息头部 + struct PackHead + { + unsigned short packlen; + unsigned short msgid; + unsigned int seqid; + unsigned short magic_code; + unsigned short rpc_error_code; + }; -void Net_PackMsg(unsigned short msgid, ::google::protobuf::Message& msg, std::string& out); -int Net_SendMsg(a8::TcpClient* tcp_client, unsigned int seqid, - unsigned short msgid, ::google::protobuf::Message& msg); -int Net_SendMsg(a8::TcpClient* tcp_client, unsigned int seqid, unsigned short msgid, - const char* msgbody, int msgbody_len); -int Net_SendMsg(a8::TcpListener* tcp_tlistener, unsigned short socket_handle, unsigned int seqid, - unsigned short msgid, ::google::protobuf::Message& msg); -int Net_BroadcastMsg(a8::TcpListener* tcp_tlistener, unsigned int seqid, - unsigned short msgid, ::google::protobuf::Message& msg); + //转发类消息头部 + struct ForwardPackHead + { + unsigned short packlen; + unsigned short sockethandle; + unsigned short msgid; + unsigned short src_msgid; + unsigned int src_seqid; + unsigned long ip_saddr; + unsigned int sign; + }; -int Net_SendProxyCMsg(a8::TcpClient* tcp_client, unsigned short msgid, ::google::protobuf::Message& msg); + struct WSProxyPackHead_C + { + unsigned short packlen; + unsigned short msgid; + unsigned int seqid; + unsigned short magic_code; -int Net_SendProxyMsg(a8::TcpListener* tcp_tlistener, int socket_handle, unsigned int seqid, unsigned short error_code, - unsigned short msgid, ::google::protobuf::Message& msg); + unsigned short socket_handle; + unsigned long ip_saddr; + }; + + struct WSProxyPackHead_S + { + unsigned short packlen; + unsigned short msgid; + unsigned int seqid; + unsigned short magic_code; + unsigned short rpc_error_code; + + unsigned short socket_handle; + unsigned short reserved; + }; + + struct MsgHdr + { + unsigned int seqid; + unsigned short msgid; + int socket_handle; + unsigned long ip_saddr; + const char* buf; + int buflen; + int offset; + Player *hum = nullptr; + const void* user_data = nullptr; + }; + + const unsigned short MAGIC_CODE = (((unsigned short)'S') << 8) | ((unsigned short)'K'); + + int Net_GetMessageId(::google::protobuf::Message& msg); + + void Net_PackMsg(unsigned short msgid, ::google::protobuf::Message& msg, std::string& out); + int Net_SendMsg(a8::TcpClient* tcp_client, unsigned int seqid, + unsigned short msgid, ::google::protobuf::Message& msg); + int Net_SendMsg(a8::TcpClient* tcp_client, unsigned int seqid, unsigned short msgid, + const char* msgbody, int msgbody_len); + int Net_SendMsg(a8::TcpListener* tcp_tlistener, unsigned short socket_handle, unsigned int seqid, + unsigned short msgid, ::google::protobuf::Message& msg); + int Net_BroadcastMsg(a8::TcpListener* tcp_tlistener, unsigned int seqid, + unsigned short msgid, ::google::protobuf::Message& msg); + + int Net_SendProxyCMsg(a8::TcpClient* tcp_client, unsigned short msgid, ::google::protobuf::Message& msg); + + int Net_SendProxyMsg(a8::TcpListener* tcp_tlistener, int socket_handle, unsigned int seqid, unsigned short error_code, + unsigned short msgid, ::google::protobuf::Message& msg); +} diff --git a/cpp/scriptengine.cc b/cpp/scriptengine.cc index a91e37e..2826191 100644 --- a/cpp/scriptengine.cc +++ b/cpp/scriptengine.cc @@ -4,85 +4,88 @@ #include "scriptengine.h" -void ScriptEngine::Init() +namespace f8 { - engine_ = new a8::PyEngine(); - if (getenv("is_dev_env")) { - engine_->work_path = "/var/data/conf_test/game1008/res/pyscripts/"; - } else { - engine_->work_path = "../config/res/pyscripts/"; + void ScriptEngine::Init() + { + engine_ = new a8::PyEngine(); + if (getenv("is_dev_env")) { + engine_->work_path = "/var/data/conf_test/game1008/res/pyscripts/"; + } else { + engine_->work_path = "../config/res/pyscripts/"; + } + engine_->Init(); + if (!engine_->IsInitialized()) { + abort(); + } } - engine_->Init(); - if (!engine_->IsInitialized()) { - abort(); + + void ScriptEngine::UnInit() + { + engine_->UnInit(); + delete engine_; + engine_ = nullptr; + } + + void ScriptEngine::Update() + { + } + + bool ScriptEngine::LoadModule(const std::string& module_name) + { + bool ret = engine_->LoadModule(module_name.c_str()); + if (!ret) { + a8::UdpLog::Instance()->Warning("load python module %s error %s\n", + { + module_name, + engine_->GetLastError() + }); + assert(false); + } + return ret; + } + + bool ScriptEngine::LoadString(const std::string& str) + { + bool ret = engine_->ExecString(str.c_str()); + if (!ret) { + a8::UdpLog::Instance()->Warning("load python string %s error %s", + { + str, + engine_->GetLastError() + }); + assert(false); + } + return ret; + } + + std::tuple ScriptEngine::CallGlobalFunc(const char* func_name, + std::initializer_list args) + { + auto ret = engine_->CallGlobalFunc(func_name, args); + if (!std::get<0>(ret)) { + a8::UdpLog::Instance()->Warning("call global function %s error %s", + { + func_name, + engine_->GetLastError() + }); + } + return ret; + } + + std::tuple ScriptEngine::CallModuleFunc(const char* module_name, + const char* func_name, + std::initializer_list args) + { + auto ret = engine_->CallModuleFunc(module_name, func_name, args); + if (!std::get<0>(ret)) { + a8::UdpLog::Instance()->Warning("call module function [%s] %s error %s", + { + module_name, + func_name, + engine_->GetLastError() + }); + } + return ret; } } - -void ScriptEngine::UnInit() -{ - engine_->UnInit(); - delete engine_; - engine_ = nullptr; -} - -void ScriptEngine::Update() -{ -} - -bool ScriptEngine::LoadModule(const std::string& module_name) -{ - bool ret = engine_->LoadModule(module_name.c_str()); - if (!ret) { - a8::UdpLog::Instance()->Warning("load python module %s error %s\n", - { - module_name, - engine_->GetLastError() - }); - assert(false); - } - return ret; -} - -bool ScriptEngine::LoadString(const std::string& str) -{ - bool ret = engine_->ExecString(str.c_str()); - if (!ret) { - a8::UdpLog::Instance()->Warning("load python string %s error %s", - { - str, - engine_->GetLastError() - }); - assert(false); - } - return ret; -} - -std::tuple ScriptEngine::CallGlobalFunc(const char* func_name, - std::initializer_list args) -{ - auto ret = engine_->CallGlobalFunc(func_name, args); - if (!std::get<0>(ret)) { - a8::UdpLog::Instance()->Warning("call global function %s error %s", - { - func_name, - engine_->GetLastError() - }); - } - return ret; -} - -std::tuple ScriptEngine::CallModuleFunc(const char* module_name, - const char* func_name, - std::initializer_list args) -{ - auto ret = engine_->CallModuleFunc(module_name, func_name, args); - if (!std::get<0>(ret)) { - a8::UdpLog::Instance()->Warning("call module function [%s] %s error %s", - { - module_name, - func_name, - engine_->GetLastError() - }); - } - return ret; -} diff --git a/cpp/scriptengine.h b/cpp/scriptengine.h index 3de6402..63e5be4 100644 --- a/cpp/scriptengine.h +++ b/cpp/scriptengine.h @@ -5,26 +5,30 @@ namespace a8 class PyEngine; }; -class ScriptEngine : public a8::Singleton +namespace f8 { - private: - ScriptEngine() {}; - friend class a8::Singleton; + class ScriptEngine : public a8::Singleton + { + private: + ScriptEngine() {}; + friend class a8::Singleton; - public: - void Init(); - void UnInit(); - void Update(); + public: + void Init(); + void UnInit(); + void Update(); - bool LoadModule(const std::string& module_name); - bool LoadString(const std::string& str); - std::tuple CallGlobalFunc(const char* func_name, - std::initializer_list args); - std::tuple CallModuleFunc(const char* module_name, - const char* func_name, - std::initializer_list args); + bool LoadModule(const std::string& module_name); + bool LoadString(const std::string& str); + std::tuple CallGlobalFunc(const char* func_name, + std::initializer_list args); + std::tuple CallModuleFunc(const char* module_name, + const char* func_name, + std::initializer_list args); - public: - a8::PyEngine *engine_ = nullptr; + public: + a8::PyEngine *engine_ = nullptr; -}; + }; + +} diff --git a/cpp/tglog.cc b/cpp/tglog.cc index 381739e..9481234 100644 --- a/cpp/tglog.cc +++ b/cpp/tglog.cc @@ -7,189 +7,192 @@ #include #include "tglog.h" -static const char* const TGLOG_ROOT = "/data/logs/%s/upload/"; -static const char* const POLY_TGLOG_ROOT = "/data/logs/%s/%s/upload/"; -static const char* const TGLOG_FILENAME = "log_$pid_%Y%m%d%H.log"; - -struct TGLogMsgNode +namespace f8 { - int game_id = 0; - std::string jsonstr; - TGLogMsgNode* next = nullptr; -}; + static const char* const TGLOG_ROOT = "/data/logs/%s/upload/"; + static const char* const POLY_TGLOG_ROOT = "/data/logs/%s/%s/upload/"; + static const char* const TGLOG_FILENAME = "log_$pid_%Y%m%d%H.log"; -struct TGLogImpl -{ - std::string filename_fmt; - std::string project_name; - bool is_poly_log = false; - - std::thread* save_thread = nullptr; - bool save_thread_shutdown = false; - - std::mutex msg_mutex; - TGLogMsgNode* top_node = nullptr; - TGLogMsgNode* bot_node = nullptr; - TGLogMsgNode* work_node = nullptr; - std::mutex *save_cond_mutex = nullptr; - std::condition_variable *save_cond = nullptr; - - TGLogImpl() + struct TGLogMsgNode { - save_cond_mutex = new std::mutex(); - save_cond = new std::condition_variable(); - } + int game_id = 0; + std::string jsonstr; + TGLogMsgNode* next = nullptr; + }; - virtual ~TGLogImpl() + struct TGLogImpl { - delete save_cond_mutex; - save_cond_mutex = nullptr; - delete save_cond; - save_cond = nullptr; - } + std::string filename_fmt; + std::string project_name; + bool is_poly_log = false; - std::string GetCurrentFileName() - { - time_t nowtime = time(nullptr); - struct tm tm_nowtime = {0}; - localtime_r(&nowtime, &tm_nowtime); + std::thread* save_thread = nullptr; + bool save_thread_shutdown = false; - char szfilename[256]; - szfilename[0] = '\0'; - strftime(szfilename, a8::ArraySize(szfilename), filename_fmt.c_str(), &tm_nowtime); - std::string filename((char*)szfilename); - return filename; - } -}; + std::mutex msg_mutex; + TGLogMsgNode* top_node = nullptr; + TGLogMsgNode* bot_node = nullptr; + TGLogMsgNode* work_node = nullptr; + std::mutex *save_cond_mutex = nullptr; + std::condition_variable *save_cond = nullptr; -void TGLog::Init() -{ - impl_ = new TGLogImpl(); - impl_->filename_fmt = TGLOG_FILENAME; - a8::ReplaceString(impl_->filename_fmt, "$pid", a8::XValue(getpid())); - - impl_->project_name = project_name_; - impl_->is_poly_log = is_poly_log_; - - impl_->save_thread_shutdown = false; - impl_->save_thread = new std::thread(&TGLog::SaveToFileThreadProc, this); -} - -void TGLog::UnInit() -{ - delete impl_; - impl_ = nullptr; -} - -void TGLog::SetProjectInfo(const std::string& project_name, bool is_poly_log) -{ - project_name_ = project_name; - is_poly_log_ = is_poly_log; -} - -void TGLog::AddTrackLog(int game_id, const std::string& accountid, unsigned long ip_saddr, - int logclass1, int logclass2, a8::XObject* prop) -{ - std::string logtime_str; - { - time_t nowtime = time(nullptr); - struct tm tm_time = {0}; - localtime_r(&nowtime, &tm_time); - - char buff[256]; - strftime(buff, a8::ArraySize(buff), "%F %T", &tm_time); - logtime_str.append((char*)buff); - } - a8::MutableXObject* xobj = a8::MutableXObject::NewObject(); - - xobj->SetVal("#account_id", accountid); - xobj->SetVal("#type", "track"); - xobj->SetVal("#time", logtime_str); - xobj->SetVal("#ip", a8::GetIpAddress(ip_saddr)); - xobj->SetVal("#event_name", a8::Format("event_%d_%d", {logclass1, logclass2})); - - xobj->SetVal("properties", *prop); - { - TGLogMsgNode *p = new TGLogMsgNode(); - p->game_id = game_id; - xobj->ToJsonStr(p->jsonstr); - impl_->msg_mutex.lock(); - if (impl_->bot_node) { - impl_->bot_node->next = p; - impl_->bot_node = p; - } else { - impl_->top_node = p; - impl_->bot_node = p; - } - impl_->msg_mutex.unlock(); + TGLogImpl() { - std::unique_lock lk(*impl_->save_cond_mutex); - impl_->save_cond->notify_all(); + save_cond_mutex = new std::mutex(); + save_cond = new std::condition_variable(); } + + virtual ~TGLogImpl() + { + delete save_cond_mutex; + save_cond_mutex = nullptr; + delete save_cond; + save_cond = nullptr; + } + + std::string GetCurrentFileName() + { + time_t nowtime = time(nullptr); + struct tm tm_nowtime = {0}; + localtime_r(&nowtime, &tm_nowtime); + + char szfilename[256]; + szfilename[0] = '\0'; + strftime(szfilename, a8::ArraySize(szfilename), filename_fmt.c_str(), &tm_nowtime); + std::string filename((char*)szfilename); + return filename; + } + }; + + void TGLog::Init() + { + impl_ = new TGLogImpl(); + impl_->filename_fmt = TGLOG_FILENAME; + a8::ReplaceString(impl_->filename_fmt, "$pid", a8::XValue(getpid())); + + impl_->project_name = project_name_; + impl_->is_poly_log = is_poly_log_; + + impl_->save_thread_shutdown = false; + impl_->save_thread = new std::thread(&TGLog::SaveToFileThreadProc, this); } -} -void TGLog::SaveToFileThreadProc() -{ - auto GetLogFile = [this] (std::map& opened_log_files_hash, int game_id, const std::string& filename) -> FILE* - { - auto itr = opened_log_files_hash.find(game_id); - if (itr == opened_log_files_hash.end()) { - std::string log_dir; - if (impl_->is_poly_log) { - log_dir = a8::Format(POLY_TGLOG_ROOT, {impl_->project_name, impl_->work_node->game_id}); - } else { - log_dir = a8::Format(TGLOG_ROOT, {impl_->project_name}); - } - a8::ForceCreateDir(log_dir); - FILE* logfile = fopen((log_dir + filename).c_str(), "a+"); - if (logfile) { - opened_log_files_hash[game_id] = logfile; - } - return logfile; - } else { - return itr->second; - } - }; + void TGLog::UnInit() + { + delete impl_; + impl_ = nullptr; + } - while (!impl_->save_thread_shutdown) { - if (!impl_->work_node && impl_->top_node) { + void TGLog::SetProjectInfo(const std::string& project_name, bool is_poly_log) + { + project_name_ = project_name; + is_poly_log_ = is_poly_log; + } + + void TGLog::AddTrackLog(int game_id, const std::string& accountid, unsigned long ip_saddr, + int logclass1, int logclass2, a8::XObject* prop) + { + std::string logtime_str; + { + time_t nowtime = time(nullptr); + struct tm tm_time = {0}; + localtime_r(&nowtime, &tm_time); + + char buff[256]; + strftime(buff, a8::ArraySize(buff), "%F %T", &tm_time); + logtime_str.append((char*)buff); + } + a8::MutableXObject* xobj = a8::MutableXObject::NewObject(); + + xobj->SetVal("#account_id", accountid); + xobj->SetVal("#type", "track"); + xobj->SetVal("#time", logtime_str); + xobj->SetVal("#ip", a8::GetIpAddress(ip_saddr)); + xobj->SetVal("#event_name", a8::Format("event_%d_%d", {logclass1, logclass2})); + + xobj->SetVal("properties", *prop); + { + TGLogMsgNode *p = new TGLogMsgNode(); + p->game_id = game_id; + xobj->ToJsonStr(p->jsonstr); impl_->msg_mutex.lock(); - impl_->work_node = impl_->top_node; - impl_->top_node = nullptr; - impl_->bot_node = nullptr; + if (impl_->bot_node) { + impl_->bot_node->next = p; + impl_->bot_node = p; + } else { + impl_->top_node = p; + impl_->bot_node = p; + } impl_->msg_mutex.unlock(); - } - - if (impl_->work_node) { - std::string filename = impl_->GetCurrentFileName(); - std::map opened_log_files_hash; - - while (impl_->work_node) { - TGLogMsgNode* nextnode = impl_->work_node->next; - TGLogMsgNode* work_node = impl_->work_node; - - FILE* logfile = GetLogFile(opened_log_files_hash, work_node->game_id, filename); - if (logfile) { - work_node->jsonstr.push_back('\n'); - fwrite(work_node->jsonstr.c_str(), 1, work_node->jsonstr.size(), logfile); - } - delete work_node; - - impl_->work_node = nextnode; + { + std::unique_lock lk(*impl_->save_cond_mutex); + impl_->save_cond->notify_all(); } - - for (auto& pair : opened_log_files_hash) { - if (pair.second) { - fclose(pair.second); - } - } - } - - { - std::unique_lock lk(*impl_->save_cond_mutex); - impl_->save_cond->wait_for(lk, std::chrono::seconds(10)); } } + void TGLog::SaveToFileThreadProc() + { + auto GetLogFile = [this] (std::map& opened_log_files_hash, int game_id, const std::string& filename) -> FILE* + { + auto itr = opened_log_files_hash.find(game_id); + if (itr == opened_log_files_hash.end()) { + std::string log_dir; + if (impl_->is_poly_log) { + log_dir = a8::Format(POLY_TGLOG_ROOT, {impl_->project_name, impl_->work_node->game_id}); + } else { + log_dir = a8::Format(TGLOG_ROOT, {impl_->project_name}); + } + a8::ForceCreateDir(log_dir); + FILE* logfile = fopen((log_dir + filename).c_str(), "a+"); + if (logfile) { + opened_log_files_hash[game_id] = logfile; + } + return logfile; + } else { + return itr->second; + } + }; + + while (!impl_->save_thread_shutdown) { + if (!impl_->work_node && impl_->top_node) { + impl_->msg_mutex.lock(); + impl_->work_node = impl_->top_node; + impl_->top_node = nullptr; + impl_->bot_node = nullptr; + impl_->msg_mutex.unlock(); + } + + if (impl_->work_node) { + std::string filename = impl_->GetCurrentFileName(); + std::map opened_log_files_hash; + + while (impl_->work_node) { + TGLogMsgNode* nextnode = impl_->work_node->next; + TGLogMsgNode* work_node = impl_->work_node; + + FILE* logfile = GetLogFile(opened_log_files_hash, work_node->game_id, filename); + if (logfile) { + work_node->jsonstr.push_back('\n'); + fwrite(work_node->jsonstr.c_str(), 1, work_node->jsonstr.size(), logfile); + } + delete work_node; + + impl_->work_node = nextnode; + } + + for (auto& pair : opened_log_files_hash) { + if (pair.second) { + fclose(pair.second); + } + } + } + + { + std::unique_lock lk(*impl_->save_cond_mutex); + impl_->save_cond->wait_for(lk, std::chrono::seconds(10)); + } + } + + } } diff --git a/cpp/tglog.h b/cpp/tglog.h index e0e3d06..663560a 100644 --- a/cpp/tglog.h +++ b/cpp/tglog.h @@ -1,25 +1,28 @@ #pragma once -struct TGLogImpl; -class TGLog : public a8::Singleton +namespace f8 { - private: - TGLog() {}; - friend class a8::Singleton; + struct TGLogImpl; + class TGLog : public a8::Singleton + { + private: + TGLog() {}; + friend class a8::Singleton; -public: - void Init(); - void UnInit(); + public: + void Init(); + void UnInit(); - void SetProjectInfo(const std::string& project_name, bool is_poly_log); - void AddTrackLog(int game_id, const std::string& accountid, unsigned long ip_saddr, - int logclass1, int logclass2, a8::XObject* prop); + void SetProjectInfo(const std::string& project_name, bool is_poly_log); + void AddTrackLog(int game_id, const std::string& accountid, unsigned long ip_saddr, + int logclass1, int logclass2, a8::XObject* prop); - private: - void SaveToFileThreadProc(); + private: + void SaveToFileThreadProc(); - private: - std::string project_name_; - bool is_poly_log_ = false; - TGLogImpl* impl_ = nullptr; -}; + private: + std::string project_name_; + bool is_poly_log_ = false; + TGLogImpl* impl_ = nullptr; + }; +} diff --git a/cpp/tiledmap.cc b/cpp/tiledmap.cc index 52809d3..2f27c71 100644 --- a/cpp/tiledmap.cc +++ b/cpp/tiledmap.cc @@ -3,355 +3,357 @@ #include #include "framework/cpp/tiledmap.h" -a8::XValue TiledObject::GetProperty(const std::string& prop_name) +namespace f8 { - auto itr = prop_hash.find(prop_name); - return itr != prop_hash.end() ? itr->second : a8::XValue(); -} - -bool TiledObject::HasProperty(const std::string& prop_name) -{ - auto itr = prop_hash.find(prop_name); - return itr != prop_hash.end(); -} - - -a8::XValue TiledLayer::GetProperty(const std::string& prop_name) -{ - auto itr = prop_hash.find(prop_name); - return itr != prop_hash.end() ? itr->second : a8::XValue(); -} - -bool TiledLayer::HasProperty(const std::string& prop_name) -{ - auto itr = prop_hash.find(prop_name); - return itr != prop_hash.end(); -} - -bool TiledMap::LoadTmxFile(const std::string& filename) -{ - a8::XObject xobj; - if (!xobj.ReadFromXmlFile(filename)) { - return false; + a8::XValue TiledObject::GetProperty(const std::string& prop_name) + { + auto itr = prop_hash.find(prop_name); + return itr != prop_hash.end() ? itr->second : a8::XValue(); } - std::shared_ptr tileset_node = xobj.At("child_node.tileset")->At(0); - tile_width = tileset_node->At("attrs.tilewidth")->AsXValue(); - tile_height = tileset_node->At("attrs.tileheight")->AsXValue(); - #if 0 - tile_count = tileset_node->At("attrs.tilecount")->AsXValue(); - tile_columns = tileset_node->At("attrs.columns")->AsXValue(); - tile_rows = tile_count/tile_columns + 1; - tile_columns += 1; - tile_count = tile_rows * tile_columns; - #endif - for (int i = 0; i < xobj.At("child_node.layer")->Size(); ++i) { - std::shared_ptr layer_node = xobj.At("child_node.layer")->At(i); - - TiledLayer layer; - { - for (int ii = 0; ii < layer_node->At("attr_names")->Size(); ++ii) { - std::string attr_name = layer_node->At("attr_names")->At(ii)->AsXValue(); - layer.prop_hash[attr_name] = layer_node->At("attrs." + attr_name)->AsXValue(); - } - std::shared_ptr prop_nodes = layer_node->At("child_node.properties")->At(0)->At("child_node.property"); - for (int j = 0; j < prop_nodes->Size(); ++j) { - std::shared_ptr prop_node = prop_nodes->At(j); - layer.prop_hash[prop_node->At("attrs.name")->AsXValue()] = prop_node->At("attrs.value")->AsXValue(); - } - - std::shared_ptr data_node = layer_node->At("child_node.data")->At(0); - layer.data = data_node->At("node_value")->AsXValue(); - } - - std::string layer_name = layer_node->At("attrs.name")->AsXValue(); - if (layer_name.compare("ground") == 0) { - tile_columns = layer_node->At("attrs.width")->AsXValue(); - tile_rows = layer_node->At("attrs.height")->AsXValue(); - tile_count = tile_rows * tile_columns; - } - auto itr = layer_hash.find(layer_name); - if (itr != layer_hash.end()) { - itr->second.push_back(layer); - } else { - layer_hash[layer_name] = std::list({layer}); - } + bool TiledObject::HasProperty(const std::string& prop_name) + { + auto itr = prop_hash.find(prop_name); + return itr != prop_hash.end(); } + + a8::XValue TiledLayer::GetProperty(const std::string& prop_name) + { + auto itr = prop_hash.find(prop_name); + return itr != prop_hash.end() ? itr->second : a8::XValue(); + } + + bool TiledLayer::HasProperty(const std::string& prop_name) + { + auto itr = prop_hash.find(prop_name); + return itr != prop_hash.end(); + } + + bool TiledMap::LoadTmxFile(const std::string& filename) + { + a8::XObject xobj; + if (!xobj.ReadFromXmlFile(filename)) { + return false; + } + + std::shared_ptr tileset_node = xobj.At("child_node.tileset")->At(0); + tile_width = tileset_node->At("attrs.tilewidth")->AsXValue(); + tile_height = tileset_node->At("attrs.tileheight")->AsXValue(); +#if 0 + tile_count = tileset_node->At("attrs.tilecount")->AsXValue(); + tile_columns = tileset_node->At("attrs.columns")->AsXValue(); + tile_rows = tile_count/tile_columns + 1; + tile_columns += 1; + tile_count = tile_rows * tile_columns; +#endif + for (int i = 0; i < xobj.At("child_node.layer")->Size(); ++i) { + std::shared_ptr layer_node = xobj.At("child_node.layer")->At(i); + + TiledLayer layer; + { + for (int ii = 0; ii < layer_node->At("attr_names")->Size(); ++ii) { + std::string attr_name = layer_node->At("attr_names")->At(ii)->AsXValue(); + layer.prop_hash[attr_name] = layer_node->At("attrs." + attr_name)->AsXValue(); + } + std::shared_ptr prop_nodes = layer_node->At("child_node.properties")->At(0)->At("child_node.property"); + for (int j = 0; j < prop_nodes->Size(); ++j) { + std::shared_ptr prop_node = prop_nodes->At(j); + layer.prop_hash[prop_node->At("attrs.name")->AsXValue()] = prop_node->At("attrs.value")->AsXValue(); + } + + std::shared_ptr data_node = layer_node->At("child_node.data")->At(0); + layer.data = data_node->At("node_value")->AsXValue(); + } + + std::string layer_name = layer_node->At("attrs.name")->AsXValue(); + if (layer_name.compare("ground") == 0) { + tile_columns = layer_node->At("attrs.width")->AsXValue(); + tile_rows = layer_node->At("attrs.height")->AsXValue(); + tile_count = tile_rows * tile_columns; + } + auto itr = layer_hash.find(layer_name); + if (itr != layer_hash.end()) { + itr->second.push_back(layer); + } else { + layer_hash[layer_name] = std::list({layer}); + } + } + std::shared_ptr objgroup_node = xobj.At("child_node.objectgroup")->At(0); for (int i = 0; i < objgroup_node->At("child_node.object")->Size(); i++) { - std::shared_ptr object_node = objgroup_node->At("child_node.object")->At(i); - TiledObject object; - { - for (int ii = 0; ii < object_node->At("attr_names")->Size(); ii++) { - std::string attr_name = object_node->At("attr_names")->At(ii)->AsXValue(); - object.prop_hash[attr_name] = object_node->At("attrs." + attr_name)->AsXValue(); - } - std::shared_ptr prop_nodes = object_node->At("child_node.properties")->At(0)->At("child_node.property"); - for (int j = 0; j < prop_nodes->Size(); j++) { - std::shared_ptr prop_node = prop_nodes->At(j); - object.prop_hash[prop_node->At("attrs.name")->AsXValue()] = prop_node->At("attrs.value")->AsXValue(); - } - } + std::shared_ptr object_node = objgroup_node->At("child_node.object")->At(i); + TiledObject object; + { + for (int ii = 0; ii < object_node->At("attr_names")->Size(); ii++) { + std::string attr_name = object_node->At("attr_names")->At(ii)->AsXValue(); + object.prop_hash[attr_name] = object_node->At("attrs." + attr_name)->AsXValue(); + } + std::shared_ptr prop_nodes = object_node->At("child_node.properties")->At(0)->At("child_node.property"); + for (int j = 0; j < prop_nodes->Size(); j++) { + std::shared_ptr prop_node = prop_nodes->At(j); + object.prop_hash[prop_node->At("attrs.name")->AsXValue()] = prop_node->At("attrs.value")->AsXValue(); + } + } - std::string object_name = object_node->At("attrs.name")->AsXValue(); - auto itr = object_group_hash.find(object_name); - if (itr != object_group_hash.end()) { - itr->second.push_back(object); - } - else + std::string object_name = object_node->At("attrs.name")->AsXValue(); + auto itr = object_group_hash.find(object_name); + if (itr != object_group_hash.end()) { + itr->second.push_back(object); + } + else { - object_group_hash[object_name] = std::list({ object }); + object_group_hash[object_name] = std::list({ object }); } } - return true; -} + return true; + } -std::list* TiledMap::GetObjectGroup(const std::string& object_class_name) -{ - auto itr = object_group_hash.find(object_class_name); - return itr != object_group_hash.end() ? &itr->second : nullptr; -} + std::list* TiledMap::GetObjectGroup(const std::string& object_class_name) + { + auto itr = object_group_hash.find(object_class_name); + return itr != object_group_hash.end() ? &itr->second : nullptr; + } -StagePoint* TiledMap::GetStageObject(int point) -{ - auto itr = stage_object_hash.find(point); - return itr != stage_object_hash.end() ? &itr->second : nullptr; -} + StagePoint* TiledMap::GetStageObject(int point) + { + auto itr = stage_object_hash.find(point); + return itr != stage_object_hash.end() ? &itr->second : nullptr; + } -bool TiledMap::HasStageObject(int point) -{ - auto itr = stage_object_hash.find(point); - return itr != stage_object_hash.end(); -} + bool TiledMap::HasStageObject(int point) + { + auto itr = stage_object_hash.find(point); + return itr != stage_object_hash.end(); + } -std::vector* TiledMap::GetStagePath(const std::string& path_name) -{ - auto itr = stage_path_hash.find(path_name); - return itr != stage_path_hash.end() ? &itr->second : nullptr; -} + std::vector* TiledMap::GetStagePath(const std::string& path_name) + { + auto itr = stage_path_hash.find(path_name); + return itr != stage_path_hash.end() ? &itr->second : nullptr; + } -bool TiledMap::HasStagePath(const std::string& path_name) -{ - auto itr = stage_path_hash.find(path_name); - return itr != stage_path_hash.end(); -} + bool TiledMap::HasStagePath(const std::string& path_name) + { + auto itr = stage_path_hash.find(path_name); + return itr != stage_path_hash.end(); + } -void TiledMap::Init() -{ - grid_cell_list.reserve(tile_count); - grid_cell_list.assign(tile_count,GridCell{}); + void TiledMap::Init() + { + grid_cell_list.reserve(tile_count); + grid_cell_list.assign(tile_count,GridCell{}); - for (auto& pair : layer_hash) { - for (auto& layer : pair.second) { + for (auto& pair : layer_hash) { + for (auto& layer : pair.second) { - std::string name = layer.GetProperty("name").GetString(); - bool has_speed = layer.HasProperty("area_id"); - int speed = layer.GetProperty("area_id"); + std::string name = layer.GetProperty("name").GetString(); + bool has_speed = layer.HasProperty("area_id"); + int speed = layer.GetProperty("area_id"); - bool has_trigger = layer.HasProperty("isTrigger"); - bool istrigger = layer.GetProperty("isTrigger");; - int trigger_point = layer.GetProperty("objName"); + bool has_trigger = layer.HasProperty("isTrigger"); + bool istrigger = layer.GetProperty("isTrigger");; + int trigger_point = layer.GetProperty("objName"); - bool has_num = layer.HasProperty("num"); - //int num = layer.GetProperty("num"); + bool has_num = layer.HasProperty("num"); + //int num = layer.GetProperty("num"); - std::vector grid_cell_path_list; - a8::StringList str_list; - str_list.SetText(layer.data.GetString().c_str()); - for (int i = 1; i < str_list.Count(); ++i) { - std::string str_line = str_list.String(i); - std::vector split_list; - a8::Split(str_line, split_list); - int split_count = split_list.size(); + std::vector grid_cell_path_list; + a8::StringList str_list; + str_list.SetText(layer.data.GetString().c_str()); + for (int i = 1; i < str_list.Count(); ++i) { + std::string str_line = str_list.String(i); + std::vector split_list; + a8::Split(str_line, split_list); + int split_count = split_list.size(); - for(int j = 0; j < split_count; ++j){ - int value = a8::XValue(split_list[j]); - if(value > 0){ - int index = (i-1)*tile_columns + j; - if (has_speed) { - grid_cell_list[index].x = j; - grid_cell_list[index].y = i-1; + for(int j = 0; j < split_count; ++j){ + int value = a8::XValue(split_list[j]); + if(value > 0){ + int index = (i-1)*tile_columns + j; + if (has_speed) { + grid_cell_list[index].x = j; + grid_cell_list[index].y = i-1; - grid_cell_list[index].speed = speed; - grid_cell_list[index].value = value; - } - if (has_trigger) { - TriggerPoint tp; - { - tp.x = j; - tp.y = i-1; - tp.point = trigger_point; - tp.istrigger = istrigger; - trigger_list.push_back(tp); + grid_cell_list[index].speed = speed; + grid_cell_list[index].value = value; + } + if (has_trigger) { + TriggerPoint tp; + { + tp.x = j; + tp.y = i-1; + tp.point = trigger_point; + tp.istrigger = istrigger; + trigger_list.push_back(tp); + } + } + if (has_num) { + grid_cell_path_list.push_back(&grid_cell_list[index]); } - } - if (has_num) { - grid_cell_path_list.push_back(&grid_cell_list[index]); } } } - } - if (grid_cell_path_list.size() > 0) { - stage_path_hash[name] = grid_cell_path_list; - } - } - } - - for (auto& pair : object_group_hash) { - for (auto& object : pair.second) { - StagePoint sp; - sp.x = object.GetProperty("tile_x"); - sp.y = object.GetProperty("tile_y"); - sp.point = object.GetProperty("name"); - std::string str_relation = object.GetProperty("relation").GetString(); - std::vector split_list; - a8::Split(str_relation, split_list, ','); - for (auto& point : split_list) { - sp.relation_list.push_back(a8::XValue(point)); - } - int point = object.GetProperty("name"); - stage_object_hash[point] = sp; - } - } -} - -void TiledMap::Dump() -{ - Init(); - #if 0 - for (auto& pair : stage_path_hash) { - a8::XPrintf("stage$:%s\n", {pair.first}); - for (auto& gc : pair.second) { - a8::XPrintf("path$x:%d,y:%d,speed:%d,value:%d\n", {gc->x, gc->y, gc->speed, gc->value}); - } - } - #endif - #if 0 - for (auto& tp : trigger_list) { - a8::XPrintf("x:%d,y:%d,point:%d,istrigger:%d\n", {tp.x, tp.y, tp.point, a8::XValue(tp.istrigger)}); - } - #endif - #if 0 - int len = grid_cell_list.size(); - a8::XPrintf("grid_cell_size:%s\n", {len}); - for (int i= 0; i < len; ++i) { - auto gc = grid_cell_list[i]; - a8::XPrintf("i:%d, x:%d,y:%d,speed:%d,value:%d\n", {i, gc.x, gc.y, gc.speed, gc.value}); - } - #endif - #if 0 - for (auto& pair : stage_object_hash) { - a8::XPrintf("object$:%s\n", {pair.first}); - a8::XPrintf("value$x:%d,y:%d,p:%s,relation:", {pair.second.x, pair.second.y, pair.second.point}); - for (auto& relation : pair.second.relation_list) { - a8::XPrintf("%s,", {relation}); - } - a8::XPrintf("\n",{}); - } - #endif - return; - - a8::XPrintf("map$tile_count:%d\n", {tile_count}); - a8::XPrintf("map$tile_width:%d\n", { tile_width }); - a8::XPrintf("map$tile_height:%d\n", { tile_height }); - a8::XPrintf("layer$layer_hash.size:%d\n", {layer_hash.size()}); - for (auto& pair : layer_hash) { - a8::XPrintf(" layer$layer_type:%s\n", {pair.first}); - for (auto& layer : pair.second) { - a8::XPrintf("data$data:%s\n",{layer.data}); - for (auto& pair2 : layer.prop_hash) { - a8::XPrintf(" layer$%s:%s\n", {pair2.first, pair2.second}); - } - } - } - a8::XPrintf("object$object_group_hash.size:%d\n", {object_group_hash.size()}); - for (auto& pair : object_group_hash) { - a8::XPrintf(" object$layer_type:%s\n", {pair.first}); - for (auto& layer : pair.second) { - for (auto& pair2 : layer.prop_hash) { - a8::XPrintf(" object$%s:%s\n", {pair2.first, pair2.second}); - } - } - } -} - -bool TiledMap::CalcCurrPos(std::vector& path_points, int old_pos_x, int old_pos_y, int time_ms, int& curr_pos_x, int& curr_pos_y) -{ - if (path_points.size() < 2) { - return false; - } - - StagePoint* sp_terminal = GetStageObject(path_points[path_points.size()-1]); - if (sp_terminal) { - if (old_pos_x == sp_terminal->x && old_pos_y == sp_terminal->y) { - curr_pos_x = old_pos_x; - curr_pos_y = old_pos_y; - return true; - } - //报错放终点 - curr_pos_x = sp_terminal->x; - curr_pos_y = sp_terminal->y; - } - - std::vector stage_path_list; - { - std::string path_point; - bool path_start = true; - for (auto& point : path_points) { - if (path_start) { - path_point = a8::XValue(point).GetString(); - path_start = !path_start; - } else { - std::string point_str = a8::XValue(point).GetString(); - path_point += "-" + point_str; - stage_path_list.push_back(path_point); - path_point = point_str; - } - } - } - std::vector grid_all_list; - { - bool exc_once = true; - for (auto& stage_path : stage_path_list) { - std::vector* grid_list = nullptr; - - std::string stage_path_reverse; - std::vector split_list; - a8::Split(stage_path,split_list,'-'); - if (split_list.size() != 2) { - return false; - } - StagePoint* sp = GetStageObject(a8::XValue(split_list.at(0))); - if (sp == nullptr) { - return false; - } - grid_list = GetStagePath(stage_path); - if (grid_list == nullptr) { - stage_path_reverse = split_list[1] + "-" + split_list[0]; - grid_list = GetStagePath(stage_path_reverse); - if (grid_list == nullptr) { - return false; + if (grid_cell_path_list.size() > 0) { + stage_path_hash[name] = grid_cell_path_list; } } + } - std::vector grid_sort_list = SortGridList(grid_list,sp); - if (grid_sort_list.size() < 2) { - return false; - } - if (exc_once) { - grid_all_list.insert(grid_all_list.end(),grid_sort_list.begin(),grid_sort_list.end()); - exc_once = false; - } else { - grid_all_list.insert(grid_all_list.end(),grid_sort_list.begin()+1,grid_sort_list.end()); + for (auto& pair : object_group_hash) { + for (auto& object : pair.second) { + StagePoint sp; + sp.x = object.GetProperty("tile_x"); + sp.y = object.GetProperty("tile_y"); + sp.point = object.GetProperty("name"); + std::string str_relation = object.GetProperty("relation").GetString(); + std::vector split_list; + a8::Split(str_relation, split_list, ','); + for (auto& point : split_list) { + sp.relation_list.push_back(a8::XValue(point)); + } + int point = object.GetProperty("name"); + stage_object_hash[point] = sp; } } } + void TiledMap::Dump() + { + Init(); +#if 0 + for (auto& pair : stage_path_hash) { + a8::XPrintf("stage$:%s\n", {pair.first}); + for (auto& gc : pair.second) { + a8::XPrintf("path$x:%d,y:%d,speed:%d,value:%d\n", {gc->x, gc->y, gc->speed, gc->value}); + } + } +#endif +#if 0 + for (auto& tp : trigger_list) { + a8::XPrintf("x:%d,y:%d,point:%d,istrigger:%d\n", {tp.x, tp.y, tp.point, a8::XValue(tp.istrigger)}); + } +#endif +#if 0 + int len = grid_cell_list.size(); + a8::XPrintf("grid_cell_size:%s\n", {len}); + for (int i= 0; i < len; ++i) { + auto gc = grid_cell_list[i]; + a8::XPrintf("i:%d, x:%d,y:%d,speed:%d,value:%d\n", {i, gc.x, gc.y, gc.speed, gc.value}); + } +#endif +#if 0 + for (auto& pair : stage_object_hash) { + a8::XPrintf("object$:%s\n", {pair.first}); + a8::XPrintf("value$x:%d,y:%d,p:%s,relation:", {pair.second.x, pair.second.y, pair.second.point}); + for (auto& relation : pair.second.relation_list) { + a8::XPrintf("%s,", {relation}); + } + a8::XPrintf("\n",{}); + } +#endif + return; + + a8::XPrintf("map$tile_count:%d\n", {tile_count}); + a8::XPrintf("map$tile_width:%d\n", { tile_width }); + a8::XPrintf("map$tile_height:%d\n", { tile_height }); + a8::XPrintf("layer$layer_hash.size:%d\n", {layer_hash.size()}); + for (auto& pair : layer_hash) { + a8::XPrintf(" layer$layer_type:%s\n", {pair.first}); + for (auto& layer : pair.second) { + a8::XPrintf("data$data:%s\n",{layer.data}); + for (auto& pair2 : layer.prop_hash) { + a8::XPrintf(" layer$%s:%s\n", {pair2.first, pair2.second}); + } + } + } + a8::XPrintf("object$object_group_hash.size:%d\n", {object_group_hash.size()}); + for (auto& pair : object_group_hash) { + a8::XPrintf(" object$layer_type:%s\n", {pair.first}); + for (auto& layer : pair.second) { + for (auto& pair2 : layer.prop_hash) { + a8::XPrintf(" object$%s:%s\n", {pair2.first, pair2.second}); + } + } + } + } + + bool TiledMap::CalcCurrPos(std::vector& path_points, int old_pos_x, int old_pos_y, int time_ms, int& curr_pos_x, int& curr_pos_y) + { + if (path_points.size() < 2) { + return false; + } + + StagePoint* sp_terminal = GetStageObject(path_points[path_points.size()-1]); + if (sp_terminal) { + if (old_pos_x == sp_terminal->x && old_pos_y == sp_terminal->y) { + curr_pos_x = old_pos_x; + curr_pos_y = old_pos_y; + return true; + } + //报错放终点 + curr_pos_x = sp_terminal->x; + curr_pos_y = sp_terminal->y; + } + + std::vector stage_path_list; + { + std::string path_point; + bool path_start = true; + for (auto& point : path_points) { + if (path_start) { + path_point = a8::XValue(point).GetString(); + path_start = !path_start; + } else { + std::string point_str = a8::XValue(point).GetString(); + path_point += "-" + point_str; + stage_path_list.push_back(path_point); + path_point = point_str; + } + } + } + std::vector grid_all_list; + { + bool exc_once = true; + for (auto& stage_path : stage_path_list) { + std::vector* grid_list = nullptr; + + std::string stage_path_reverse; + std::vector split_list; + a8::Split(stage_path,split_list,'-'); + if (split_list.size() != 2) { + return false; + } + StagePoint* sp = GetStageObject(a8::XValue(split_list.at(0))); + if (sp == nullptr) { + return false; + } + grid_list = GetStagePath(stage_path); + if (grid_list == nullptr) { + stage_path_reverse = split_list[1] + "-" + split_list[0]; + grid_list = GetStagePath(stage_path_reverse); + if (grid_list == nullptr) { + return false; + } + } + + std::vector grid_sort_list = SortGridList(grid_list,sp); + if (grid_sort_list.size() < 2) { + return false; + } + if (exc_once) { + grid_all_list.insert(grid_all_list.end(),grid_sort_list.begin(),grid_sort_list.end()); + exc_once = false; + } else { + grid_all_list.insert(grid_all_list.end(),grid_sort_list.begin()+1,grid_sort_list.end()); + } + } + } + if (grid_all_list.empty()) { return false; } @@ -458,3 +460,4 @@ void TiledMap::SetGridCellSpeedByAreaId(std::map& area_speed_hash) } } } +} diff --git a/cpp/tiledmap.h b/cpp/tiledmap.h index b6d2ec4..c2273cc 100644 --- a/cpp/tiledmap.h +++ b/cpp/tiledmap.h @@ -1,87 +1,90 @@ #pragma once -class TiledObject +namespace f8 { - public: - a8::XValue GetProperty(const std::string& prop_name); - bool HasProperty(const std::string& prop_name); + class TiledObject + { + public: + a8::XValue GetProperty(const std::string& prop_name); + bool HasProperty(const std::string& prop_name); - public: - std::map prop_hash; -}; + public: + std::map prop_hash; + }; -class TiledLayer -{ - public: + class TiledLayer + { + public: - a8::XValue GetProperty(const std::string& prop_name); - bool HasProperty(const std::string& prop_name); + a8::XValue GetProperty(const std::string& prop_name); + bool HasProperty(const std::string& prop_name); - public: - std::map prop_hash; - a8::XValue data; -}; + public: + std::map prop_hash; + a8::XValue data; + }; -//格子对象 -struct GridCell -{ - int x = 0; - int y = 0; - int speed = 0; - int value = 0; -}; + //格子对象 + struct GridCell + { + int x = 0; + int y = 0; + int speed = 0; + int value = 0; + }; -struct StagePoint -{ - int x = 0; - int y = 0; - int point = 0; - std::vector relation_list; -}; + struct StagePoint + { + int x = 0; + int y = 0; + int point = 0; + std::vector relation_list; + }; -struct TriggerPoint -{ - int x = 0; - int y = 0; - int point = 0; - bool istrigger = false; -}; + struct TriggerPoint + { + int x = 0; + int y = 0; + int point = 0; + bool istrigger = false; + }; -class TiledMap -{ - public: - TiledLayer* ground_layer = nullptr; - std::list speed_layers; - int tile_count = 0; //瓦片地图的尺寸。(以瓦片数量为单位) - int tile_width = 0; //瓦片的宽。(以像素点为单位) - int tile_height = 0; //瓦片的高。(以像素点为单位) - int tile_rows = 0; //行 - int tile_columns = 0; //列 + class TiledMap + { + public: + TiledLayer* ground_layer = nullptr; + std::list speed_layers; + int tile_count = 0; //瓦片地图的尺寸。(以瓦片数量为单位) + int tile_width = 0; //瓦片的宽。(以像素点为单位) + int tile_height = 0; //瓦片的高。(以像素点为单位) + int tile_rows = 0; //行 + int tile_columns = 0; //列 - bool LoadTmxFile(const std::string& filename); - std::list* GetObjectGroup(const std::string& object_class_name); - void Dump(); - void Init(); - StagePoint* GetStageObject(int point); - bool HasStageObject(int point); + bool LoadTmxFile(const std::string& filename); + std::list* GetObjectGroup(const std::string& object_class_name); + void Dump(); + void Init(); + StagePoint* GetStageObject(int point); + bool HasStageObject(int point); - std::vector* GetStagePath(const std::string& path_name); - bool HasStagePath(const std::string& path_name); + std::vector* GetStagePath(const std::string& path_name); + bool HasStagePath(const std::string& path_name); - std::vector SortGridList(std::vector* grid_list, StagePoint* sp); - bool CalcCurrPos(std::vector& path_points, int old_pos_x, int old_pos_y, int time_ms, int& curr_pos_x, int& curr_pos_y); + std::vector SortGridList(std::vector* grid_list, StagePoint* sp); + bool CalcCurrPos(std::vector& path_points, int old_pos_x, int old_pos_y, int time_ms, int& curr_pos_x, int& curr_pos_y); - //速度设置 - void SetGridCellSpeedByAreaId(std::map& area_speed_hash);//key:area_id value:speed + //速度设置 + void SetGridCellSpeedByAreaId(std::map& area_speed_hash);//key:area_id value:speed - private: - std::map> layer_hash; - std::map> object_group_hash; + private: + std::map> layer_hash; + std::map> object_group_hash; - std::map stage_object_hash; //key point - std::vector trigger_list; - std::map> stage_path_hash; //key 11|2 - std::vector grid_cell_list; //所有的格子数组 size = x * y + std::map stage_object_hash; //key point + std::vector trigger_list; + std::map> stage_path_hash; //key 11|2 + std::vector grid_cell_list; //所有的格子数组 size = x * y - std::map> grid_cell_hash; //key 11|2 坐标 -}; + std::map> grid_cell_hash; //key 11|2 坐标 + }; +} diff --git a/cpp/utils.cc b/cpp/utils.cc index 18eee64..9b2b826 100644 --- a/cpp/utils.cc +++ b/cpp/utils.cc @@ -9,169 +9,172 @@ #include "framework/cpp/utils.h" -bool ReadCsvMetaFile(const std::string& filename, - google::protobuf::Message* prototype, - std::function push_back_func) +namespace f8 { - const google::protobuf::Descriptor* descriptor = prototype->GetDescriptor(); - const google::protobuf::Reflection* reflection = prototype->GetReflection(); + bool ReadCsvMetaFile(const std::string& filename, + google::protobuf::Message* prototype, + std::function push_back_func) + { + const google::protobuf::Descriptor* descriptor = prototype->GetDescriptor(); + const google::protobuf::Reflection* reflection = prototype->GetReflection(); - a8::CsvReader reader; - reader.Load(filename); - while (reader.NextLine()) { - google::protobuf::Message* msg = prototype->New(); + a8::CsvReader reader; + reader.Load(filename); + while (reader.NextLine()) { + google::protobuf::Message* msg = prototype->New(); - for (int i = 0; i < descriptor->field_count(); ++i) { - const google::protobuf::FieldDescriptor* field_desc = descriptor->field(i); - const std::string& field_name = field_desc->name(); - if (field_name.empty() || field_name[0] == '_') { - continue; - } + for (int i = 0; i < descriptor->field_count(); ++i) { + const google::protobuf::FieldDescriptor* field_desc = descriptor->field(i); + const std::string& field_name = field_desc->name(); + if (field_name.empty() || field_name[0] == '_') { + continue; + } - switch (field_desc->cpp_type()) { - case google::protobuf::FieldDescriptor::CPPTYPE_STRING: - { - reflection->SetString(msg, field_desc, reader.GetValue(field_name)); - } - break; - case google::protobuf::FieldDescriptor::CPPTYPE_INT32: - { - reflection->SetInt32(msg, field_desc, reader.GetValue(field_name)); - } - break; - case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: - { - reflection->SetUInt32(msg, field_desc, reader.GetValue(field_name)); - } - break; - case google::protobuf::FieldDescriptor::CPPTYPE_INT64: - { - reflection->SetInt64(msg, field_desc, reader.GetValue(field_name)); - } - break; - case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: - { - reflection->SetUInt64(msg, field_desc, reader.GetValue(field_name)); - } - break; - case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: - { - reflection->SetFloat(msg, field_desc, (double)reader.GetValue(field_name)); - } - break; - case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: - { - reflection->SetDouble(msg, field_desc, reader.GetValue(field_name)); - } - break; - default: - { - abort(); - } - break; - }//end switch - }//end for - push_back_func(msg); - delete msg; + switch (field_desc->cpp_type()) { + case google::protobuf::FieldDescriptor::CPPTYPE_STRING: + { + reflection->SetString(msg, field_desc, reader.GetValue(field_name)); + } + break; + case google::protobuf::FieldDescriptor::CPPTYPE_INT32: + { + reflection->SetInt32(msg, field_desc, reader.GetValue(field_name)); + } + break; + case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: + { + reflection->SetUInt32(msg, field_desc, reader.GetValue(field_name)); + } + break; + case google::protobuf::FieldDescriptor::CPPTYPE_INT64: + { + reflection->SetInt64(msg, field_desc, reader.GetValue(field_name)); + } + break; + case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: + { + reflection->SetUInt64(msg, field_desc, reader.GetValue(field_name)); + } + break; + case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: + { + reflection->SetFloat(msg, field_desc, (double)reader.GetValue(field_name)); + } + break; + case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: + { + reflection->SetDouble(msg, field_desc, reader.GetValue(field_name)); + } + break; + default: + { + abort(); + } + break; + }//end switch + }//end for + push_back_func(msg); + delete msg; + } + + return true; } - return true; -} - -void InitMysqlConnection(a8::mysql::Query* query) -{ - a8::UdpLog::Instance()->Info("show variables like 'character%';", {}); - a8::UdpLog::Instance()->Info("Variable_name\tValue", {}); + void InitMysqlConnection(a8::mysql::Query* query) + { + a8::UdpLog::Instance()->Info("show variables like 'character%';", {}); + a8::UdpLog::Instance()->Info("Variable_name\tValue", {}); #if 1 - query->ExecScript("SET character_set_server=utf8;", {}); - query->ExecScript("SET NAMES utf8;", {}); + query->ExecScript("SET character_set_server=utf8;", {}); + query->ExecScript("SET NAMES utf8;", {}); #endif - query->ExecQuery("show variables like 'character%';", {}); - while (!query->Eof()) { - std::string line; - line = query->GetValue(0).GetString() + "\t" + query->GetValue(1).GetString(); - a8::UdpLog::Instance()->Info(line.c_str(), {}); - assert(!(query->GetValue(0).GetString() == "character_set_client" && - query->GetValue(1).GetString() != "utf8")); - assert(!(query->GetValue(0).GetString() == "character_set_connection" && - query->GetValue(1).GetString() != "utf8")); - assert(!(query->GetValue(0).GetString() == "character_set_database" && - query->GetValue(1).GetString() != "utf8")); - assert(!(query->GetValue(0).GetString() == "character_set_results" && - query->GetValue(1).GetString() != "utf8")); - assert(!(query->GetValue(0).GetString() == "character_set_server" && - query->GetValue(1).GetString() != "utf8")); - assert(!(query->GetValue(0).GetString() == "character_set_system" && - query->GetValue(1).GetString() != "utf8")); - query->Next(); + query->ExecQuery("show variables like 'character%';", {}); + while (!query->Eof()) { + std::string line; + line = query->GetValue(0).GetString() + "\t" + query->GetValue(1).GetString(); + a8::UdpLog::Instance()->Info(line.c_str(), {}); + assert(!(query->GetValue(0).GetString() == "character_set_client" && + query->GetValue(1).GetString() != "utf8")); + assert(!(query->GetValue(0).GetString() == "character_set_connection" && + query->GetValue(1).GetString() != "utf8")); + assert(!(query->GetValue(0).GetString() == "character_set_database" && + query->GetValue(1).GetString() != "utf8")); + assert(!(query->GetValue(0).GetString() == "character_set_results" && + query->GetValue(1).GetString() != "utf8")); + assert(!(query->GetValue(0).GetString() == "character_set_server" && + query->GetValue(1).GetString() != "utf8")); + assert(!(query->GetValue(0).GetString() == "character_set_system" && + query->GetValue(1).GetString() != "utf8")); + query->Next(); + } + + a8::UdpLog::Instance()->Info("show variables like '%collation%';", {}); + a8::UdpLog::Instance()->Info("Variable_name\tValue", {}); + query->ExecQuery("show variables like '%collation%';", {}); + while (!query->Eof()) { + std::string line; + line = query->GetValue(0).GetString() + "\t" + query->GetValue(1).GetString(); + a8::UdpLog::Instance()->Info(line.c_str(), {}); + assert(query->GetValue(1).GetString() == "utf8_general_ci"); + query->Next(); + } } - a8::UdpLog::Instance()->Info("show variables like '%collation%';", {}); - a8::UdpLog::Instance()->Info("Variable_name\tValue", {}); - query->ExecQuery("show variables like '%collation%';", {}); - while (!query->Eof()) { - std::string line; - line = query->GetValue(0).GetString() + "\t" + query->GetValue(1).GetString(); - a8::UdpLog::Instance()->Info(line.c_str(), {}); - assert(query->GetValue(1).GetString() == "utf8_general_ci"); - query->Next(); + bool CheckRegisterTimeInSessionId(const std::string& accountid, const std::string& sessionid) + { + std::vector strings; + a8::Split(sessionid, strings, '_'); + if (strings.size() < 4) { + return false; + } + return true; } -} -bool CheckRegisterTimeInSessionId(const std::string& accountid, const std::string& sessionid) -{ - std::vector strings; - a8::Split(sessionid, strings, '_'); - if (strings.size() < 4) { - return false; + time_t ExtractRegisterTimeFromSessionId(const std::string& sessionid) + { + std::vector strings; + a8::Split(sessionid, strings, '_'); + if (strings.size() < 4) { + return false; + } + std::string session_createtime = strings[0]; + std::string account_registertime = strings[1]; + std::string md51 = strings[2]; + std::string md52 = strings[3]; + return a8::XValue(account_registertime); } - return true; -} -time_t ExtractRegisterTimeFromSessionId(const std::string& sessionid) -{ - std::vector strings; - a8::Split(sessionid, strings, '_'); - if (strings.size() < 4) { - return false; + bool IsValidSessionId(const std::string& accountid, const std::string& sessionid) + { + std::vector strings; + a8::Split(sessionid, strings, '_'); + if (strings.size() < 4) { + return false; + } + return true; } - std::string session_createtime = strings[0]; - std::string account_registertime = strings[1]; - std::string md51 = strings[2]; - std::string md52 = strings[3]; - return a8::XValue(account_registertime); -} -bool IsValidSessionId(const std::string& accountid, const std::string& sessionid) -{ - std::vector strings; - a8::Split(sessionid, strings, '_'); - if (strings.size() < 4) { - return false; + int ExtractGameIdFromAccountId(const std::string& accountid) + { + std::vector strings; + a8::Split(accountid, strings, '_'); + if (strings.size() < 2) { + return false; + } + std::string channelid = strings[0]; + std::string gameid = strings[1]; + return a8::XValue(gameid); } - return true; -} -int ExtractGameIdFromAccountId(const std::string& accountid) -{ - std::vector strings; - a8::Split(accountid, strings, '_'); - if (strings.size() < 2) { - return false; + int ExtractChannelIdFromAccountId(const std::string& accountid) + { + std::vector strings; + a8::Split(accountid, strings, '_'); + if (strings.size() < 2) { + return false; + } + std::string channelid = strings[0]; + std::string gameid = strings[1]; + return a8::XValue(channelid); } - std::string channelid = strings[0]; - std::string gameid = strings[1]; - return a8::XValue(gameid); -} - -int ExtractChannelIdFromAccountId(const std::string& accountid) -{ - std::vector strings; - a8::Split(accountid, strings, '_'); - if (strings.size() < 2) { - return false; - } - std::string channelid = strings[0]; - std::string gameid = strings[1]; - return a8::XValue(channelid); } diff --git a/cpp/utils.h b/cpp/utils.h index 3372c96..1fa0281 100644 --- a/cpp/utils.h +++ b/cpp/utils.h @@ -17,68 +17,71 @@ namespace a8 } } -bool ReadCsvMetaFile(const std::string& filename, - google::protobuf::Message* prototype, - std::function push_back_func); - -template -bool ReadCsvMetaFile(const std::string& filename, std::list& meta_list) +namespace f8 { - T dummy; - return ReadCsvMetaFile(filename, - &dummy, - [&meta_list] (google::protobuf::Message* msg) - { - T t; - t.CopyFrom(*msg); - meta_list.emplace_back(t); - }); -} + bool ReadCsvMetaFile(const std::string& filename, + google::protobuf::Message* prototype, + std::function push_back_func); -template -static void RepeatedFieldToVector(const T1& t1, T2& t2) -{ - t2.clear(); - for (auto& val : t1) { - t2.push_back(val); + template + bool ReadCsvMetaFile(const std::string& filename, std::list& meta_list) + { + T dummy; + return ReadCsvMetaFile(filename, + &dummy, + [&meta_list] (google::protobuf::Message* msg) + { + T t; + t.CopyFrom(*msg); + meta_list.emplace_back(t); + }); } -} -template -static void RepeatedFieldToSet(const T1& t1, T2& t2) -{ - t2.clear(); - for (auto& val : t1) { - t2.insert(val); + template + static void RepeatedFieldToVector(const T1& t1, T2& t2) + { + t2.clear(); + for (auto& val : t1) { + t2.push_back(val); + } } -} -template -static void VectorToRepeatedField(const T1& t1, T2& t2) -{ - t2.Clear(); - for (auto& val : t1) { - *t2.Add() = val; + template + static void RepeatedFieldToSet(const T1& t1, T2& t2) + { + t2.clear(); + for (auto& val : t1) { + t2.insert(val); + } } -} -template -static void SetToRepeatedField(const T1& t1, T2& t2) -{ - t2.Clear(); - for (auto& val : t1) { - *t2.Add() = val; + template + static void VectorToRepeatedField(const T1& t1, T2& t2) + { + t2.Clear(); + for (auto& val : t1) { + *t2.Add() = val; + } } + + template + static void SetToRepeatedField(const T1& t1, T2& t2) + { + t2.Clear(); + for (auto& val : t1) { + *t2.Add() = val; + } + } + + void InitMysqlConnection(a8::mysql::Query* query); + + bool CheckRegisterTimeInSessionId(const std::string& accountid, const std::string& sessionid); + time_t ExtractRegisterTimeFromSessionId(const std::string& sessionid); + bool IsValidSessionId(const std::string& accountid, const std::string& sessionid); + int ExtractGameIdFromAccountId(const std::string& accountid); + int ExtractChannelIdFromAccountId(const std::string& accountid); } - -void InitMysqlConnection(a8::mysql::Query* query); - -bool CheckRegisterTimeInSessionId(const std::string& accountid, const std::string& sessionid); -time_t ExtractRegisterTimeFromSessionId(const std::string& sessionid); -bool IsValidSessionId(const std::string& accountid, const std::string& sessionid); -int ExtractGameIdFromAccountId(const std::string& accountid); -int ExtractChannelIdFromAccountId(const std::string& accountid);