diff --git a/dCommon/GeneralUtils.cpp b/dCommon/GeneralUtils.cpp index df641a1b3..99ca687a7 100644 --- a/dCommon/GeneralUtils.cpp +++ b/dCommon/GeneralUtils.cpp @@ -241,7 +241,7 @@ std::vector GeneralUtils::SplitString(std::wstring& str, wchar_t d return vector; } -std::vector GeneralUtils::SplitString(std::u16string& str, char16_t delimiter) { +std::vector GeneralUtils::SplitString(const std::u16string& str, char16_t delimiter) { std::vector vector = std::vector(); std::u16string current; diff --git a/dCommon/GeneralUtils.h b/dCommon/GeneralUtils.h index 25aac7837..0555a45e9 100644 --- a/dCommon/GeneralUtils.h +++ b/dCommon/GeneralUtils.h @@ -139,7 +139,7 @@ namespace GeneralUtils { std::vector SplitString(std::wstring& str, wchar_t delimiter); - std::vector SplitString(std::u16string& str, char16_t delimiter); + std::vector SplitString(const std::u16string& str, char16_t delimiter); std::vector SplitString(const std::string& str, char delimiter); diff --git a/dGame/dUtilities/SlashCommandHandler.cpp b/dGame/dUtilities/SlashCommandHandler.cpp index 6f346d880..8693abe9f 100644 --- a/dGame/dUtilities/SlashCommandHandler.cpp +++ b/dGame/dUtilities/SlashCommandHandler.cpp @@ -82,44 +82,24 @@ #include "CDZoneTableTable.h" void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entity* entity, const SystemAddress& sysAddr) { + auto commandCopy = command; + // Sanity check that a command was given + if (command.empty() || command.front() != u'/') return; + commandCopy.erase(commandCopy.begin()); + + // Split the command by spaces std::string chatCommand; std::vector args; + auto wideCommand = GeneralUtils::SplitString(commandCopy, u' '); + if (wideCommand.empty()) return; - uint32_t breakIndex = 0; - for (uint32_t i = 1; i < command.size(); ++i) { - if (command[i] == L' ') { - breakIndex = i; - break; - } - - chatCommand.push_back(static_cast(command[i])); - breakIndex++; - } - - uint32_t index = ++breakIndex; - while (true) { - std::string arg; - - while (index < command.size()) { - if (command[index] == L' ') { - args.push_back(arg); - arg = ""; - index++; - continue; - } - - arg.push_back(static_cast(command[index])); - index++; - } - - if (arg != "") { - args.push_back(arg); - } - - break; - } + // Convert the command to lowercase + chatCommand = GeneralUtils::UTF16ToWTF8(wideCommand.front()); + std::transform(chatCommand.begin(), chatCommand.end(), chatCommand.begin(), ::tolower); + wideCommand.erase(wideCommand.begin()); - //Game::logger->Log("SlashCommandHandler", "Received chat command \"%s\"", GeneralUtils::UTF16ToWTF8(command).c_str()); + // Convert the arguements to not u16strings + for (auto wideArg : wideCommand) args.push_back(GeneralUtils::UTF16ToWTF8(wideArg)); User* user = UserManager::Instance()->GetUser(sysAddr); if ((chatCommand == "setgmlevel" || chatCommand == "makegm" || chatCommand == "gmlevel") && user->GetMaxGMLevel() > eGameMasterLevel::CIVILIAN) {