2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-11-30 08:17:48 +00:00

refactor: streamline menu dumping

This commit is contained in:
Jan Laupetin
2025-08-03 19:53:30 +02:00
parent 250556ab61
commit a442d2e49e
27 changed files with 2805 additions and 2772 deletions

View File

@@ -1,298 +0,0 @@
#include "AbstractMenuDumper.h"
#include "Parsing/Impl/ParserSingleInputStream.h"
#include "Parsing/Simple/SimpleLexer.h"
#include <algorithm>
#include <cmath>
#include <sstream>
AbstractMenuDumper::AbstractMenuDumper(std::ostream& stream)
: m_stream(stream),
m_indent(0u)
{
}
void AbstractMenuDumper::IncIndent()
{
m_indent++;
}
void AbstractMenuDumper::DecIndent()
{
if (m_indent > 0)
m_indent--;
}
void AbstractMenuDumper::Indent() const
{
for (auto i = 0u; i < m_indent; i++)
m_stream << " ";
}
void AbstractMenuDumper::StartScope(const std::string& scopeName)
{
Indent();
m_stream << scopeName << "\n";
Indent();
m_stream << "{\n";
IncIndent();
}
void AbstractMenuDumper::StartMenuDefScope()
{
StartScope("menuDef");
}
void AbstractMenuDumper::StartItemDefScope()
{
StartScope("itemDef");
}
void AbstractMenuDumper::StartFunctionDefScope()
{
StartScope("functionDef");
}
void AbstractMenuDumper::EndScope()
{
DecIndent();
Indent();
m_stream << "}\n";
}
std::vector<std::string> AbstractMenuDumper::CreateScriptTokenList(const char* script)
{
const std::string scriptString(script);
std::istringstream stringStream(scriptString);
ParserSingleInputStream inputStream(stringStream, "MenuScript");
SimpleLexer::Config lexerConfig;
lexerConfig.m_emit_new_line_tokens = false;
lexerConfig.m_read_strings = true;
lexerConfig.m_string_escape_sequences = true;
lexerConfig.m_read_integer_numbers = false;
lexerConfig.m_read_floating_point_numbers = false;
SimpleLexer lexer(&inputStream, std::move(lexerConfig));
std::vector<std::string> result;
auto hasLexerTokens = true;
while (hasLexerTokens)
{
const auto& token = lexer.GetToken(0);
switch (token.m_type)
{
case SimpleParserValueType::IDENTIFIER:
result.emplace_back(token.IdentifierValue());
break;
case SimpleParserValueType::STRING:
result.emplace_back(token.StringValue());
break;
case SimpleParserValueType::CHARACTER:
result.emplace_back(1, token.CharacterValue());
break;
case SimpleParserValueType::INVALID:
case SimpleParserValueType::END_OF_FILE:
hasLexerTokens = false;
break;
default:
assert(false);
break;
}
lexer.PopTokens(1);
}
return result;
}
bool AbstractMenuDumper::DoesTokenNeedQuotationMarks(const std::string& token)
{
if (token.empty())
return true;
const auto hasAlNumCharacter = std::ranges::any_of(token,
[](const char& c)
{
return isalnum(c);
});
if (!hasAlNumCharacter)
return false;
const auto hasNonIdentifierCharacter = std::ranges::any_of(token,
[](const char& c)
{
return !isalnum(c) && c != '_';
});
return hasNonIdentifierCharacter;
}
void AbstractMenuDumper::WriteEscapedString(const std::string_view& str) const
{
m_stream << "\"";
for (const auto& c : str)
{
switch (c)
{
case '\r':
m_stream << "\\r";
break;
case '\n':
m_stream << "\\n";
break;
case '\t':
m_stream << "\\t";
break;
case '\f':
m_stream << "\\f";
break;
case '"':
m_stream << "\\\"";
break;
default:
m_stream << c;
break;
}
}
m_stream << "\"";
}
const std::string& AbstractMenuDumper::BoolValue(const bool value)
{
return value ? BOOL_VALUE_TRUE : BOOL_VALUE_FALSE;
}
void AbstractMenuDumper::WriteKey(const std::string& keyName) const
{
m_stream << keyName;
if (keyName.size() < MENU_KEY_SPACING)
{
const auto spacingLength = MENU_KEY_SPACING - keyName.size();
for (auto i = 0u; i < spacingLength; i++)
m_stream << " ";
}
}
void AbstractMenuDumper::WriteStringProperty(const std::string& propertyKey, const std::string& propertyValue) const
{
if (propertyValue.empty())
return;
Indent();
WriteKey(propertyKey);
WriteEscapedString(propertyValue);
m_stream << "\n";
}
void AbstractMenuDumper::WriteStringProperty(const std::string& propertyKey, const char* propertyValue) const
{
if (propertyValue == nullptr || propertyValue[0] == '\0')
return;
Indent();
WriteKey(propertyKey);
WriteEscapedString(propertyValue);
m_stream << "\n";
}
void AbstractMenuDumper::WriteBoolProperty(const std::string& propertyKey, const bool propertyValue, const bool defaultValue) const
{
if (propertyValue == defaultValue)
return;
Indent();
WriteKey(propertyKey);
m_stream << BoolValue(propertyValue) << "\n";
}
void AbstractMenuDumper::WriteIntProperty(const std::string& propertyKey, const int propertyValue, const int defaultValue) const
{
if (propertyValue == defaultValue)
return;
Indent();
WriteKey(propertyKey);
m_stream << propertyValue << "\n";
}
void AbstractMenuDumper::WriteFloatProperty(const std::string& propertyKey, const float propertyValue, const float defaultValue) const
{
if (std::fabs(propertyValue - defaultValue) < std::numeric_limits<float>::epsilon())
return;
Indent();
WriteKey(propertyKey);
m_stream << propertyValue << "\n";
}
void AbstractMenuDumper::WriteColorProperty(const std::string& propertyKey, const float (&propertyValue)[4], const float (&defaultValue)[4]) const
{
if (std::fabs(propertyValue[0] - defaultValue[0]) < std::numeric_limits<float>::epsilon()
&& std::fabs(propertyValue[1] - defaultValue[1]) < std::numeric_limits<float>::epsilon()
&& std::fabs(propertyValue[2] - defaultValue[2]) < std::numeric_limits<float>::epsilon()
&& std::fabs(propertyValue[3] - defaultValue[3]) < std::numeric_limits<float>::epsilon())
{
return;
}
Indent();
WriteKey(propertyKey);
m_stream << propertyValue[0] << " " << propertyValue[1] << " " << propertyValue[2] << " " << propertyValue[3] << "\n";
}
void AbstractMenuDumper::WriteKeywordProperty(const std::string& propertyKey, const bool shouldWrite) const
{
if (!shouldWrite)
return;
Indent();
WriteKey(propertyKey);
m_stream << "\n";
}
void AbstractMenuDumper::WriteFlagsProperty(const std::string& propertyKey, const int flagsValue) const
{
for (auto i = 0u; i < sizeof(flagsValue) * 8; i++)
{
if (flagsValue & (1 << i))
{
Indent();
WriteKey(propertyKey);
m_stream << i << "\n";
}
}
}
void AbstractMenuDumper::Start()
{
Indent();
m_stream << "{\n";
IncIndent();
}
void AbstractMenuDumper::End()
{
for (auto i = 0u; i < m_indent; i++)
{
DecIndent();
Indent();
m_stream << "}\n";
}
}
void AbstractMenuDumper::IncludeMenu(const std::string& menuPath) const
{
Indent();
m_stream << "loadMenu { \"" << menuPath << "\" }\n";
}

