Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom menu function case sensitivity #60

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/ObjLoading/Game/IW4/Menu/MenuConverterIW4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "Parsing/Simple/Expression/SimpleExpressionConditionalOperator.h"
#include "Parsing/Simple/Expression/SimpleExpressionUnaryOperation.h"
#include "Utils/ClassUtils.h"
#include "Utils/StringUtils.h"

#include <cassert>
#include <cstring>
Expand Down Expand Up @@ -204,12 +205,15 @@ namespace IW4
const CommonMenuDef* menu,
const CommonItemDef* item) const
{
Statement_s* functionStatement = m_conversion_zone_state->FindFunction(functionCall->m_function_name);
std::string lowerCaseFunctionName(functionCall->m_function_name);
utils::MakeStringLowerCase(lowerCaseFunctionName);

Statement_s* functionStatement = m_conversion_zone_state->FindFunction(lowerCaseFunctionName);

if (functionStatement == nullptr)
{
// Function was not converted yet: Convert it now
const auto foundCommonFunction = m_parsing_zone_state->m_functions_by_name.find(functionCall->m_function_name);
const auto foundCommonFunction = m_parsing_zone_state->m_functions_by_name.find(lowerCaseFunctionName);

if (foundCommonFunction == m_parsing_zone_state->m_functions_by_name.end())
throw MenuConversionException("Failed to find definition for custom function \"" + functionCall->m_function_name + "\"", menu, item);
Expand Down
10 changes: 7 additions & 3 deletions src/ObjLoading/Game/IW5/Menu/MenuConverterIW5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "Parsing/Simple/Expression/SimpleExpressionConditionalOperator.h"
#include "Parsing/Simple/Expression/SimpleExpressionUnaryOperation.h"
#include "Utils/ClassUtils.h"
#include "Utils/StringUtils.h"

#include <cassert>
#include <cstring>
Expand Down Expand Up @@ -204,18 +205,21 @@ namespace IW5
const CommonMenuDef* menu,
const CommonItemDef* item) const
{
Statement_s* functionStatement = m_conversion_zone_state->FindFunction(functionCall->m_function_name);
std::string lowerCaseFunctionName(functionCall->m_function_name);
utils::MakeStringLowerCase(lowerCaseFunctionName);

Statement_s* functionStatement = m_conversion_zone_state->FindFunction(lowerCaseFunctionName);

if (functionStatement == nullptr)
{
// Function was not converted yet: Convert it now
const auto foundCommonFunction = m_parsing_zone_state->m_functions_by_name.find(functionCall->m_function_name);
const auto foundCommonFunction = m_parsing_zone_state->m_functions_by_name.find(lowerCaseFunctionName);

if (foundCommonFunction == m_parsing_zone_state->m_functions_by_name.end())
throw MenuConversionException("Failed to find definition for custom function \"" + functionCall->m_function_name + "\"", menu, item);

functionStatement = ConvertExpression(foundCommonFunction->second->m_value.get(), menu, item);
functionStatement = m_conversion_zone_state->AddFunction(foundCommonFunction->second->m_name, functionStatement);
functionStatement = m_conversion_zone_state->AddFunction(lowerCaseFunctionName, functionStatement);
}

expressionEntry functionEntry{};
Expand Down
12 changes: 10 additions & 2 deletions src/ObjLoading/Parsing/Menu/Matcher/MenuExpressionMatchers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ const std::map<std::string, size_t>& MenuExpressionMatchers::GetBaseFunctionMapF
if (!iw4FunctionMapInitialized)
{
for (size_t i = IW4::expressionFunction_e::EXP_FUNC_DYN_START; i < std::extent_v<decltype(IW4::g_expFunctionNames)>; i++)
iw4FunctionMap.emplace(std::make_pair(IW4::g_expFunctionNames[i], i));
{
std::string functionName(IW4::g_expFunctionNames[i]);
utils::MakeStringLowerCase(functionName);
iw4FunctionMap.emplace(std::make_pair(functionName, i));
}
}

return iw4FunctionMap;
Expand All @@ -71,7 +75,11 @@ const std::map<std::string, size_t>& MenuExpressionMatchers::GetBaseFunctionMapF
if (!iw5FunctionMapInitialized)
{
for (size_t i = IW5::expressionFunction_e::EXP_FUNC_DYN_START; i < std::extent_v<decltype(IW5::g_expFunctionNames)>; i++)
iw5FunctionMap.emplace(std::make_pair(IW5::g_expFunctionNames[i], i));
{
std::string functionName(IW5::g_expFunctionNames[i]);
utils::MakeStringLowerCase(functionName);
iw5FunctionMap.emplace(std::make_pair(std::move(functionName), i));
}
}

return iw5FunctionMap;
Expand Down
7 changes: 6 additions & 1 deletion src/ObjLoading/Parsing/Menu/MenuAssetZoneState.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#include "MenuAssetZoneState.h"

#include "Utils/StringUtils.h"

using namespace menu;

void MenuAssetZoneState::AddFunction(std::unique_ptr<CommonFunctionDef> function)
{
m_functions_by_name.emplace(std::make_pair(function->m_name, function.get()));
std::string lowerCaseFunctionName(function->m_name);
utils::MakeStringLowerCase(lowerCaseFunctionName);

m_functions_by_name.emplace(std::make_pair(lowerCaseFunctionName, function.get()));
m_functions.emplace_back(std::move(function));
}

Expand Down
6 changes: 5 additions & 1 deletion src/ObjLoading/Parsing/Menu/MenuFileParserState.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "MenuFileParserState.h"

#include "Utils/StringUtils.h"

using namespace menu;

MenuFileParserState::EventHandlerConditionState::EventHandlerConditionState(CommonEventHandlerCondition* condition)
Expand Down Expand Up @@ -32,7 +34,9 @@ MenuFileParserState::MenuFileParserState(const FeatureLevel featureLevel, const
{
for (const auto& function : zoneState->m_functions)
{
m_functions_by_name.emplace(std::make_pair(function->m_name, function.get()));
std::string lowerCaseName(function->m_name);
utils::MakeStringLowerCase(lowerCaseName);
m_functions_by_name.emplace(std::make_pair(lowerCaseName, function.get()));
}

for (const auto& menu : zoneState->m_menus)
Expand Down
2 changes: 2 additions & 0 deletions src/ObjLoading/Parsing/Menu/MenuFileParserState.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ namespace menu
std::vector<std::unique_ptr<CommonFunctionDef>> m_functions;
std::vector<std::unique_ptr<CommonMenuDef>> m_menus;

// Function names are case-insensitive, therefore keys are always lowercase
std::map<std::string, CommonFunctionDef*> m_functions_by_name;

std::map<std::string, CommonMenuDef*> m_menus_by_name;

bool m_in_global_scope;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ namespace menu::function_scope_sequences
throw ParsingException(result.NextCapture(CAPTURE_TOKEN).GetPos(), ss.str());
}

const auto existingFunction = state->m_functions_by_name.find(state->m_current_function->m_name);
auto lowerCaseName = state->m_current_function->m_name;
utils::MakeStringLowerCase(lowerCaseName);

const auto existingFunction = state->m_functions_by_name.find(lowerCaseName);
if (existingFunction == state->m_functions_by_name.end())
{
state->m_functions_by_name.emplace(std::make_pair(state->m_current_function->m_name, state->m_current_function));
state->m_functions_by_name.emplace(std::make_pair(lowerCaseName, state->m_current_function));
state->m_current_function = nullptr;
}
else if (!state->m_current_function->m_value->Equals(existingFunction->second->m_value.get()))
Expand Down