From 76a98e65fd3ad1560f90cdd80d685f627e2ad4a6 Mon Sep 17 00:00:00 2001 From: JezuzLizard Date: Tue, 12 Dec 2023 20:59:26 -0800 Subject: [PATCH] Replace magic numbers with macros from zutil.h. Compress animtrees when linking. --- .../T6/AssetLoaders/AssetLoaderRawFile.cpp | 80 +++++++++++++++++-- .../Game/T6/AssetLoaders/AssetLoaderRawFile.h | 6 ++ .../T6/AssetDumpers/AssetDumperRawFile.cpp | 5 +- 3 files changed, 83 insertions(+), 8 deletions(-) diff --git a/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderRawFile.cpp b/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderRawFile.cpp index 559f339b..f7334b67 100644 --- a/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderRawFile.cpp +++ b/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderRawFile.cpp @@ -3,10 +3,16 @@ #include "Game/T6/T6.h" #include "Pool/GlobalAssetPool.h" +#include +#include #include +#include +#include using namespace T6; +namespace fs = std::filesystem; + void* AssetLoaderRawFile::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) { auto* rawFile = memory->Create(); @@ -20,13 +26,61 @@ bool AssetLoaderRawFile::CanLoadFromRaw() const return true; } -bool AssetLoaderRawFile::LoadFromRaw( - const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const +bool AssetLoaderRawFile::LoadAnimtree( + const SearchPathOpenFile& file, const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager) { - const auto file = searchPath->Open(assetName); - if (!file.IsOpen()) + 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) + COMPRESSED_BUFFER_SIZE_PADDING); + auto* compressedBuffer = static_cast(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)]); + + int ret = deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -DEF_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY); + + if (ret != Z_OK) + { + throw std::runtime_error("Initializing deflate failed"); + } + + ret = deflate(&zs, Z_FINISH); + + if (ret != Z_STREAM_END) + { + std::cout << "Deflate failed for loading animtree file \"" << assetName << "\"" << std::endl; + deflateEnd(&zs); + return false; + } + + const auto compressedSize = compressionBufferSize + sizeof(uint32_t) - zs.avail_out; + + reinterpret_cast(compressedBuffer)[0] = static_cast(file.m_length + 1); // outLen + + auto* rawFile = memory->Create(); + rawFile->name = memory->Dup(assetName.c_str()); + rawFile->len = static_cast(compressedSize); + rawFile->buffer = static_cast(compressedBuffer); + + manager->AddAsset(ASSET_TYPE_RAWFILE, 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); @@ -39,6 +93,20 @@ bool AssetLoaderRawFile::LoadFromRaw( rawFile->buffer = static_cast(fileBuffer); manager->AddAsset(ASSET_TYPE_RAWFILE, 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 == ".atr") + return LoadAnimtree(file, assetName, searchPath, memory, manager); + + return LoadDefault(file, assetName, searchPath, memory, manager); } diff --git a/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderRawFile.h b/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderRawFile.h index d4083e5d..7e4c6f93 100644 --- a/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderRawFile.h +++ b/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderRawFile.h @@ -8,6 +8,12 @@ namespace T6 { class AssetLoaderRawFile final : public BasicAssetLoader { + static constexpr size_t COMPRESSED_BUFFER_SIZE_PADDING = 64; + + static bool LoadAnimtree( + 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; diff --git a/src/ObjWriting/Game/T6/AssetDumpers/AssetDumperRawFile.cpp b/src/ObjWriting/Game/T6/AssetDumpers/AssetDumperRawFile.cpp index 41c71f29..5440c69f 100644 --- a/src/ObjWriting/Game/T6/AssetDumpers/AssetDumperRawFile.cpp +++ b/src/ObjWriting/Game/T6/AssetDumpers/AssetDumperRawFile.cpp @@ -2,6 +2,7 @@ #include #include +#include using namespace T6; @@ -39,7 +40,7 @@ void AssetDumperRawFile::DumpAnimtree(AssetDumpingContext& context, XAssetInfo(&rawFile->buffer[4]); - zs.avail_in = inLen - 4; + zs.avail_in = inLen - sizeof(uint32_t); Bytef buffer[0x1000];