#include "AssetLoaderRawFile.h" #include "Game/IW4/IW4.h" #include "Pool/GlobalAssetPool.h" #include #include #include #include using namespace IW4; 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::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 auto uncompressedBuffer = std::make_unique(static_cast(file.m_length)); file.m_stream->read(uncompressedBuffer.get(), file.m_length); if (file.m_stream->gcount() != file.m_length) return false; const auto compressionBufferSize = static_cast(file.m_length + 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); zs.avail_out = compressionBufferSize; zs.next_in = reinterpret_cast(uncompressedBuffer.get()); zs.next_out = reinterpret_cast(compressedBuffer); 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 rawfile \"" << assetName << "\"\n"; deflateEnd(&zs); return false; } const auto compressedSize = compressionBufferSize - zs.avail_out; auto* rawFile = memory->Create(); rawFile->name = memory->Dup(assetName.c_str()); rawFile->compressedLen = static_cast(compressedSize); rawFile->len = static_cast(file.m_length); rawFile->data.compressedBuffer = static_cast(compressedBuffer); deflateEnd(&zs); manager->AddAsset(assetName, rawFile); return true; }