View File

@@ -1,53 +0,0 @@
#pragma once
#include <cstddef>
#include <ostream>
#include <string>
#include <vector>
class AbstractMenuDumper
{
protected:
static constexpr auto MENU_KEY_SPACING = 28u;
static const inline std::string BOOL_VALUE_TRUE = "1";
static const inline std::string BOOL_VALUE_FALSE = "0";
static constexpr inline float COLOR_0000[4]{0.0f, 0.0f, 0.0f, 0.0f};
static constexpr inline float COLOR_1111[4]{1.0f, 1.0f, 1.0f, 1.0f};
std::ostream& m_stream;
size_t m_indent;
void IncIndent();
void DecIndent();
void Indent() const;
void StartScope(const std::string& scopeName);
void StartMenuDefScope();
void StartItemDefScope();
void StartFunctionDefScope();
void EndScope();
static std::vector<std::string> CreateScriptTokenList(const char* script);
static bool DoesTokenNeedQuotationMarks(const std::string& token);
void WriteEscapedString(const std::string_view& str) const;
static const std::string& BoolValue(bool value);
void WriteKey(const std::string& keyName) const;
void WriteStringProperty(const std::string& propertyKey, const std::string& propertyValue) const;
void WriteStringProperty(const std::string& propertyKey, const char* propertyValue) const;
void WriteBoolProperty(const std::string& propertyKey, bool propertyValue, bool defaultValue) const;
void WriteIntProperty(const std::string& propertyKey, int propertyValue, int defaultValue) const;
void WriteFloatProperty(const std::string& propertyKey, float propertyValue, float defaultValue) const;
void WriteColorProperty(const std::string& propertyKey, const float (&propertyValue)[4], const float (&defaultValue)[4]) const;
void WriteKeywordProperty(const std::string& propertyKey, bool shouldWrite) const;
void WriteFlagsProperty(const std::string& propertyKey, int flagsValue) const;
explicit AbstractMenuDumper(std::ostream& stream);
public:
void Start();
void End();
void IncludeMenu(const std::string& menuPath) const;
};

