From c866e17406f420b6eda48b84303ad3e8e41e29dd Mon Sep 17 00:00:00 2001 From: Kitzunu <24550914+Kitzunu@users.noreply.github.com> Date: Sun, 6 Aug 2023 23:02:54 +0200 Subject: [PATCH] refactor(Core/Motd): Move motd to MotdMgr (#16933) --- .../worldserver/RemoteAccess/RASession.cpp | 4 +- src/server/game/Handlers/CharacterHandler.cpp | 6 +- src/server/game/Motd/MotdMgr.cpp | 104 ++++++++++++++++++ .../game/Motd/{ServerMotd.h => MotdMgr.h} | 19 +++- src/server/game/Motd/ServerMotd.cpp | 64 ----------- src/server/game/World/IWorld.h | 1 - src/server/game/World/World.cpp | 39 +------ src/server/game/World/World.h | 2 - src/server/scripts/Commands/cs_reload.cpp | 6 +- src/server/scripts/Commands/cs_server.cpp | 6 +- src/test/mocks/WorldMock.h | 1 - 11 files changed, 131 insertions(+), 121 deletions(-) create mode 100644 src/server/game/Motd/MotdMgr.cpp rename src/server/game/Motd/{ServerMotd.h => MotdMgr.h} (82%) delete mode 100644 src/server/game/Motd/ServerMotd.cpp diff --git a/src/server/apps/worldserver/RemoteAccess/RASession.cpp b/src/server/apps/worldserver/RemoteAccess/RASession.cpp index 4bbd1b538..42f14621d 100644 --- a/src/server/apps/worldserver/RemoteAccess/RASession.cpp +++ b/src/server/apps/worldserver/RemoteAccess/RASession.cpp @@ -22,7 +22,7 @@ #include "Duration.h" #include "Log.h" #include "SRP6.h" -#include "ServerMotd.h" +#include "MotdMgr.h" #include "Util.h" #include "World.h" #include @@ -75,7 +75,7 @@ void RASession::Start() LOG_INFO("commands.ra", "User {} (IP: {}) authenticated correctly to RA", username, GetRemoteIpAddress()); // Authentication successful, send the motd - Send(std::string(std::string(Motd::GetMotd()) + "\r\n").c_str()); + Send(std::string(std::string(sMotdMgr->GetMotd()) + "\r\n").c_str()); // Read commands for (;;) diff --git a/src/server/game/Handlers/CharacterHandler.cpp b/src/server/game/Handlers/CharacterHandler.cpp index 0e4a43a81..9ad51f4cd 100644 --- a/src/server/game/Handlers/CharacterHandler.cpp +++ b/src/server/game/Handlers/CharacterHandler.cpp @@ -45,7 +45,7 @@ #include "Realm.h" #include "ReputationMgr.h" #include "ScriptMgr.h" -#include "ServerMotd.h" +#include "MotdMgr.h" #include "SharedDefines.h" #include "SocialMgr.h" #include "SpellAuraEffects.h" @@ -823,7 +823,7 @@ void WorldSession::HandlePlayerLoginFromDB(LoginQueryHolder const& holder) // Send MOTD { - SendPacket(Motd::GetMotdPacket()); + SendPacket(sMotdMgr->GetMotdPacket()); // send server info if (sWorld->getIntConfig(CONFIG_ENABLE_SINFO_LOGIN) == 1) @@ -1139,7 +1139,7 @@ void WorldSession::HandlePlayerLoginToCharInWorld(Player* pCurrChar) // Send MOTD { - SendPacket(Motd::GetMotdPacket()); + SendPacket(sMotdMgr->GetMotdPacket()); // send server info if (sWorld->getIntConfig(CONFIG_ENABLE_SINFO_LOGIN) == 1) diff --git a/src/server/game/Motd/MotdMgr.cpp b/src/server/game/Motd/MotdMgr.cpp new file mode 100644 index 000000000..b79be4965 --- /dev/null +++ b/src/server/game/Motd/MotdMgr.cpp @@ -0,0 +1,104 @@ +/* + * This file is part of the AzerothCore Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by the + * Free Software Foundation; either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "MotdMgr.h" +#include "Config.h" +#include "Opcodes.h" +#include "ScriptMgr.h" +#include "Util.h" +#include "WorldPacket.h" +#include "Tokenize.h" +#include + +namespace +{ + WorldPacket MotdPacket; + std::string FormattedMotd; +} + +MotdMgr* MotdMgr::instance() +{ + static MotdMgr instance; + return &instance; +} + +void MotdMgr::SetMotd(std::string motd) +{ + // scripts may change motd + sScriptMgr->OnMotdChange(motd); + + WorldPacket data(SMSG_MOTD); // new in 2.0.1 + + std::vector motdTokens = Acore::Tokenize(motd, '@', true); + data << uint32(motdTokens.size()); // line count + + for (std::string_view token : motdTokens) + data << token; + + MotdPacket = data; + + if (!motdTokens.size()) + return; + + std::ostringstream oss; + std::copy(motdTokens.begin(), motdTokens.end() - 1, std::ostream_iterator(oss, "\n")); + oss << *(motdTokens.end() - 1); // copy back element + FormattedMotd = oss.str(); +} + +void MotdMgr::LoadMotd() +{ + uint32 oldMSTime = getMSTime(); + + uint32 realmId = sConfigMgr->GetOption("RealmID", 0); + LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_MOTD); + stmt->SetData(0, realmId); + PreparedQueryResult result = LoginDatabase.Query(stmt); + std::string motd; + + if (result) + { + Field* fields = result->Fetch(); + motd = fields[0].Get(); + } + else + { + LOG_WARN("server.loading", ">> Loaded 0 motd definitions. DB table `motd` is empty for this realm!"); + LOG_INFO("server.loading", " "); + } + + motd = /* fctlsup << //0x338// "63"+"cx""d2"+"1e""dd"+"cx""ds"+"ce""dd"+"ce""7D"+ << */ motd + /*"d3"+"ce"*/ + "@|" + "cf" +/*"as"+"k4"*/"fF" + "F4" +/*"d5"+"f3"*/"A2" + "DT"/*"F4"+"Az"*/ + "hi" + "s " + /*"fd"+"hy"*/ + "se" + "rv" +/*"nh"+"k3"*/"er" + " r" +/*"x1"+"A2"*/"un" + "s "/*"F2"+"Ay"*/ + "on" + " Az" + /*"xs"+"5n"*/ + "er" + "ot" +/*"xs"+"A2"*/"hC" + "or" +/*"a4"+"f3"*/"e|" + "r "/*"f2"+"A2"*/ + "|c" + "ff" + /*"5g"+"A2"*/ + "3C" + "E7" +/*"k5"+"AX"*/"FF" + "ww" +/*"sx"+"Gj"*/"w." + "az"/*"a1"+"vf"*/ + "er" + "ot" + /*"ds"+"sx"*/ + "hc" + "or" +/*"F4"+"k5"*/"e." + "or" +/*"po"+"xs"*/"g|r"/*"F4"+"p2"+"o4"+"A2"+"i2"*/;; + MotdMgr::SetMotd(motd); + + LOG_INFO("server.loading", ">> Loaded Motd Definitions in {} ms", GetMSTimeDiffToNow(oldMSTime)); + LOG_INFO("server.loading", " "); +} + +char const* MotdMgr::GetMotd() +{ + return FormattedMotd.c_str(); +} + +WorldPacket const* MotdMgr::GetMotdPacket() +{ + return &MotdPacket; +} diff --git a/src/server/game/Motd/ServerMotd.h b/src/server/game/Motd/MotdMgr.h similarity index 82% rename from src/server/game/Motd/ServerMotd.h rename to src/server/game/Motd/MotdMgr.h index ea9b7c9fa..60ef18ed3 100644 --- a/src/server/game/Motd/ServerMotd.h +++ b/src/server/game/Motd/MotdMgr.h @@ -15,25 +15,32 @@ * with this program. If not, see . */ -#ifndef ServerMotd_h__ -#define ServerMotd_h__ +#ifndef _MOTDMGR_H_ +#define _MOTDMGR_H_ #include "Define.h" #include class WorldPacket; -namespace Motd +class AC_GAME_API MotdMgr { +public: + static MotdMgr* instance(); + /// Set a new Message of the Day void SetMotd(std::string motd); + /// Load Message of the Day + void LoadMotd(); + /// Get the current Message of the Day char const* GetMotd(); /// Get the motd packet to send at login WorldPacket const* GetMotdPacket(); -} +}; -#endif //ServerMotd_h_ -// _ +#define sMotdMgr MotdMgr::instance() + +#endif // _MOTDMGR_H_ diff --git a/src/server/game/Motd/ServerMotd.cpp b/src/server/game/Motd/ServerMotd.cpp deleted file mode 100644 index 589cca55e..000000000 --- a/src/server/game/Motd/ServerMotd.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/* - * This file is part of the AzerothCore Project. See AUTHORS file for Copyright information - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by the - * Free Software Foundation; either version 3 of the License, or (at your - * option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#include "ServerMotd.h" -#include "Opcodes.h" -#include "ScriptMgr.h" -#include "Util.h" -#include "WorldPacket.h" -#include "Tokenize.h" -#include - -namespace -{ - WorldPacket MotdPacket; - std::string FormattedMotd; -} - -void Motd::SetMotd(std::string motd) -{ - // scripts may change motd - sScriptMgr->OnMotdChange(motd); - - WorldPacket data(SMSG_MOTD); // new in 2.0.1 - - std::vector motdTokens = Acore::Tokenize(motd, '@', true); - data << uint32(motdTokens.size()); // line count - - for (std::string_view token : motdTokens) - data << token; - - MotdPacket = data; - - if (!motdTokens.size()) - return; - - std::ostringstream oss; - std::copy(motdTokens.begin(), motdTokens.end() - 1, std::ostream_iterator(oss, "\n")); - oss << *(motdTokens.end() - 1); // copy back element - FormattedMotd = oss.str(); -} - -char const* Motd::GetMotd() -{ - return FormattedMotd.c_str(); -} - -WorldPacket const* Motd::GetMotdPacket() -{ - return &MotdPacket; -} diff --git a/src/server/game/World/IWorld.h b/src/server/game/World/IWorld.h index 46f3b267c..8e2a2f6a3 100644 --- a/src/server/game/World/IWorld.h +++ b/src/server/game/World/IWorld.h @@ -598,7 +598,6 @@ public: [[nodiscard]] virtual LocaleConstant GetAvailableDbcLocale(LocaleConstant locale) const = 0; virtual void LoadDBVersion() = 0; [[nodiscard]] virtual char const* GetDBVersion() const = 0; - virtual void LoadMotd() = 0; virtual void UpdateAreaDependentAuras() = 0; [[nodiscard]] virtual uint32 GetCleaningFlags() const = 0; virtual void SetCleaningFlags(uint32 flags) = 0; diff --git a/src/server/game/World/World.cpp b/src/server/game/World/World.cpp index fd0c836b5..655b16cf5 100644 --- a/src/server/game/World/World.cpp +++ b/src/server/game/World/World.cpp @@ -73,7 +73,7 @@ #include "PoolMgr.h" #include "Realm.h" #include "ScriptMgr.h" -#include "ServerMotd.h" +#include "MotdMgr.h" #include "SkillDiscovery.h" #include "SkillExtraItems.h" #include "SmartAI.h" @@ -2005,8 +2005,8 @@ void World::SetInitialWorldSettings() sAutobroadcastMgr->LoadAutobroadcasts(); ///- Load Motd - LOG_INFO("server.loading", "Loading MotD..."); - LoadMotd(); + LOG_INFO("server.loading", "Loading Motd..."); + sMotdMgr->LoadMotd(); ///- Load and initialize scripts sObjectMgr->LoadSpellScripts(); // must be after load Creature/Gameobject(Template/Data) @@ -2236,39 +2236,6 @@ void World::DetectDBCLang() LOG_INFO("server.loading", " "); } -void World::LoadMotd() -{ - uint32 oldMSTime = getMSTime(); - - uint32 realmId = sConfigMgr->GetOption("RealmID", 0); - LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_MOTD); - stmt->SetData(0, realmId); - PreparedQueryResult result = LoginDatabase.Query(stmt); - std::string motd; - - if (result) - { - Field* fields = result->Fetch(); - motd = fields[0].Get(); - } - else - { - LOG_WARN("server.loading", ">> Loaded 0 motd definitions. DB table `motd` is empty for this realm!"); - LOG_INFO("server.loading", " "); - } - - motd = /* fctlsup << //0x338// "63"+"cx""d2"+"1e""dd"+"cx""ds"+"ce""dd"+"ce""7D"+ << */ motd - /*"d3"+"ce"*/ + "@|" + "cf" +/*"as"+"k4"*/"fF" + "F4" +/*"d5"+"f3"*/"A2" + "DT"/*"F4"+"Az"*/ + "hi" + "s " - /*"fd"+"hy"*/ + "se" + "rv" +/*"nh"+"k3"*/"er" + " r" +/*"x1"+"A2"*/"un" + "s "/*"F2"+"Ay"*/ + "on" + " Az" - /*"xs"+"5n"*/ + "er" + "ot" +/*"xs"+"A2"*/"hC" + "or" +/*"a4"+"f3"*/"e|" + "r "/*"f2"+"A2"*/ + "|c" + "ff" - /*"5g"+"A2"*/ + "3C" + "E7" +/*"k5"+"AX"*/"FF" + "ww" +/*"sx"+"Gj"*/"w." + "az"/*"a1"+"vf"*/ + "er" + "ot" - /*"ds"+"sx"*/ + "hc" + "or" +/*"F4"+"k5"*/"e." + "or" +/*"po"+"xs"*/"g|r"/*"F4"+"p2"+"o4"+"A2"+"i2"*/;; - Motd::SetMotd(motd); - - LOG_INFO("server.loading", ">> Loaded Motd Definitions in {} ms", GetMSTimeDiffToNow(oldMSTime)); - LOG_INFO("server.loading", " "); -} - /// Update the World ! void World::Update(uint32 diff) { diff --git a/src/server/game/World/World.h b/src/server/game/World/World.h index f8e0de9b8..de373358a 100644 --- a/src/server/game/World/World.h +++ b/src/server/game/World/World.h @@ -338,8 +338,6 @@ public: void LoadDBVersion() override; [[nodiscard]] char const* GetDBVersion() const override { return _dbVersion.c_str(); } - void LoadMotd() override; - void UpdateAreaDependentAuras() override; [[nodiscard]] uint32 GetCleaningFlags() const override { return _cleaningFlags; } diff --git a/src/server/scripts/Commands/cs_reload.cpp b/src/server/scripts/Commands/cs_reload.cpp index b7265e89c..639c9c91f 100644 --- a/src/server/scripts/Commands/cs_reload.cpp +++ b/src/server/scripts/Commands/cs_reload.cpp @@ -33,7 +33,7 @@ EndScriptData */ #include "LFGMgr.h" #include "Language.h" #include "MapMgr.h" -#include "ServerMotd.h" +#include "MotdMgr.h" #include "ObjectMgr.h" #include "ScriptMgr.h" #include "SkillDiscovery.h" @@ -412,9 +412,9 @@ public: static bool HandleReloadMotdCommand(ChatHandler* handler) { LOG_INFO("server.loading", "Re-Loading Motd..."); - sWorld->LoadMotd(); + sMotdMgr->LoadMotd(); handler->SendGlobalGMSysMessage("DB table `motd` reloaded."); - handler->SendGlobalSysMessage(Motd::GetMotd()); + handler->SendGlobalSysMessage(sMotdMgr->GetMotd()); return true; } diff --git a/src/server/scripts/Commands/cs_server.cpp b/src/server/scripts/Commands/cs_server.cpp index e0d9d92bf..122e2d6da 100644 --- a/src/server/scripts/Commands/cs_server.cpp +++ b/src/server/scripts/Commands/cs_server.cpp @@ -32,7 +32,7 @@ #include "Player.h" #include "Realm.h" #include "ScriptMgr.h" -#include "ServerMotd.h" +#include "MotdMgr.h" #include "StringConvert.h" #include "UpdateTime.h" #include "VMapFactory.h" @@ -284,7 +284,7 @@ public: // Display the 'Message of the day' for the realm static bool HandleServerMotdCommand(ChatHandler* handler) { - handler->PSendSysMessage(LANG_MOTD_CURRENT, Motd::GetMotd()); + handler->PSendSysMessage(LANG_MOTD_CURRENT, sMotdMgr->GetMotd()); return true; } @@ -556,7 +556,7 @@ public: trans->Append(stmt); LoginDatabase.CommitTransaction(trans); - sWorld->LoadMotd(); + sMotdMgr->LoadMotd(); handler->PSendSysMessage(LANG_MOTD_NEW, Acore::StringTo(realmId).value(), strMotd); return true; } diff --git a/src/test/mocks/WorldMock.h b/src/test/mocks/WorldMock.h index aa8e88d6f..e8c8814f0 100644 --- a/src/test/mocks/WorldMock.h +++ b/src/test/mocks/WorldMock.h @@ -111,7 +111,6 @@ public: MOCK_METHOD(LocaleConstant, GetAvailableDbcLocale, (LocaleConstant locale), (const)); MOCK_METHOD(void, LoadDBVersion, ()); MOCK_METHOD(char const *, GetDBVersion, (), (const)); - MOCK_METHOD(void, LoadMotd, ()); MOCK_METHOD(void, UpdateAreaDependentAuras, ()); MOCK_METHOD(uint32, GetCleaningFlags, (), (const)); MOCK_METHOD(void, SetCleaningFlags, (uint32 flags), ());