Skip to content

Commit

Permalink
Allow case insensitive commands (#1047)
Browse files Browse the repository at this point in the history
  • Loading branch information
EmosewaMC authored Apr 8, 2023
1 parent 5412501 commit 33f9e9c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 36 deletions.
2 changes: 1 addition & 1 deletion dCommon/GeneralUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ std::vector<std::wstring> GeneralUtils::SplitString(std::wstring& str, wchar_t d
return vector;
}

std::vector<std::u16string> GeneralUtils::SplitString(std::u16string& str, char16_t delimiter) {
std::vector<std::u16string> GeneralUtils::SplitString(const std::u16string& str, char16_t delimiter) {
std::vector<std::u16string> vector = std::vector<std::u16string>();
std::u16string current;

Expand Down
2 changes: 1 addition & 1 deletion dCommon/GeneralUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ namespace GeneralUtils {

std::vector<std::wstring> SplitString(std::wstring& str, wchar_t delimiter);

std::vector<std::u16string> SplitString(std::u16string& str, char16_t delimiter);
std::vector<std::u16string> SplitString(const std::u16string& str, char16_t delimiter);

std::vector<std::string> SplitString(const std::string& str, char delimiter);

Expand Down
48 changes: 14 additions & 34 deletions dGame/dUtilities/SlashCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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<unsigned char>(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<char>(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) {
Expand Down

0 comments on commit 33f9e9c

Please sign in to comment.