2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2026-05-25 02:51: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
+69
View File
@@ -5,6 +5,7 @@
#include "IW5/GameIW5.h"
#include "T5/GameT5.h"
#include "T6/GameT6.h"
#include "Utils/StringUtils.h"
#include <cassert>
@@ -24,3 +25,71 @@ IGame* IGame::GetGameById(GameId gameId)
return result;
}
AbstractGame::AbstractGame(const char* const* assetTypeNames,
const asset_type_t assetTypeCount,
const char* const* subAssetTypeNames,
const asset_type_t subAssetTypeCount)
: m_asset_type_names(assetTypeNames),
m_asset_type_count(assetTypeCount),
m_sub_asset_type_names(subAssetTypeNames),
m_sub_asset_type_count(subAssetTypeCount)
{
for (asset_type_t assetType = 0; assetType < assetTypeCount; ++assetType)
{
assert(assetTypeNames[assetType] != nullptr);
AddAssetTypeNameAlias(assetType, assetTypeNames[assetType]);
}
}
const std::vector<GameLanguagePrefix>& AbstractGame::GetLanguagePrefixes() const
{
static std::vector<GameLanguagePrefix> prefixes;
return prefixes;
}
asset_type_t AbstractGame::GetAssetTypeCount() const
{
return m_asset_type_count;
}
std::optional<const char*> AbstractGame::GetAssetTypeName(const asset_type_t assetType) const
{
if (assetType < m_asset_type_count)
return m_asset_type_names[assetType];
return std::nullopt;
}
std::optional<asset_type_t> AbstractGame::FindAssetTypeByName(const std::string& potentialAssetTypeName) const
{
std::string lowerCaseName = potentialAssetTypeName;
utils::MakeStringLowerCase(lowerCaseName);
const auto existingAssetType = m_asset_type_name_lookup.find(lowerCaseName);
if (existingAssetType != m_asset_type_name_lookup.end())
return existingAssetType->second;
return std::nullopt;
}
asset_type_t AbstractGame::GetSubAssetTypeCount() const
{
return m_sub_asset_type_count;
}
std::optional<const char*> AbstractGame::GetSubAssetTypeName(const asset_type_t subAssetType) const
{
if (subAssetType < m_sub_asset_type_count)
return m_sub_asset_type_names[subAssetType];
return std::nullopt;
}
void AbstractGame::AddAssetTypeNameAlias(const asset_type_t assetType, const std::string& assetTypeName)
{
std::string lowerCaseName = assetTypeName;
utils::MakeStringLowerCase(lowerCaseName);
m_asset_type_name_lookup.emplace(lowerCaseName, assetType);
}
+35
View File
@@ -1,11 +1,13 @@
#pragma once
#include "GameLanguage.h"
#include "IAsset.h"
#include "Zone/ZoneTypes.h"
#include <cstdint>
#include <optional>
#include <type_traits>
#include <unordered_map>
#include <vector>
enum class GameId : std::uint8_t
@@ -68,9 +70,42 @@ public:
[[nodiscard]] virtual asset_type_t GetAssetTypeCount() const = 0;
[[nodiscard]] virtual std::optional<const char*> GetAssetTypeName(asset_type_t assetType) const = 0;
[[nodiscard]] virtual std::optional<asset_type_t> FindAssetTypeByName(const std::string& potentialAssetTypeName) const = 0;
[[nodiscard]] virtual asset_type_t GetSubAssetTypeCount() const = 0;
[[nodiscard]] virtual std::optional<const char*> GetSubAssetTypeName(asset_type_t subAssetType) const = 0;
static IGame* GetGameById(GameId gameId);
};
class AbstractGame : public IGame
{
public:
AbstractGame(const char* const* assetTypeNames, asset_type_t assetTypeCount, const char* const* subAssetTypeNames, asset_type_t subAssetTypeCount);
[[nodiscard]] const std::vector<GameLanguagePrefix>& GetLanguagePrefixes() const override;
[[nodiscard]] asset_type_t GetAssetTypeCount() const override;
[[nodiscard]] std::optional<const char*> GetAssetTypeName(asset_type_t assetType) const override;
[[nodiscard]] std::optional<asset_type_t> FindAssetTypeByName(const std::string& potentialAssetTypeName) const override;
[[nodiscard]] asset_type_t GetSubAssetTypeCount() const override;
[[nodiscard]] std::optional<const char*> GetSubAssetTypeName(asset_type_t subAssetType) const override;
protected:
template<AssetDefinition Asset_t> void AddAssetTypeNameAlias(const std::string& assetTypeName)
{
AddAssetTypeNameAlias(Asset_t::EnumEntry, assetTypeName);
}
private:
void AddAssetTypeNameAlias(asset_type_t assetType, const std::string& assetTypeName);
const char* const* m_asset_type_names;
asset_type_t m_asset_type_count;
const char* const* m_sub_asset_type_names;
asset_type_t m_sub_asset_type_count;
std::unordered_map<std::string, asset_type_t> m_asset_type_name_lookup;
};
+7 -32
View File
@@ -27,6 +27,13 @@ namespace
namespace IW3
{
Game::Game()
: AbstractGame(ASSET_TYPE_NAMES, std::extent_v<decltype(ASSET_TYPE_NAMES)>, SUB_ASSET_TYPE_NAMES, std::extent_v<decltype(SUB_ASSET_TYPE_NAMES)>)
{
AddAssetTypeNameAlias<AssetTechniqueSet>("techset");
AddAssetTypeNameAlias<AssetLightDef>("gfxlightdef");
}
GameId Game::GetId() const
{
return GameId::IW3;
@@ -43,36 +50,4 @@ namespace IW3
static std::string shortName = "IW3";
return shortName;
}
const std::vector<GameLanguagePrefix>& Game::GetLanguagePrefixes() const
{
static std::vector<GameLanguagePrefix> prefixes;
return prefixes;
}
asset_type_t Game::GetAssetTypeCount() const
{
return ASSET_TYPE_COUNT;
}
std::optional<const char*> Game::GetAssetTypeName(const asset_type_t assetType) const
{
if (assetType < std::extent_v<decltype(ASSET_TYPE_NAMES)>)
return ASSET_TYPE_NAMES[assetType];
return std::nullopt;
}
asset_type_t Game::GetSubAssetTypeCount() const
{
return SUB_ASSET_TYPE_COUNT;
}
std::optional<const char*> Game::GetSubAssetTypeName(const asset_type_t subAssetType) const
{
if (subAssetType < std::extent_v<decltype(SUB_ASSET_TYPE_NAMES)>)
return SUB_ASSET_TYPE_NAMES[subAssetType];
return std::nullopt;
}
} // namespace IW3
+3 -7
View File
@@ -4,17 +4,13 @@
namespace IW3
{
class Game final : public IGame
class Game final : public AbstractGame
{
public:
Game();
[[nodiscard]] GameId GetId() const override;
[[nodiscard]] const std::string& GetFullName() const override;
[[nodiscard]] const std::string& GetShortName() const override;
[[nodiscard]] const std::vector<GameLanguagePrefix>& GetLanguagePrefixes() const override;
[[nodiscard]] asset_type_t GetAssetTypeCount() const override;
[[nodiscard]] std::optional<const char*> GetAssetTypeName(asset_type_t assetType) const override;
[[nodiscard]] asset_type_t GetSubAssetTypeCount() const override;
[[nodiscard]] std::optional<const char*> GetSubAssetTypeName(asset_type_t subAssetType) const override;
};
} // namespace IW3
+7 -32
View File
@@ -29,6 +29,13 @@ namespace
namespace IW4
{
Game::Game()
: AbstractGame(ASSET_TYPE_NAMES, std::extent_v<decltype(ASSET_TYPE_NAMES)>, SUB_ASSET_TYPE_NAMES, std::extent_v<decltype(SUB_ASSET_TYPE_NAMES)>)
{
AddAssetTypeNameAlias<AssetTechniqueSet>("techset");
AddAssetTypeNameAlias<AssetLightDef>("gfxlightdef");
}
GameId Game::GetId() const
{
return GameId::IW4;
@@ -45,36 +52,4 @@ namespace IW4
static std::string shortName = "IW4";
return shortName;
}
const std::vector<GameLanguagePrefix>& Game::GetLanguagePrefixes() const
{
static std::vector<GameLanguagePrefix> prefixes;
return prefixes;
}
asset_type_t Game::GetAssetTypeCount() const
{
return ASSET_TYPE_COUNT;
}
std::optional<const char*> Game::GetAssetTypeName(const asset_type_t assetType) const
{
if (assetType < std::extent_v<decltype(ASSET_TYPE_NAMES)>)
return ASSET_TYPE_NAMES[assetType];
return std::nullopt;
}
asset_type_t Game::GetSubAssetTypeCount() const
{
return SUB_ASSET_TYPE_COUNT;
}
std::optional<const char*> Game::GetSubAssetTypeName(const asset_type_t subAssetType) const
{
if (subAssetType < std::extent_v<decltype(SUB_ASSET_TYPE_NAMES)>)
return SUB_ASSET_TYPE_NAMES[subAssetType];
return std::nullopt;
}
} // namespace IW4
+4 -7
View File
@@ -1,19 +1,16 @@
#pragma once
#include "Game/IGame.h"
namespace IW4
{
class Game final : public IGame
class Game final : public AbstractGame
{
public:
Game();
[[nodiscard]] GameId GetId() const override;
[[nodiscard]] const std::string& GetFullName() const override;
[[nodiscard]] const std::string& GetShortName() const override;
[[nodiscard]] const std::vector<GameLanguagePrefix>& GetLanguagePrefixes() const override;
[[nodiscard]] asset_type_t GetAssetTypeCount() const override;
[[nodiscard]] std::optional<const char*> GetAssetTypeName(asset_type_t assetType) const override;
[[nodiscard]] asset_type_t GetSubAssetTypeCount() const override;
[[nodiscard]] std::optional<const char*> GetSubAssetTypeName(asset_type_t subAssetType) const override;
};
} // namespace IW4
+7 -32
View File
@@ -66,6 +66,13 @@ namespace
namespace IW5
{
Game::Game()
: AbstractGame(ASSET_TYPE_NAMES, std::extent_v<decltype(ASSET_TYPE_NAMES)>, SUB_ASSET_TYPE_NAMES, std::extent_v<decltype(SUB_ASSET_TYPE_NAMES)>)
{
AddAssetTypeNameAlias<AssetTechniqueSet>("techset");
AddAssetTypeNameAlias<AssetLightDef>("gfxlightdef");
}
GameId Game::GetId() const
{
return GameId::IW5;
@@ -82,36 +89,4 @@ namespace IW5
static std::string shortName = "IW5";
return shortName;
}
const std::vector<GameLanguagePrefix>& Game::GetLanguagePrefixes() const
{
static std::vector<GameLanguagePrefix> prefixes;
return prefixes;
}
asset_type_t Game::GetAssetTypeCount() const
{
return ASSET_TYPE_COUNT;
}
std::optional<const char*> Game::GetAssetTypeName(const asset_type_t assetType) const
{
if (assetType < std::extent_v<decltype(ASSET_TYPE_NAMES)>)
return ASSET_TYPE_NAMES[assetType];
return std::nullopt;
}
asset_type_t Game::GetSubAssetTypeCount() const
{
return SUB_ASSET_TYPE_COUNT;
}
std::optional<const char*> Game::GetSubAssetTypeName(const asset_type_t subAssetType) const
{
if (subAssetType < std::extent_v<decltype(SUB_ASSET_TYPE_NAMES)>)
return SUB_ASSET_TYPE_NAMES[subAssetType];
return std::nullopt;
}
} // namespace IW5
+4 -7
View File
@@ -1,19 +1,16 @@
#pragma once
#include "Game/IGame.h"
namespace IW5
{
class Game final : public IGame
class Game final : public AbstractGame
{
public:
Game();
[[nodiscard]] GameId GetId() const override;
[[nodiscard]] const std::string& GetFullName() const override;
[[nodiscard]] const std::string& GetShortName() const override;
[[nodiscard]] const std::vector<GameLanguagePrefix>& GetLanguagePrefixes() const override;
[[nodiscard]] asset_type_t GetAssetTypeCount() const override;
[[nodiscard]] std::optional<const char*> GetAssetTypeName(asset_type_t assetType) const override;
[[nodiscard]] asset_type_t GetSubAssetTypeCount() const override;
[[nodiscard]] std::optional<const char*> GetSubAssetTypeName(asset_type_t subAssetType) const override;
};
} // namespace IW5
+7 -26
View File
@@ -30,6 +30,13 @@ namespace
namespace T5
{
Game::Game()
: AbstractGame(ASSET_TYPE_NAMES, std::extent_v<decltype(ASSET_TYPE_NAMES)>, SUB_ASSET_TYPE_NAMES, std::extent_v<decltype(SUB_ASSET_TYPE_NAMES)>)
{
AddAssetTypeNameAlias<AssetTechniqueSet>("techset");
AddAssetTypeNameAlias<AssetLightDef>("gfxlightdef");
}
GameId Game::GetId() const
{
return GameId::T5;
@@ -67,30 +74,4 @@ namespace T5
return prefixes;
}
asset_type_t Game::GetAssetTypeCount() const
{
return ASSET_TYPE_COUNT;
}
std::optional<const char*> Game::GetAssetTypeName(const asset_type_t assetType) const
{
if (assetType < std::extent_v<decltype(ASSET_TYPE_NAMES)>)
return ASSET_TYPE_NAMES[assetType];
return std::nullopt;
}
asset_type_t Game::GetSubAssetTypeCount() const
{
return SUB_ASSET_TYPE_COUNT;
}
std::optional<const char*> Game::GetSubAssetTypeName(const asset_type_t subAssetType) const
{
if (subAssetType < std::extent_v<decltype(SUB_ASSET_TYPE_NAMES)>)
return SUB_ASSET_TYPE_NAMES[subAssetType];
return std::nullopt;
}
} // namespace T5
+4 -6
View File
@@ -1,19 +1,17 @@
#pragma once
#include "Game/IGame.h"
namespace T5
{
class Game final : public IGame
class Game final : public AbstractGame
{
public:
Game();
[[nodiscard]] GameId GetId() const override;
[[nodiscard]] const std::string& GetFullName() const override;
[[nodiscard]] const std::string& GetShortName() const override;
[[nodiscard]] const std::vector<GameLanguagePrefix>& GetLanguagePrefixes() const override;
[[nodiscard]] asset_type_t GetAssetTypeCount() const override;
[[nodiscard]] std::optional<const char*> GetAssetTypeName(asset_type_t assetType) const override;
[[nodiscard]] asset_type_t GetSubAssetTypeCount() const override;
[[nodiscard]] std::optional<const char*> GetSubAssetTypeName(asset_type_t subAssetType) const override;
};
} // namespace T5
+7 -26
View File
@@ -83,6 +83,13 @@ namespace
namespace T6
{
Game::Game()
: AbstractGame(ASSET_TYPE_NAMES, std::extent_v<decltype(ASSET_TYPE_NAMES)>, SUB_ASSET_TYPE_NAMES, std::extent_v<decltype(SUB_ASSET_TYPE_NAMES)>)
{
AddAssetTypeNameAlias<AssetTechniqueSet>("techset");
AddAssetTypeNameAlias<AssetLightDef>("gfxlightdef");
}
GameId Game::GetId() const
{
return GameId::T6;
@@ -123,30 +130,4 @@ namespace T6
return prefixes;
}
asset_type_t Game::GetAssetTypeCount() const
{
return ASSET_TYPE_COUNT;
}
std::optional<const char*> Game::GetAssetTypeName(const asset_type_t assetType) const
{
if (assetType < std::extent_v<decltype(ASSET_TYPE_NAMES)>)
return ASSET_TYPE_NAMES[assetType];
return std::nullopt;
}
asset_type_t Game::GetSubAssetTypeCount() const
{
return SUB_ASSET_TYPE_COUNT;
}
std::optional<const char*> Game::GetSubAssetTypeName(const asset_type_t subAssetType) const
{
if (subAssetType < std::extent_v<decltype(SUB_ASSET_TYPE_NAMES)>)
return SUB_ASSET_TYPE_NAMES[subAssetType];
return std::nullopt;
}
} // namespace T6
+4 -6
View File
@@ -1,19 +1,17 @@
#pragma once
#include "Game/IGame.h"
namespace T6
{
class Game final : public IGame
class Game final : public AbstractGame
{
public:
Game();
[[nodiscard]] GameId GetId() const override;
[[nodiscard]] const std::string& GetFullName() const override;
[[nodiscard]] const std::string& GetShortName() const override;
[[nodiscard]] const std::vector<GameLanguagePrefix>& GetLanguagePrefixes() const override;
[[nodiscard]] asset_type_t GetAssetTypeCount() const override;
[[nodiscard]] std::optional<const char*> GetAssetTypeName(asset_type_t assetType) const override;
[[nodiscard]] asset_type_t GetSubAssetTypeCount() const override;
[[nodiscard]] std::optional<const char*> GetSubAssetTypeName(asset_type_t subAssetType) const override;
};
} // namespace T6
+1
View File
@@ -1,4 +1,5 @@
#pragma once
#include <cstdint>
#include <limits>