2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2026-05-17 07:21:43 +00:00

feat: accept aliases for asset type names

This commit is contained in:
Jan Laupetin
2026-05-02 17:42:55 +02:00
parent 0cbe0c2891
commit 08c128addd
20 changed files with 175 additions and 251 deletions
@@ -3,7 +3,6 @@
#include "Csv/CsvStream.h"
#include "Game/IGame.h"
#include "Utils/Logging/Log.h"
#include "Zone/AssetNameResolver.h"
#include <format>
@@ -12,9 +11,9 @@ namespace
class AssetListInputStream
{
public:
AssetListInputStream(std::istream& stream, const GameId game)
AssetListInputStream(std::istream& stream, const GameId gameId)
: m_stream(stream),
m_asset_name_resolver(game)
m_game(*IGame::GetGameById(gameId))
{
}
@@ -33,7 +32,7 @@ namespace
continue;
const auto& typeName = row[0];
const auto maybeType = m_asset_name_resolver.GetAssetTypeByName(typeName);
const auto maybeType = m_game.FindAssetTypeByName(typeName);
if (!maybeType)
{
con::error("Unknown asset type name \"{}\"", typeName);
@@ -60,7 +59,7 @@ namespace
private:
CsvInputStream m_stream;
AssetNameResolver m_asset_name_resolver;
IGame& m_game;
};
} // namespace
-35
View File
@@ -1,35 +0,0 @@
#include "AssetNameResolver.h"
#include "Utils/StringUtils.h"
#include <cassert>
AssetNameResolver::AssetNameResolver(const GameId gameId)
{
const auto* game = IGame::GetGameById(gameId);
const auto assetTypeCount = game->GetAssetTypeCount();
for (asset_type_t assetType = 0; assetType < assetTypeCount; assetType++)
{
auto maybeAssetTypeName = game->GetAssetTypeName(assetType);
assert(maybeAssetTypeName);
if (!maybeAssetTypeName)
continue;
std::string lowerCaseName(*maybeAssetTypeName);
utils::MakeStringLowerCase(lowerCaseName);
m_asset_types_by_name.emplace(lowerCaseName, assetType);
}
}
std::optional<asset_type_t> AssetNameResolver::GetAssetTypeByName(const std::string& assetTypeName) const
{
std::string lowerCaseName = assetTypeName;
utils::MakeStringLowerCase(lowerCaseName);
const auto existingAssetType = m_asset_types_by_name.find(assetTypeName);
if (existingAssetType != m_asset_types_by_name.end())
return existingAssetType->second;
return std::nullopt;
}
-20
View File
@@ -1,20 +0,0 @@
#pragma once
#include "Game/IGame.h"
#include "Zone/ZoneTypes.h"
#include <optional>
#include <string>
#include <unordered_map>
class AssetNameResolver
{
public:
AssetNameResolver() = default;
explicit AssetNameResolver(GameId gameId);
[[nodiscard]] std::optional<asset_type_t> GetAssetTypeByName(const std::string& assetTypeName) const;
private:
std::unordered_map<std::string, asset_type_t> m_asset_types_by_name;
};
@@ -23,7 +23,12 @@ void SequenceZoneDefinitionEntry::ProcessMatch(ZoneDefinitionParserState* state,
{
const auto& typeNameToken = result.NextCapture(CAPTURE_TYPE_NAME);
const auto maybeAssetType = state->m_asset_name_resolver.GetAssetTypeByName(typeNameToken.FieldValue());
const auto maybeAssetType = state->m_game.and_then(
[&typeNameToken](const IGame* game)
{
return game->FindAssetTypeByName(typeNameToken.FieldValue());
});
if (!maybeAssetType)
throw ParsingException(typeNameToken.GetPos(), "Unknown asset type");
@@ -12,10 +12,10 @@ ZoneDefinitionParserState::ZoneDefinitionParserState(std::string targetName, ISe
m_definition->m_name = std::move(targetName);
}
void ZoneDefinitionParserState::SetGame(const GameId game)
void ZoneDefinitionParserState::SetGame(const GameId gameId)
{
m_definition->m_game = game;
m_asset_name_resolver = AssetNameResolver(game);
m_definition->m_game = gameId;
m_game = IGame::GetGameById(gameId);
}
namespace
@@ -2,7 +2,6 @@
#include "Parsing/IParserLineStream.h"
#include "SearchPath/ISearchPath.h"
#include "Zone/AssetNameResolver.h"
#include "Zone/Definition/ZoneDefinition.h"
#include <memory>
@@ -15,7 +14,7 @@ class ZoneDefinitionParserState
public:
ZoneDefinitionParserState(std::string targetName, ISearchPath& searchPath, IParserLineStream& underlyingStream);
void SetGame(GameId game);
void SetGame(GameId gameId);
void StartIPak(std::string ipakName);
void StartIwd(std::string iwdName);
@@ -26,7 +25,7 @@ public:
IParserLineStream& m_underlying_stream;
std::unordered_set<std::string> m_inclusions;
AssetNameResolver m_asset_name_resolver;
std::optional<IGame*> m_game;
std::optional<ZoneDefinitionObjContainer> m_current_ipak;
std::optional<ZoneDefinitionObjContainer> m_current_iwd;