View File

@@ -0,0 +1,301 @@
#include "AbstractMenuWriter.h"
#include "Parsing/Impl/ParserSingleInputStream.h"
#include "Parsing/Simple/SimpleLexer.h"
#include <algorithm>
#include <cmath>
#include <sstream>
namespace menu
{
AbstractBaseWriter::AbstractBaseWriter(std::ostream& stream)
: m_stream(stream),
m_indent(0u)
{
}
void AbstractBaseWriter::IncIndent()
{
m_indent++;
}
void AbstractBaseWriter::DecIndent()
{
if (m_indent > 0)
m_indent--;
}
void AbstractBaseWriter::Indent() const
{
for (auto i = 0u; i < m_indent; i++)
m_stream << " ";
}
void AbstractBaseWriter::StartScope(const std::string& scopeName)
{
Indent();
m_stream << scopeName << "\n";
Indent();
m_stream << "{\n";
IncIndent();
}
void AbstractBaseWriter::StartMenuDefScope()
{
StartScope("menuDef");
}
void AbstractBaseWriter::StartItemDefScope()
{
StartScope("itemDef");
}
void AbstractBaseWriter::StartFunctionDefScope()
{
StartScope("functionDef");
}
void AbstractBaseWriter::EndScope()
{
DecIndent();
Indent();
m_stream << "}\n";
}
std::vector<std::string> AbstractBaseWriter::CreateScriptTokenList(const char* script)
{
const std::string scriptString(script);
std::istringstream stringStream(scriptString);
ParserSingleInputStream inputStream(stringStream, "MenuScript");
SimpleLexer::Config lexerConfig;
lexerConfig.m_emit_new_line_tokens = false;
lexerConfig.m_read_strings = true;
lexerConfig.m_string_escape_sequences = true;
lexerConfig.m_read_integer_numbers = false;
lexerConfig.m_read_floating_point_numbers = false;
SimpleLexer lexer(&inputStream, std::move(lexerConfig));
std::vector<std::string> result;
auto hasLexerTokens = true;
while (hasLexerTokens)
{
const auto& token = lexer.GetToken(0);
switch (token.m_type)
{
case SimpleParserValueType::IDENTIFIER:
result.emplace_back(token.IdentifierValue());
break;
case SimpleParserValueType::STRING:
result.emplace_back(token.StringValue());
break;
case SimpleParserValueType::CHARACTER:
result.emplace_back(1, token.CharacterValue());
break;
case SimpleParserValueType::INVALID:
case SimpleParserValueType::END_OF_FILE:
hasLexerTokens = false;
break;
default:
assert(false);
break;
}
lexer.PopTokens(1);
}
return result;
}
bool AbstractBaseWriter::DoesTokenNeedQuotationMarks(const std::string& token)
{
if (token.empty())
return true;
const auto hasAlNumCharacter = std::ranges::any_of(token,
[](const char& c)
{
return isalnum(c);
});
if (!hasAlNumCharacter)
return false;
const auto hasNonIdentifierCharacter = std::ranges::any_of(token,
[](const char& c)
{
return !isalnum(c) && c != '_';
});
return hasNonIdentifierCharacter;
}
void AbstractBaseWriter::WriteEscapedString(const std::string_view& str) const
{
m_stream << "\"";
for (const auto& c : str)
{
switch (c)
{
case '\r':
m_stream << "\\r";
break;
case '\n':
m_stream << "\\n";
break;
case '\t':
m_stream << "\\t";
break;
case '\f':
m_stream << "\\f";
break;
case '"':
m_stream << "\\\"";
break;
default:
m_stream << c;
break;
}
}
m_stream << "\"";
}
const std::string& AbstractBaseWriter::BoolValue(const bool value)
{
return value ? BOOL_VALUE_TRUE : BOOL_VALUE_FALSE;
}
void AbstractBaseWriter::WriteKey(const std::string& keyName) const
{
m_stream << keyName;
if (keyName.size() < MENU_KEY_SPACING)
{
const auto spacingLength = MENU_KEY_SPACING - keyName.size();
for (auto i = 0u; i < spacingLength; i++)
m_stream << " ";
}
}
void AbstractBaseWriter::WriteStringProperty(const std::string& propertyKey, const std::string& propertyValue) const
{
if (propertyValue.empty())
return;
Indent();
WriteKey(propertyKey);
WriteEscapedString(propertyValue);
m_stream << "\n";
}
void AbstractBaseWriter::WriteStringProperty(const std::string& propertyKey, const char* propertyValue) const
{
if (propertyValue == nullptr || propertyValue[0] == '\0')
return;
Indent();
WriteKey(propertyKey);
WriteEscapedString(propertyValue);
m_stream << "\n";
}
void AbstractBaseWriter::WriteBoolProperty(const std::string& propertyKey, const bool propertyValue, const bool defaultValue) const
{
if (propertyValue == defaultValue)
return;
Indent();
WriteKey(propertyKey);
m_stream << BoolValue(propertyValue) << "\n";
}
void AbstractBaseWriter::WriteIntProperty(const std::string& propertyKey, const int propertyValue, const int defaultValue) const
{
if (propertyValue == defaultValue)
return;
Indent();
WriteKey(propertyKey);
m_stream << propertyValue << "\n";
}
void AbstractBaseWriter::WriteFloatProperty(const std::string& propertyKey, const float propertyValue, const float defaultValue) const
{
if (std::fabs(propertyValue - defaultValue) < std::numeric_limits<float>::epsilon())
return;
Indent();
WriteKey(propertyKey);
m_stream << propertyValue << "\n";
}
void AbstractBaseWriter::WriteColorProperty(const std::string& propertyKey, const float (&propertyValue)[4], const float (&defaultValue)[4]) const
{
if (std::fabs(propertyValue[0] - defaultValue[0]) < std::numeric_limits<float>::epsilon()
&& std::fabs(propertyValue[1] - defaultValue[1]) < std::numeric_limits<float>::epsilon()
&& std::fabs(propertyValue[2] - defaultValue[2]) < std::numeric_limits<float>::epsilon()
&& std::fabs(propertyValue[3] - defaultValue[3]) < std::numeric_limits<float>::epsilon())
{
return;
}
Indent();
WriteKey(propertyKey);
m_stream << propertyValue[0] << " " << propertyValue[1] << " " << propertyValue[2] << " " << propertyValue[3] << "\n";
}
void AbstractBaseWriter::WriteKeywordProperty(const std::string& propertyKey, const bool shouldWrite) const
{
if (!shouldWrite)
return;
Indent();
WriteKey(propertyKey);
m_stream << "\n";
}
void AbstractBaseWriter::WriteFlagsProperty(const std::string& propertyKey, const int flagsValue) const
{
for (auto i = 0u; i < sizeof(flagsValue) * 8; i++)
{
if (flagsValue & (1 << i))
{
Indent();
WriteKey(propertyKey);
m_stream << i << "\n";
}
}
}
void AbstractBaseWriter::Start()
{
Indent();
m_stream << "{\n";
IncIndent();
}
void AbstractBaseWriter::End()
{
for (auto i = 0u; i < m_indent; i++)
{
DecIndent();
Indent();
m_stream << "}\n";
}
}
void AbstractBaseWriter::IncludeMenu(const std::string& menuPath) const
{
Indent();
m_stream << "loadMenu { \"" << menuPath << "\" }\n";
}
} // namespace menu

