From a5873a301fb974bc52d61325ad71246a3d4529e6 Mon Sep 17 00:00:00 2001 From: Jan Date: Thu, 26 Dec 2024 11:18:26 +0100 Subject: [PATCH] chore: adjust IW3 asset loaders to fit IW4 format --- .../Game/IW3/Image/AssetLoaderImageIW3.cpp | 205 ++++++++++-------- .../Game/IW3/Image/AssetLoaderImageIW3.h | 14 +- .../IW3/Localize/AssetLoaderLocalizeIW3.cpp | 54 +++-- .../IW3/Localize/AssetLoaderLocalizeIW3.h | 17 +- src/ObjLoading/Game/IW3/ObjLoaderIW3.cpp | 10 +- src/ObjLoading/Game/IW3/ObjLoaderIW3.h | 2 +- .../IW3/RawFile/AssetLoaderRawFileIW3.cpp | 65 ++++-- .../Game/IW3/RawFile/AssetLoaderRawFileIW3.h | 14 +- .../StringTable/AssetLoaderStringTableIW3.cpp | 52 +++-- .../StringTable/AssetLoaderStringTableIW3.h | 14 +- 10 files changed, 246 insertions(+), 201 deletions(-) diff --git a/src/ObjLoading/Game/IW3/Image/AssetLoaderImageIW3.cpp b/src/ObjLoading/Game/IW3/Image/AssetLoaderImageIW3.cpp index 187e2684..6f65b5af 100644 --- a/src/ObjLoading/Game/IW3/Image/AssetLoaderImageIW3.cpp +++ b/src/ObjLoading/Game/IW3/Image/AssetLoaderImageIW3.cpp @@ -12,101 +12,120 @@ using namespace IW3; -AssetLoaderImage::AssetLoaderImage(MemoryManager& memory, ISearchPath& searchPath) - : m_memory(memory), - m_search_path(searchPath) +namespace { -} - -AssetCreationResult AssetLoaderImage::CreateAsset(const std::string& assetName, AssetCreationContext& context) -{ - // Do not load any GfxImages from raw for now that are not loaded - // TODO: Load iwis and add streaming info to asset - if (assetName.empty() || assetName[0] != '*') - return AssetCreationResult::NoAction(); - - std::string safeAssetName = assetName; - std::ranges::replace(safeAssetName, '*', '_'); - - const auto file = m_search_path.Open(std::format("images/{}.dds", safeAssetName)); - if (!file.IsOpen()) - return AssetCreationResult::NoAction(); - - const auto texture = dds::LoadDds(*file.m_stream); - if (!texture) + class ImageLoader final : public AssetCreator { - std::cerr << std::format("Failed to load dds file for image asset \"{}\"\n", assetName); - return AssetCreationResult::Failure(); - } - - auto* image = m_memory.Alloc(); - image->name = m_memory.Dup(assetName.c_str()); - image->picmip.platform[0] = 0; - image->picmip.platform[1] = 0; - image->noPicmip = !texture->HasMipMaps(); - image->semantic = TS_FUNCTION; - image->track = 0; - image->cardMemory.platform[0] = 0; - image->cardMemory.platform[1] = 0; - image->width = static_cast(texture->GetWidth()); - image->height = static_cast(texture->GetHeight()); - image->depth = static_cast(texture->GetDepth()); - image->category = IMG_CATEGORY_AUTO_GENERATED; - image->delayLoadPixels = false; - - switch (texture->GetTextureType()) - { - case TextureType::T_2D: - image->mapType = MAPTYPE_2D; - break; - - case TextureType::T_3D: - image->mapType = MAPTYPE_3D; - break; - - case TextureType::T_CUBE: - image->mapType = MAPTYPE_CUBE; - break; - - default: - image->mapType = MAPTYPE_NONE; - break; - } - - const auto mipCount = texture->HasMipMaps() ? texture->GetMipMapCount() : 1; - const auto faceCount = texture->GetFaceCount(); - - size_t dataSize = 0; - for (auto mipLevel = 0; mipLevel < mipCount; mipLevel++) - dataSize += texture->GetSizeOfMipLevel(mipLevel) * faceCount; - - auto* loadDef = static_cast(m_memory.AllocRaw(offsetof(GfxImageLoadDef, data) + dataSize)); - image->texture.loadDef = loadDef; - loadDef->levelCount = static_cast(mipCount); - loadDef->flags = 0; - if (!texture->HasMipMaps()) - loadDef->flags |= iwi6::IMG_FLAG_NOMIPMAPS; - if (texture->GetTextureType() == TextureType::T_CUBE) - loadDef->flags |= iwi6::IMG_FLAG_CUBEMAP; - if (texture->GetTextureType() == TextureType::T_3D) - loadDef->flags |= iwi6::IMG_FLAG_VOLMAP; - loadDef->dimensions[0] = image->width; - loadDef->dimensions[1] = image->height; - loadDef->dimensions[2] = image->depth; - loadDef->format = static_cast(texture->GetFormat()->GetD3DFormat()); - loadDef->resourceSize = dataSize; - - char* currentDataBuffer = loadDef->data; - for (auto mipLevel = 0; mipLevel < mipCount; mipLevel++) - { - const auto mipSize = texture->GetSizeOfMipLevel(mipLevel); - - for (auto face = 0; face < faceCount; face++) + public: + ImageLoader(MemoryManager& memory, ISearchPath& searchPath) + : m_memory(memory), + m_search_path(searchPath) { - memcpy(currentDataBuffer, texture->GetBufferForMipLevel(mipLevel, face), mipSize); - currentDataBuffer += mipSize; } - } - return AssetCreationResult::Success(context.AddAsset(assetName, image)); -} + AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override + { + // Do not load any GfxImages from raw for now that are not loaded + // TODO: Load iwis and add streaming info to asset + if (assetName.empty() || assetName[0] != '*') + return AssetCreationResult::NoAction(); + + std::string safeAssetName = assetName; + std::ranges::replace(safeAssetName, '*', '_'); + + const auto file = m_search_path.Open(std::format("images/{}.dds", safeAssetName)); + if (!file.IsOpen()) + return AssetCreationResult::NoAction(); + + const auto texture = dds::LoadDds(*file.m_stream); + if (!texture) + { + std::cerr << std::format("Failed to load dds file for image asset \"{}\"\n", assetName); + return AssetCreationResult::Failure(); + } + + auto* image = m_memory.Alloc(); + image->name = m_memory.Dup(assetName.c_str()); + image->picmip.platform[0] = 0; + image->picmip.platform[1] = 0; + image->noPicmip = !texture->HasMipMaps(); + image->semantic = TS_FUNCTION; + image->track = 0; + image->cardMemory.platform[0] = 0; + image->cardMemory.platform[1] = 0; + image->width = static_cast(texture->GetWidth()); + image->height = static_cast(texture->GetHeight()); + image->depth = static_cast(texture->GetDepth()); + image->category = IMG_CATEGORY_AUTO_GENERATED; + image->delayLoadPixels = false; + + switch (texture->GetTextureType()) + { + case TextureType::T_2D: + image->mapType = MAPTYPE_2D; + break; + + case TextureType::T_3D: + image->mapType = MAPTYPE_3D; + break; + + case TextureType::T_CUBE: + image->mapType = MAPTYPE_CUBE; + break; + + default: + image->mapType = MAPTYPE_NONE; + break; + } + + const auto mipCount = texture->HasMipMaps() ? texture->GetMipMapCount() : 1; + const auto faceCount = texture->GetFaceCount(); + + size_t dataSize = 0; + for (auto mipLevel = 0; mipLevel < mipCount; mipLevel++) + dataSize += texture->GetSizeOfMipLevel(mipLevel) * faceCount; + + auto* loadDef = static_cast(m_memory.AllocRaw(offsetof(GfxImageLoadDef, data) + dataSize)); + image->texture.loadDef = loadDef; + loadDef->levelCount = static_cast(mipCount); + loadDef->flags = 0; + if (!texture->HasMipMaps()) + loadDef->flags |= iwi6::IMG_FLAG_NOMIPMAPS; + if (texture->GetTextureType() == TextureType::T_CUBE) + loadDef->flags |= iwi6::IMG_FLAG_CUBEMAP; + if (texture->GetTextureType() == TextureType::T_3D) + loadDef->flags |= iwi6::IMG_FLAG_VOLMAP; + loadDef->dimensions[0] = image->width; + loadDef->dimensions[1] = image->height; + loadDef->dimensions[2] = image->depth; + loadDef->format = static_cast(texture->GetFormat()->GetD3DFormat()); + loadDef->resourceSize = dataSize; + + char* currentDataBuffer = loadDef->data; + for (auto mipLevel = 0; mipLevel < mipCount; mipLevel++) + { + const auto mipSize = texture->GetSizeOfMipLevel(mipLevel); + + for (auto face = 0; face < faceCount; face++) + { + memcpy(currentDataBuffer, texture->GetBufferForMipLevel(mipLevel, face), mipSize); + currentDataBuffer += mipSize; + } + } + + return AssetCreationResult::Success(context.AddAsset(assetName, image)); + } + + private: + MemoryManager& m_memory; + ISearchPath& m_search_path; + }; +} // namespace + +namespace IW3 +{ + std::unique_ptr> CreateImageLoader(MemoryManager& memory, ISearchPath& searchPath) + { + return std::make_unique(memory, searchPath); + } +} // namespace IW3 diff --git a/src/ObjLoading/Game/IW3/Image/AssetLoaderImageIW3.h b/src/ObjLoading/Game/IW3/Image/AssetLoaderImageIW3.h index 3947214b..ce4dbb81 100644 --- a/src/ObjLoading/Game/IW3/Image/AssetLoaderImageIW3.h +++ b/src/ObjLoading/Game/IW3/Image/AssetLoaderImageIW3.h @@ -5,17 +5,9 @@ #include "SearchPath/ISearchPath.h" #include "Utils/MemoryManager.h" +#include + namespace IW3 { - class AssetLoaderImage final : public AssetCreator - { - public: - AssetLoaderImage(MemoryManager& memory, ISearchPath& searchPath); - - AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override; - - private: - MemoryManager& m_memory; - ISearchPath& m_search_path; - }; + std::unique_ptr> CreateImageLoader(MemoryManager& memory, ISearchPath& searchPath); } // namespace IW3 diff --git a/src/ObjLoading/Game/IW3/Localize/AssetLoaderLocalizeIW3.cpp b/src/ObjLoading/Game/IW3/Localize/AssetLoaderLocalizeIW3.cpp index 69b9c071..c7f2572f 100644 --- a/src/ObjLoading/Game/IW3/Localize/AssetLoaderLocalizeIW3.cpp +++ b/src/ObjLoading/Game/IW3/Localize/AssetLoaderLocalizeIW3.cpp @@ -1,23 +1,45 @@ #include "AssetLoaderLocalizeIW3.h" +#include "Localize/CommonLocalizeLoader.h" + using namespace IW3; -AssetLoaderLocalize::AssetLoaderLocalize(MemoryManager& memory, ISearchPath& searchPath, Zone& zone) - : CommonLocalizeLoader(searchPath, zone), - m_memory(memory) +namespace { -} + class LocalizeLoader final : public AssetCreator, public CommonLocalizeLoader + { + public: + LocalizeLoader(MemoryManager& memory, ISearchPath& searchPath, Zone& zone) + : CommonLocalizeLoader(searchPath, zone), + m_memory(memory) + { + } -AssetCreationResult AssetLoaderLocalize::CreateAsset(const std::string& assetName, AssetCreationContext& context) + AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override + { + return CreateLocalizeAsset(assetName, context); + } + + protected: + AssetCreationResult CreateAssetFromCommonAsset(const CommonLocalizeEntry& localizeEntry, AssetCreationContext& context) override + + { + auto* asset = m_memory.Alloc(); + asset->name = m_memory.Dup(localizeEntry.m_key.c_str()); + asset->value = m_memory.Dup(localizeEntry.m_value.c_str()); + + return AssetCreationResult::Success(context.AddAsset(localizeEntry.m_key, asset)); + } + + private: + MemoryManager& m_memory; + }; +} // namespace + +namespace IW3 { - return CreateLocalizeAsset(assetName, context); -} - -AssetCreationResult AssetLoaderLocalize::CreateAssetFromCommonAsset(const CommonLocalizeEntry& localizeEntry, AssetCreationContext& context) -{ - auto* asset = m_memory.Alloc(); - asset->name = m_memory.Dup(localizeEntry.m_key.c_str()); - asset->value = m_memory.Dup(localizeEntry.m_value.c_str()); - - return AssetCreationResult::Success(context.AddAsset(localizeEntry.m_key, asset)); -} + std::unique_ptr> CreateLocalizeLoader(MemoryManager& memory, ISearchPath& searchPath, Zone& zone) + { + return std::make_unique(memory, searchPath, zone); + } +} // namespace IW3 diff --git a/src/ObjLoading/Game/IW3/Localize/AssetLoaderLocalizeIW3.h b/src/ObjLoading/Game/IW3/Localize/AssetLoaderLocalizeIW3.h index d730961b..5469d4ca 100644 --- a/src/ObjLoading/Game/IW3/Localize/AssetLoaderLocalizeIW3.h +++ b/src/ObjLoading/Game/IW3/Localize/AssetLoaderLocalizeIW3.h @@ -2,22 +2,13 @@ #include "Asset/IAssetCreator.h" #include "Game/IW3/IW3.h" -#include "Localize/CommonLocalizeLoader.h" #include "SearchPath/ISearchPath.h" #include "Utils/MemoryManager.h" +#include "Zone/Zone.h" + +#include namespace IW3 { - class AssetLoaderLocalize final : public AssetCreator, public CommonLocalizeLoader - { - public: - AssetLoaderLocalize(MemoryManager& memory, ISearchPath& searchPath, Zone& zone); - AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override; - - protected: - AssetCreationResult CreateAssetFromCommonAsset(const CommonLocalizeEntry& localizeEntry, AssetCreationContext& context) override; - - private: - MemoryManager& m_memory; - }; + std::unique_ptr> CreateLocalizeLoader(MemoryManager& memory, ISearchPath& searchPath, Zone& zone); } // namespace IW3 diff --git a/src/ObjLoading/Game/IW3/ObjLoaderIW3.cpp b/src/ObjLoading/Game/IW3/ObjLoaderIW3.cpp index dca69017..9d06750a 100644 --- a/src/ObjLoading/Game/IW3/ObjLoaderIW3.cpp +++ b/src/ObjLoading/Game/IW3/ObjLoaderIW3.cpp @@ -92,7 +92,7 @@ namespace // collection.AddAssetCreator(std::make_unique(memory)); // collection.AddAssetCreator(std::make_unique(memory)); // collection.AddAssetCreator(std::make_unique(memory)); - collection.AddAssetCreator(std::make_unique(memory, searchPath)); + collection.AddAssetCreator(CreateImageLoader(memory, searchPath)); // collection.AddAssetCreator(std::make_unique(memory)); // collection.AddAssetCreator(std::make_unique(memory)); // collection.AddAssetCreator(std::make_unique(memory)); @@ -107,17 +107,17 @@ namespace // collection.AddAssetCreator(std::make_unique(memory)); // collection.AddAssetCreator(std::make_unique(memory)); // collection.AddAssetCreator(std::make_unique(memory)); - collection.AddAssetCreator(std::make_unique(memory, searchPath, zone)); + collection.AddAssetCreator(CreateLocalizeLoader(memory, searchPath, zone)); // collection.AddAssetCreator(std::make_unique(memory)); // collection.AddAssetCreator(std::make_unique(memory)); // collection.AddAssetCreator(std::make_unique(memory)); // collection.AddAssetCreator(std::make_unique(memory)); - collection.AddAssetCreator(std::make_unique(memory, searchPath)); - collection.AddAssetCreator(std::make_unique(memory, searchPath)); + collection.AddAssetCreator(CreateRawFileLoader(memory, searchPath)); + collection.AddAssetCreator(CreateStringTableLoader(memory, searchPath)); } } // namespace -void ObjLoader::ConfigureCreatorCollection(AssetCreatorCollection& collection, Zone& zone, ISearchPath& searchPath) const +void ObjLoader::ConfigureCreatorCollection(AssetCreatorCollection& collection, Zone& zone, ISearchPath& searchPath, IGdtQueryable& gdt) const { ConfigureDefaultCreators(collection, zone); ConfigureLoaders(collection, zone, searchPath); diff --git a/src/ObjLoading/Game/IW3/ObjLoaderIW3.h b/src/ObjLoading/Game/IW3/ObjLoaderIW3.h index 35d83adb..a11c9d51 100644 --- a/src/ObjLoading/Game/IW3/ObjLoaderIW3.h +++ b/src/ObjLoading/Game/IW3/ObjLoaderIW3.h @@ -11,6 +11,6 @@ namespace IW3 void LoadReferencedContainersForZone(ISearchPath& searchPath, Zone& zone) const override; void UnloadContainersOfZone(Zone& zone) const override; - void ConfigureCreatorCollection(AssetCreatorCollection& collection, Zone& zone, ISearchPath& searchPath) const override; + void ConfigureCreatorCollection(AssetCreatorCollection& collection, Zone& zone, ISearchPath& searchPath, IGdtQueryable& gdt) const override; }; } // namespace IW3 diff --git a/src/ObjLoading/Game/IW3/RawFile/AssetLoaderRawFileIW3.cpp b/src/ObjLoading/Game/IW3/RawFile/AssetLoaderRawFileIW3.cpp index b304a2f3..92a19b21 100644 --- a/src/ObjLoading/Game/IW3/RawFile/AssetLoaderRawFileIW3.cpp +++ b/src/ObjLoading/Game/IW3/RawFile/AssetLoaderRawFileIW3.cpp @@ -6,29 +6,48 @@ using namespace IW3; -AssetLoaderRawFile::AssetLoaderRawFile(MemoryManager& memory, ISearchPath& searchPath) - : m_memory(memory), - m_search_path(searchPath) +namespace { -} + class RawFileLoader final : public AssetCreator + { + public: + RawFileLoader(MemoryManager& memory, ISearchPath& searchPath) + : m_memory(memory), + m_search_path(searchPath) + { + } -AssetCreationResult AssetLoaderRawFile::CreateAsset(const std::string& assetName, AssetCreationContext& context) + AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override + { + const auto file = m_search_path.Open(assetName); + if (!file.IsOpen()) + return AssetCreationResult::NoAction(); + + auto* rawFile = m_memory.Alloc(); + rawFile->name = m_memory.Dup(assetName.c_str()); + rawFile->len = static_cast(file.m_length); + + auto* fileBuffer = m_memory.Alloc(static_cast(file.m_length + 1)); + file.m_stream->read(fileBuffer, file.m_length); + if (file.m_stream->gcount() != file.m_length) + return AssetCreationResult::Failure(); + fileBuffer[rawFile->len] = '\0'; + + rawFile->buffer = fileBuffer; + + return AssetCreationResult::Success(context.AddAsset(assetName, rawFile)); + } + + private: + MemoryManager& m_memory; + ISearchPath& m_search_path; + }; +} // namespace + +namespace IW3 { - const auto file = m_search_path.Open(assetName); - if (!file.IsOpen()) - return AssetCreationResult::NoAction(); - - auto* rawFile = m_memory.Alloc(); - rawFile->name = m_memory.Dup(assetName.c_str()); - rawFile->len = static_cast(file.m_length); - - auto* fileBuffer = m_memory.Alloc(static_cast(file.m_length + 1)); - file.m_stream->read(fileBuffer, file.m_length); - if (file.m_stream->gcount() != file.m_length) - return AssetCreationResult::Failure(); - fileBuffer[rawFile->len] = '\0'; - - rawFile->buffer = fileBuffer; - - return AssetCreationResult::Success(context.AddAsset(assetName, rawFile)); -} + std::unique_ptr> CreateRawFileLoader(MemoryManager& memory, ISearchPath& searchPath) + { + return std::make_unique(memory, searchPath); + } +} // namespace IW3 diff --git a/src/ObjLoading/Game/IW3/RawFile/AssetLoaderRawFileIW3.h b/src/ObjLoading/Game/IW3/RawFile/AssetLoaderRawFileIW3.h index dbb1bd22..cddae3ea 100644 --- a/src/ObjLoading/Game/IW3/RawFile/AssetLoaderRawFileIW3.h +++ b/src/ObjLoading/Game/IW3/RawFile/AssetLoaderRawFileIW3.h @@ -5,17 +5,9 @@ #include "SearchPath/ISearchPath.h" #include "Utils/MemoryManager.h" +#include + namespace IW3 { - class AssetLoaderRawFile final : public AssetCreator - { - public: - AssetLoaderRawFile(MemoryManager& memory, ISearchPath& searchPath); - - AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override; - - private: - MemoryManager& m_memory; - ISearchPath& m_search_path; - }; + std::unique_ptr> CreateRawFileLoader(MemoryManager& memory, ISearchPath& searchPath); } // namespace IW3 diff --git a/src/ObjLoading/Game/IW3/StringTable/AssetLoaderStringTableIW3.cpp b/src/ObjLoading/Game/IW3/StringTable/AssetLoaderStringTableIW3.cpp index 6a846669..bb7303ec 100644 --- a/src/ObjLoading/Game/IW3/StringTable/AssetLoaderStringTableIW3.cpp +++ b/src/ObjLoading/Game/IW3/StringTable/AssetLoaderStringTableIW3.cpp @@ -1,29 +1,47 @@ #include "AssetLoaderStringTableIW3.h" #include "Game/IW3/IW3.h" -#include "Pool/GlobalAssetPool.h" #include "StringTable/StringTableLoader.h" #include using namespace IW3; -AssetLoaderStringTable::AssetLoaderStringTable(MemoryManager& memory, ISearchPath& searchPath) - : m_memory(memory), - m_search_path(searchPath) +namespace { -} + class StringTableLoader final : public AssetCreator + { + public: + StringTableLoader(MemoryManager& memory, ISearchPath& searchPath) + : m_memory(memory), + m_search_path(searchPath) + { + } -AssetCreationResult AssetLoaderStringTable::CreateAsset(const std::string& assetName, AssetCreationContext& context) + AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override + { + const auto file = m_search_path.Open(assetName); + if (!file.IsOpen()) + return AssetCreationResult::NoAction(); + + string_table::StringTableLoaderV1 loader; + auto* stringTable = loader.LoadFromStream(assetName, m_memory, *file.m_stream); + if (!stringTable) + return AssetCreationResult::Failure(); + + return AssetCreationResult::Success(context.AddAsset(assetName, stringTable)); + } + + private: + MemoryManager& m_memory; + ISearchPath& m_search_path; + }; +} // namespace + +namespace IW3 { - const auto file = m_search_path.Open(assetName); - if (!file.IsOpen()) - return AssetCreationResult::NoAction(); - - string_table::StringTableLoaderV1 loader; - auto* stringTable = loader.LoadFromStream(assetName, m_memory, *file.m_stream); - if (!stringTable) - return AssetCreationResult::Failure(); - - return AssetCreationResult::Success(context.AddAsset(assetName, stringTable)); -} + std::unique_ptr> CreateStringTableLoader(MemoryManager& memory, ISearchPath& searchPath) + { + return std::make_unique(memory, searchPath); + } +} // namespace IW3 diff --git a/src/ObjLoading/Game/IW3/StringTable/AssetLoaderStringTableIW3.h b/src/ObjLoading/Game/IW3/StringTable/AssetLoaderStringTableIW3.h index 618eb8a9..76702596 100644 --- a/src/ObjLoading/Game/IW3/StringTable/AssetLoaderStringTableIW3.h +++ b/src/ObjLoading/Game/IW3/StringTable/AssetLoaderStringTableIW3.h @@ -5,17 +5,9 @@ #include "SearchPath/ISearchPath.h" #include "Utils/MemoryManager.h" +#include + namespace IW3 { - class AssetLoaderStringTable final : public AssetCreator - { - public: - AssetLoaderStringTable(MemoryManager& memory, ISearchPath& searchPath); - - AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override; - - private: - MemoryManager& m_memory; - ISearchPath& m_search_path; - }; + std::unique_ptr> CreateStringTableLoader(MemoryManager& memory, ISearchPath& searchPath); } // namespace IW3