From d8bc156ffd8077358534bb508d5f47db3e49e735 Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 28 Dec 2024 23:51:57 +0100 Subject: [PATCH] chore: fix loading and writing code for T5 --- .../T5/AssetLoaders/AssetLoaderPhysPreset.cpp | 0 .../T5/AssetLoaders/AssetLoaderPhysPreset.h | 0 .../T5/AssetLoaders/AssetLoaderRawFile.cpp | 116 ----------------- .../Game/T5/AssetLoaders/AssetLoaderRawFile.h | 24 ---- .../AssetLoaders/AssetLoaderStringTable.cpp | 39 ------ .../T5/AssetLoaders/AssetLoaderStringTable.h | 17 --- .../T5/AssetLoaders/AssetLoaderWeapon.cpp | 0 .../Game/T5/AssetLoaders/AssetLoaderWeapon.h | 0 .../T5/AssetLoaders/AssetLoaderXModel.cpp | 47 ------- .../Game/T5/AssetLoaders/AssetLoaderXModel.h | 19 --- .../InfoStringToStructConverter.cpp | 0 .../InfoString/InfoStringToStructConverter.h | 0 .../T5/Localize/AssetLoaderLocalizeT5.cpp | 23 ---- .../Game/T5/Localize/AssetLoaderLocalizeT5.h | 25 ---- .../Game/T5/Localize/LoaderLocalizeT5.cpp | 44 +++++++ .../Game/T5/Localize/LoaderLocalizeT5.h | 14 ++ src/ObjLoading/Game/T5/ObjLoaderT5.cpp | 15 ++- src/ObjLoading/Game/T5/ObjLoaderT5.h | 2 +- .../Game/T5/RawFile/LoaderRawFileT5.cpp | 123 ++++++++++++++++++ .../Game/T5/RawFile/LoaderRawFileT5.h | 13 ++ .../T5/StringTable/LoaderStringTableT5.cpp | 46 +++++++ .../Game/T5/StringTable/LoaderStringTableT5.h | 13 ++ 22 files changed, 263 insertions(+), 317 deletions(-) delete mode 100644 src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderPhysPreset.cpp delete mode 100644 src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderPhysPreset.h delete mode 100644 src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderRawFile.cpp delete mode 100644 src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderRawFile.h delete mode 100644 src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderStringTable.cpp delete mode 100644 src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderStringTable.h delete mode 100644 src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderWeapon.cpp delete mode 100644 src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderWeapon.h delete mode 100644 src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderXModel.cpp delete mode 100644 src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderXModel.h delete mode 100644 src/ObjLoading/Game/T5/InfoString/InfoStringToStructConverter.cpp delete mode 100644 src/ObjLoading/Game/T5/InfoString/InfoStringToStructConverter.h delete mode 100644 src/ObjLoading/Game/T5/Localize/AssetLoaderLocalizeT5.cpp delete mode 100644 src/ObjLoading/Game/T5/Localize/AssetLoaderLocalizeT5.h create mode 100644 src/ObjLoading/Game/T5/Localize/LoaderLocalizeT5.cpp create mode 100644 src/ObjLoading/Game/T5/Localize/LoaderLocalizeT5.h create mode 100644 src/ObjLoading/Game/T5/RawFile/LoaderRawFileT5.cpp create mode 100644 src/ObjLoading/Game/T5/RawFile/LoaderRawFileT5.h create mode 100644 src/ObjLoading/Game/T5/StringTable/LoaderStringTableT5.cpp create mode 100644 src/ObjLoading/Game/T5/StringTable/LoaderStringTableT5.h diff --git a/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderPhysPreset.cpp b/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderPhysPreset.cpp deleted file mode 100644 index e69de29b..00000000 diff --git a/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderPhysPreset.h b/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderPhysPreset.h deleted file mode 100644 index e69de29b..00000000 diff --git a/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderRawFile.cpp b/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderRawFile.cpp deleted file mode 100644 index 53a5ae1a..00000000 --- a/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderRawFile.cpp +++ /dev/null @@ -1,116 +0,0 @@ -#include "AssetLoaderRawFile.h" - -#include "Game/T5/T5.h" -#include "Pool/GlobalAssetPool.h" - -#include -#include -#include -#include - -using namespace T5; - -namespace fs = std::filesystem; - -void* AssetLoaderRawFile::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) -{ - auto* rawFile = memory->Create(); - memset(rawFile, 0, sizeof(RawFile)); - rawFile->name = memory->Dup(assetName.c_str()); - return rawFile; -} - -bool AssetLoaderRawFile::CanLoadFromRaw() const -{ - return true; -} - -bool AssetLoaderRawFile::LoadGsc( - const SearchPathOpenFile& file, const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager) -{ - const auto uncompressedBuffer = std::make_unique(static_cast(file.m_length + 1)); - file.m_stream->read(uncompressedBuffer.get(), file.m_length); - if (file.m_stream->gcount() != file.m_length) - return false; - uncompressedBuffer[static_cast(file.m_length)] = '\0'; - - const auto compressionBufferSize = static_cast(file.m_length + 1 + sizeof(uint32_t) + sizeof(uint32_t) + COMPRESSED_BUFFER_SIZE_PADDING); - auto* compressedBuffer = memory->Alloc(compressionBufferSize); - - z_stream_s zs{}; - - zs.zalloc = Z_NULL; - zs.zfree = Z_NULL; - zs.opaque = Z_NULL; - zs.avail_in = static_cast(file.m_length + 1); - zs.avail_out = compressionBufferSize; - zs.next_in = reinterpret_cast(uncompressedBuffer.get()); - zs.next_out = reinterpret_cast(&compressedBuffer[sizeof(uint32_t) + sizeof(uint32_t)]); - - int ret = deflateInit(&zs, Z_DEFAULT_COMPRESSION); - - if (ret != Z_OK) - { - throw std::runtime_error("Initializing deflate failed"); - } - - ret = deflate(&zs, Z_FINISH); - - if (ret != Z_STREAM_END) - { - std::cerr << "Deflate failed for loading gsc file \"" << assetName << "\"\n"; - deflateEnd(&zs); - return false; - } - - const auto compressedSize = compressionBufferSize - zs.avail_out; - - reinterpret_cast(compressedBuffer)[0] = static_cast(file.m_length + 1); // outLen - reinterpret_cast(compressedBuffer)[1] = compressedSize; // inLen - - auto* rawFile = memory->Create(); - rawFile->name = memory->Dup(assetName.c_str()); - rawFile->len = static_cast(compressedSize + sizeof(uint32_t) + sizeof(uint32_t)); - rawFile->buffer = static_cast(compressedBuffer); - - deflateEnd(&zs); - - manager->AddAsset(assetName, rawFile); - - return true; -} - -bool AssetLoaderRawFile::LoadDefault( - const SearchPathOpenFile& file, const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager) -{ - auto* rawFile = memory->Create(); - rawFile->name = memory->Dup(assetName.c_str()); - rawFile->len = static_cast(file.m_length); - - auto* fileBuffer = 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 false; - fileBuffer[rawFile->len] = '\0'; - - rawFile->buffer = fileBuffer; - manager->AddAsset(assetName, rawFile); - - return true; -} - -bool AssetLoaderRawFile::LoadFromRaw( - const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const -{ - const auto file = searchPath->Open(assetName); - if (!file.IsOpen()) - return false; - - const fs::path rawFilePath(assetName); - const auto extension = rawFilePath.extension().string(); - - if (extension == ".gsc" || extension == ".csc") - return LoadGsc(file, assetName, searchPath, memory, manager); - - return LoadDefault(file, assetName, searchPath, memory, manager); -} diff --git a/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderRawFile.h b/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderRawFile.h deleted file mode 100644 index 54bc33a7..00000000 --- a/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderRawFile.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once -#include "AssetLoading/BasicAssetLoader.h" -#include "AssetLoading/IAssetLoadingManager.h" -#include "Game/T5/T5.h" -#include "SearchPath/ISearchPath.h" - -namespace T5 -{ - class AssetLoaderRawFile final : public BasicAssetLoader - { - static constexpr size_t COMPRESSED_BUFFER_SIZE_PADDING = 64; - - static bool LoadGsc( - const SearchPathOpenFile& file, const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager); - static bool LoadDefault( - const SearchPathOpenFile& file, const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager); - - public: - _NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override; - _NODISCARD bool CanLoadFromRaw() const override; - bool - LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const override; - }; -} // namespace T5 diff --git a/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderStringTable.cpp b/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderStringTable.cpp deleted file mode 100644 index 3d06701e..00000000 --- a/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderStringTable.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "AssetLoaderStringTable.h" - -#include "Csv/CsvStream.h" -#include "Game/T5/CommonT5.h" -#include "Game/T5/T5.h" -#include "Pool/GlobalAssetPool.h" -#include "StringTable/StringTableLoader.h" - -#include - -using namespace T5; - -void* AssetLoaderStringTable::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) -{ - auto* stringTable = memory->Create(); - memset(stringTable, 0, sizeof(StringTable)); - stringTable->name = memory->Dup(assetName.c_str()); - return stringTable; -} - -bool AssetLoaderStringTable::CanLoadFromRaw() const -{ - return true; -} - -bool AssetLoaderStringTable::LoadFromRaw( - const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const -{ - const auto file = searchPath->Open(assetName); - if (!file.IsOpen()) - return false; - - string_table::StringTableLoaderV3 loader; - auto* stringTable = loader.LoadFromStream(assetName, *memory, *file.m_stream); - - manager->AddAsset(assetName, stringTable); - - return true; -} diff --git a/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderStringTable.h b/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderStringTable.h deleted file mode 100644 index fc90f9a8..00000000 --- a/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderStringTable.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once -#include "AssetLoading/BasicAssetLoader.h" -#include "AssetLoading/IAssetLoadingManager.h" -#include "Game/T5/T5.h" -#include "SearchPath/ISearchPath.h" - -namespace T5 -{ - class AssetLoaderStringTable final : public BasicAssetLoader - { - public: - _NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override; - _NODISCARD bool CanLoadFromRaw() const override; - bool - LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const override; - }; -} // namespace T5 diff --git a/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderWeapon.cpp b/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderWeapon.cpp deleted file mode 100644 index e69de29b..00000000 diff --git a/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderWeapon.h b/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderWeapon.h deleted file mode 100644 index e69de29b..00000000 diff --git a/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderXModel.cpp b/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderXModel.cpp deleted file mode 100644 index 22dee30f..00000000 --- a/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderXModel.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include "AssetLoaderXModel.h" - -#include "Game/T5/T5.h" -#include "Game/T5/XModel/XModelLoaderT5.h" -#include "Pool/GlobalAssetPool.h" - -#include -#include -#include - -using namespace T5; - -void* AssetLoaderXModel::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) -{ - auto* asset = memory->Alloc(); - asset->name = memory->Dup(assetName.c_str()); - return asset; -} - -bool AssetLoaderXModel::CanLoadFromRaw() const -{ - return true; -} - -bool AssetLoaderXModel::LoadFromRaw( - const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const -{ - const auto file = searchPath->Open(std::format("xmodel/{}.json", assetName)); - if (!file.IsOpen()) - return false; - - auto* xmodel = memory->Alloc(); - xmodel->name = memory->Dup(assetName.c_str()); - - std::vector dependencies; - if (LoadXModel(*file.m_stream, *xmodel, memory, manager, dependencies)) - { - manager->AddAsset(assetName, xmodel, std::move(dependencies)); - } - else - { - std::cerr << std::format("Failed to load xmodel \"{}\"\n", assetName); - return false; - } - - return true; -} diff --git a/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderXModel.h b/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderXModel.h deleted file mode 100644 index 4a669630..00000000 --- a/src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderXModel.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once -#include "AssetLoading/BasicAssetLoader.h" -#include "AssetLoading/IAssetLoadingManager.h" -#include "Game/T5/T5.h" -#include "SearchPath/ISearchPath.h" - -namespace T5 -{ - class AssetLoaderXModel final : public BasicAssetLoader - { - static std::string GetFileNameForAsset(const std::string& assetName); - - public: - _NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override; - _NODISCARD bool CanLoadFromRaw() const override; - bool - LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const override; - }; -} // namespace T5 diff --git a/src/ObjLoading/Game/T5/InfoString/InfoStringToStructConverter.cpp b/src/ObjLoading/Game/T5/InfoString/InfoStringToStructConverter.cpp deleted file mode 100644 index e69de29b..00000000 diff --git a/src/ObjLoading/Game/T5/InfoString/InfoStringToStructConverter.h b/src/ObjLoading/Game/T5/InfoString/InfoStringToStructConverter.h deleted file mode 100644 index e69de29b..00000000 diff --git a/src/ObjLoading/Game/T5/Localize/AssetLoaderLocalizeT5.cpp b/src/ObjLoading/Game/T5/Localize/AssetLoaderLocalizeT5.cpp deleted file mode 100644 index 5cfe997a..00000000 --- a/src/ObjLoading/Game/T5/Localize/AssetLoaderLocalizeT5.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "AssetLoaderLocalizeT5.h" - -using namespace T5; - -AssetLoaderLocalize::AssetLoaderLocalize(MemoryManager& memory, ISearchPath& searchPath, Zone& zone) - : CommonLocalizeLoader(searchPath, zone), - m_memory(memory) -{ -} - -AssetCreationResult AssetLoaderLocalize::CreateAsset(const std::string& assetName, AssetCreationContext& context) -{ - 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)); -} diff --git a/src/ObjLoading/Game/T5/Localize/AssetLoaderLocalizeT5.h b/src/ObjLoading/Game/T5/Localize/AssetLoaderLocalizeT5.h deleted file mode 100644 index 8478f9c6..00000000 --- a/src/ObjLoading/Game/T5/Localize/AssetLoaderLocalizeT5.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include "Asset/AssetCreationContext.h" -#include "Asset/IAssetCreator.h" -#include "Game/T5/T5.h" -#include "Localize/CommonLocalizeLoader.h" -#include "SearchPath/ISearchPath.h" -#include "Utils/MemoryManager.h" -#include "Zone/Zone.h" - -namespace T5 -{ - 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; - }; -} // namespace T5 diff --git a/src/ObjLoading/Game/T5/Localize/LoaderLocalizeT5.cpp b/src/ObjLoading/Game/T5/Localize/LoaderLocalizeT5.cpp new file mode 100644 index 00000000..175b71fb --- /dev/null +++ b/src/ObjLoading/Game/T5/Localize/LoaderLocalizeT5.cpp @@ -0,0 +1,44 @@ +#include "LoaderLocalizeT5.h" + +#include "Localize/CommonLocalizeLoader.h" + +using namespace T5; + +namespace +{ + class LocalizeLoader final : public AssetCreator, public CommonLocalizeLoader + { + public: + LocalizeLoader(MemoryManager& memory, ISearchPath& searchPath, Zone& zone) + : CommonLocalizeLoader(searchPath, zone), + m_memory(memory) + { + } + + 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 T5 +{ + std::unique_ptr> CreateLocalizeLoader(MemoryManager& memory, ISearchPath& searchPath, Zone& zone) + { + return std::make_unique(memory, searchPath, zone); + } +} // namespace T5 diff --git a/src/ObjLoading/Game/T5/Localize/LoaderLocalizeT5.h b/src/ObjLoading/Game/T5/Localize/LoaderLocalizeT5.h new file mode 100644 index 00000000..fd71deb5 --- /dev/null +++ b/src/ObjLoading/Game/T5/Localize/LoaderLocalizeT5.h @@ -0,0 +1,14 @@ +#pragma once + +#include "Asset/IAssetCreator.h" +#include "Game/T5/T5.h" +#include "SearchPath/ISearchPath.h" +#include "Utils/MemoryManager.h" +#include "Zone/Zone.h" + +#include + +namespace T5 +{ + std::unique_ptr> CreateLocalizeLoader(MemoryManager& memory, ISearchPath& searchPath, Zone& zone); +} // namespace T5 diff --git a/src/ObjLoading/Game/T5/ObjLoaderT5.cpp b/src/ObjLoading/Game/T5/ObjLoaderT5.cpp index a712018c..5308111e 100644 --- a/src/ObjLoading/Game/T5/ObjLoaderT5.cpp +++ b/src/ObjLoading/Game/T5/ObjLoaderT5.cpp @@ -3,8 +3,11 @@ #include "Asset/GlobalAssetPoolsLoader.h" #include "Game/T5/GameT5.h" #include "Game/T5/T5.h" -#include "Localize/AssetLoaderLocalizeT5.h" +#include "Game/T5/XModel/LoaderXModelT5.h" +#include "Localize/LoaderLocalizeT5.h" #include "ObjLoading.h" +#include "RawFile/LoaderRawFileT5.h" +#include "StringTable/LoaderStringTableT5.h" #include @@ -100,7 +103,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)); + collection.AddAssetCreator(CreateXModelLoader(memory, searchPath, zone)); // collection.AddAssetCreator(std::make_unique(memory)); // collection.AddAssetCreator(std::make_unique(memory)); // collection.AddAssetCreator(std::make_unique(memory)); @@ -117,13 +120,13 @@ 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)); + 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)); - // collection.AddAssetCreator(std::make_unique(memory)); + collection.AddAssetCreator(CreateRawFileLoader(memory, searchPath)); + collection.AddAssetCreator(CreateStringTableLoader(memory, searchPath)); // collection.AddAssetCreator(std::make_unique(memory)); // collection.AddAssetCreator(std::make_unique(memory)); // collection.AddAssetCreator(std::make_unique(memory)); @@ -132,7 +135,7 @@ namespace } } // 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/T5/ObjLoaderT5.h b/src/ObjLoading/Game/T5/ObjLoaderT5.h index 190eb78c..56954718 100644 --- a/src/ObjLoading/Game/T5/ObjLoaderT5.h +++ b/src/ObjLoading/Game/T5/ObjLoaderT5.h @@ -12,6 +12,6 @@ namespace T5 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 T5 diff --git a/src/ObjLoading/Game/T5/RawFile/LoaderRawFileT5.cpp b/src/ObjLoading/Game/T5/RawFile/LoaderRawFileT5.cpp new file mode 100644 index 00000000..4ffa79db --- /dev/null +++ b/src/ObjLoading/Game/T5/RawFile/LoaderRawFileT5.cpp @@ -0,0 +1,123 @@ +#include "LoaderRawFileT5.h" + +#include "Game/T5/T5.h" + +#include +#include +#include +#include +#include + +using namespace T5; +namespace fs = std::filesystem; + +namespace +{ + constexpr size_t COMPRESSED_BUFFER_SIZE_PADDING = 64; + + class RawFileLoader final : public AssetCreator + { + public: + RawFileLoader(MemoryManager& memory, ISearchPath& searchPath) + : m_memory(memory), + m_search_path(searchPath) + { + } + + AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override + { + const auto file = m_search_path.Open(assetName); + if (!file.IsOpen()) + return AssetCreationResult::NoAction(); + + const fs::path rawFilePath(assetName); + const auto extension = rawFilePath.extension().string(); + + if (extension == ".gsc" || extension == ".csc") + return LoadGsc(file, assetName, context); + + return LoadDefault(file, assetName, context); + } + + private: + AssetCreationResult LoadGsc(const SearchPathOpenFile& file, const std::string& assetName, AssetCreationContext& context) + { + const auto uncompressedBuffer = std::make_unique(static_cast(file.m_length + 1)); + + file.m_stream->read(uncompressedBuffer.get(), file.m_length); + if (file.m_stream->gcount() != file.m_length) + return AssetCreationResult::Failure(); + + uncompressedBuffer[static_cast(file.m_length)] = '\0'; + + const auto compressionBufferSize = static_cast(file.m_length + 1 + sizeof(uint32_t) + sizeof(uint32_t) + COMPRESSED_BUFFER_SIZE_PADDING); + auto* compressedBuffer = m_memory.Alloc(compressionBufferSize); + + z_stream_s zs{}; + zs.zalloc = Z_NULL; + zs.zfree = Z_NULL; + zs.opaque = Z_NULL; + zs.avail_in = static_cast(file.m_length + 1); + zs.avail_out = compressionBufferSize; + zs.next_in = reinterpret_cast(uncompressedBuffer.get()); + zs.next_out = reinterpret_cast(&compressedBuffer[sizeof(uint32_t) + sizeof(uint32_t)]); + + int ret = deflateInit(&zs, Z_DEFAULT_COMPRESSION); + + if (ret != Z_OK) + throw std::runtime_error("Initializing deflate failed"); + + ret = deflate(&zs, Z_FINISH); + + if (ret != Z_STREAM_END) + { + std::cerr << std::format("Deflate failed for loading gsc file \"{}\"\n", assetName); + deflateEnd(&zs); + return AssetCreationResult::Failure(); + } + + const auto compressedSize = compressionBufferSize - zs.avail_out; + + reinterpret_cast(compressedBuffer)[0] = static_cast(file.m_length + 1); // outLen + reinterpret_cast(compressedBuffer)[1] = compressedSize; // inLen + + auto* rawFile = m_memory.Alloc(); + rawFile->name = m_memory.Dup(assetName.c_str()); + rawFile->len = static_cast(compressedSize + sizeof(uint32_t) + sizeof(uint32_t)); + rawFile->buffer = static_cast(compressedBuffer); + + deflateEnd(&zs); + + return AssetCreationResult::Success(context.AddAsset(assetName, rawFile)); + } + + AssetCreationResult LoadDefault(const SearchPathOpenFile& file, const std::string& assetName, AssetCreationContext& context) + { + 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)); + } + + MemoryManager& m_memory; + ISearchPath& m_search_path; + }; +} // namespace + +namespace T5 +{ + std::unique_ptr> CreateRawFileLoader(MemoryManager& memory, ISearchPath& searchPath) + { + return std::make_unique(memory, searchPath); + } +} // namespace T5 diff --git a/src/ObjLoading/Game/T5/RawFile/LoaderRawFileT5.h b/src/ObjLoading/Game/T5/RawFile/LoaderRawFileT5.h new file mode 100644 index 00000000..42e83ed3 --- /dev/null +++ b/src/ObjLoading/Game/T5/RawFile/LoaderRawFileT5.h @@ -0,0 +1,13 @@ +#pragma once + +#include "Asset/IAssetCreator.h" +#include "Game/T5/T5.h" +#include "SearchPath/ISearchPath.h" +#include "Utils/MemoryManager.h" + +#include + +namespace T5 +{ + std::unique_ptr> CreateRawFileLoader(MemoryManager& memory, ISearchPath& searchPath); +} // namespace T5 diff --git a/src/ObjLoading/Game/T5/StringTable/LoaderStringTableT5.cpp b/src/ObjLoading/Game/T5/StringTable/LoaderStringTableT5.cpp new file mode 100644 index 00000000..f50b423a --- /dev/null +++ b/src/ObjLoading/Game/T5/StringTable/LoaderStringTableT5.cpp @@ -0,0 +1,46 @@ +#include "LoaderStringTableT5.h" + +#include "Game/T5/CommonT5.h" +#include "Game/T5/T5.h" +#include "StringTable/StringTableLoader.h" + +#include + +using namespace T5; + +namespace +{ + class StringTableLoader final : public AssetCreator + { + public: + StringTableLoader(MemoryManager& memory, ISearchPath& searchPath) + : m_memory(memory), + m_search_path(searchPath) + { + } + + 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::StringTableLoaderV3 loader; + auto* stringTable = loader.LoadFromStream(assetName, m_memory, *file.m_stream); + + return AssetCreationResult::Success(context.AddAsset(assetName, stringTable)); + } + + private: + MemoryManager& m_memory; + ISearchPath& m_search_path; + }; +} // namespace + +namespace T5 +{ + std::unique_ptr> CreateStringTableLoader(MemoryManager& memory, ISearchPath& searchPath) + { + return std::make_unique(memory, searchPath); + } +} // namespace T5 diff --git a/src/ObjLoading/Game/T5/StringTable/LoaderStringTableT5.h b/src/ObjLoading/Game/T5/StringTable/LoaderStringTableT5.h new file mode 100644 index 00000000..3479639b --- /dev/null +++ b/src/ObjLoading/Game/T5/StringTable/LoaderStringTableT5.h @@ -0,0 +1,13 @@ +#pragma once + +#include "Asset/IAssetCreator.h" +#include "Game/T5/T5.h" +#include "SearchPath/ISearchPath.h" +#include "Utils/MemoryManager.h" + +#include + +namespace T5 +{ + std::unique_ptr> CreateStringTableLoader(MemoryManager& memory, ISearchPath& searchPath); +} // namespace T5