From de1b22769fb274d54f5d64f02973adc176fb1bd6 Mon Sep 17 00:00:00 2001 From: Kitzunu <24550914+Kitzunu@users.noreply.github.com> Date: Sun, 19 Jan 2025 21:43:58 +0100 Subject: [PATCH 1/6] refactor(Core/Loot): Simplify GetFishLoot with for loop and merge GetFishLootJunk functionality --- .../game/Entities/GameObject/GameObject.cpp | 44 +++++-------------- .../game/Entities/GameObject/GameObject.h | 3 +- src/server/game/Entities/Player/Player.cpp | 2 +- 3 files changed, 14 insertions(+), 35 deletions(-) diff --git a/src/server/game/Entities/GameObject/GameObject.cpp b/src/server/game/Entities/GameObject/GameObject.cpp index 163e390f2c1a09..c6aa4b9f214d43 100644 --- a/src/server/game/Entities/GameObject/GameObject.cpp +++ b/src/server/game/Entities/GameObject/GameObject.cpp @@ -1001,43 +1001,23 @@ void GameObject::Delete() AddObjectToRemoveList(); } -void GameObject::GetFishLoot(Loot* fishloot, Player* loot_owner) +void GameObject::GetFishLoot(Loot* fishLoot, Player* lootOwner, bool junk) { - fishloot->clear(); + fishLoot->clear(); - uint32 zone, subzone; - uint32 defaultzone = 1; - GetZoneAndAreaId(zone, subzone); + uint32 zone, area; + uint32 defaultZone = 1; + GetZoneAndAreaId(zone, area); - // if subzone loot exist use it - fishloot->FillLoot(subzone, LootTemplates_Fishing, loot_owner, true, true); - if (fishloot->empty()) //use this becase if zone or subzone has set LOOT_MODE_JUNK_FISH,Even if no normal drop, fishloot->FillLoot return true. it wrong. + uint16 lootMode = junk ? LOOT_MODE_JUNK_FISH : LOOT_MODE_DEFAULT; + uint32 lootZones[] = { area, zone, defaultZone }; + for (uint32 fillZone : lootZones) { - //subzone no result,use zone loot - fishloot->FillLoot(zone, LootTemplates_Fishing, loot_owner, true, true); - //use zone 1 as default, somewhere fishing got nothing,becase subzone and zone not set, like Off the coast of Storm Peaks. - if (fishloot->empty()) - fishloot->FillLoot(defaultzone, LootTemplates_Fishing, loot_owner, true, true); - } -} - -void GameObject::GetFishLootJunk(Loot* fishloot, Player* loot_owner) -{ - fishloot->clear(); + fishLoot->FillLoot(fillZone, LootTemplates_Fishing, lootOwner, true, true, lootMode); - uint32 zone, subzone; - uint32 defaultzone = 1; - GetZoneAndAreaId(zone, subzone); - - // if subzone loot exist use it - fishloot->FillLoot(subzone, LootTemplates_Fishing, loot_owner, true, true, LOOT_MODE_JUNK_FISH); - if (fishloot->empty()) //use this becase if zone or subzone has normal mask drop, then fishloot->FillLoot return true. - { - //use zone loot - fishloot->FillLoot(zone, LootTemplates_Fishing, loot_owner, true, true, LOOT_MODE_JUNK_FISH); - if (fishloot->empty()) - //use zone 1 as default - fishloot->FillLoot(defaultzone, LootTemplates_Fishing, loot_owner, true, true, LOOT_MODE_JUNK_FISH); + // If the loot is filled and the loot is eligible, then we break out of the loop. + if (!fishLoot->empty() && !fishLoot->isLooted()) + break; } } diff --git a/src/server/game/Entities/GameObject/GameObject.h b/src/server/game/Entities/GameObject/GameObject.h index 5a214226d690d4..9c7f50bf470340 100644 --- a/src/server/game/Entities/GameObject/GameObject.h +++ b/src/server/game/Entities/GameObject/GameObject.h @@ -198,8 +198,7 @@ class GameObject : public WorldObject, public GridObject, public Mov void Refresh(); void DespawnOrUnsummon(Milliseconds delay = 0ms, Seconds forcedRespawnTime = 0s); void Delete(); - void GetFishLoot(Loot* loot, Player* loot_owner); - void GetFishLootJunk(Loot* loot, Player* loot_owner); + void GetFishLoot(Loot* fishLoot, Player* lootOwner, bool junk = false); [[nodiscard]] GameobjectTypes GetGoType() const { return GameobjectTypes(GetByteValue(GAMEOBJECT_BYTES_1, 1)); } void SetGoType(GameobjectTypes type) { SetByteValue(GAMEOBJECT_BYTES_1, 1, type); } [[nodiscard]] GOState GetGoState() const { return GOState(GetByteValue(GAMEOBJECT_BYTES_1, 0)); } diff --git a/src/server/game/Entities/Player/Player.cpp b/src/server/game/Entities/Player/Player.cpp index dfed1d21618d75..ae6838a95c24fa 100644 --- a/src/server/game/Entities/Player/Player.cpp +++ b/src/server/game/Entities/Player/Player.cpp @@ -7885,7 +7885,7 @@ void Player::SendLoot(ObjectGuid guid, LootType loot_type) if (loot_type == LOOT_FISHING) go->GetFishLoot(loot, this); else if (loot_type == LOOT_FISHING_JUNK) - go->GetFishLootJunk(loot, this); + go->GetFishLoot(loot, this, true); if (go->GetGOInfo()->type == GAMEOBJECT_TYPE_CHEST && go->GetGOInfo()->chest.groupLootRules) { From ca57a8484dc53ee54f31a347afde4e58742aac8a Mon Sep 17 00:00:00 2001 From: Kitzunu <24550914+Kitzunu@users.noreply.github.com> Date: Sun, 19 Jan 2025 21:48:07 +0100 Subject: [PATCH 2/6] document the code --- src/server/game/Entities/GameObject/GameObject.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/server/game/Entities/GameObject/GameObject.cpp b/src/server/game/Entities/GameObject/GameObject.cpp index c6aa4b9f214d43..52013f2f03fa29 100644 --- a/src/server/game/Entities/GameObject/GameObject.cpp +++ b/src/server/game/Entities/GameObject/GameObject.cpp @@ -1010,6 +1010,8 @@ void GameObject::GetFishLoot(Loot* fishLoot, Player* lootOwner, bool junk) GetZoneAndAreaId(zone, area); uint16 lootMode = junk ? LOOT_MODE_JUNK_FISH : LOOT_MODE_DEFAULT; + // Check to fill loot in the order area - zone - defaultZone. + // This is because area and zone is not set in some places, like Off the coast of Storm Peaks. uint32 lootZones[] = { area, zone, defaultZone }; for (uint32 fillZone : lootZones) { From f9a3be9475339d331aa491207ffdba352b9aa3ad Mon Sep 17 00:00:00 2001 From: Kitzunu <24550914+Kitzunu@users.noreply.github.com> Date: Sun, 19 Jan 2025 21:50:08 +0100 Subject: [PATCH 3/6] Update GameObject.cpp --- src/server/game/Entities/GameObject/GameObject.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server/game/Entities/GameObject/GameObject.cpp b/src/server/game/Entities/GameObject/GameObject.cpp index 52013f2f03fa29..fc7d04c6133a20 100644 --- a/src/server/game/Entities/GameObject/GameObject.cpp +++ b/src/server/game/Entities/GameObject/GameObject.cpp @@ -1001,7 +1001,7 @@ void GameObject::Delete() AddObjectToRemoveList(); } -void GameObject::GetFishLoot(Loot* fishLoot, Player* lootOwner, bool junk) +void GameObject::GetFishLoot(Loot* fishLoot, Player* lootOwner, bool junk /*= false*/) { fishLoot->clear(); From db1602f8ffee2e86423356a7b620259ace920c4d Mon Sep 17 00:00:00 2001 From: Kitzunu <24550914+Kitzunu@users.noreply.github.com> Date: Thu, 23 Jan 2025 19:54:45 +0100 Subject: [PATCH 4/6] int --- src/server/game/Motd/MotdMgr.cpp | 96 ++++++++++++++------------------ src/server/game/Motd/MotdMgr.h | 6 +- 2 files changed, 44 insertions(+), 58 deletions(-) diff --git a/src/server/game/Motd/MotdMgr.cpp b/src/server/game/Motd/MotdMgr.cpp index a63bce7b6d0ef7..67593a604f9f9c 100644 --- a/src/server/game/Motd/MotdMgr.cpp +++ b/src/server/game/Motd/MotdMgr.cpp @@ -62,22 +62,34 @@ void MotdMgr::CreateWorldPackages() 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); - // Load the main motd for the realm and assign it to enUS if available - std::string motd = LoadDefaultMotd(realmId); + if (result) + { + Field* fields = result->Fetch(); + std::string motd = fields[0].Get(); // Return the main motd if found - // Check if motd was loaded; if not, set default only for enUS - if (motd.empty()) - SetDefaultMotd(); // Only sets enUS default if motd is empty - else MotdMap[DEFAULT_LOCALE] = motd; // Assign the loaded motd to enUS - // Load localized texts if available - LoadLocalizedMotds(realmId); + // Load localized texts if available + LoadMotdLocale(realmId); + + // Create all world packages after loading motd and localized texts + CreateWorldPackages(); + } + else + { + LOG_INFO("server.loading", ">> Loaded 0 motd definitions. DB table `motd` is empty for this realm!"); + LOG_INFO("server.loading", " "); + } - // Create all world packages after loading motd and localized texts - CreateWorldPackages(); + LOG_INFO("server.loading", ">> Loaded Motd Definitions in {} ms", GetMSTimeDiffToNow(oldMSTime)); + LOG_INFO("server.loading", " "); } char const* MotdMgr::GetMotd(LocaleConstant locale) @@ -100,57 +112,18 @@ WorldPacket const* MotdMgr::GetMotdPacket(LocaleConstant locale) return &MotdPackets[DEFAULT_LOCALE]; // Fallback to enUS if locale is not found } -std::string MotdMgr::LoadDefaultMotd(uint32 realmId) -{ - LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_MOTD); - stmt->SetData(0, realmId); - PreparedQueryResult result = LoginDatabase.Query(stmt); - - if (result) - { - Field* fields = result->Fetch(); - return fields[0].Get(); // Return the main motd if found - } - - return ""; // Return empty string if no motd found -} - -void MotdMgr::SetDefaultMotd() +void MotdMgr::LoadMotdLocale(uint32 realmId) { - std::string motd = /* fctlsup << //0x338// "63"+"cx""d2"+"1e""dd"+"cx""ds"+"ce""dd"+"ce""7D"+ << */ - /*"d3"+"ce"*/ std::string("@|") + "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"*/; - - MotdMap[DEFAULT_LOCALE] = motd; - - // Log that no motd was found and a default is being used for enUS - LOG_WARN("server.loading", ">> Loaded 0 motd definitions. DB table `motd` is empty for this realm!"); - LOG_INFO("server.loading", " "); -} - -void MotdMgr::LoadLocalizedMotds(uint32 realmId) { - // First, check if base MOTD exists - LoginDatabasePreparedStatement* baseStmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_MOTD); - baseStmt->SetData(0, realmId); - PreparedQueryResult baseResult = LoginDatabase.Query(baseStmt); - - if (!baseResult) - { - LOG_ERROR("server.loading", "No base MOTD found for realm {}. Localized MOTDs will not be loaded.", realmId); - return; - } + uint32 oldMSTime = getMSTime(); - // Now load localized versions LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_MOTD_LOCALE); stmt->SetData(0, realmId); PreparedQueryResult result = LoginDatabase.Query(stmt); if (result) { - do { + do + { Field* fields = result->Fetch(); // fields[0] is the locale string and fields[1] is the localized motd text std::string localizedText = fields[1].Get(); @@ -163,13 +136,28 @@ void MotdMgr::LoadLocalizedMotds(uint32 realmId) { MotdMap[localeId] = localizedText; } while (result->NextRow()); } + else + { + LOG_INFO("server.loading", ">> Loaded 0 motd locale definitions. DB table `motd_localized` is empty for this realm!"); + LOG_INFO("server.loading", " "); + } + + LOG_INFO("server.loading", ">> Loaded Motd locale Definitions in {} ms", GetMSTimeDiffToNow(oldMSTime)); + LOG_INFO("server.loading", " "); } -WorldPacket MotdMgr::CreateWorldPacket(std::string const& motd) +WorldPacket MotdMgr::CreateWorldPacket(std::string motd) { // Create a new WorldPacket for this locale WorldPacket data(SMSG_MOTD); // new in 2.0.1 + 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"*/; + // Tokenize the motd string by '@' std::vector motdTokens = Acore::Tokenize(motd, '@', true); data << uint32(motdTokens.size()); // line count diff --git a/src/server/game/Motd/MotdMgr.h b/src/server/game/Motd/MotdMgr.h index 9374ef2f794d8f..d7a7f48ac308e8 100644 --- a/src/server/game/Motd/MotdMgr.h +++ b/src/server/game/Motd/MotdMgr.h @@ -51,11 +51,9 @@ class AC_GAME_API MotdMgr // Loads the default motd from the motd table std::string LoadDefaultMotd(uint32 realmId); // Loads all available localized motd for the realm - void LoadLocalizedMotds(uint32 realmId); - // Sets the default mode if none is found in the database - void SetDefaultMotd(); + void LoadMotdLocale(uint32 realmId); // Create a worldpacket for a given motd localization - WorldPacket CreateWorldPacket(std::string const& motd); + WorldPacket CreateWorldPacket(std::string motd); }; #define sMotdMgr MotdMgr::instance() From 93cef9ed576607a6239b7c5dfbe759be15af0c44 Mon Sep 17 00:00:00 2001 From: Kitzunu <24550914+Kitzunu@users.noreply.github.com> Date: Thu, 23 Jan 2025 21:31:50 +0100 Subject: [PATCH 5/6] refactor(Core/Motd): improve and simplify --- .../rev_1737663805709739900.sql | 5 ++ src/common/Common.cpp | 11 +++- src/common/Common.h | 1 + .../Database/Implementation/LoginDatabase.cpp | 4 +- .../Database/Implementation/LoginDatabase.h | 4 +- src/server/game/Miscellaneous/Language.h | 3 +- src/server/game/Motd/MotdMgr.cpp | 54 ++++++++-------- src/server/game/Motd/MotdMgr.h | 5 -- src/server/scripts/Commands/cs_server.cpp | 63 ++++++------------- 9 files changed, 65 insertions(+), 85 deletions(-) create mode 100644 data/sql/updates/pending_db_world/rev_1737663805709739900.sql diff --git a/data/sql/updates/pending_db_world/rev_1737663805709739900.sql b/data/sql/updates/pending_db_world/rev_1737663805709739900.sql new file mode 100644 index 00000000000000..17c56398ae109f --- /dev/null +++ b/data/sql/updates/pending_db_world/rev_1737663805709739900.sql @@ -0,0 +1,5 @@ +-- +DELETE FROM `acore_string` WHERE `entry` IN (56, 82); +INSERT INTO `acore_string` (`entry`, `content_default`, `locale_deDE`, `locale_zhCN`, `locale_esES`, `locale_esMX`) VALUES +(56, 'Current Message of the day:', 'Aktuelle Nachricht des Tages:', '当前每日信息:', 'Mensaje actual del día:', 'Mensaje actual del día:'), +(82, '{}: {}', '{}: {}', '{}: {}', '{}: {}', '{}: {}'); diff --git a/src/common/Common.cpp b/src/common/Common.cpp index e25611e068c4f0..530dec961f5ca0 100644 --- a/src/common/Common.cpp +++ b/src/common/Common.cpp @@ -30,13 +30,20 @@ char const* localeNames[TOTAL_LOCALES] = "ruRU" }; +bool IsLocaleValid(std::string const& locale) +{ + for (int i = 0; i < TOTAL_LOCALES; ++i) + if (locale == localeNames[i]) + return true; + + return false; +} + LocaleConstant GetLocaleByName(const std::string& name) { for (uint32 i = 0; i < TOTAL_LOCALES; ++i) if (name == localeNames[i]) - { return LocaleConstant(i); - } return LOCALE_enUS; // including enGB case } diff --git a/src/common/Common.h b/src/common/Common.h index 6b7f482db62681..87ec6b4d2bd948 100644 --- a/src/common/Common.h +++ b/src/common/Common.h @@ -83,6 +83,7 @@ enum LocaleConstant AC_COMMON_API extern char const* localeNames[TOTAL_LOCALES]; +AC_COMMON_API bool IsLocaleValid(std::string const& locale); AC_COMMON_API LocaleConstant GetLocaleByName(const std::string& name); AC_COMMON_API const std::string GetNameByLocaleConstant(LocaleConstant localeConstant); AC_COMMON_API void CleanStringForMysqlQuery(std::string& str); diff --git a/src/server/database/Database/Implementation/LoginDatabase.cpp b/src/server/database/Database/Implementation/LoginDatabase.cpp index 51eb7d8ec197e7..3fcf259433d810 100644 --- a/src/server/database/Database/Implementation/LoginDatabase.cpp +++ b/src/server/database/Database/Implementation/LoginDatabase.cpp @@ -117,8 +117,8 @@ void LoginDatabaseConnection::DoPrepareStatements() PrepareStatement(LOGIN_SEL_AUTOBROADCAST, "SELECT id, weight, text FROM autobroadcast WHERE realmid = ? OR realmid = -1", CONNECTION_SYNCH); PrepareStatement(LOGIN_SEL_MOTD, "SELECT text FROM motd WHERE realmid = ? OR realmid = -1 ORDER BY realmid DESC", CONNECTION_SYNCH); PrepareStatement(LOGIN_SEL_MOTD_LOCALE, "SELECT locale, text FROM motd_localized WHERE realmid = ? OR realmid = -1 ORDER BY realmid DESC", CONNECTION_SYNCH); - PrepareStatement(LOGIN_INS_MOTD, "INSERT INTO motd (realmid, text) VALUES (?, ?) ON DUPLICATE KEY UPDATE text = ?", CONNECTION_ASYNC); - PrepareStatement(LOGIN_INS_MOTD_LOCALE, "INSERT INTO motd_localized (realmid, locale, text) VALUES(?, ?, ?) ON DUPLICATE KEY UPDATE text = ?;", CONNECTION_ASYNC); + PrepareStatement(LOGIN_REP_MOTD, "REPLACE INTO motd (realmid, text) VALUES (?, ?)", CONNECTION_ASYNC); + PrepareStatement(LOGIN_REP_MOTD_LOCALE, "REPLACE INTO motd_localized (realmid, locale, text) VALUES (?, ?, ?)", CONNECTION_ASYNC); PrepareStatement(LOGIN_INS_ACCOUNT_MUTE, "INSERT INTO account_muted VALUES (?, UNIX_TIMESTAMP(), ?, ?, ?)", CONNECTION_ASYNC); PrepareStatement(LOGIN_SEL_ACCOUNT_MUTE_INFO, "SELECT mutedate, mutetime, mutereason, mutedby FROM account_muted WHERE guid = ? ORDER BY mutedate ASC", CONNECTION_SYNCH); PrepareStatement(LOGIN_DEL_ACCOUNT_MUTED, "DELETE FROM account_muted WHERE guid = ?", CONNECTION_ASYNC); diff --git a/src/server/database/Database/Implementation/LoginDatabase.h b/src/server/database/Database/Implementation/LoginDatabase.h index f6791ac8011ea0..323c580094bbe2 100644 --- a/src/server/database/Database/Implementation/LoginDatabase.h +++ b/src/server/database/Database/Implementation/LoginDatabase.h @@ -99,8 +99,8 @@ enum LoginDatabaseStatements : uint32 LOGIN_SEL_AUTOBROADCAST, LOGIN_SEL_MOTD, LOGIN_SEL_MOTD_LOCALE, - LOGIN_INS_MOTD, - LOGIN_INS_MOTD_LOCALE, + LOGIN_REP_MOTD, + LOGIN_REP_MOTD_LOCALE, LOGIN_SEL_LAST_ATTEMPT_IP, LOGIN_SEL_LAST_IP, LOGIN_INS_ALDL_IP_LOGGING, diff --git a/src/server/game/Miscellaneous/Language.h b/src/server/game/Miscellaneous/Language.h index 2911adad27d90b..fe1c85faf37559 100644 --- a/src/server/game/Miscellaneous/Language.h +++ b/src/server/game/Miscellaneous/Language.h @@ -111,7 +111,8 @@ enum AcoreStrings LANG_RBAC_PERM_REVOKED_NOT_IN_LIST = 79, LANG_PVPSTATS = 80, LANG_PVPSTATS_DISABLED = 81, - // Free 82 - 86 + LANG_GENERIC_TWO_CURLIES_WITH_COLON = 82, + // Free 83 - 86 LANG_UNKNOWN_ERROR = 87, LANG_2FA_COMMANDS_NOT_SETUP = 88, diff --git a/src/server/game/Motd/MotdMgr.cpp b/src/server/game/Motd/MotdMgr.cpp index 67593a604f9f9c..8435c035765460 100644 --- a/src/server/game/Motd/MotdMgr.cpp +++ b/src/server/game/Motd/MotdMgr.cpp @@ -39,11 +39,6 @@ MotdMgr* MotdMgr::instance() return &instance; } -bool MotdMgr::IsValidLocale(std::string const& locale) { - // Use std::find to search for the locale in the array - return std::find(std::begin(localeNames), std::end(localeNames), locale) != std::end(localeNames); -} - void MotdMgr::SetMotd(std::string motd, LocaleConstant locale) { // scripts may change motd @@ -74,7 +69,7 @@ void MotdMgr::LoadMotd() Field* fields = result->Fetch(); std::string motd = fields[0].Get(); // Return the main motd if found - MotdMap[DEFAULT_LOCALE] = motd; // Assign the loaded motd to enUS + MotdMap[LOCALE_enUS] = motd; // Assign the loaded motd to enUS // Load localized texts if available LoadMotdLocale(realmId); @@ -85,33 +80,14 @@ void MotdMgr::LoadMotd() else { LOG_INFO("server.loading", ">> Loaded 0 motd definitions. DB table `motd` is empty for this realm!"); + LOG_INFO("server.loading", ">> Loaded 0 motd locale definitions. DB table `motd` needs an entry to be able to load DB table `motd_locale`!"); LOG_INFO("server.loading", " "); } - LOG_INFO("server.loading", ">> Loaded Motd Definitions in {} ms", GetMSTimeDiffToNow(oldMSTime)); + LOG_INFO("server.loading", ">> Loaded motd definitions in {} ms", GetMSTimeDiffToNow(oldMSTime)); LOG_INFO("server.loading", " "); } -char const* MotdMgr::GetMotd(LocaleConstant locale) -{ - // Return localized motd if available, otherwise fallback to enUS - auto it = MotdMap.find(locale); - if (it != MotdMap.end()) - return it->second.c_str(); - - return MotdMap[DEFAULT_LOCALE].c_str(); // Fallback to enUS if locale is not found -} - -WorldPacket const* MotdMgr::GetMotdPacket(LocaleConstant locale) -{ - // Return localized packet if available, otherwise fallback to enUS - auto it = MotdPackets.find(locale); - if (it != MotdPackets.end()) - return &it->second; - - return &MotdPackets[DEFAULT_LOCALE]; // Fallback to enUS if locale is not found -} - void MotdMgr::LoadMotdLocale(uint32 realmId) { uint32 oldMSTime = getMSTime(); @@ -130,7 +106,7 @@ void MotdMgr::LoadMotdLocale(uint32 realmId) // Convert locale string to LocaleConstant LocaleConstant localeId = GetLocaleByName(fields[0].Get()); - if (localeId == DEFAULT_LOCALE) + if (localeId == LOCALE_enUS) continue; MotdMap[localeId] = localizedText; @@ -142,10 +118,30 @@ void MotdMgr::LoadMotdLocale(uint32 realmId) LOG_INFO("server.loading", " "); } - LOG_INFO("server.loading", ">> Loaded Motd locale Definitions in {} ms", GetMSTimeDiffToNow(oldMSTime)); + LOG_INFO("server.loading", ">> Loaded motd locale definitions in {} ms", GetMSTimeDiffToNow(oldMSTime)); LOG_INFO("server.loading", " "); } +char const* MotdMgr::GetMotd(LocaleConstant locale) +{ + // Return localized motd if available, otherwise fallback to enUS + auto it = MotdMap.find(locale); + if (it != MotdMap.end()) + return it->second.c_str(); + + return MotdMap[LOCALE_enUS].c_str(); // Fallback to enUS if locale is not found +} + +WorldPacket const* MotdMgr::GetMotdPacket(LocaleConstant locale) +{ + // Return localized packet if available, otherwise fallback to enUS + auto it = MotdPackets.find(locale); + if (it != MotdPackets.end()) + return &it->second; + + return &MotdPackets[LOCALE_enUS]; // Fallback to enUS if locale is not found +} + WorldPacket MotdMgr::CreateWorldPacket(std::string motd) { // Create a new WorldPacket for this locale diff --git a/src/server/game/Motd/MotdMgr.h b/src/server/game/Motd/MotdMgr.h index d7a7f48ac308e8..d752a799e3eb00 100644 --- a/src/server/game/Motd/MotdMgr.h +++ b/src/server/game/Motd/MotdMgr.h @@ -44,12 +44,7 @@ class AC_GAME_API MotdMgr /// Returns the current motd packet for the given locale WorldPacket const* GetMotdPacket(LocaleConstant locale); - // Checks if string is valid locale - bool IsValidLocale(std::string const& locale); - private: - // Loads the default motd from the motd table - std::string LoadDefaultMotd(uint32 realmId); // Loads all available localized motd for the realm void LoadMotdLocale(uint32 realmId); // Create a worldpacket for a given motd localization diff --git a/src/server/scripts/Commands/cs_server.cpp b/src/server/scripts/Commands/cs_server.cpp index 2e5f3ff34a74cb..a789420a478a55 100644 --- a/src/server/scripts/Commands/cs_server.cpp +++ b/src/server/scripts/Commands/cs_server.cpp @@ -289,11 +289,9 @@ class server_commandscript : public CommandScript // Display the 'Message of the day' for the realm static bool HandleServerMotdCommand(ChatHandler* handler) { - LocaleConstant localeConstant = DEFAULT_LOCALE; - if (Player* player = handler->GetPlayer()) - localeConstant = player->GetSession()->GetSessionDbLocaleIndex(); - - handler->PSendSysMessage(LANG_MOTD_CURRENT, sMotdMgr->GetMotd(localeConstant)); + handler->PSendSysMessage(LANG_MOTD_CURRENT); + for (uint32 i = 0; i < TOTAL_LOCALES; ++i) + handler->PSendSysMessage(LANG_GENERIC_TWO_CURLIES_WITH_COLON, GetNameByLocaleConstant(LocaleConstant(i)), sMotdMgr->GetMotd(LocaleConstant(i))); return true; } @@ -525,7 +523,7 @@ class server_commandscript : public CommandScript } // Define the 'Message of the day' for the realm - static bool HandleServerSetMotdCommand(ChatHandler* handler, Optional realmId, Optional locale, Tail motd) + static bool HandleServerSetMotdCommand(ChatHandler* handler, Optional realmId, std::string locale, Tail motd) { std::wstring wMotd = std::wstring(); std::string strMotd = std::string(); @@ -534,39 +532,21 @@ class server_commandscript : public CommandScript if (!realmId) realmId = static_cast(realm.Id.Realm); - if (motd.empty()) - return false; - - // Convert Tail (motd) to std::string - std::ostringstream motdStream; - motdStream << motd; - std::string motdString = motdStream.str(); // Convert Tail to std::string // Determine the locale; default to "enUS" if not provided - LocaleConstant localeConstant = DEFAULT_LOCALE; - if (locale.has_value()) - { - if (sMotdMgr->IsValidLocale(locale.value())) - { - localeConstant = GetLocaleByName(locale.value()); - } - else - { - motdStream.str(""); - motdStream << locale.value() << " " << motd; - motdString = motdStream.str(); - localeConstant = DEFAULT_LOCALE; - locale = GetNameByLocaleConstant(localeConstant); - } - } + LocaleConstant localeConstant; + if (IsLocaleValid(locale)) + localeConstant = GetLocaleByName(locale); else { - // Set to default locale string - localeConstant = DEFAULT_LOCALE; - locale = GetNameByLocaleConstant(localeConstant); + handler->SendErrorMessage("locale ({}) is not valid. Valid locales: enUS, koKR, frFR, deDE, zhCN, zhWE, esES, esMX, ruRU.", locale); + return false; } + if (motd.empty()) + return false; + // Convert the concatenated motdString to UTF-8 and ensure encoding consistency - if (!Utf8toWStr(motdString, wMotd)) + if (!Utf8toWStr(motd, wMotd)) return false; if (!WStrToUtf8(wMotd, strMotd)) @@ -575,34 +555,29 @@ class server_commandscript : public CommandScript // Start a transaction for the database operations LoginDatabaseTransaction trans = LoginDatabase.BeginTransaction(); - if (localeConstant == DEFAULT_LOCALE) + if (localeConstant == LOCALE_enUS) { // Insert or update in the main motd table for enUS - LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_INS_MOTD); + LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_REP_MOTD); stmt->SetData(0, realmId.value()); // realmId for insertion stmt->SetData(1, strMotd); // motd text for insertion - stmt->SetData(2, strMotd); // motd text for ON DUPLICATE KEY UPDATE trans->Append(stmt); } else { // Insert or update in the motd_localized table for other locales - LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_INS_MOTD_LOCALE); + LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_REP_MOTD_LOCALE); stmt->SetData(0, realmId.value()); // realmId for insertion - stmt->SetData(1, locale.value()); // locale for insertion + stmt->SetData(1, locale); // locale for insertion stmt->SetData(2, strMotd); // motd text for insertion - stmt->SetData(3, strMotd); // motd text for ON DUPLICATE KEY UPDATE trans->Append(stmt); } // Commit the transaction & update db LoginDatabase.CommitTransaction(trans); - // Update the in-memory maps for the current realm. Otherwise, do not update - if (realmId == -1 || realmId == static_cast(realm.Id.Realm)) - sMotdMgr->SetMotd(strMotd, localeConstant); - - handler->PSendSysMessage(LANG_MOTD_NEW, realmId.value(), locale.value(), strMotd); + sMotdMgr->SetMotd(strMotd, localeConstant); + handler->PSendSysMessage(LANG_MOTD_NEW, realmId.value(), locale, strMotd); return true; } From 62f30a0492476020ed9f56ba399f1d878c3f16c7 Mon Sep 17 00:00:00 2001 From: Kitzunu <24550914+Kitzunu@users.noreply.github.com> Date: Thu, 23 Jan 2025 21:50:07 +0100 Subject: [PATCH 6/6] more cleanup --- src/server/game/Motd/MotdMgr.cpp | 38 +++++++++++++++----------------- src/server/game/Motd/MotdMgr.h | 6 ++--- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/server/game/Motd/MotdMgr.cpp b/src/server/game/Motd/MotdMgr.cpp index 8435c035765460..8c85222d27169c 100644 --- a/src/server/game/Motd/MotdMgr.cpp +++ b/src/server/game/Motd/MotdMgr.cpp @@ -48,13 +48,6 @@ void MotdMgr::SetMotd(std::string motd, LocaleConstant locale) MotdPackets[locale] = CreateWorldPacket(motd); } -void MotdMgr::CreateWorldPackages() -{ - for (auto const& [locale, motd] : MotdMap) - // Store the constructed packet in MotdPackets with the locale as the key - MotdPackets[locale] = CreateWorldPacket(motd); -} - void MotdMgr::LoadMotd() { uint32 oldMSTime = getMSTime(); @@ -67,15 +60,11 @@ void MotdMgr::LoadMotd() if (result) { Field* fields = result->Fetch(); - std::string motd = fields[0].Get(); // Return the main motd if found - - MotdMap[LOCALE_enUS] = motd; // Assign the loaded motd to enUS + std::string motd = fields[0].Get(); - // Load localized texts if available - LoadMotdLocale(realmId); + SetMotd(motd, LOCALE_enUS); - // Create all world packages after loading motd and localized texts - CreateWorldPackages(); + LoadMotdLocale(); } else { @@ -88,10 +77,13 @@ void MotdMgr::LoadMotd() LOG_INFO("server.loading", " "); } -void MotdMgr::LoadMotdLocale(uint32 realmId) +void MotdMgr::LoadMotdLocale() { uint32 oldMSTime = getMSTime(); + uint32 count = 0; + LOG_INFO("server.loading", "Loading Motd locale..."); + uint32 realmId = sConfigMgr->GetOption("RealmID", 0); LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_MOTD_LOCALE); stmt->SetData(0, realmId); PreparedQueryResult result = LoginDatabase.Query(stmt); @@ -102,14 +94,21 @@ void MotdMgr::LoadMotdLocale(uint32 realmId) { Field* fields = result->Fetch(); // fields[0] is the locale string and fields[1] is the localized motd text + std::string locale = fields[0].Get(); std::string localizedText = fields[1].Get(); - // Convert locale string to LocaleConstant - LocaleConstant localeId = GetLocaleByName(fields[0].Get()); + if (!IsLocaleValid(locale)) + { + LOG_ERROR("server.loading", "DB table `motd_localized` has invalid locale ({}), skipped.", locale); + continue; + } + + LocaleConstant localeId = GetLocaleByName(locale); if (localeId == LOCALE_enUS) continue; - MotdMap[localeId] = localizedText; + SetMotd(localizedText, localeId); + ++count; } while (result->NextRow()); } else @@ -118,8 +117,7 @@ void MotdMgr::LoadMotdLocale(uint32 realmId) LOG_INFO("server.loading", " "); } - LOG_INFO("server.loading", ">> Loaded motd locale definitions in {} ms", GetMSTimeDiffToNow(oldMSTime)); - LOG_INFO("server.loading", " "); + LOG_INFO("server.loading", ">> Loaded {} motd locale definitions in {} ms", count, GetMSTimeDiffToNow(oldMSTime)); } char const* MotdMgr::GetMotd(LocaleConstant locale) diff --git a/src/server/game/Motd/MotdMgr.h b/src/server/game/Motd/MotdMgr.h index d752a799e3eb00..1f85ba921c20df 100644 --- a/src/server/game/Motd/MotdMgr.h +++ b/src/server/game/Motd/MotdMgr.h @@ -29,9 +29,6 @@ class AC_GAME_API MotdMgr public: static MotdMgr* instance(); - /// Converts the localized string to world packages - void CreateWorldPackages(); - /// Set a new Message of the Day void SetMotd(std::string motd, LocaleConstant locale); @@ -46,7 +43,8 @@ class AC_GAME_API MotdMgr private: // Loads all available localized motd for the realm - void LoadMotdLocale(uint32 realmId); + void LoadMotdLocale(); + // Create a worldpacket for a given motd localization WorldPacket CreateWorldPacket(std::string motd); };