Skip to content

Commit

Permalink
Made the console variables case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunkTreasure1 committed Dec 29, 2023
1 parent 91130e9 commit d6d7ea9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Volt/Sandbox/src/Sandbox/Window/LogPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void LogPanel::UpdateMainContent()
{
auto variable = Volt::ConsoleVariableRegistry::GetVariable(strings[0]);

std::string message = strings[0] + " = ";
std::string message = std::string(variable->GetName()) + " = ";

if (strings.size() > 1)
{
Expand Down
42 changes: 25 additions & 17 deletions Volt/Volt/src/Volt/Console/ConsoleVariableRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "Volt/Core/Base.h"

#include "Volt/Utility/StringUtility.h"

#include <unordered_map>
#include <string_view>

Expand All @@ -28,7 +30,7 @@ namespace Volt
class RegisteredConsoleVariable : public RegisteredConsoleVariableBase
{
public:
RegisteredConsoleVariable(std::string_view variableName, const T& defaultValue, std::string_view description);
RegisteredConsoleVariable(const std::string& variableName, const T& defaultValue, std::string_view description);

[[nodiscard]] const void* Get() const override;
void Set(const void* value) override;
Expand All @@ -42,7 +44,7 @@ namespace Volt

private:
T m_value;
std::string_view m_variableName;
std::string m_variableName;
std::string_view m_description;
};

Expand Down Expand Up @@ -99,27 +101,28 @@ namespace Volt
static Weak<RegisteredConsoleVariable<T>> RegisterVariable(std::string_view variableName, const T& defaultValue, std::string_view description);

template<ValidConsoleVariableType T>
static Weak<RegisteredConsoleVariable<T>> FindVariable(std::string_view variableName);
static Weak<RegisteredConsoleVariable<T>> FindVariable(const std::string& variableName);

inline static Weak<RegisteredConsoleVariableBase> GetVariable(std::string_view variableName);
inline static Weak<RegisteredConsoleVariableBase> GetVariable(const std::string& variableName);
inline static bool VariableExists(const std::string& variableName);

static std::unordered_map<std::string_view, Ref<RegisteredConsoleVariableBase>>& GetRegisteredVariables() { return s_registeredVariables; }
static std::unordered_map<std::string, Ref<RegisteredConsoleVariableBase>>& GetRegisteredVariables() { return s_registeredVariables; }

private:
ConsoleVariableRegistry() = delete;

inline static std::unordered_map<std::string_view, Ref<RegisteredConsoleVariableBase>> s_registeredVariables;
inline static std::unordered_map<std::string, Ref<RegisteredConsoleVariableBase>> s_registeredVariables;
};

inline bool ConsoleVariableRegistry::VariableExists(const std::string& variableName)
{
return s_registeredVariables.contains(std::string_view(variableName));
std::string tempVarName = ::Utility::ToLower(std::string(variableName));
return s_registeredVariables.contains(tempVarName);
}

template<ValidConsoleVariableType T>
inline RegisteredConsoleVariable<T>::RegisteredConsoleVariable(std::string_view variableName, const T& defaultValue, std::string_view description)
: m_value(defaultValue)
inline RegisteredConsoleVariable<T>::RegisteredConsoleVariable(const std::string& variableName, const T& defaultValue, std::string_view description)
: m_value(defaultValue), m_variableName(variableName), m_description(description)
{
}

Expand All @@ -144,20 +147,24 @@ namespace Volt
template<ValidConsoleVariableType T>
inline Weak<RegisteredConsoleVariable<T>> ConsoleVariableRegistry::RegisterVariable(std::string_view variableName, const T& defaultValue, std::string_view description)
{
Ref<RegisteredConsoleVariable<T>> consoleVariable = CreateRef<RegisteredConsoleVariable<T>>(variableName, defaultValue, description);
std::string tempVarName = ::Utility::ToLower(std::string(variableName));

Ref<RegisteredConsoleVariable<T>> consoleVariable = CreateRef<RegisteredConsoleVariable<T>>(tempVarName, defaultValue, description);

VT_CORE_ASSERT(!s_registeredVariables.contains(variableName), "Command variable with name already registered!");
s_registeredVariables[variableName] = consoleVariable;
VT_CORE_ASSERT(!s_registeredVariables.contains(tempVarName), "Command variable with name already registered!");
s_registeredVariables[tempVarName] = consoleVariable;

return consoleVariable;
}

template<ValidConsoleVariableType T>
inline Weak<RegisteredConsoleVariable<T>> ConsoleVariableRegistry::FindVariable(std::string_view variableName)
inline Weak<RegisteredConsoleVariable<T>> ConsoleVariableRegistry::FindVariable(const std::string& variableName)
{
if (s_registeredVariables.contains(variableName))
const std::string tempVarName = ::Utility::ToLower(variableName);

if (s_registeredVariables.contains(tempVarName))
{
return s_registeredVariables.at(variableName);
return s_registeredVariables.at(tempVarName);
}

return Weak<RegisteredConsoleVariable<T>>();
Expand All @@ -170,8 +177,9 @@ namespace Volt
VT_CORE_ASSERT(m_variableReference, "Variable with name not found!");
}

inline Weak<RegisteredConsoleVariableBase> ConsoleVariableRegistry::GetVariable(std::string_view variableName)
inline Weak<RegisteredConsoleVariableBase> ConsoleVariableRegistry::GetVariable(const std::string& variableName)
{
return s_registeredVariables.at(variableName);
const std::string tempVarName = ::Utility::ToLower(variableName);
return s_registeredVariables.at(tempVarName);
}
}
2 changes: 0 additions & 2 deletions Volt/Volt/src/Volt/Rendering/SceneRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@
#include "Volt/Utility/Noise.h"
#include "Volt/Platform/ThreadUtility.h"

#include "Volt/Console/ConsoleVariableRegistry.h"

namespace Volt
{
namespace Utility
Expand Down

0 comments on commit d6d7ea9

Please sign in to comment.