feat: IW3 menu dumping and loading (#909)

* feat: IW3 menu dumping

* fix: IW3 menu dumper preserve menu ownerdraw flag masks

* fix: IW3 menu dumper preserve numeric script arguments

Emit numeric tokens without quotes so the linker keeps colour arguments separate.

Before: `"0.1" "0.1" "0.12" "0.5"` becomes `"0.10.10.120.5"`
After: `0.1 0.1 0.12 0.5`

This fixes `mouseExit` colour commands failing to clear menu hover borders.

* fix: IW3 menu dumper preserve empty item text

Keep explicit empty text distinct from null text when dumping menu items.

This fixes CD-key values overflowing their input boxes.

* test: cover IW3 menu dumper material case

* test: cover IW3 menu list path precedence

* chore: clang format

* fix: reverse order of union to avoid edge case bug in zcg

* feat: IW3 menu loading

* fix: validate menu expression name table

* chore: explain numeric token quoting in comment

* refactor: simplify menu dump fallback path

* chore: add more dynamic window flags

* refactor: reuse IW3 menu window flag constants

* refactor: use explicit menu item feature lookup

* refactor: represent menu item text as optional

* refactor: clarify shared IW3 and IW4 menu constants

IW4 only extends the type enum so they can be shared

* refactor: clarify IW3 menu zone state naming

* fix: log IW3 menu conversion failures

* refactor: align IW3 menu list loader with IW4

* refactor: align IW3 menu dumpers with IW4

* refactor: align IW3 menu converter with IW4

* chore: use default enum numeration for operationEnum

* chore: small optimization on edit field capability initialization

* fix: iw3 menu optimizations for rect not considering relative position to parent

* chore: align iw3 menu converting closer to iw4

* chore: align iw3 menu dumping closer to iw4

* fix: not dumping menu flags properties correctly

* chore: adjust iw3 expression dumping to work similar to iw4

* chore: adjust iw3 expression converting to work similar to iw4

* chore: use unordered_map for MenuExpressionMatchers

---------

Co-authored-by: Jan Laupetin <[email protected]>
This commit is contained in:
mo
2026-08-31 00:04:01 +02:00
committed by GitHub
co-authored by Jan Laupetin
parent 4d4921e230
commit 51770e7ae5
42 changed files with 3218 additions and 60 deletions
@@ -5,6 +5,7 @@
#include "Parsing/Simple/Expression/ISimpleExpression.h"
#include <map>
#include <optional>
#include <string>
#include <vector>
@@ -92,7 +93,7 @@ namespace menu
};
std::string m_name;
std::string m_text;
std::optional<std::string> m_text;
bool m_text_save_game = false;
bool m_text_cinematic_subtitle = false;
std::string m_group;
@@ -106,7 +107,7 @@ namespace menu
int m_border = 0;
double m_border_size = 0;
int m_owner_draw = 0;
int m_owner_draw_flags = 0;
unsigned m_owner_draw_flags = 0;
int m_align = 0;
int m_text_align = 0;
double m_text_align_x = 0;
@@ -24,10 +24,11 @@ namespace menu
CommonColor m_fore_color = CommonColor(1.0, 1.0, 1.0, 1.0);
CommonColor m_border_color;
CommonColor m_focus_color;
CommonColor m_disable_color;
CommonColor m_outline_color;
std::string m_background;
int m_owner_draw = 0;
int m_owner_draw_flags = 0;
unsigned m_owner_draw_flags = 0;
std::string m_sound_loop;
std::unique_ptr<ISimpleExpression> m_sound_loop_exp;
double m_fade_clamp = 0;
@@ -4,6 +4,7 @@ namespace menu
{
enum class FeatureLevel
{
IW3,
IW4,
IW5
};
@@ -1,5 +1,7 @@
#include "MenuExpressionMatchers.h"
#include "Game/IW3/IW3.h"
#include "Game/IW3/MenuConstantsIW3.h"
#include "Game/IW4/IW4.h"
#include "Game/IW4/MenuConstantsIW4.h"
#include "Game/IW5/IW5.h"
@@ -48,11 +50,29 @@ std::unique_ptr<SimpleExpressionMatchers::matcher_t> MenuExpressionMatchers::Par
});
}
const std::map<std::string, size_t>& MenuExpressionMatchers::GetBaseFunctionMapForFeatureLevel(const FeatureLevel featureLevel)
const std::unordered_map<std::string, size_t>& MenuExpressionMatchers::GetBaseFunctionMapForFeatureLevel(const FeatureLevel featureLevel)
{
if (featureLevel == FeatureLevel::IW3)
{
static std::unordered_map<std::string, size_t> iw3FunctionMap;
static bool iw3FunctionMapInitialized = false;
if (!iw3FunctionMapInitialized)
{
for (size_t i = IW3::OP_FIRSTFUNCTIONCALL; i < std::extent_v<decltype(IW3::g_expFunctionNames)>; i++)
{
std::string functionName(IW3::g_expFunctionNames[i]);
utils::MakeStringLowerCase(functionName);
iw3FunctionMap.emplace(std::make_pair(std::move(functionName), i));
}
}
return iw3FunctionMap;
}
if (featureLevel == FeatureLevel::IW4)
{
static std::map<std::string, size_t> iw4FunctionMap;
static std::unordered_map<std::string, size_t> iw4FunctionMap;
static bool iw4FunctionMapInitialized = false;
if (!iw4FunctionMapInitialized)
@@ -69,7 +89,7 @@ const std::map<std::string, size_t>& MenuExpressionMatchers::GetBaseFunctionMapF
}
if (featureLevel == FeatureLevel::IW5)
{
static std::map<std::string, size_t> iw5FunctionMap;
static std::unordered_map<std::string, size_t> iw5FunctionMap;
static bool iw5FunctionMapInitialized = false;
if (!iw5FunctionMapInitialized)
@@ -4,15 +4,12 @@
#include "Parsing/Simple/Expression/SimpleExpressionMatchers.h"
#include <memory>
#include <unordered_map>
namespace menu
{
class MenuExpressionMatchers final : public SimpleExpressionMatchers
{
const MenuFileParserState* m_state;
static const std::map<std::string, size_t>& GetBaseFunctionMapForFeatureLevel(FeatureLevel featureLevel);
public:
MenuExpressionMatchers();
explicit MenuExpressionMatchers(const MenuFileParserState* state);
@@ -20,5 +17,10 @@ namespace menu
protected:
std::unique_ptr<matcher_t> ParseOperandExtension(const supplier_t* labelSupplier) const override;
std::unique_ptr<ISimpleExpression> ProcessOperandExtension(SequenceResult<SimpleParserValue>& result) const override;
private:
const MenuFileParserState* m_state;
static const std::unordered_map<std::string, size_t>& GetBaseFunctionMapForFeatureLevel(FeatureLevel featureLevel);
};
} // namespace menu
@@ -39,6 +39,9 @@ void MenuFileReader::SetupDefinesProxy()
defines->AddDefine(DefinesStreamProxy::Define("PC", "1"));
switch (m_feature_level)
{
case FeatureLevel::IW3:
defines->AddDefine(DefinesStreamProxy::Define("FEATURE_LEVEL_IW3", "1"));
break;
case FeatureLevel::IW4:
defines->AddDefine(DefinesStreamProxy::Define("FEATURE_LEVEL_IW4", "1"));
break;
@@ -281,7 +281,7 @@ namespace
AddMatchers({
create
.Or({
create.Numeric(),
create.ScriptStrictNumeric(),
create.String(),
create.Identifier(),
create.Type(SimpleParserValueType::CHARACTER),
@@ -928,6 +928,15 @@ EventHandlerSetScopeSequences::EventHandlerSetScopeSequences(std::vector<std::un
void EventHandlerSetScopeSequences::AddSequences(const FeatureLevel featureLevel, const bool permissive) const
{
AddSequence(std::make_unique<SequenceSkipEmptyStatements>());
// IW3 stores event handlers as opaque script strings rather than structured handlers.
if (featureLevel == FeatureLevel::IW3)
{
AddSequence(std::make_unique<SequenceCloseBlock>());
AddSequence(std::make_unique<SequenceSkipScriptToken>());
return;
}
// If else and stuff
// Creating factory with no label supplier. Cannot use labels with it.
@@ -109,7 +109,8 @@ GlobalScopeSequences::GlobalScopeSequences(std::vector<std::unique_ptr<MenuFileP
void GlobalScopeSequences::AddSequences(const FeatureLevel featureLevel, const bool permissive) const
{
AddSequence(std::make_unique<SequenceCloseBlock>());
AddSequence(std::make_unique<SequenceFunctionDef>());
if (featureLevel != FeatureLevel::IW3)
AddSequence(std::make_unique<SequenceFunctionDef>());
AddSequence(std::make_unique<SequenceMenuDef>());
AddSequence(std::make_unique<SequenceLoadMenu>());
}
@@ -24,7 +24,7 @@ namespace
class ItemScopeOperations
{
inline static const CommonItemFeatureType IW4_FEATURE_TYPE_BY_TYPE[0x18]{
inline static const CommonItemFeatureType IW3_IW4_FEATURE_TYPE_BY_TYPE[0x18]{
CommonItemFeatureType::EDIT_FIELD, // ITEM_TYPE_TEXT
CommonItemFeatureType::NONE, // ITEM_TYPE_BUTTON
CommonItemFeatureType::NONE, // ITEM_TYPE_RADIOBUTTON
@@ -78,32 +78,34 @@ namespace
CommonItemFeatureType::EDIT_FIELD, // ITEM_TYPE_PASSWORDFIELD
};
public:
static void SetItemType(CommonItemDef& item, const FeatureLevel featureLevel, const TokenPos& pos, const int type)
static CommonItemFeatureType GetFeatureTypeForItemType(const FeatureLevel featureLevel, const TokenPos& pos, const int type)
{
if (type < 0)
throw ParsingException(pos, "Invalid item type");
if (item.m_feature_type != CommonItemFeatureType::NONE)
throw ParsingException(pos, "Item type has already been set");
item.m_type = type;
switch (featureLevel)
{
case FeatureLevel::IW3:
case FeatureLevel::IW4:
if (static_cast<unsigned>(type) >= std::extent_v<decltype(IW4_FEATURE_TYPE_BY_TYPE)>)
if (static_cast<unsigned>(type) >= std::extent_v<decltype(IW3_IW4_FEATURE_TYPE_BY_TYPE)>)
throw ParsingException(pos, "Invalid item type");
item.m_feature_type = IW4_FEATURE_TYPE_BY_TYPE[static_cast<unsigned>(type)];
break;
return IW3_IW4_FEATURE_TYPE_BY_TYPE[static_cast<unsigned>(type)];
case FeatureLevel::IW5:
default:
if (static_cast<unsigned>(type) >= std::extent_v<decltype(IW5_FEATURE_TYPE_BY_TYPE)>)
throw ParsingException(pos, "Invalid item type");
item.m_feature_type = IW5_FEATURE_TYPE_BY_TYPE[static_cast<unsigned>(type)];
break;
return IW5_FEATURE_TYPE_BY_TYPE[static_cast<unsigned>(type)];
}
}
public:
static void SetItemType(CommonItemDef& item, const FeatureLevel featureLevel, const TokenPos& pos, const int type)
{
const auto featureType = GetFeatureTypeForItemType(featureLevel, pos, type);
if (item.m_feature_type != CommonItemFeatureType::NONE)
throw ParsingException(pos, "Item type has already been set");
item.m_type = type;
item.m_feature_type = featureType;
switch (item.m_feature_type)
{
@@ -130,9 +132,16 @@ namespace
throw ParsingException(pos, "Item must have be listbox to use this declaration");
}
static void EnsureHasEditFieldFeatures(const CommonItemDef& item, const TokenPos& pos)
static void EnsureHasEditFieldFeatures(CommonItemDef& item, const FeatureLevel featureLevel, const TokenPos& pos)
{
if (item.m_feature_type != CommonItemFeatureType::EDIT_FIELD || !item.m_edit_field_features)
// Due to default item types, edit field capabilities might not have been initialized
if (item.m_feature_type == CommonItemFeatureType::NONE
&& GetFeatureTypeForItemType(featureLevel, pos, item.m_type) == CommonItemFeatureType::EDIT_FIELD)
{
item.m_feature_type = CommonItemFeatureType::EDIT_FIELD;
item.m_edit_field_features = std::make_unique<CommonItemFeaturesEditField>();
}
else if (item.m_feature_type != CommonItemFeatureType::EDIT_FIELD || !item.m_edit_field_features)
throw ParsingException(pos, "Item must have be edit field to use this declaration");
}
@@ -360,7 +369,7 @@ namespace
{
assert(state->m_current_item);
ItemScopeOperations::EnsureHasEditFieldFeatures(*state->m_current_item, result.NextCapture(CAPTURE_FIRST_TOKEN).GetPos());
ItemScopeOperations::EnsureHasEditFieldFeatures(*state->m_current_item, state->m_feature_level, result.NextCapture(CAPTURE_FIRST_TOKEN).GetPos());
state->m_current_item->m_dvar = MenuMatcherFactory::TokenTextValue(result.NextCapture(CAPTURE_DVAR_NAME));
state->m_current_item->m_edit_field_features->m_def_val = MenuMatcherFactory::TokenNumericExpressionValue(state, result);
state->m_current_item->m_edit_field_features->m_min_val = MenuMatcherFactory::TokenNumericExpressionValue(state, result);
@@ -455,8 +464,8 @@ namespace
static constexpr auto CAPTURE_FIRST_TOKEN = 1;
static constexpr auto CAPTURE_COLUMN_COUNT = 2;
static constexpr const char* SYNTAX_IW4 = "<xpos> <width> <maxChars> [alignment]";
static constexpr auto BASE_COLUMN_COUNT_IW4 = 3;
static constexpr const char* SYNTAX_IW3_IW4 = "<xpos> <width> <maxChars> [alignment]";
static constexpr auto BASE_COLUMN_COUNT_IW3_IW4 = 3;
static constexpr const char* SYNTAX_IW5 = "<xpos> <ypos> <width> <height> <maxChars> [alignment]";
static constexpr auto BASE_COLUMN_COUNT_IW5 = 5;
@@ -478,7 +487,7 @@ namespace
void ProcessMatch(MenuFileParserState* state, SequenceResult<SimpleParserValue>& result) const override
{
assert(state->m_current_item);
assert(state->m_feature_level == FeatureLevel::IW4 || state->m_feature_level == FeatureLevel::IW5);
assert(state->m_feature_level == FeatureLevel::IW3 || state->m_feature_level == FeatureLevel::IW4 || state->m_feature_level == FeatureLevel::IW5);
const auto& firstToken = result.NextCapture(CAPTURE_FIRST_TOKEN);
ItemScopeOperations::EnsureHasListboxFeatures(*state->m_current_item, firstToken.GetPos());
@@ -491,10 +500,10 @@ namespace
size_t baseColumnCount;
const char* syntax;
bool hasHeightValues;
if (state->m_feature_level == FeatureLevel::IW4)
if (state->m_feature_level == FeatureLevel::IW3 || state->m_feature_level == FeatureLevel::IW4)
{
baseColumnCount = BASE_COLUMN_COUNT_IW4;
syntax = SYNTAX_IW4;
baseColumnCount = BASE_COLUMN_COUNT_IW3_IW4;
syntax = SYNTAX_IW3_IW4;
hasHeightValues = false;
}
else
@@ -1151,25 +1160,29 @@ void ItemScopeSequences::AddSequences(const FeatureLevel featureLevel, const boo
AddSequence(std::make_unique<GenericStringPropertySequence>("localvar",
[](const MenuFileParserState* state, const TokenPos& pos, const std::string& value)
{
ItemScopeOperations::EnsureHasEditFieldFeatures(*state->m_current_item, pos);
ItemScopeOperations::EnsureHasEditFieldFeatures(
*state->m_current_item, state->m_feature_level, pos);
state->m_current_item->m_edit_field_features->m_local_var = value;
}));
AddSequence(std::make_unique<GenericIntPropertySequence>("maxChars",
[](const MenuFileParserState* state, const TokenPos& pos, const int value)
{
ItemScopeOperations::EnsureHasEditFieldFeatures(*state->m_current_item, pos);
ItemScopeOperations::EnsureHasEditFieldFeatures(
*state->m_current_item, state->m_feature_level, pos);
state->m_current_item->m_edit_field_features->m_max_chars = value;
}));
AddSequence(std::make_unique<GenericIntPropertySequence>("maxPaintChars",
[](const MenuFileParserState* state, const TokenPos& pos, const int value)
{
ItemScopeOperations::EnsureHasEditFieldFeatures(*state->m_current_item, pos);
ItemScopeOperations::EnsureHasEditFieldFeatures(
*state->m_current_item, state->m_feature_level, pos);
state->m_current_item->m_edit_field_features->m_max_paint_chars = value;
}));
AddSequence(std::make_unique<GenericKeywordPropertySequence>("maxCharsGotoNext",
[](const MenuFileParserState* state, const TokenPos& pos)
{
ItemScopeOperations::EnsureHasEditFieldFeatures(*state->m_current_item, pos);
ItemScopeOperations::EnsureHasEditFieldFeatures(
*state->m_current_item, state->m_feature_level, pos);
state->m_current_item->m_edit_field_features->m_max_chars_goto_next = true;
}));
@@ -309,6 +309,11 @@ void MenuScopeSequences::AddSequences(const FeatureLevel featureLevel, const boo
{
state->m_current_menu->m_focus_color = value;
}));
AddSequence(std::make_unique<GenericColorPropertySequence>("disablecolor",
[](const MenuFileParserState* state, const TokenPos&, const CommonColor value)
{
state->m_current_menu->m_disable_color = value;
}));
AddSequence(std::make_unique<GenericColorPropertySequence>("outlinecolor",
[](const MenuFileParserState* state, const TokenPos&, const CommonColor& value)
{