mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-22 12:47:53 -05:00
refactor: use asset_type_t for ZoneDefinition
This commit is contained in:
@ -1,12 +1,13 @@
|
||||
#include "AssetList.h"
|
||||
|
||||
AssetListEntry::AssetListEntry()
|
||||
: m_is_reference(false)
|
||||
: m_type(0u),
|
||||
m_is_reference(false)
|
||||
{
|
||||
}
|
||||
|
||||
AssetListEntry::AssetListEntry(std::string type, std::string name, const bool isReference)
|
||||
: m_type(std::move(type)),
|
||||
AssetListEntry::AssetListEntry(const asset_type_t type, std::string name, const bool isReference)
|
||||
: m_type(type),
|
||||
m_name(std::move(name)),
|
||||
m_is_reference(isReference)
|
||||
{
|
||||
|
@ -1,16 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "Zone/ZoneTypes.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class AssetListEntry
|
||||
{
|
||||
public:
|
||||
std::string m_type;
|
||||
asset_type_t m_type;
|
||||
std::string m_name;
|
||||
bool m_is_reference;
|
||||
|
||||
AssetListEntry();
|
||||
AssetListEntry(std::string type, std::string name, bool isReference);
|
||||
AssetListEntry(asset_type_t type, std::string name, bool isReference);
|
||||
};
|
||||
|
||||
class AssetList
|
||||
|
@ -1,13 +1,18 @@
|
||||
#include "AssetListStream.h"
|
||||
|
||||
AssetListInputStream::AssetListInputStream(std::istream& stream)
|
||||
: m_stream(stream)
|
||||
#include <format>
|
||||
|
||||
AssetListInputStream::AssetListInputStream(std::istream& stream, const GameId game)
|
||||
: m_stream(stream),
|
||||
m_asset_name_resolver(IAssetNameResolver::GetResolverForGame(game))
|
||||
{
|
||||
}
|
||||
|
||||
bool AssetListInputStream::NextEntry(AssetListEntry& entry) const
|
||||
bool AssetListInputStream::NextEntry(AssetListEntry& entry, bool* failure) const
|
||||
{
|
||||
std::vector<std::string> row;
|
||||
if (failure)
|
||||
*failure = false;
|
||||
|
||||
while (true)
|
||||
{
|
||||
@ -17,7 +22,17 @@ bool AssetListInputStream::NextEntry(AssetListEntry& entry) const
|
||||
if (row.empty())
|
||||
continue;
|
||||
|
||||
entry.m_type = row[0];
|
||||
const auto& typeName = row[0];
|
||||
const auto maybeType = m_asset_name_resolver->GetAssetTypeByName(typeName);
|
||||
if (!maybeType)
|
||||
{
|
||||
std::cerr << std::format("Unknown asset type name \"{}\"\n", typeName);
|
||||
if (failure)
|
||||
*failure = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
entry.m_type = *maybeType;
|
||||
if (row.size() >= 3 && row[1].empty())
|
||||
{
|
||||
entry.m_name = row[2];
|
||||
@ -33,14 +48,15 @@ bool AssetListInputStream::NextEntry(AssetListEntry& entry) const
|
||||
}
|
||||
}
|
||||
|
||||
AssetListOutputStream::AssetListOutputStream(std::ostream& stream)
|
||||
: m_stream(stream)
|
||||
AssetListOutputStream::AssetListOutputStream(std::ostream& stream, const GameId game)
|
||||
: m_stream(stream),
|
||||
m_asset_name_resolver(IAssetNameResolver::GetResolverForGame(game))
|
||||
{
|
||||
}
|
||||
|
||||
void AssetListOutputStream::WriteEntry(const AssetListEntry& entry)
|
||||
{
|
||||
m_stream.WriteColumn(entry.m_type);
|
||||
m_stream.WriteColumn(*m_asset_name_resolver->GetAssetTypeName(entry.m_type));
|
||||
m_stream.WriteColumn(entry.m_name);
|
||||
m_stream.NextRow();
|
||||
}
|
||||
|
@ -1,25 +1,31 @@
|
||||
#pragma once
|
||||
#include "AssetList.h"
|
||||
#include "Csv/CsvStream.h"
|
||||
#include "Game/IGame.h"
|
||||
|
||||
#include <Zone/AssetNameResolver.h>
|
||||
#include <iostream>
|
||||
|
||||
class AssetListInputStream
|
||||
{
|
||||
CsvInputStream m_stream;
|
||||
|
||||
public:
|
||||
explicit AssetListInputStream(std::istream& stream);
|
||||
AssetListInputStream(std::istream& stream, GameId game);
|
||||
|
||||
bool NextEntry(AssetListEntry& entry) const;
|
||||
bool NextEntry(AssetListEntry& entry, bool* failure) const;
|
||||
|
||||
private:
|
||||
CsvInputStream m_stream;
|
||||
const IAssetNameResolver* m_asset_name_resolver;
|
||||
};
|
||||
|
||||
class AssetListOutputStream
|
||||
{
|
||||
CsvOutputStream m_stream;
|
||||
|
||||
public:
|
||||
explicit AssetListOutputStream(std::ostream& stream);
|
||||
AssetListOutputStream(std::ostream& stream, GameId game);
|
||||
|
||||
void WriteEntry(const AssetListEntry& entry);
|
||||
|
||||
private:
|
||||
CsvOutputStream m_stream;
|
||||
const IAssetNameResolver* m_asset_name_resolver;
|
||||
};
|
||||
|
45
src/ZoneCommon/Zone/AssetNameResolver.cpp
Normal file
45
src/ZoneCommon/Zone/AssetNameResolver.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
#include "AssetNameResolver.h"
|
||||
|
||||
#include "Game/IW3/AssetNameResolverIW3.h"
|
||||
#include "Game/IW4/AssetNameResolverIW4.h"
|
||||
#include "Game/IW5/AssetNameResolverIW5.h"
|
||||
#include "Game/T5/AssetNameResolverT5.h"
|
||||
#include "Game/T6/AssetNameResolverT6.h"
|
||||
#include "Utils/StringUtils.h"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
const IAssetNameResolver* IAssetNameResolver::GetResolverForGame(GameId game)
|
||||
{
|
||||
static const IAssetNameResolver* assetNameResolvers[static_cast<unsigned>(GameId::COUNT)]{
|
||||
new IW3::AssetNameResolver(),
|
||||
new IW4::AssetNameResolver(),
|
||||
new IW5::AssetNameResolver(),
|
||||
new T5::AssetNameResolver(),
|
||||
new T6::AssetNameResolver(),
|
||||
};
|
||||
|
||||
assert(static_cast<unsigned>(game) < static_cast<unsigned>(GameId::COUNT));
|
||||
const auto* result = assetNameResolvers[static_cast<unsigned>(game)];
|
||||
assert(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void HashMapBasedAssetNameResolver::AddAssetTypeName(asset_type_t assetType, std::string name)
|
||||
{
|
||||
utils::MakeStringLowerCase(name);
|
||||
m_asset_types_by_name.emplace(std::move(name), assetType);
|
||||
}
|
||||
|
||||
std::optional<asset_type_t> HashMapBasedAssetNameResolver::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;
|
||||
}
|
37
src/ZoneCommon/Zone/AssetNameResolver.h
Normal file
37
src/ZoneCommon/Zone/AssetNameResolver.h
Normal file
@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include "Game/IGame.h"
|
||||
#include "Zone/ZoneTypes.h"
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
class IAssetNameResolver
|
||||
{
|
||||
public:
|
||||
IAssetNameResolver() = default;
|
||||
virtual ~IAssetNameResolver() = default;
|
||||
IAssetNameResolver(const IAssetNameResolver& other) = default;
|
||||
IAssetNameResolver(IAssetNameResolver&& other) noexcept = default;
|
||||
IAssetNameResolver& operator=(const IAssetNameResolver& other) = default;
|
||||
IAssetNameResolver& operator=(IAssetNameResolver&& other) noexcept = default;
|
||||
|
||||
[[nodiscard]] virtual GameId GetGameId() const = 0;
|
||||
[[nodiscard]] virtual std::optional<asset_type_t> GetAssetTypeByName(const std::string& assetTypeName) const = 0;
|
||||
[[nodiscard]] virtual std::optional<const char*> GetAssetTypeName(asset_type_t assetType) const = 0;
|
||||
|
||||
static const IAssetNameResolver* GetResolverForGame(GameId game);
|
||||
};
|
||||
|
||||
class HashMapBasedAssetNameResolver : public IAssetNameResolver
|
||||
{
|
||||
public:
|
||||
[[nodiscard]] std::optional<asset_type_t> GetAssetTypeByName(const std::string& assetTypeName) const override;
|
||||
|
||||
protected:
|
||||
void AddAssetTypeName(asset_type_t assetType, std::string name);
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, asset_type_t> m_asset_types_by_name;
|
||||
};
|
@ -1,55 +1,44 @@
|
||||
#include "ZoneDefinition.h"
|
||||
|
||||
ZoneDefinitionEntry::ZoneDefinitionEntry()
|
||||
: m_is_reference(false)
|
||||
{
|
||||
}
|
||||
|
||||
ZoneDefinitionEntry::ZoneDefinitionEntry(std::string type, std::string name, const bool isReference)
|
||||
: m_asset_type(std::move(type)),
|
||||
ZoneDefinitionAsset::ZoneDefinitionAsset(const asset_type_t type, std::string name, const bool isReference)
|
||||
: m_asset_type(type),
|
||||
m_asset_name(std::move(name)),
|
||||
m_is_reference(isReference)
|
||||
{
|
||||
}
|
||||
|
||||
ZoneMetaDataEntry::ZoneMetaDataEntry() = default;
|
||||
ZoneDefinitionProperties::ZoneDefinitionProperties() = default;
|
||||
|
||||
ZoneMetaDataEntry::ZoneMetaDataEntry(std::string key, std::string value)
|
||||
: m_key(std::move(key)),
|
||||
m_value(std::move(value))
|
||||
void ZoneDefinitionProperties::AddProperty(std::string key, std::string value)
|
||||
{
|
||||
m_properties.emplace(std::move(key), std::move(value));
|
||||
}
|
||||
|
||||
void ZoneDefinition::AddMetaData(std::string key, std::string value)
|
||||
void ZoneDefinitionProperties::Include(const ZoneDefinitionProperties& otherProperties)
|
||||
{
|
||||
for (const auto& property : otherProperties.m_properties)
|
||||
AddProperty(property.first, property.second);
|
||||
}
|
||||
|
||||
ZoneDefinition::ZoneDefinition()
|
||||
: m_type(ProjectType::NONE),
|
||||
m_game(GameId::COUNT)
|
||||
{
|
||||
auto metaData = std::make_unique<ZoneMetaDataEntry>(std::move(key), std::move(value));
|
||||
auto* metaDataPtr = metaData.get();
|
||||
m_metadata.emplace_back(std::move(metaData));
|
||||
m_metadata_lookup.emplace(std::make_pair(metaDataPtr->m_key, metaDataPtr));
|
||||
}
|
||||
|
||||
void ZoneDefinition::Include(const AssetList& assetListToInclude)
|
||||
{
|
||||
for (const auto& entry : assetListToInclude.m_entries)
|
||||
{
|
||||
m_assets.emplace_back(entry.m_type, entry.m_name, false);
|
||||
}
|
||||
}
|
||||
|
||||
void ZoneDefinition::Include(const ZoneDefinition& definitionToInclude)
|
||||
{
|
||||
for (const auto& metaData : definitionToInclude.m_metadata)
|
||||
{
|
||||
AddMetaData(metaData->m_key, metaData->m_value);
|
||||
}
|
||||
m_properties.Include(definitionToInclude.m_properties);
|
||||
|
||||
for (const auto& ignore : definitionToInclude.m_ignores)
|
||||
{
|
||||
m_ignores.emplace_back(ignore);
|
||||
}
|
||||
|
||||
for (const auto& asset : definitionToInclude.m_assets)
|
||||
{
|
||||
m_assets.emplace_back(asset);
|
||||
}
|
||||
}
|
||||
|
@ -1,46 +1,66 @@
|
||||
#pragma once
|
||||
|
||||
#include "Game/IGame.h"
|
||||
#include "Zone/AssetList/AssetList.h"
|
||||
#include "Zone/ZoneTypes.h"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
class ZoneDefinitionEntry
|
||||
enum class ProjectType
|
||||
{
|
||||
public:
|
||||
std::string m_asset_type;
|
||||
std::string m_asset_name;
|
||||
bool m_is_reference;
|
||||
NONE,
|
||||
FASTFILE,
|
||||
IPAK,
|
||||
|
||||
ZoneDefinitionEntry();
|
||||
ZoneDefinitionEntry(std::string type, std::string name, bool isReference);
|
||||
COUNT
|
||||
};
|
||||
|
||||
class ZoneMetaDataEntry
|
||||
static constexpr const char* ProjectType_Names[]{
|
||||
"none",
|
||||
"fastfile",
|
||||
"ipak",
|
||||
};
|
||||
static_assert(std::extent_v<decltype(ProjectType_Names)> == static_cast<unsigned>(ProjectType::COUNT));
|
||||
|
||||
class ZoneDefinitionAsset
|
||||
{
|
||||
public:
|
||||
std::string m_key;
|
||||
std::string m_value;
|
||||
ZoneDefinitionAsset(asset_type_t type, std::string name, bool isReference);
|
||||
|
||||
ZoneMetaDataEntry();
|
||||
ZoneMetaDataEntry(std::string key, std::string value);
|
||||
asset_type_t m_asset_type;
|
||||
std::string m_asset_name;
|
||||
bool m_is_reference;
|
||||
};
|
||||
|
||||
class ZoneDefinitionProperties
|
||||
{
|
||||
public:
|
||||
ZoneDefinitionProperties();
|
||||
|
||||
void AddProperty(std::string key, std::string value);
|
||||
void Include(const ZoneDefinitionProperties& otherProperties);
|
||||
|
||||
std::unordered_map<std::string, std::string> m_properties;
|
||||
};
|
||||
|
||||
class ZoneDefinition
|
||||
{
|
||||
public:
|
||||
ZoneDefinition();
|
||||
|
||||
void Include(const AssetList& assetListToInclude);
|
||||
void Include(const ZoneDefinition& definitionToInclude);
|
||||
|
||||
std::string m_name;
|
||||
std::vector<std::unique_ptr<ZoneMetaDataEntry>> m_metadata;
|
||||
std::unordered_multimap<std::string, ZoneMetaDataEntry*> m_metadata_lookup;
|
||||
ProjectType m_type;
|
||||
GameId m_game;
|
||||
ZoneDefinitionProperties m_properties;
|
||||
std::vector<std::string> m_includes;
|
||||
std::vector<std::string> m_asset_lists;
|
||||
std::vector<std::string> m_ignores;
|
||||
std::vector<std::string> m_targets_to_build;
|
||||
std::vector<ZoneDefinitionEntry> m_assets;
|
||||
|
||||
void AddMetaData(std::string key, std::string value);
|
||||
void Include(const AssetList& assetListToInclude);
|
||||
void Include(const ZoneDefinition& definitionToInclude);
|
||||
std::vector<std::string> m_gdts;
|
||||
std::vector<ZoneDefinitionAsset> m_assets;
|
||||
};
|
||||
|
Reference in New Issue
Block a user