add enum stringification functionality from third party source

This commit is contained in:
jadebenn 2023-12-17 02:03:07 -06:00
parent 24c2361248
commit a9ca0198b0
6 changed files with 1672 additions and 1606 deletions

File diff suppressed because it is too large Load Diff

View File

@ -34,6 +34,7 @@
#include "eMissionTaskType.h" #include "eMissionTaskType.h"
#include "eReplicaComponentType.h" #include "eReplicaComponentType.h"
#include "eConnectionType.h" #include "eConnectionType.h"
#include "eGameMessageType.h"
#include "ePlayerFlag.h" #include "ePlayerFlag.h"
#include "dConfig.h" #include "dConfig.h"
@ -53,7 +54,7 @@ void GameMessageHandler::HandleMessage(RakNet::BitStream* inStream, const System
return; return;
} }
if (messageID != eGameMessageType::READY_FOR_UPDATES) LOG_DEBUG("received game message ID: %i", messageID); if (messageID != eGameMessageType::READY_FOR_UPDATES) LOG_DEBUG("received game message ID: (%4i) %s", messageID, eGameMessageType_as_string(messageID));
switch (messageID) { switch (messageID) {
@ -344,12 +345,12 @@ void GameMessageHandler::HandleMessage(RakNet::BitStream* inStream, const System
SyncSkill sync = SyncSkill(inStream); // inStream replaced &bitStream SyncSkill sync = SyncSkill(inStream); // inStream replaced &bitStream
ostringstream buffer; std::ostringstream buffer;
for (unsigned int k = 0; k < sync.sBitStream.size(); k++) { for (unsigned int k = 0; k < sync.sBitStream.size(); k++) {
char s; char s;
s = sync.sBitStream.at(k); s = sync.sBitStream.at(k);
buffer << setw(2) << hex << setfill('0') << (int)s << " "; buffer << std::setw(2) << std::hex << std::setfill('0') << static_cast<int>(s) << " ";
} }
if (usr != nullptr) { if (usr != nullptr) {
@ -691,7 +692,7 @@ void GameMessageHandler::HandleMessage(RakNet::BitStream* inStream, const System
GameMessages::HandleCancelDonationOnPlayer(inStream, entity); GameMessages::HandleCancelDonationOnPlayer(inStream, entity);
break; break;
default: default:
LOG_DEBUG("Unknown game message ID: %i", messageID); LOG_DEBUG("Unknown game message ID: (%4i) %s", messageID, eGameMessageType_as_string(messageID));
break; break;
} }
} }

View File

@ -19,8 +19,7 @@
#include "Logger.h" #include "Logger.h"
#include "GameMessages.h" #include "GameMessages.h"
#include "CDClientDatabase.h" #include "CDClientDatabase.h"
#include "eGameMessageType.h"
enum class eGameMessageType : uint16_t;
namespace GameMessageHandler { namespace GameMessageHandler {
void HandleMessage(RakNet::BitStream* inStream, const SystemAddress& sysAddr, LWOOBJID objectID, eGameMessageType messageID); void HandleMessage(RakNet::BitStream* inStream, const SystemAddress& sysAddr, LWOOBJID objectID, eGameMessageType messageID);

View File

@ -5,4 +5,5 @@ set(DGAME_DUTILITIES_SOURCES "BrickDatabase.cpp"
"Mail.cpp" "Mail.cpp"
"Preconditions.cpp" "Preconditions.cpp"
"SlashCommandHandler.cpp" "SlashCommandHandler.cpp"
"StringifyEnums.cpp"
"VanityUtilities.cpp" PARENT_SCOPE) "VanityUtilities.cpp" PARENT_SCOPE)

View File

@ -0,0 +1,6 @@
#define GENERATE_ENUM_STRINGS // Start string generation
#include <map>
#include "eGameMessageType.h"
#undef GENERATE_ENUM_STRINGS // Stop string generation

View File

@ -0,0 +1,55 @@
// Source - https://mariusbancila.ro/blog/2023/08/17/how-to-convert-an-enum-to-string-in-cpp/
#pragma once
#undef DECL_ENUM_ELEMENT
#undef BEGIN_ENUM
#undef END_ENUM
#ifndef GENERATE_ENUM_STRINGS
#define DECLARE_ENUM_ELEMENT(element) element,
#define BEGIN_ENUM(ENUM_NAME, TYPE) typedef enum class tag##ENUM_NAME : TYPE {
#define BEGIN_ENUM_INT(ENUM_NAME) BEGIN_ENUM(ENUM_NAME, int)
#define END_ENUM(ENUM_NAME) } ENUM_NAME; const char* ENUM_NAME##_as_string(enum tag##ENUM_NAME index);
#define DECLARE_ENUM_ELEMENT_WITH_VALUE(element, value) element = value,
#define BEGIN_ENUM_WITH_VALUES(ENUM_NAME, TYPE) BEGIN_ENUM(ENUM_NAME, TYPE)
#define BEGIN_ENUM_WITH_VALUES_INT(ENUM_NAME) BEGIN_ENUM(ENUM_NAME, int)
#define END_ENUM_WITH_VALUES(ENUM_NAME, TYPE) END_ENUM(ENUM_NAME)
#else
#define NO_VALUE "<none>"
#define DECLARE_ENUM_ELEMENT(element) #element,
#define BEGIN_ENUM(ENUM_NAME, TYPE) enum class tag##ENUM_NAME : TYPE;\
const char* ENUM_NAME##_as_string(enum tag##ENUM_NAME value) {\
std::size_t index = static_cast<std::size_t>(value);\
static const char* s_##ENUM_NAME[] = {
#define BEGIN_ENUM_INT(ENUM_NAME) BEGIN_ENUM(ENUM_NAME, int)
#define END_ENUM(ENUM_NAME) };\
static const std::size_t s_##ENUM_NAME_len = sizeof(s_##ENUM_NAME)/sizeof(const char*);\
if(index >=0 && index < s_##ENUM_NAME_len)\
return s_##ENUM_NAME[index]; \
return NO_VALUE;\
}
#define DECLARE_ENUM_ELEMENT_WITH_VALUE(element, value) {value, #element},
#define BEGIN_ENUM_WITH_VALUES(ENUM_NAME, TYPE) enum class tag##ENUM_NAME : TYPE;\
const char* ENUM_NAME##_as_string(enum tag##ENUM_NAME value) {\
std::map<TYPE, const char*> sv = {
#define BEGIN_ENUM_WITH_VALUES_INT(ENUM_NAME) BEGIN_ENUM_WITH_VALUES(ENUM_NAME, int)
#define END_ENUM_WITH_VALUES(ENUM_NAME, TYPE) };\
auto it = sv.find(static_cast<TYPE>(value));\
if (it != sv.end())\
return it->second;\
return NO_VALUE;\
}
#endif