diff --git a/src/game/ChatCommands/Level3.cpp b/src/game/ChatCommands/Level3.cpp index 5ff3e49df..f2bd05e3f 100644 --- a/src/game/ChatCommands/Level3.cpp +++ b/src/game/ChatCommands/Level3.cpp @@ -64,6 +64,7 @@ #include "AuctionHouseBot/AuctionHouseBot.h" #include "SQLStorages.h" #include "DisableMgr.h" +#include "CommandMgr.h" static uint32 ahbotQualityIds[MAX_AUCTION_QUALITY] = { @@ -402,6 +403,7 @@ bool ChatHandler::HandleReloadAllLocalesCommand(char* /*args*/) HandleReloadLocalesPageTextCommand((char*)"a"); HandleReloadLocalesPointsOfInterestCommand((char*)"a"); HandleReloadLocalesQuestCommand((char*)"a"); + HandleReloadLocalesCommandHelpCommand((char*)"a"); return true; } @@ -1143,6 +1145,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."); + 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 ecae7fe9f..297133b92 100644 --- a/src/game/WorldHandlers/Chat.cpp +++ b/src/game/WorldHandlers/Chat.cpp @@ -40,6 +40,8 @@ #include "PoolManager.h" #include "GameEventMgr.h" #include "AuctionHouseBot/AuctionHouseBot.h" +#include "CommandMgr.h" + #ifdef ENABLE_ELUNA #include "LuaEngine.h" #endif /* ENABLE_ELUNA */ @@ -578,6 +580,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_level_reward", SEC_ADMINISTRATOR, true, &ChatHandler::HandleReloadMailLevelRewardCommand, "", NULL }, { "mail_loot_template", SEC_ADMINISTRATOR, true, &ChatHandler::HandleReloadLootTemplatesMailCommand, "", NULL }, { "mangos_string", SEC_ADMINISTRATOR, true, &ChatHandler::HandleReloadMangosStringCommand, "", NULL }, @@ -853,15 +856,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; @@ -1270,13 +1273,30 @@ void ChatHandler::ExecuteCommand(const char* text) else if (!HasSentErrorMessage()) { 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 + { SendSysMessage(LANG_CMD_SYNTAX); + } if (ChatCommand* showCommand = (strlen(command->Name) == 0 && parentCommand ? parentCommand : command)) + { if (ChatCommand* childs = showCommand->ChildCommands) + { ShowHelpForSubCommands(childs, showCommand->Name); + } + } SetSentErrorMessage(true); } @@ -1322,7 +1342,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. @@ -1339,6 +1359,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; @@ -1478,8 +1499,20 @@ 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) if (ShowHelpForSubCommands(childCommands, showCommand ? showCommand->Name : "")) diff --git a/src/game/WorldHandlers/Chat.h b/src/game/WorldHandlers/Chat.h index b5eda5c09..fdd088eca 100644 --- a/src/game/WorldHandlers/Chat.h +++ b/src/game/WorldHandlers/Chat.h @@ -54,12 +54,31 @@ 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; + 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 @@ -150,7 +169,7 @@ class ChatHandler void SendGlobalSysMessage(const char* str); - 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); @@ -340,6 +359,7 @@ class ChatHandler bool HandleLookupPlayerEmailCommand(char* args); bool HandleLookupPoolCommand(char* args); bool HandleLookupQuestCommand(char* args); + bool HandleReloadLocalesCommandHelpCommand(char* args); bool HandleLookupSkillCommand(char* args); bool HandleLookupSpellCommand(char* args); bool HandleLookupTaxiNodeCommand(char* args); diff --git a/src/game/WorldHandlers/CommandMgr.cpp b/src/game/WorldHandlers/CommandMgr.cpp new file mode 100644 index 000000000..e75a71f4f --- /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 000000000..ce5539982 --- /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 b2b2b113c..fbcdefb65 100644 --- a/src/game/WorldHandlers/World.cpp +++ b/src/game/WorldHandlers/World.cpp @@ -79,6 +79,7 @@ #include "revision.h" #include "Language.h" #include "DisableMgr.h" +#include "CommandMgr.h" #ifdef ENABLE_ELUNA #include "LuaEngine.h" @@ -1399,6 +1400,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 110ef335e..5079e0ede 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 11 - #define WORLD_DB_CONTENT_NR 041 - #define WORLD_DB_UPDATE_DESCRIPTION "initial_cleanup_part3" + #define WORLD_DB_STRUCTURE_NR 12 + #define WORLD_DB_CONTENT_NR 001 + #define WORLD_DB_UPDATE_DESCRIPTION "GM_Commands_localization" #endif // __REVISION_H__