View File

@@ -0,0 +1,59 @@
#pragma once
#include "IMenuWriter.h"
#include <cstddef>
#include <ostream>
#include <string>
#include <vector>
namespace menu
{
class AbstractBaseWriter : public IWriter
{
protected:
static constexpr auto MENU_KEY_SPACING = 28u;
static const inline std::string BOOL_VALUE_TRUE = "1";
static const inline std::string BOOL_VALUE_FALSE = "0";
static constexpr inline float COLOR_0000[4]{0.0f, 0.0f, 0.0f, 0.0f};
static constexpr inline float COLOR_1111[4]{1.0f, 1.0f, 1.0f, 1.0f};
public:
void Start() override;
void End() override;
void IncludeMenu(const std::string& menuPath) const override;
protected:
explicit AbstractBaseWriter(std::ostream& stream);
void IncIndent();
void DecIndent();
void Indent() const;
void StartScope(const std::string& scopeName);
void StartMenuDefScope();
void StartItemDefScope();
void StartFunctionDefScope();
void EndScope();
static std::vector<std::string> CreateScriptTokenList(const char* script);
static bool DoesTokenNeedQuotationMarks(const std::string& token);
void WriteEscapedString(const std::string_view& str) const;
static const std::string& BoolValue(bool value);
void WriteKey(const std::string& keyName) const;
void WriteStringProperty(const std::string& propertyKey, const std::string& propertyValue) const;
void WriteStringProperty(const std::string& propertyKey, const char* propertyValue) const;
void WriteBoolProperty(const std::string& propertyKey, bool propertyValue, bool defaultValue) const;
void WriteIntProperty(const std::string& propertyKey, int propertyValue, int defaultValue) const;
void WriteFloatProperty(const std::string& propertyKey, float propertyValue, float defaultValue) const;
void WriteColorProperty(const std::string& propertyKey, const float (&propertyValue)[4], const float (&defaultValue)[4]) const;
void WriteKeywordProperty(const std::string& propertyKey, bool shouldWrite) const;
void WriteFlagsProperty(const std::string& propertyKey, int flagsValue) const;
std::ostream& m_stream;
size_t m_indent;
};
} // namespace menu

View File

@@ -0,0 +1,18 @@
#pragma once
#include <string>
namespace menu
{
class IWriter
{
public:
IWriter() = default;
virtual ~IWriter() = default;
virtual void Start() = 0;
virtual void End() = 0;
virtual void IncludeMenu(const std::string& menuPath) const = 0;
};
} // namespace menu