From 0cc4691574372b563bfffa9e6f27f0d83bf19d2e Mon Sep 17 00:00:00 2001 From: Anthony Date: Fri, 10 Jan 2025 05:12:01 -0500 Subject: [PATCH] Case-insensitive server console commands/settings (#759) * feat: Case-insensitive server console commands/settings * use std::transform --- Code/components/console/ConsoleRegistry.cpp | 30 ++++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/Code/components/console/ConsoleRegistry.cpp b/Code/components/console/ConsoleRegistry.cpp index 1e3e4fb3d..ee587c249 100644 --- a/Code/components/console/ConsoleRegistry.cpp +++ b/Code/components/console/ConsoleRegistry.cpp @@ -42,10 +42,10 @@ std::unique_ptr SplitLineTokens(const TiltedPhoques::St std::optional BoolifyString(std::string_view view) { // let the compiler optimize this. - if (view == "true" || view == "TRUE" || view == "1") + if (view == "true" || view == "True" || view == "TRUE" || view == "1") return true; - if (view == "false" || view == "FALSE" || view == "0") + if (view == "false" || view == "False" || view == "FALSE" || view == "0") return false; return std::nullopt; @@ -183,7 +183,18 @@ void ConsoleRegistry::AddSetting(TiltedPhoques::UniquePtr apSetting CommandBase* ConsoleRegistry::FindCommand(const char* acName) { // TODO: Maybe lookup by some hash... - auto it = std::find_if(m_commands.begin(), m_commands.end(), [&](CommandBase* apCommand) { return std::strcmp(apCommand->m_name, acName) == 0; }); + auto it = std::find_if( + m_commands.begin(), m_commands.end(), + [&](CommandBase* apCommand) + { + TiltedPhoques::String findName(acName); + TiltedPhoques::String settingName(apCommand->m_name); + + std::transform(findName.begin(), findName.end(), findName.begin(), [](unsigned char c) { return std::tolower(c); }); + std::transform(settingName.begin(), settingName.end(), settingName.begin(), [](unsigned char c) { return std::tolower(c); }); + + return std::strcmp(settingName.c_str(), findName.c_str()) == 0; + }); if (it == m_commands.end()) return nullptr; return *it; @@ -192,7 +203,18 @@ CommandBase* ConsoleRegistry::FindCommand(const char* acName) SettingBase* ConsoleRegistry::FindSetting(const char* acName) { // TODO: Maybe lookup by some hash... - auto it = std::find_if(m_settings.begin(), m_settings.end(), [&](SettingBase* apSetting) { return std::strcmp(apSetting->name, acName) == 0; }); + auto it = std::find_if( + m_settings.begin(), m_settings.end(), + [&](SettingBase* apSetting) + { + TiltedPhoques::String findName(acName); + TiltedPhoques::String settingName(apSetting->name); + + std::transform(findName.begin(), findName.end(), findName.begin(), [](unsigned char c) { return std::tolower(c); }); + std::transform(settingName.begin(), settingName.end(), settingName.begin(), [](unsigned char c) { return std::tolower(c); }); + + return std::strcmp(settingName.c_str(), findName.c_str()) == 0; + }); if (it == m_settings.end()) return nullptr; return *it;