From e03da83ff33dc028c6879740e205e4dcc6e2bda4 Mon Sep 17 00:00:00 2001 From: dongresource Date: Wed, 16 Sep 2020 21:46:15 +0200 Subject: [PATCH] Rearranged the codebase a little. * Deleted empty Player.cpp * Moved the helper functions from the obsolete CNStructs.cpp into main.cpp and deleted it * Renamed CombatManager to MobManager, as that will likely become it's main focus soon --- Makefile | 6 ++-- src/CNProtocol.cpp | 2 +- src/CNStructs.cpp | 31 ------------------- src/ChatManager.cpp | 8 +++-- src/{CombatManager.cpp => MobManager.cpp} | 14 ++++----- src/{CombatManager.hpp => MobManager.hpp} | 2 +- src/Player.cpp | 1 - src/main.cpp | 36 +++++++++++++++++++++-- 8 files changed, 50 insertions(+), 50 deletions(-) delete mode 100644 src/CNStructs.cpp rename src/{CombatManager.cpp => MobManager.cpp} (92%) rename src/{CombatManager.hpp => MobManager.hpp} (93%) delete mode 100644 src/Player.cpp diff --git a/Makefile b/Makefile index aba5138..554efc0 100644 --- a/Makefile +++ b/Makefile @@ -30,20 +30,18 @@ CSRC=\ CXXSRC=\ src/ChatManager.cpp\ - src/CombatManager.cpp\ src/CNLoginServer.cpp\ src/CNProtocol.cpp\ src/CNShardServer.cpp\ src/CNShared.cpp\ - src/CNStructs.cpp\ src/Database.cpp\ src/Defines.cpp\ src/main.cpp\ src/MissionManager.cpp\ + src/MobManager.cpp\ src/NanoManager.cpp\ src/ItemManager.cpp\ src/NPCManager.cpp\ - src/Player.cpp\ src/PlayerManager.cpp\ src/settings.cpp\ src/TransportManager.cpp\ @@ -64,7 +62,6 @@ CXXHDR=\ src/contrib/INIReader.hpp\ src/contrib/JSON.hpp\ src/ChatManager.hpp\ - src/CombatManager.hpp\ src/CNLoginServer.hpp\ src/CNProtocol.hpp\ src/CNShardServer.hpp\ @@ -75,6 +72,7 @@ CXXHDR=\ src/contrib/INIReader.hpp\ src/contrib/JSON.hpp\ src/MissionManager.hpp\ + src/MobManager.hpp\ src/NanoManager.hpp\ src/ItemManager.hpp\ src/NPCManager.hpp\ diff --git a/src/CNProtocol.cpp b/src/CNProtocol.cpp index baf5bdb..7fbb3ed 100644 --- a/src/CNProtocol.cpp +++ b/src/CNProtocol.cpp @@ -167,7 +167,7 @@ void CNSocket::step() { // we aren't reading a packet yet, try to start looking for one int recved = recv(sock, (buffer_t*)readBuffer, sizeof(int32_t), 0); if (!SOCKETERROR(recved)) { - // we got out packet size!!!! + // we got our packet size!!!! readSize = *((int32_t*)readBuffer); // sanity check if (readSize > CN_PACKET_BUFFER_SIZE) { diff --git a/src/CNStructs.cpp b/src/CNStructs.cpp deleted file mode 100644 index 3501594..0000000 --- a/src/CNStructs.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "CNStructs.hpp" -#include - -std::string U16toU8(char16_t* src) { - try { - std::wstring_convert,char16_t> convert; - return convert.to_bytes(src); - } catch(const std::exception& e) { - return ""; - } -} - -// returns number of char16_t that was written at des -size_t U8toU16(std::string src, char16_t* des) { - std::wstring_convert,char16_t> convert; - std::u16string tmp = convert.from_bytes(src); - - // copy utf16 string to buffer - memcpy(des, tmp.c_str(), sizeof(char16_t) * tmp.length()); - des[tmp.length()] = '\0'; - - return tmp.length(); -} - -time_t getTime() { - using namespace std::chrono; - - milliseconds value = duration_cast((time_point_cast(high_resolution_clock::now())).time_since_epoch()); - - return (time_t)value.count(); -} diff --git a/src/ChatManager.cpp b/src/ChatManager.cpp index ac6fe8a..ca0de64 100644 --- a/src/ChatManager.cpp +++ b/src/ChatManager.cpp @@ -13,7 +13,7 @@ void ChatManager::chatHandler(CNSocket* sock, CNPacketData* data) { if (data->size != sizeof(sP_CL2FE_REQ_SEND_FREECHAT_MESSAGE)) return; // malformed packet sP_CL2FE_REQ_SEND_FREECHAT_MESSAGE* chat = (sP_CL2FE_REQ_SEND_FREECHAT_MESSAGE*)data->buf; - PlayerView plr = PlayerManager::players[sock]; + PlayerView& plr = PlayerManager::players[sock]; // send to client INITSTRUCT(sP_FE2CL_REP_SEND_FREECHAT_MESSAGE_SUCC, resp); @@ -27,11 +27,12 @@ void ChatManager::chatHandler(CNSocket* sock, CNPacketData* data) { otherSock->sendPacket((void*)&resp, P_FE2CL_REP_SEND_FREECHAT_MESSAGE_SUCC, sizeof(sP_FE2CL_REP_SEND_FREECHAT_MESSAGE_SUCC)); } } + void ChatManager::menuChatHandler(CNSocket* sock, CNPacketData* data) { if (data->size != sizeof(sP_CL2FE_REQ_SEND_MENUCHAT_MESSAGE)) return; // malformed packet sP_CL2FE_REQ_SEND_MENUCHAT_MESSAGE* chat = (sP_CL2FE_REQ_SEND_MENUCHAT_MESSAGE*)data->buf; - PlayerView plr = PlayerManager::players[sock]; + PlayerView& plr = PlayerManager::players[sock]; // send to client INITSTRUCT(sP_FE2CL_REP_SEND_MENUCHAT_MESSAGE_SUCC, resp); @@ -45,6 +46,7 @@ void ChatManager::menuChatHandler(CNSocket* sock, CNPacketData* data) { otherSock->sendPacket((void*)&resp, P_FE2CL_REP_SEND_MENUCHAT_MESSAGE_SUCC, sizeof(sP_FE2CL_REP_SEND_MENUCHAT_MESSAGE_SUCC)); } } + void ChatManager::emoteHandler(CNSocket* sock, CNPacketData* data) { if (data->size != sizeof(sP_CL2FE_REQ_PC_AVATAR_EMOTES_CHAT)) return; // ignore the malformed packet @@ -52,7 +54,7 @@ void ChatManager::emoteHandler(CNSocket* sock, CNPacketData* data) { // you can dance with friends!!!!!!!! sP_CL2FE_REQ_PC_AVATAR_EMOTES_CHAT* emote = (sP_CL2FE_REQ_PC_AVATAR_EMOTES_CHAT*)data->buf; - PlayerView plr = PlayerManager::players[sock]; + PlayerView& plr = PlayerManager::players[sock]; // send to client INITSTRUCT(sP_FE2CL_REP_PC_AVATAR_EMOTES_CHAT, resp); diff --git a/src/CombatManager.cpp b/src/MobManager.cpp similarity index 92% rename from src/CombatManager.cpp rename to src/MobManager.cpp index e932b23..3cda968 100644 --- a/src/CombatManager.cpp +++ b/src/MobManager.cpp @@ -1,4 +1,4 @@ -#include "CombatManager.hpp" +#include "MobManager.hpp" #include "PlayerManager.hpp" #include "NPCManager.hpp" #include "ItemManager.hpp" @@ -6,7 +6,7 @@ #include -void CombatManager::init() { +void MobManager::init() { REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_ATTACK_NPCs, pcAttackNpcs); REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_COMBAT_BEGIN, combatBegin); @@ -14,7 +14,7 @@ void CombatManager::init() { REGISTER_SHARD_PACKET(P_CL2FE_DOT_DAMAGE_ONOFF, dotDamageOnOff); } -void CombatManager::pcAttackNpcs(CNSocket *sock, CNPacketData *data) { +void MobManager::pcAttackNpcs(CNSocket *sock, CNPacketData *data) { sP_CL2FE_REQ_PC_ATTACK_NPCs* pkt = (sP_CL2FE_REQ_PC_ATTACK_NPCs*)data->buf; Player *plr = PlayerManager::getPlayer(sock); @@ -82,11 +82,11 @@ void CombatManager::pcAttackNpcs(CNSocket *sock, CNPacketData *data) { s->sendPacket((void*)respbuf, P_FE2CL_PC_ATTACK_NPCs, resplen); } -void CombatManager::combatBegin(CNSocket *sock, CNPacketData *data) {} // stub -void CombatManager::combatEnd(CNSocket *sock, CNPacketData *data) {} // stub -void CombatManager::dotDamageOnOff(CNSocket *sock, CNPacketData *data) {} // stub +void MobManager::combatBegin(CNSocket *sock, CNPacketData *data) {} // stub +void MobManager::combatEnd(CNSocket *sock, CNPacketData *data) {} // stub +void MobManager::dotDamageOnOff(CNSocket *sock, CNPacketData *data) {} // stub -void CombatManager::giveReward(CNSocket *sock) { +void MobManager::giveReward(CNSocket *sock) { Player *plr = PlayerManager::getPlayer(sock); const size_t resplen = sizeof(sP_FE2CL_REP_REWARD_ITEM) + sizeof(sItemReward); diff --git a/src/CombatManager.hpp b/src/MobManager.hpp similarity index 93% rename from src/CombatManager.hpp rename to src/MobManager.hpp index 5d321ac..dde0d23 100644 --- a/src/CombatManager.hpp +++ b/src/MobManager.hpp @@ -4,7 +4,7 @@ #include "CNShared.hpp" #include "CNShardServer.hpp" -namespace CombatManager { +namespace MobManager { void init(); void pcAttackNpcs(CNSocket *sock, CNPacketData *data); diff --git a/src/Player.cpp b/src/Player.cpp deleted file mode 100644 index 9e7a7e3..0000000 --- a/src/Player.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "Player.hpp" diff --git a/src/main.cpp b/src/main.cpp index d517d41..85f94c6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,7 +2,7 @@ #include "CNShardServer.hpp" #include "PlayerManager.hpp" #include "ChatManager.hpp" -#include "CombatManager.hpp" +#include "MobManager.hpp" #include "ItemManager.hpp" #include "MissionManager.hpp" #include "NanoManager.hpp" @@ -21,6 +21,7 @@ #include #endif #include +#include #include // HACK @@ -92,7 +93,7 @@ int main() { TableData::init(); PlayerManager::init(); ChatManager::init(); - CombatManager::init(); + MobManager::init(); ItemManager::init(); MissionManager::init(); NanoManager::init(); @@ -117,3 +118,34 @@ int main() { #endif return 0; } + +// helper functions + +std::string U16toU8(char16_t* src) { + try { + std::wstring_convert,char16_t> convert; + return convert.to_bytes(src); + } catch(const std::exception& e) { + return ""; + } +} + +// returns number of char16_t that was written at des +size_t U8toU16(std::string src, char16_t* des) { + std::wstring_convert,char16_t> convert; + std::u16string tmp = convert.from_bytes(src); + + // copy utf16 string to buffer + memcpy(des, tmp.c_str(), sizeof(char16_t) * tmp.length()); + des[tmp.length()] = '\0'; + + return tmp.length(); +} + +time_t getTime() { + using namespace std::chrono; + + milliseconds value = duration_cast((time_point_cast(high_resolution_clock::now())).time_since_epoch()); + + return (time_t)value.count(); +}