refactor(Core/Motd): Move motd to MotdMgr (#16933)

This commit is contained in:
Kitzunu
2023-08-06 23:02:54 +02:00
committed by GitHub
parent 6d61c686ed
commit c866e17406
11 changed files with 131 additions and 121 deletions

View File

@@ -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 <boost/asio/buffer.hpp>
@@ -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 (;;)

View File

@@ -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)

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#include "MotdMgr.h"
#include "Config.h"
#include "Opcodes.h"
#include "ScriptMgr.h"
#include "Util.h"
#include "WorldPacket.h"
#include "Tokenize.h"
#include <iterator>
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<std::string_view> 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<std::string_view>(oss, "\n"));
oss << *(motdTokens.end() - 1); // copy back element
FormattedMotd = oss.str();
}
void MotdMgr::LoadMotd()
{
uint32 oldMSTime = getMSTime();
uint32 realmId = sConfigMgr->GetOption<int32>("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<std::string>();
}
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;
}

View File

@@ -15,25 +15,32 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ServerMotd_h__
#define ServerMotd_h__
#ifndef _MOTDMGR_H_
#define _MOTDMGR_H_
#include "Define.h"
#include <string>
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_

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#include "ServerMotd.h"
#include "Opcodes.h"
#include "ScriptMgr.h"
#include "Util.h"
#include "WorldPacket.h"
#include "Tokenize.h"
#include <iterator>
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<std::string_view> 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<std::string_view>(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;
}

View File

@@ -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;

View File

@@ -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<int32>("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<std::string>();
}
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)
{

View File

@@ -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; }

View File

@@ -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;
}

View File

@@ -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<int32>(realmId).value(), strMotd);
return true;
}

View File

@@ -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), ());