From 6de224a2facfbf2f140869a6345ff6024acfe7cf Mon Sep 17 00:00:00 2001 From: jadebenn Date: Sat, 23 Dec 2023 18:11:00 -0600 Subject: [PATCH] chore: miscellaneous code, formatting, and syntax cleanup (#1351) * miscellaneous code, formatting, and syntax cleanup * update * update again * updated to account for feedback --- dCommon/GeneralUtils.h | 26 ++- dCommon/dEnums/StringifiedEnum.h | 8 +- dGame/Entity.cpp | 5 +- dGame/dBehaviors/BasicAttackBehavior.cpp | 10 +- dGame/dComponents/DestroyableComponent.cpp | 2 +- dGame/dGameMessages/GameMessageHandler.cpp | 4 +- dGame/dGameMessages/GameMessages.cpp | 172 +++++++++--------- dNet/MasterPackets.cpp | 8 +- dNet/WorldPackets.cpp | 40 ++-- dWorldServer/WorldServer.cpp | 4 +- tests/dCommonTests/CMakeLists.txt | 1 + .../dCommonTests/CastUnderlyingTypeTests.cpp | 24 +++ .../dEnumsTests/MagicEnumTests.cpp | 6 +- 13 files changed, 169 insertions(+), 141 deletions(-) create mode 100644 tests/dCommonTests/CastUnderlyingTypeTests.cpp diff --git a/dCommon/GeneralUtils.h b/dCommon/GeneralUtils.h index d93fd0e7..964a864b 100644 --- a/dCommon/GeneralUtils.h +++ b/dCommon/GeneralUtils.h @@ -234,18 +234,30 @@ namespace GeneralUtils { return T(); } -// on Windows we need to undef these or else they conflict with our numeric limits calls -// DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS -#ifdef _WIN32 -#undef min -#undef max -#endif + /** + * Casts the value of an enum entry to its underlying type + * @param entry Enum entry to cast + * @returns The enum entry's value in its underlying type + */ + template + inline constexpr typename std::underlying_type_t CastUnderlyingType(const eType entry) { + static_assert(std::is_enum_v, "Not an enum"); + + return static_cast>(entry); + } + + // on Windows we need to undef these or else they conflict with our numeric limits calls + // DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS + #ifdef _WIN32 + #undef min + #undef max + #endif template inline T GenerateRandomNumber() { // Make sure it is a numeric type static_assert(std::is_arithmetic::value, "Not an arithmetic type"); - + return GenerateRandomNumber(std::numeric_limits::min(), std::numeric_limits::max()); } } diff --git a/dCommon/dEnums/StringifiedEnum.h b/dCommon/dEnums/StringifiedEnum.h index 30cffd22..ee7402ec 100644 --- a/dCommon/dEnums/StringifiedEnum.h +++ b/dCommon/dEnums/StringifiedEnum.h @@ -7,19 +7,19 @@ namespace StringifiedEnum { template const std::string_view ToString(const T e) { + static_assert(std::is_enum_v, "Not an enum"); // Check type + constexpr auto sv = &magic_enum::enum_entries(); std::string_view output; const auto it = std::lower_bound( sv->begin(), sv->end(), e, - [&](const std::pair& lhs, const T rhs) - { return lhs.first < rhs; } + [&](const std::pair& lhs, const T rhs) { return lhs.first < rhs; } ); if (it != sv->end() && it->first == e) { output = it->second; - } - else { + } else { output = "UNKNOWN"; } return output; diff --git a/dGame/Entity.cpp b/dGame/Entity.cpp index 38960d70..cbbbb4d4 100644 --- a/dGame/Entity.cpp +++ b/dGame/Entity.cpp @@ -336,7 +336,7 @@ void Entity::Initialize() { CDDestructibleComponentTable* destCompTable = CDClientManager::Instance().GetTable(); std::vector destCompData = destCompTable->Query([=](CDDestructibleComponent entry) { return (entry.id == componentID); }); - + bool isSmashable = GetVarAs(u"is_smashable") != 0; if (buffComponentID > 0 || collectibleComponentID > 0 || isSmashable) { DestroyableComponent* comp = AddComponent(); @@ -543,8 +543,7 @@ void Entity::Initialize() { // Known bug with moving platform in FV that casues it to build at the end instead of the start. // This extends the smash time so players can ride up the lift. - if (m_TemplateID == 9483) - { + if (m_TemplateID == 9483) { rebuildComponent->SetResetTime(rebuildComponent->GetResetTime() + 25); } } diff --git a/dGame/dBehaviors/BasicAttackBehavior.cpp b/dGame/dBehaviors/BasicAttackBehavior.cpp index 36615a9f..914e67a5 100644 --- a/dGame/dBehaviors/BasicAttackBehavior.cpp +++ b/dGame/dBehaviors/BasicAttackBehavior.cpp @@ -22,7 +22,6 @@ void BasicAttackBehavior::Handle(BehaviorContext* context, RakNet::BitStream* bi if (entity->IsPlayer() && !this->m_DontApplyImmune) { const float immunityTime = Game::zoneManager->GetWorldConfig()->globalImmunityTime; destroyableComponent->SetDamageCooldownTimer(immunityTime); - LOG_DEBUG("Target targetEntity %llu took damage, setting damage cooldown timer to %f s", branch.target, immunityTime); } } @@ -188,11 +187,7 @@ void BasicAttackBehavior::DoBehaviorCalculation(BehaviorContext* context, RakNet return; } - const float immunityTime = Game::zoneManager->GetWorldConfig()->globalImmunityTime; - LOG_DEBUG("Damage cooldown timer currently %f s", destroyableComponent->GetDamageCooldownTimer()); - - const bool isImmune = (destroyableComponent->IsImmune()) || (destroyableComponent->IsCooldownImmune()); - + const bool isImmune = destroyableComponent->IsImmune() || destroyableComponent->IsCooldownImmune(); bitStream->Write(isImmune); if (isImmune) { @@ -219,8 +214,7 @@ void BasicAttackBehavior::DoBehaviorCalculation(BehaviorContext* context, RakNet //Handle player damage cooldown if (isSuccess && targetEntity->IsPlayer() && !this->m_DontApplyImmune) { - destroyableComponent->SetDamageCooldownTimer(immunityTime); - LOG_DEBUG("Target targetEntity %llu took damage, setting damage cooldown timer to %f s", branch.target, immunityTime); + destroyableComponent->SetDamageCooldownTimer(Game::zoneManager->GetWorldConfig()->globalImmunityTime); } eBasicAttackSuccessTypes successState = eBasicAttackSuccessTypes::FAILIMMUNE; diff --git a/dGame/dComponents/DestroyableComponent.cpp b/dGame/dComponents/DestroyableComponent.cpp index ce3743e8..8c5321f4 100644 --- a/dGame/dComponents/DestroyableComponent.cpp +++ b/dGame/dComponents/DestroyableComponent.cpp @@ -557,7 +557,7 @@ void DestroyableComponent::Damage(uint32_t damage, const LWOOBJID source, uint32 } if (IsImmune() || IsCooldownImmune()) { - LOG_DEBUG("Target targetEntity %llu is immune!", m_Parent->GetObjectID()); //Immune is succesfully proc'd + LOG_DEBUG("Target targetEntity %llu is immune!", m_Parent->GetObjectID()); return; } diff --git a/dGame/dGameMessages/GameMessageHandler.cpp b/dGame/dGameMessages/GameMessageHandler.cpp index fbaa913b..63b9d7e9 100644 --- a/dGame/dGameMessages/GameMessageHandler.cpp +++ b/dGame/dGameMessages/GameMessageHandler.cpp @@ -53,7 +53,7 @@ void GameMessageHandler::HandleMessage(RakNet::BitStream* inStream, const System return; } - if (messageID != eGameMessageType::READY_FOR_UPDATES) LOG_DEBUG("Received GM with ID and name: %4i, %s", messageID, StringifiedEnum::ToString(messageID).data()); + if (messageID != eGameMessageType::READY_FOR_UPDATES) LOG_DEBUG("Received GM with ID and name: %4i, %s", messageID, StringifiedEnum::ToString(messageID).data()); switch (messageID) { @@ -694,7 +694,7 @@ void GameMessageHandler::HandleMessage(RakNet::BitStream* inStream, const System GameMessages::SendVendorStatusUpdate(entity, sysAddr, true); break; default: - LOG_DEBUG("Received Unknown GM with ID: %4i, %s", messageID, StringifiedEnum::ToString(messageID).data()); + LOG_DEBUG("Received Unknown GM with ID: %4i, %s", messageID, StringifiedEnum::ToString(messageID).data()); break; } } diff --git a/dGame/dGameMessages/GameMessages.cpp b/dGame/dGameMessages/GameMessages.cpp index 9d903b3b..09f9fdbb 100644 --- a/dGame/dGameMessages/GameMessages.cpp +++ b/dGame/dGameMessages/GameMessages.cpp @@ -111,7 +111,7 @@ void GameMessages::SendFireEventClientSide(const LWOOBJID& objectID, const Syste uint32_t argSize = args.size(); bitStream.Write(argSize); for (uint32_t k = 0; k < argSize; k++) { - bitStream.Write(static_cast(args[k])); + bitStream.Write(args[k]); } bitStream.Write(object); bitStream.Write0(); @@ -221,13 +221,13 @@ void GameMessages::SendInvalidZoneTransferList(Entity* entity, const SystemAddre uint32_t CustomerFeedbackURLLength = feedbackURL.size(); bitStream.Write(CustomerFeedbackURLLength); for (uint32_t k = 0; k < CustomerFeedbackURLLength; k++) { - bitStream.Write(static_cast(feedbackURL[k])); + bitStream.Write(feedbackURL[k]); } uint32_t InvalidMapTransferListLength = invalidMapTransferList.size(); bitStream.Write(InvalidMapTransferListLength); for (uint32_t k = 0; k < InvalidMapTransferListLength; k++) { - bitStream.Write(static_cast(invalidMapTransferList[k])); + bitStream.Write(invalidMapTransferList[k]); } bitStream.Write(feedbackOnExit); @@ -318,17 +318,17 @@ void GameMessages::SendPlayNDAudioEmitter(Entity* entity, const SystemAddress& s CMSGHEADER; bitStream.Write(entity->GetObjectID()); - bitStream.Write((uint16_t)eGameMessageType::PLAY_ND_AUDIO_EMITTER); + bitStream.Write(eGameMessageType::PLAY_ND_AUDIO_EMITTER); bitStream.Write0(); bitStream.Write0(); uint32_t length = audioGUID.size(); bitStream.Write(length); for (uint32_t k = 0; k < length; k++) { - bitStream.Write(static_cast(audioGUID[k])); + bitStream.Write(audioGUID[k]); } - bitStream.Write(uint32_t(0)); + bitStream.Write(0); bitStream.Write0(); bitStream.Write0(); @@ -375,7 +375,7 @@ void GameMessages::SendPlatformResync(Entity* entity, const SystemAddress& sysAd } bitStream.Write(entity->GetObjectID()); - bitStream.Write((uint16_t)eGameMessageType::PLATFORM_RESYNC); + bitStream.Write(eGameMessageType::PLATFORM_RESYNC); bool bReverse = false; int eCommand = 0; @@ -432,7 +432,7 @@ void GameMessages::SendChatModeUpdate(const LWOOBJID& objectID, eGameMasterLevel CBITSTREAM; CMSGHEADER; bitStream.Write(objectID); - bitStream.Write((uint16_t)eGameMessageType::UPDATE_CHAT_MODE); + bitStream.Write(eGameMessageType::UPDATE_CHAT_MODE); bitStream.Write(level); SEND_PACKET_BROADCAST; } @@ -441,7 +441,7 @@ void GameMessages::SendGMLevelBroadcast(const LWOOBJID& objectID, eGameMasterLev CBITSTREAM; CMSGHEADER; bitStream.Write(objectID); - bitStream.Write((uint16_t)eGameMessageType::SET_GM_LEVEL); + bitStream.Write(eGameMessageType::SET_GM_LEVEL); bitStream.Write1(); bitStream.Write(level); SEND_PACKET_BROADCAST; @@ -472,9 +472,9 @@ void GameMessages::SendAddItemToInventoryClientSync(Entity* entity, const System bitStream.Write(extraInfo.name.size()); if (extraInfo.name.size() > 0) { for (uint32_t i = 0; i < extraInfo.name.size(); ++i) { - bitStream.Write(static_cast(extraInfo.name[i])); + bitStream.Write(extraInfo.name[i]); } - bitStream.Write(static_cast(0x00)); + bitStream.Write(0x00); } bitStream.Write(item->GetLot()); @@ -511,7 +511,7 @@ void GameMessages::SendNotifyClientFlagChange(const LWOOBJID& objectID, uint32_t CMSGHEADER; bitStream.Write(objectID); - bitStream.Write((uint16_t)eGameMessageType::NOTIFY_CLIENT_FLAG_CHANGE); + bitStream.Write(eGameMessageType::NOTIFY_CLIENT_FLAG_CHANGE); bitStream.Write(bFlag); bitStream.Write(iFlagID); @@ -523,7 +523,7 @@ void GameMessages::SendChangeObjectWorldState(const LWOOBJID& objectID, eObjectW CMSGHEADER; bitStream.Write(objectID); - bitStream.Write((uint16_t)eGameMessageType::CHANGE_OBJECT_WORLD_STATE); + bitStream.Write(eGameMessageType::CHANGE_OBJECT_WORLD_STATE); bitStream.Write(state); if (sysAddr == UNASSIGNED_SYSTEM_ADDRESS) SEND_PACKET_BROADCAST @@ -578,7 +578,7 @@ void GameMessages::SendNotifyMissionTask(Entity* entity, const SystemAddress& sy CMSGHEADER; bitStream.Write(entity->GetObjectID()); - bitStream.Write((uint16_t)eGameMessageType::NOTIFY_MISSION_TASK); + bitStream.Write(eGameMessageType::NOTIFY_MISSION_TASK); bitStream.Write(missionID); bitStream.Write(taskMask); @@ -596,7 +596,7 @@ void GameMessages::SendModifyLEGOScore(Entity* entity, const SystemAddress& sysA CMSGHEADER; bitStream.Write(entity->GetObjectID()); - bitStream.Write((uint16_t)eGameMessageType::MODIFY_LEGO_SCORE); + bitStream.Write(eGameMessageType::MODIFY_LEGO_SCORE); bitStream.Write(score); bitStream.Write(sourceType != eLootSourceType::NONE); @@ -610,14 +610,14 @@ void GameMessages::SendUIMessageServerToSingleClient(Entity* entity, const Syste CMSGHEADER; bitStream.Write(entity->GetObjectID()); - bitStream.Write((uint16_t)eGameMessageType::UI_MESSAGE_SERVER_TO_SINGLE_CLIENT); + bitStream.Write(eGameMessageType::UI_MESSAGE_SERVER_TO_SINGLE_CLIENT); bitStream.Write(args); uint32_t strMessageNameLength = message.size(); bitStream.Write(strMessageNameLength); for (uint32_t k = 0; k < strMessageNameLength; k++) { - bitStream.Write(static_cast(message[k])); + bitStream.Write(message[k]); } SEND_PACKET; @@ -629,14 +629,14 @@ void GameMessages::SendUIMessageServerToAllClients(const std::string& message, A LWOOBJID empty = 0; bitStream.Write(empty); - bitStream.Write((uint16_t)eGameMessageType::UI_MESSAGE_SERVER_TO_ALL_CLIENTS); + bitStream.Write(eGameMessageType::UI_MESSAGE_SERVER_TO_ALL_CLIENTS); bitStream.Write(args); uint32_t strMessageNameLength = message.size(); bitStream.Write(strMessageNameLength); for (uint32_t k = 0; k < strMessageNameLength; k++) { - bitStream.Write(static_cast(message[k])); + bitStream.Write(message[k]); } SEND_PACKET_BROADCAST; @@ -647,11 +647,11 @@ void GameMessages::SendPlayEmbeddedEffectOnAllClientsNearObject(Entity* entity, CMSGHEADER; bitStream.Write(entity->GetObjectID()); - bitStream.Write((uint16_t)eGameMessageType::PLAY_EMBEDDED_EFFECT_ON_ALL_CLIENTS_NEAR_OBJECT); + bitStream.Write(eGameMessageType::PLAY_EMBEDDED_EFFECT_ON_ALL_CLIENTS_NEAR_OBJECT); - bitStream.Write(static_cast(effectName.length())); + bitStream.Write(effectName.length()); for (uint32_t k = 0; k < effectName.length(); k++) { - bitStream.Write(static_cast(effectName[k])); + bitStream.Write(effectName[k]); } bitStream.Write(fromObjectID); bitStream.Write(radius); @@ -668,7 +668,7 @@ void GameMessages::SendPlayFXEffect(const LWOOBJID& entity, int32_t effectID, co CMSGHEADER; bitStream.Write(entity); - bitStream.Write((uint16_t)eGameMessageType::PLAY_FX_EFFECT); + bitStream.Write(eGameMessageType::PLAY_FX_EFFECT); bitStream.Write(effectID != -1); if (effectID != -1) bitStream.Write(effectID); @@ -716,22 +716,22 @@ void GameMessages::SendBroadcastTextToChatbox(Entity* entity, const SystemAddres CMSGHEADER; bitStream.Write(entity->GetObjectID()); - bitStream.Write((uint16_t)eGameMessageType::BROADCAST_TEXT_TO_CHATBOX); + bitStream.Write(eGameMessageType::BROADCAST_TEXT_TO_CHATBOX); LWONameValue attribs; attribs.name = attrs; attribs.length = attrs.size(); - bitStream.Write(static_cast(attribs.length)); + bitStream.Write(attribs.length); for (uint32_t i = 0; i < attribs.length; ++i) { - bitStream.Write(static_cast(attribs.name[i])); + bitStream.Write(attribs.name[i]); } - bitStream.Write(static_cast(0x00)); // Null Terminator + bitStream.Write(0x00); // Null Terminator uint32_t wsTextLength = wsText.size(); bitStream.Write(wsTextLength); for (uint32_t k = 0; k < wsTextLength; k++) { - bitStream.Write(static_cast(wsText[k])); + bitStream.Write(wsText[k]); } SEND_PACKET_BROADCAST; @@ -772,7 +772,7 @@ void GameMessages::SendRebuildNotifyState(Entity* entity, eRebuildState prevStat CMSGHEADER; bitStream.Write(entity->GetObjectID()); - bitStream.Write((uint16_t)eGameMessageType::REBUILD_NOTIFY_STATE); + bitStream.Write(eGameMessageType::REBUILD_NOTIFY_STATE); bitStream.Write(prevState); bitStream.Write(state); @@ -786,7 +786,7 @@ void GameMessages::SendEnableRebuild(Entity* entity, bool enable, bool fail, boo CMSGHEADER; bitStream.Write(entity->GetObjectID()); - bitStream.Write((uint16_t)eGameMessageType::ENABLE_REBUILD); + bitStream.Write(eGameMessageType::ENABLE_REBUILD); bitStream.Write(enable); bitStream.Write(fail); @@ -806,7 +806,7 @@ void GameMessages::SendTerminateInteraction(const LWOOBJID& objectID, eTerminate CMSGHEADER; bitStream.Write(objectID); - bitStream.Write((uint16_t)eGameMessageType::TERMINATE_INTERACTION); + bitStream.Write(eGameMessageType::TERMINATE_INTERACTION); bitStream.Write(terminator); bitStream.Write(type); @@ -842,7 +842,7 @@ void GameMessages::SendDie(Entity* entity, const LWOOBJID& killerID, const LWOOB bitStream.Write(entity->GetObjectID()); - bitStream.Write((uint16_t)eGameMessageType::DIE); + bitStream.Write(eGameMessageType::DIE); bitStream.Write(bClientDeath); bitStream.Write(bSpawnLoot); @@ -853,7 +853,7 @@ void GameMessages::SendDie(Entity* entity, const LWOOBJID& killerID, const LWOOB uint32_t deathTypeLength = deathType.size(); bitStream.Write(deathTypeLength); for (uint32_t k = 0; k < deathTypeLength; k++) { - bitStream.Write(static_cast(deathType[k])); + bitStream.Write(deathType[k]); } bitStream.Write(directionRelative_AngleXZ); @@ -976,7 +976,7 @@ void GameMessages::SendStop2DAmbientSound(Entity* entity, bool force, std::strin CMSGHEADER; bitStream.Write(entity->GetObjectID()); - bitStream.Write((uint16_t)eGameMessageType::STOP2_D_AMBIENT_SOUND); + bitStream.Write(eGameMessageType::STOP2_D_AMBIENT_SOUND); uint32_t audioGUIDSize = audioGUID.size(); @@ -984,7 +984,7 @@ void GameMessages::SendStop2DAmbientSound(Entity* entity, bool force, std::strin bitStream.Write(audioGUIDSize); for (uint32_t k = 0; k < audioGUIDSize; k++) { - bitStream.Write(static_cast(audioGUID[k])); + bitStream.Write(audioGUID[k]); } bitStream.Write(result); @@ -999,13 +999,12 @@ void GameMessages::SendPlay2DAmbientSound(Entity* entity, std::string audioGUID, CMSGHEADER; bitStream.Write(entity->GetObjectID()); - bitStream.Write((uint16_t)eGameMessageType::PLAY2_D_AMBIENT_SOUND); - + bitStream.Write(eGameMessageType::PLAY2_D_AMBIENT_SOUND); uint32_t audioGUIDSize = audioGUID.size(); bitStream.Write(audioGUIDSize); for (uint32_t k = 0; k < audioGUIDSize; k++) { - bitStream.Write(static_cast(audioGUID[k])); + bitStream.Write(audioGUID[k]); } bitStream.Write(result); @@ -1018,7 +1017,7 @@ void GameMessages::SendSetNetworkScriptVar(Entity* entity, const SystemAddress& CMSGHEADER; bitStream.Write(entity->GetObjectID()); - bitStream.Write((uint16_t)eGameMessageType::SCRIPT_NETWORK_VAR_UPDATE); + bitStream.Write(eGameMessageType::SCRIPT_NETWORK_VAR_UPDATE); // FIXME: this is a bad place to need to do a conversion because we have no clue whether data is utf8 or plain ascii // an this has performance implications @@ -1027,9 +1026,9 @@ void GameMessages::SendSetNetworkScriptVar(Entity* entity, const SystemAddress& bitStream.Write(dataSize); for (auto value : u16Data) { - bitStream.Write(uint16_t(value)); + bitStream.Write(value); } - if (dataSize > 0) bitStream.Write(uint16_t(0)); + if (dataSize > 0) bitStream.Write(0); if (sysAddr == UNASSIGNED_SYSTEM_ADDRESS) SEND_PACKET_BROADCAST; SEND_PACKET; @@ -1144,16 +1143,16 @@ void GameMessages::SendPlayerReachedRespawnCheckpoint(Entity* entity, const NiPo CMSGHEADER; bitStream.Write(entity->GetObjectID()); - bitStream.Write((uint16_t)eGameMessageType::PLAYER_REACHED_RESPAWN_CHECKPOINT); + bitStream.Write(eGameMessageType::PLAYER_REACHED_RESPAWN_CHECKPOINT); bitStream.Write(position.x); bitStream.Write(position.y); bitStream.Write(position.z); - const bool isNotIdentity = rotation != NiQuaternion::IDENTITY; - bitStream.Write(isNotIdentity); - - if (isNotIdentity) { + const bool bIsNotIdentity = rotation != NiQuaternion::IDENTITY; + bitStream.Write(bIsNotIdentity); + + if (bIsNotIdentity) { bitStream.Write(rotation.w); bitStream.Write(rotation.x); bitStream.Write(rotation.y); @@ -1176,7 +1175,7 @@ void GameMessages::SendAddSkill(Entity* entity, TSkillID skillID, BehaviorSlot s CMSGHEADER; bitStream.Write(entity->GetObjectID()); - bitStream.Write((uint16_t)eGameMessageType::ADD_SKILL); + bitStream.Write(eGameMessageType::ADD_SKILL); bitStream.Write(AICombatWeight != 0); if (AICombatWeight != 0) bitStream.Write(AICombatWeight); @@ -1345,8 +1344,8 @@ void GameMessages::SendRemoveItemFromInventory(Entity* entity, const SystemAddre bitStream.Write(eInvType); bitStream.Write1(); bitStream.Write(eLootTypeSource); - bitStream.Write(static_cast(0)); //extra info - //bitStream.Write(static_cast(0)); //extra info + bitStream.Write(0); //extra info + //bitStream.Write(0); //extra info bitStream.Write(forceDeletion); bitStream.Write0(); bitStream.Write1(); @@ -1469,11 +1468,11 @@ void GameMessages::SendMatchUpdate(Entity* entity, const SystemAddress& sysAddr, bitStream.Write(entity->GetObjectID()); bitStream.Write(eGameMessageType::MATCH_UPDATE); - bitStream.Write(uint32_t(data.size())); + bitStream.Write(data.size()); for (char character : data) { - bitStream.Write(uint16_t(character)); + bitStream.Write(character); } - if (data.size() > 0) bitStream.Write(uint16_t(0)); + if (data.size() > 0) bitStream.Write(0); bitStream.Write(type); SEND_PACKET; @@ -1557,7 +1556,7 @@ void GameMessages::NotifyLevelRewards(LWOOBJID objectID, const SystemAddress& sy CMSGHEADER; bitStream.Write(objectID); - bitStream.Write((uint16_t)eGameMessageType::NOTIFY_LEVEL_REWARDS); + bitStream.Write(eGameMessageType::NOTIFY_LEVEL_REWARDS); bitStream.Write(level); bitStream.Write(sending_rewards); @@ -1743,7 +1742,7 @@ void GameMessages::SendSetRailMovement(const LWOOBJID& objectID, bool pathGoForw bitStream.Write(pathGoForward); - bitStream.Write(uint32_t(pathName.size())); + bitStream.Write(pathName.size()); for (auto character : pathName) { bitStream.Write(character); } @@ -1782,14 +1781,14 @@ void GameMessages::SendStartRailMovement(const LWOOBJID& objectID, std::u16strin bitStream.Write(cameraLocked); bitStream.Write(collisionEnabled); - bitStream.Write(uint32_t(loopSound.size())); + bitStream.Write(loopSound.size()); for (auto character : loopSound) { bitStream.Write(character); } bitStream.Write(goForward); - bitStream.Write(uint32_t(pathName.size())); + bitStream.Write(pathName.size()); for (auto character : pathName) { bitStream.Write(character); } @@ -1812,12 +1811,12 @@ void GameMessages::SendStartRailMovement(const LWOOBJID& objectID, std::u16strin bitStream.Write(railActivatorObjectID); } - bitStream.Write(uint32_t(startSound.size())); + bitStream.Write(startSound.size()); for (auto character : startSound) { bitStream.Write(character); } - bitStream.Write(uint32_t(stopSound.size())); + bitStream.Write(stopSound.size()); for (auto character : stopSound) { bitStream.Write(character); } @@ -1835,7 +1834,7 @@ void GameMessages::SendNotifyClientObject(const LWOOBJID& objectID, std::u16stri bitStream.Write(objectID); bitStream.Write(eGameMessageType::NOTIFY_CLIENT_OBJECT); - bitStream.Write(uint32_t(name.size())); + bitStream.Write(name.size()); for (auto character : name) { bitStream.Write(character); } @@ -1846,7 +1845,7 @@ void GameMessages::SendNotifyClientObject(const LWOOBJID& objectID, std::u16stri bitStream.Write(paramObj); - bitStream.Write(uint32_t(paramStr.size())); + bitStream.Write(paramStr.size()); for (auto character : paramStr) { bitStream.Write(character); } @@ -1864,7 +1863,7 @@ void GameMessages::SendNotifyClientZoneObject(const LWOOBJID& objectID, const st bitStream.Write(objectID); bitStream.Write(eGameMessageType::NOTIFY_CLIENT_ZONE_OBJECT); - bitStream.Write(uint32_t(name.size())); + bitStream.Write(name.size()); for (const auto& character : name) { bitStream.Write(character); } @@ -1873,7 +1872,7 @@ void GameMessages::SendNotifyClientZoneObject(const LWOOBJID& objectID, const st bitStream.Write(param2); bitStream.Write(paramObj); - bitStream.Write(uint32_t(paramStr.size())); + bitStream.Write(paramStr.size()); for (const auto& character : paramStr) { bitStream.Write(character); } @@ -1890,9 +1889,9 @@ void GameMessages::SendNotifyClientFailedPrecondition(LWOOBJID objectId, const S bitStream.Write(objectId); bitStream.Write(eGameMessageType::NOTIFY_CLIENT_FAILED_PRECONDITION); - bitStream.Write(uint32_t(failedReason.size())); + bitStream.Write(failedReason.size()); for (uint16_t character : failedReason) { - bitStream.Write(uint16_t(character)); + bitStream.Write(character); } bitStream.Write(preconditionID); @@ -2016,7 +2015,7 @@ void GameMessages::SendLockNodeRotation(Entity* entity, std::string nodeName) { bitStream.Write(entity->GetObjectID()); bitStream.Write(eGameMessageType::LOCK_NODE_ROTATION); - bitStream.Write(uint32_t(nodeName.size())); + bitStream.Write(nodeName.size()); for (char character : nodeName) { bitStream.Write(character); } @@ -2051,7 +2050,7 @@ void GameMessages::SendGetModelsOnProperty(LWOOBJID objectId, std::map(models.size())); + bitStream.Write(models.size()); for (const auto& pair : models) { bitStream.Write(pair.first); @@ -2433,7 +2432,7 @@ void GameMessages::HandleBBBLoadItemRequest(RakNet::BitStream* inStream, Entity* void GameMessages::SendBlueprintLoadItemResponse(const SystemAddress& sysAddr, bool success, LWOOBJID oldItemId, LWOOBJID newItemId) { CBITSTREAM; BitStreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::BLUEPRINT_LOAD_RESPONSE_ITEMID); - bitStream.Write(static_cast(success)); + bitStream.Write(success); bitStream.Write(oldItemId); bitStream.Write(newItemId); SEND_PACKET; @@ -3058,7 +3057,7 @@ void GameMessages::SendNotifyObject(LWOOBJID objectId, LWOOBJID objIDSender, std bitStream.Write(eGameMessageType::NOTIFY_OBJECT); bitStream.Write(objIDSender); - bitStream.Write(static_cast(name.size())); + bitStream.Write(name.size()); for (const auto character : name) { bitStream.Write(character); } @@ -3115,7 +3114,7 @@ void GameMessages::SendServerTradeInvite(LWOOBJID objectId, bool bNeedInvitePopU bitStream.Write(bNeedInvitePopUp); bitStream.Write(i64Requestor); - bitStream.Write(static_cast(wsName.size())); + bitStream.Write(wsName.size()); for (const auto character : wsName) { bitStream.Write(character); } @@ -3133,7 +3132,7 @@ void GameMessages::SendServerTradeInitialReply(LWOOBJID objectId, LWOOBJID i64In bitStream.Write(i64Invitee); bitStream.Write(resultType); - bitStream.Write(static_cast(wsName.size())); + bitStream.Write(wsName.size()); for (const auto character : wsName) { bitStream.Write(character); } @@ -3151,7 +3150,7 @@ void GameMessages::SendServerTradeFinalReply(LWOOBJID objectId, bool bResult, LW bitStream.Write(bResult); bitStream.Write(i64Invitee); - bitStream.Write(static_cast(wsName.size())); + bitStream.Write(wsName.size()); for (const auto character : wsName) { bitStream.Write(character); } @@ -3193,7 +3192,7 @@ void GameMessages::SendServerTradeUpdate(LWOOBJID objectId, uint64_t coins, cons bitStream.Write(false); bitStream.Write(coins); - bitStream.Write(static_cast(items.size())); + bitStream.Write(items.size()); for (const auto& item : items) { bitStream.Write(item.itemId); @@ -3402,7 +3401,7 @@ void GameMessages::SendNotifyPetTamingPuzzleSelected(LWOOBJID objectId, const st bitStream.Write(objectId); bitStream.Write(eGameMessageType::NOTIFY_TAMING_PUZZLE_SELECTED); - bitStream.Write(static_cast(bricks.size())); + bitStream.Write(bricks.size()); for (const auto& brick : bricks) { bitStream.Write(brick.designerID); bitStream.Write(brick.materialID); @@ -3451,7 +3450,7 @@ void GameMessages::SendAddPetToPlayer(LWOOBJID objectId, int32_t iElementalType, bitStream.Write(eGameMessageType::ADD_PET_TO_PLAYER); bitStream.Write(iElementalType); - bitStream.Write(static_cast(name.size())); + bitStream.Write(name.size()); for (const auto character : name) { bitStream.Write(character); } @@ -3585,7 +3584,7 @@ void GameMessages::SendSetPetName(LWOOBJID objectId, std::u16string name, LWOOBJ bitStream.Write(objectId); bitStream.Write(eGameMessageType::SET_PET_NAME); - bitStream.Write(static_cast(name.size())); + bitStream.Write(name.size()); for (const auto character : name) { bitStream.Write(character); } @@ -3624,12 +3623,12 @@ void GameMessages::SendPetNameChanged(LWOOBJID objectId, int32_t moderationStatu bitStream.Write(moderationStatus); - bitStream.Write(static_cast(name.size())); + bitStream.Write(name.size()); for (const auto character : name) { bitStream.Write(character); } - bitStream.Write(static_cast(ownerName.size())); + bitStream.Write(ownerName.size()); for (const auto character : ownerName) { bitStream.Write(character); } @@ -3907,19 +3906,19 @@ void GameMessages::SendDisplayMessageBox(LWOOBJID objectId, bool bShow, LWOOBJID bitStream.Write(bShow); bitStream.Write(callbackClient); - bitStream.Write(static_cast(identifier.size())); + bitStream.Write(identifier.size()); for (const auto character : identifier) { bitStream.Write(character); } bitStream.Write(imageID); - bitStream.Write(static_cast(text.size())); + bitStream.Write(text.size()); for (const auto character : text) { bitStream.Write(character); } - bitStream.Write(static_cast(userData.size())); + bitStream.Write(userData.size()); for (const auto character : userData) { bitStream.Write(character); } @@ -3936,7 +3935,7 @@ void GameMessages::SendDisplayChatBubble(LWOOBJID objectId, const std::u16string bitStream.Write(objectId); bitStream.Write(eGameMessageType::DISPLAY_CHAT_BUBBLE); - bitStream.Write(static_cast(text.size())); + bitStream.Write(text.size()); for (const auto character : text) { bitStream.Write(character); } @@ -4127,8 +4126,7 @@ void GameMessages::HandleRequestDie(RakNet::BitStream* inStream, Entity* entity, } racingControlComponent->OnRequestDie(entity); - } - else { + } else { auto* destroyableComponent = entity->GetComponent(); if (!destroyableComponent) return; @@ -4245,7 +4243,7 @@ void GameMessages::SendModuleAssemblyDBDataForClient(LWOOBJID objectId, LWOOBJID bitStream.Write(assemblyID); - bitStream.Write(static_cast(data.size())); + bitStream.Write(data.size()); for (auto character : data) { bitStream.Write(character); } @@ -4390,7 +4388,7 @@ void GameMessages::SendNotifyRacingClient(LWOOBJID objectId, int32_t eventType, bitStream.Write(paramObj); - bitStream.Write(static_cast(paramStr.size())); + bitStream.Write(paramStr.size()); for (auto character : paramStr) { bitStream.Write(character); } @@ -4585,7 +4583,7 @@ void GameMessages::SendShowActivityCountdown(LWOOBJID objectId, bool bPlayAdditi bitStream.Write(bPlayCountdownSound); - bitStream.Write(static_cast(sndName.size())); + bitStream.Write(sndName.size()); for (auto character : sndName) { bitStream.Write(character); } @@ -5601,7 +5599,7 @@ void GameMessages::HandleModularBuildFinish(RakNet::BitStream* inStream, Entity* if (entity->GetLOT() != 9980 || Game::server->GetZoneID() != 1200) { if (missionComponent != nullptr) { missionComponent->Progress(eMissionTaskType::SCRIPT, entity->GetLOT(), entity->GetObjectID()); - if (count >= 7 && everyPieceSwapped) missionComponent->Progress(eMissionTaskType::RACING, LWOOBJID_EMPTY, (LWOOBJID)eRacingTaskParam::MODULAR_BUILDING); + if (count >= 7 && everyPieceSwapped) missionComponent->Progress(eMissionTaskType::RACING, LWOOBJID_EMPTY, static_cast(eRacingTaskParam::MODULAR_BUILDING)); } } diff --git a/dNet/MasterPackets.cpp b/dNet/MasterPackets.cpp index 03330cc9..624b92ad 100644 --- a/dNet/MasterPackets.cpp +++ b/dNet/MasterPackets.cpp @@ -31,7 +31,7 @@ void MasterPackets::SendZoneTransferRequest(dServer* server, uint64_t requestID, BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::REQUEST_ZONE_TRANSFER); bitStream.Write(requestID); - bitStream.Write(static_cast(mythranShift)); + bitStream.Write(mythranShift); bitStream.Write(zoneID); bitStream.Write(cloneID); @@ -58,7 +58,7 @@ void MasterPackets::SendZoneRequestPrivate(dServer* server, uint64_t requestID, BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::REQUEST_PRIVATE_ZONE); bitStream.Write(requestID); - bitStream.Write(static_cast(mythranShift)); + bitStream.Write(mythranShift); bitStream.Write(password.size()); for (auto character : password) { @@ -83,11 +83,11 @@ void MasterPackets::SendZoneTransferResponse(dServer* server, const SystemAddres BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::REQUEST_ZONE_TRANSFER_RESPONSE); bitStream.Write(requestID); - bitStream.Write(static_cast(mythranShift)); + bitStream.Write(mythranShift); bitStream.Write(zoneID); bitStream.Write(zoneInstance); bitStream.Write(zoneClone); - bitStream.Write(static_cast(serverPort)); + bitStream.Write(serverPort); bitStream.Write(LUString(serverIP, static_cast(serverIP.size() + 1))); server->Send(&bitStream, sysAddr, false); diff --git a/dNet/WorldPackets.cpp b/dNet/WorldPackets.cpp index d6de9186..1ee98c83 100644 --- a/dNet/WorldPackets.cpp +++ b/dNet/WorldPackets.cpp @@ -21,19 +21,19 @@ void WorldPackets::SendLoadStaticZone(const SystemAddress& sysAddr, float x, flo BitStreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::LOAD_STATIC_ZONE); auto zone = Game::zoneManager->GetZone()->GetZoneID(); - bitStream.Write(static_cast(zone.GetMapID())); - bitStream.Write(static_cast(zone.GetInstanceID())); - //bitStream.Write(static_cast(zone.GetCloneID())); + bitStream.Write(zone.GetMapID()); + bitStream.Write(zone.GetInstanceID()); + //bitStream.Write(zone.GetCloneID()); bitStream.Write(0); bitStream.Write(checksum); - bitStream.Write(static_cast(0)); // ?? + bitStream.Write(0); // ?? bitStream.Write(x); bitStream.Write(y); bitStream.Write(z); - bitStream.Write(static_cast(0)); // Change this to eventually use 4 on activity worlds + bitStream.Write(0); // Change this to eventually use 4 on activity worlds SEND_PACKET; } @@ -45,18 +45,18 @@ void WorldPackets::SendCharacterList(const SystemAddress& sysAddr, User* user) { BitStreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::CHARACTER_LIST_RESPONSE); std::vector characters = user->GetCharacters(); - bitStream.Write(static_cast(characters.size())); - bitStream.Write(static_cast(0)); //character index in front, just picking 0 + bitStream.Write(characters.size()); + bitStream.Write(0); //character index in front, just picking 0 for (uint32_t i = 0; i < characters.size(); ++i) { bitStream.Write(characters[i]->GetObjectID()); - bitStream.Write(static_cast(0)); + bitStream.Write(0); bitStream.Write(LUWString(characters[i]->GetName())); bitStream.Write(LUWString(characters[i]->GetUnapprovedName())); - bitStream.Write(static_cast(characters[i]->GetNameRejected())); - bitStream.Write(static_cast(false)); + bitStream.Write(characters[i]->GetNameRejected()); + bitStream.Write(false); bitStream.Write(LUString("", 10)); @@ -70,16 +70,16 @@ void WorldPackets::SendCharacterList(const SystemAddress& sysAddr, User* user) { bitStream.Write(characters[i]->GetEyebrows()); bitStream.Write(characters[i]->GetEyes()); bitStream.Write(characters[i]->GetMouth()); - bitStream.Write(static_cast(0)); + bitStream.Write(0); - bitStream.Write(static_cast(characters[i]->GetZoneID())); - bitStream.Write(static_cast(characters[i]->GetZoneInstance())); + bitStream.Write(characters[i]->GetZoneID()); + bitStream.Write(characters[i]->GetZoneInstance()); bitStream.Write(characters[i]->GetZoneClone()); bitStream.Write(characters[i]->GetLastLogin()); const auto& equippedItems = characters[i]->GetEquippedItems(); - bitStream.Write(static_cast(equippedItems.size())); + bitStream.Write(equippedItems.size()); for (uint32_t j = 0; j < equippedItems.size(); ++j) { bitStream.Write(equippedItems[j]); @@ -106,7 +106,7 @@ void WorldPackets::SendCharacterRenameResponse(const SystemAddress& sysAddr, eRe void WorldPackets::SendCharacterDeleteResponse(const SystemAddress& sysAddr, bool response) { RakNet::BitStream bitStream; BitStreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::DELETE_CHARACTER_RESPONSE); - bitStream.Write(static_cast(response)); + bitStream.Write(response); SEND_PACKET; } @@ -115,8 +115,8 @@ void WorldPackets::SendTransferToWorld(const SystemAddress& sysAddr, const std:: BitStreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::TRANSFER_TO_WORLD); bitStream.Write(LUString(serverIP)); - bitStream.Write(static_cast(serverPort)); - bitStream.Write(static_cast(mythranShift)); + bitStream.Write(serverPort); + bitStream.Write(mythranShift); SEND_PACKET; } @@ -124,7 +124,7 @@ void WorldPackets::SendTransferToWorld(const SystemAddress& sysAddr, const std:: void WorldPackets::SendServerState(const SystemAddress& sysAddr) { RakNet::BitStream bitStream; BitStreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::SERVER_STATES); - bitStream.Write(static_cast(1)); //If the server is receiving this request, it probably is ready anyway. + bitStream.Write(1); //If the server is receiving this request, it probably is ready anyway. SEND_PACKET; } @@ -204,8 +204,8 @@ void WorldPackets::SendChatModerationResponse(const SystemAddress& sysAddr, bool bitStream.Write(unacceptedItems.empty()); // Is sentence ok? bitStream.Write(0x16); // Source ID, unknown - bitStream.Write(static_cast(requestID)); // request ID - bitStream.Write(static_cast(0)); // chat mode + bitStream.Write(requestID); // request ID + bitStream.Write(0); // chat mode bitStream.Write(LUWString(receiver, 42)); // receiver name diff --git a/dWorldServer/WorldServer.cpp b/dWorldServer/WorldServer.cpp index caa0dab3..078ea50f 100644 --- a/dWorldServer/WorldServer.cpp +++ b/dWorldServer/WorldServer.cpp @@ -217,7 +217,7 @@ int main(int argc, char** argv) { uint32_t chatPort = 1501; if (Game::config->GetValue("chat_server_port") != "") chatPort = std::atoi(Game::config->GetValue("chat_server_port").c_str()); - auto chatSock = SocketDescriptor(uint16_t(ourPort + 2), 0); + auto chatSock = SocketDescriptor(static_cast(ourPort + 2), 0); Game::chatServer = RakNetworkFactory::GetRakPeerInterface(); Game::chatServer->Startup(1, 30, &chatSock, 1); Game::chatServer->Connect(masterIP.c_str(), chatPort, "3.25 ND1", 8); @@ -1246,7 +1246,7 @@ void HandlePacket(Packet* packet) { default: const auto messageId = *reinterpret_cast(&packet->data[3]); - const std::string_view messageIdString = StringifiedEnum::ToString(messageId); + const std::string_view messageIdString = StringifiedEnum::ToString(messageId); LOG("Unknown world packet received: %4i, %s", messageId, messageIdString.data()); } } diff --git a/tests/dCommonTests/CMakeLists.txt b/tests/dCommonTests/CMakeLists.txt index d1025489..be23d866 100644 --- a/tests/dCommonTests/CMakeLists.txt +++ b/tests/dCommonTests/CMakeLists.txt @@ -1,6 +1,7 @@ set(DCOMMONTEST_SOURCES "AMFDeserializeTests.cpp" "Amf3Tests.cpp" + "CastUnderlyingTypeTests.cpp" "HeaderSkipTest.cpp" "TestCDFeatureGatingTable.cpp" "TestLDFFormat.cpp" diff --git a/tests/dCommonTests/CastUnderlyingTypeTests.cpp b/tests/dCommonTests/CastUnderlyingTypeTests.cpp new file mode 100644 index 00000000..9cdfcdd3 --- /dev/null +++ b/tests/dCommonTests/CastUnderlyingTypeTests.cpp @@ -0,0 +1,24 @@ +#include + +#include "GeneralUtils.h" + +#include "eGameMasterLevel.h" +#include "eGameMessageType.h" +#include "eWorldMessageType.h" + +#define ASSERT_TYPE_EQ(TYPE, ENUM)\ + ASSERT_TRUE(typeid(TYPE) == typeid(GeneralUtils::CastUnderlyingType(static_cast(0)))); + +#define ASSERT_TYPE_NE(TYPE, ENUM)\ + ASSERT_FALSE(typeid(TYPE) == typeid(GeneralUtils::CastUnderlyingType(static_cast(0)))); + +// Verify that the underlying enum types are being cast correctly +TEST(CastUnderlyingTypeTests, VerifyCastUnderlyingType) { + ASSERT_TYPE_EQ(uint8_t, eGameMasterLevel); + ASSERT_TYPE_EQ(uint16_t, eGameMessageType); + ASSERT_TYPE_EQ(uint32_t, eWorldMessageType) + + ASSERT_TYPE_NE(void, eGameMasterLevel); + ASSERT_TYPE_NE(void, eGameMessageType); + ASSERT_TYPE_NE(void, eWorldMessageType) +} diff --git a/tests/dCommonTests/dEnumsTests/MagicEnumTests.cpp b/tests/dCommonTests/dEnumsTests/MagicEnumTests.cpp index 48b192f6..2ad92914 100644 --- a/tests/dCommonTests/dEnumsTests/MagicEnumTests.cpp +++ b/tests/dCommonTests/dEnumsTests/MagicEnumTests.cpp @@ -20,7 +20,7 @@ // Test World Message Enum Reflection TEST(MagicEnumTest, eWorldMessageTypeTest) { Game::logger = new Logger("./MagicEnumTest_eWorldMessageTypeTest.log", true, true); - + ENUM_EQ(eWorldMessageType, 1, VALIDATION); ENUM_EQ(eWorldMessageType, 2, CHARACTER_LIST_REQUEST); ENUM_EQ(eWorldMessageType, 3, CHARACTER_CREATE_REQUEST); @@ -74,9 +74,9 @@ TEST(MagicEnumTest, eWorldMessageTypeTest) { // Test Game Message Enum Reflection TEST(MagicEnumTest, eGameMessageTypeTest) { - + Game::logger = new Logger("./MagicEnumTest_eGameMessageTypeTest.log", true, true); - + // Only doing the first and last 10 for the sake of my sanity ENUM_EQ(eGameMessageType, 0, GET_POSITION); ENUM_EQ(eGameMessageType, 1, GET_ROTATION);