diff --git a/src/game/ChatCommands/ReloadCommands.cpp b/src/game/ChatCommands/ReloadCommands.cpp index 76f46e3b..7715da0e 100644 --- a/src/game/ChatCommands/ReloadCommands.cpp +++ b/src/game/ChatCommands/ReloadCommands.cpp @@ -31,6 +31,7 @@ #include "CreatureEventAIMgr.h" #include "BattleGroundMgr.h" #include "ItemEnchantmentMgr.h" +#include "CommandMgr.h" /********************************************************************** CommandTable : commandTable @@ -186,6 +187,7 @@ bool ChatHandler::HandleReloadAllLocalesCommand(char* /*args*/) HandleReloadLocalesPageTextCommand((char*)"a"); HandleReloadLocalesPointsOfInterestCommand((char*)"a"); HandleReloadLocalesQuestCommand((char*)"a"); + HandleReloadLocalesCommandHelpCommand((char*)"a"); return true; } @@ -576,7 +578,6 @@ bool ChatHandler::HandleReloadSpellPetAurasCommand(char* /*args*/) return true; } - bool ChatHandler::HandleReloadPageTextsCommand(char* /*args*/) { sLog.outString("Re-Loading Page Texts..."); @@ -866,6 +867,14 @@ bool ChatHandler::HandleReloadLocalesNpcTextCommand(char* /*args*/) return true; } +bool ChatHandler::HandleReloadLocalesCommandHelpCommand(char* /*args*/) +{ + sLog.outString("Re-Loading Locales Command Help ... "); + sCommandMgr.LoadCommandHelpLocale(); + SendGlobalSysMessage("DB table `locales_command` reloaded.", SEC_MODERATOR); + return true; +} + bool ChatHandler::HandleReloadLocalesPageTextCommand(char* /*args*/) { sLog.outString("Re-Loading Locales Page Text ... "); diff --git a/src/game/WorldHandlers/Chat.cpp b/src/game/WorldHandlers/Chat.cpp index b2a73f82..7858c3b9 100644 --- a/src/game/WorldHandlers/Chat.cpp +++ b/src/game/WorldHandlers/Chat.cpp @@ -40,6 +40,7 @@ #include "PoolManager.h" #include "GameEventMgr.h" #include "AuctionHouseBot/AuctionHouseBot.h" +#include "CommandMgr.h" #ifdef ENABLE_ELUNA #include "LuaEngine.h" @@ -532,6 +533,7 @@ ChatCommand* ChatHandler::getCommandTable() { "locales_page_text", SEC_ADMINISTRATOR, true, &ChatHandler::HandleReloadLocalesPageTextCommand, "", NULL }, { "locales_points_of_interest", SEC_ADMINISTRATOR, true, &ChatHandler::HandleReloadLocalesPointsOfInterestCommand, "", NULL }, { "locales_quest", SEC_ADMINISTRATOR, true, &ChatHandler::HandleReloadLocalesQuestCommand, "", NULL }, + { "locales_command", SEC_ADMINISTRATOR, true, &ChatHandler::HandleReloadLocalesCommandHelpCommand, "", NULL }, { "mail_loot_template", SEC_ADMINISTRATOR, true, &ChatHandler::HandleReloadLootTemplatesMailCommand, "", NULL }, { "mangos_string", SEC_ADMINISTRATOR, true, &ChatHandler::HandleReloadMangosStringCommand, "", NULL }, { "npc_text", SEC_ADMINISTRATOR, true, &ChatHandler::HandleReloadNpcTextCommand, "", NULL }, @@ -807,15 +809,15 @@ ChatCommand* ChatHandler::getCommandTable() // check hardcoded part integrity CheckIntegrity(commandTable, NULL); - QueryResult* result = WorldDatabase.Query("SELECT `name`,`security`,`help` FROM `command`"); + QueryResult* result = WorldDatabase.Query("SELECT `id`, `command_text`,`security`,`help_text` FROM `command`"); if (result) { do { Field* fields = result->Fetch(); - std::string name = fields[0].GetCppString(); - - SetDataForCommandInTable(commandTable, name.c_str(), fields[1].GetUInt16(), fields[2].GetCppString()); + uint32 id = fields[0].GetUInt32(); + std::string name = fields[1].GetCppString(); + SetDataForCommandInTable(commandTable, id, name.c_str(), fields[2].GetUInt16(), fields[3].GetCppString()); } while (result->NextRow()); delete result; @@ -1305,7 +1307,16 @@ void ChatHandler::ExecuteCommand(const char* text) { if (!command->Help.empty()) { - SendSysMessage(command->Help.c_str()); + std::string helpText = command->Help; + + // Attemp to localize help text if not in CLI mode + if (m_session) + { + int loc_idx = m_session->GetSessionDbLocaleIndex(); + sCommandMgr.GetCommandHelpLocaleString(command->Id, loc_idx, &helpText); + } + + SendSysMessage(helpText.c_str()); } else { @@ -1362,7 +1373,7 @@ void ChatHandler::ExecuteCommand(const char* text) * * All problems found while command search and updated output as to DB errors log */ -bool ChatHandler::SetDataForCommandInTable(ChatCommand* commandTable, const char* text, uint32 security, std::string const& help) +bool ChatHandler::SetDataForCommandInTable(ChatCommand* commandTable, uint32 id, const char* text, uint32 security, std::string const& help) { std::string fullcommand = text; // original `text` can't be used. It content destroyed in command code processing. @@ -1379,6 +1390,7 @@ bool ChatHandler::SetDataForCommandInTable(ChatCommand* commandTable, const char DETAIL_LOG("Table `command` overwrite for command '%s' default security (%u) by %u", fullcommand.c_str(), command->SecurityLevel, security); + command->Id = id; command->SecurityLevel = security; command->Help = help; return true; @@ -1534,9 +1546,18 @@ bool ChatHandler::ShowHelpForCommand(ChatCommand* table, const char* cmd) break; } - if (command && !command->Help.empty()) + if (!command->Help.empty()) { - SendSysMessage(command->Help.c_str()); + std::string helpText = command->Help; + + // Attemp to localize help text if not in CLI mode + if (m_session) + { + int loc_idx = m_session->GetSessionDbLocaleIndex(); + sCommandMgr.GetCommandHelpLocaleString(command->Id, loc_idx, &helpText); + } + + SendSysMessage(helpText.c_str()); } if (childCommands) diff --git a/src/game/WorldHandlers/Chat.h b/src/game/WorldHandlers/Chat.h index 28834423..e523aebe 100644 --- a/src/game/WorldHandlers/Chat.h +++ b/src/game/WorldHandlers/Chat.h @@ -51,13 +51,32 @@ class Unit; class ChatCommand { - public: - const char* Name; - uint32 SecurityLevel; // function pointer required correct align (use uint32) - bool AllowConsole; - bool (ChatHandler::*Handler)(char* args); - std::string Help; - ChatCommand* ChildCommands; +public: + uint32 Id; + const char* Name; + uint32 SecurityLevel; // function pointer required correct align (use uint32) + bool AllowConsole; + bool (ChatHandler::* Handler)(char* args); + std::string Help; + ChatCommand* ChildCommands; + + ChatCommand( + const char* pName, + uint32 pSecurityLevel, + bool pAllowConsole, + bool (ChatHandler::* pHandler)(char* args), + std::string pHelp, + ChatCommand* pChildCommands + ) + : Id(-1) + { + Name = pName; + SecurityLevel = pSecurityLevel; + AllowConsole = pAllowConsole; + Handler = pHandler; + Help = pHelp; + ChildCommands = pChildCommands; + } }; enum ChatCommandSearchResult @@ -152,7 +171,7 @@ class ChatHandler void SendGlobalSysMessage(const char* str, AccountTypes minSec = SEC_PLAYER); - bool SetDataForCommandInTable(ChatCommand* table, const char* text, uint32 security, std::string const& help); + bool SetDataForCommandInTable(ChatCommand* table, uint32 id, const char* text, uint32 security, std::string const& help); void ExecuteCommand(const char* text); void LogCommand(char const* fullcmd); @@ -444,6 +463,7 @@ class ChatHandler bool HandleReloadLocalesPageTextCommand(char* args); bool HandleReloadLocalesPointsOfInterestCommand(char* args); bool HandleReloadLocalesQuestCommand(char* args); + bool HandleReloadLocalesCommandHelpCommand(char* args); bool HandleReloadLootTemplatesCreatureCommand(char* args); bool HandleReloadLootTemplatesDisenchantCommand(char* args); bool HandleReloadLootTemplatesFishingCommand(char* args); diff --git a/src/game/WorldHandlers/CommandMgr.cpp b/src/game/WorldHandlers/CommandMgr.cpp new file mode 100644 index 00000000..515a1b22 --- /dev/null +++ b/src/game/WorldHandlers/CommandMgr.cpp @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2015-2020 MaNGOS project + * Copyright (C) 2008-2015 TrinityCore + * Copyright (C) 2005-2009 MaNGOS + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 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 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 "Common.h" +#include "SharedDefines.h" +#include "Policies/Singleton.h" +#include "ObjectGuid.h" +#include "Language.h" +#include "CommandMgr.h" +#include "ObjectMgr.h" +#include "ProgressBar.h" + +class ChatCommand; // Forward declaration of + +INSTANTIATE_SINGLETON_1(CommandMgr); + +CommandMgr::CommandMgr() {} +CommandMgr::~CommandMgr() {} + +// Perhaps migrate all this in ObjectMgr.cpp ? +void CommandMgr::LoadCommandHelpLocale() +{ + m_CommandHelpLocaleMap.clear(); + + uint32 count=0; + + QueryResult* result = WorldDatabase.Query("SELECT " + "`id`," + "`help_text_loc1`," + "`help_text_loc2`," + "`help_text_loc3`," + "`help_text_loc4`," + "`help_text_loc5`," + "`help_text_loc6`," + "`help_text_loc7`," + "`help_text_loc8`" + " FROM `locales_command` ORDER BY `id` ASC " + ); + + if (!result) + { + sLog.outString(); + sLog.outString(">> Loaded 0 locales command help definitions. DB table `locales_command` is empty."); + return; + } + + BarGoLink bar(result->GetRowCount()); + + do + { + Field* fields = result->Fetch(); + bar.step(); + + uint32 commandId = fields[0].GetUInt32(); // to assign with db data + + CommandHelpLocale& data = m_CommandHelpLocaleMap[commandId]; + for (int i = 1; i <= MAX_LOCALE; ++i) + { + std::string str = fields[i].GetCppString(); + if (!str.empty()) + { + int idx = sObjectMgr.GetOrNewIndexForLocale(LocaleConstant(i)); + if (idx >= 0) + { + if ((int32)data.HelpText.size() <= idx) + { + data.HelpText.resize(idx + 1); + } + data.HelpText[idx] = str; + } + } + } + + ++count; + } while (result->NextRow()); + + sLog.outString(); + sLog.outString(">> Loaded %u locale command help definitions", count); + +} + +CommandHelpLocale const* CommandMgr::GetCommandLocale(uint32 commandId) const +{ + CommandHelpLocaleMap::const_iterator itr = m_CommandHelpLocaleMap.find(commandId); + if (itr == m_CommandHelpLocaleMap.end()) + { + return NULL; + } + return &itr->second; +} + +void CommandMgr::GetCommandHelpLocaleString(uint32 commandId, int32 loc_idx, std::string* namePtr) const +{ + if (loc_idx >= 0) + { + if (CommandHelpLocale const* il = GetCommandLocale(commandId)) + { + if (namePtr && il->HelpText.size() > size_t(loc_idx) && !il->HelpText[loc_idx].empty()) + { + *namePtr = il->HelpText[loc_idx]; + } + } + } +} diff --git a/src/game/WorldHandlers/CommandMgr.h b/src/game/WorldHandlers/CommandMgr.h new file mode 100644 index 00000000..ce553998 --- /dev/null +++ b/src/game/WorldHandlers/CommandMgr.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2015-2020 MaNGOS project + * Copyright (C) 2008-2015 TrinityCore + * Copyright (C) 2005-2009 MaNGOS + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 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 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 . + */ + +#ifndef COMMANDMGR_H +#define COMMANDMGR_H + + +struct CommandHelpLocale +{ + std::vector HelpText; +}; + +typedef UNORDERED_MAP CommandHelpLocaleMap; + + +class CommandMgr +{ + public: + CommandMgr(); + ~CommandMgr(); + + void LoadCommandHelpLocale(); + void GetCommandHelpLocaleString(uint32 entry, int32 loc_idx, std::string* namePtr) const; + + private: + CommandHelpLocale const* GetCommandLocale(uint32 commandId) const; + CommandHelpLocaleMap m_CommandHelpLocaleMap; +}; + + +#define sCommandMgr MaNGOS::Singleton::Instance() + +#endif \ No newline at end of file diff --git a/src/game/WorldHandlers/World.cpp b/src/game/WorldHandlers/World.cpp index 8e9962d7..dc15c137 100644 --- a/src/game/WorldHandlers/World.cpp +++ b/src/game/WorldHandlers/World.cpp @@ -72,6 +72,7 @@ #include "LFGMgr.h" #include "DisableMgr.h" #include "Language.h" +#include "CommandMgr.h" #include "revision.h" #ifdef ENABLE_ELUNA @@ -1301,6 +1302,7 @@ void World::SetInitialWorldSettings() sObjectMgr.LoadPageTextLocales(); // must be after PageText loading sObjectMgr.LoadGossipMenuItemsLocales(); // must be after gossip menu items loading sObjectMgr.LoadPointOfInterestLocales(); // must be after POI loading + sCommandMgr.LoadCommandHelpLocale(); sLog.outString(">>> Localization strings loaded"); sLog.outString(); diff --git a/src/shared/revision.h b/src/shared/revision.h index 2d7ab361..bbbca061 100644 --- a/src/shared/revision.h +++ b/src/shared/revision.h @@ -37,7 +37,7 @@ #define CHAR_DB_UPDATE_DESCRIPTION "Add_Field_Comments" #define WORLD_DB_VERSION_NR 21 - #define WORLD_DB_STRUCTURE_NR 19 + #define WORLD_DB_STRUCTURE_NR 20 #define WORLD_DB_CONTENT_NR 001 - #define WORLD_DB_UPDATE_DESCRIPTION "GM_tickets_handling_fixes_pt2" + #define WORLD_DB_UPDATE_DESCRIPTION "GM_Commands_localization" #endif // __REVISION_H__