mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-21 00:25:44 +00:00
Fix rawfile dumping and loading for iw5
This commit is contained in:
parent
7afc5d42b0
commit
e544d043bd
@ -1,6 +1,9 @@
|
||||
#include "AssetLoaderRawFile.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <zlib.h>
|
||||
|
||||
#include "Game/IW5/IW5.h"
|
||||
#include "Pool/GlobalAssetPool.h"
|
||||
@ -26,17 +29,48 @@ bool AssetLoaderRawFile::LoadFromRaw(const std::string& assetName, ISearchPath*
|
||||
if (!file.IsOpen())
|
||||
return false;
|
||||
|
||||
auto* rawFile = memory->Create<RawFile>();
|
||||
rawFile->name = memory->Dup(assetName.c_str());
|
||||
rawFile->compressedLen = static_cast<int>(file.m_length + 1);
|
||||
|
||||
auto* fileBuffer = static_cast<char*>(memory->Alloc(static_cast<size_t>(file.m_length + 1)));
|
||||
file.m_stream->read(fileBuffer, file.m_length);
|
||||
const auto uncompressedBuffer = std::make_unique<char[]>(static_cast<size_t>(file.m_length));
|
||||
file.m_stream->read(uncompressedBuffer.get(), file.m_length);
|
||||
if (file.m_stream->gcount() != file.m_length)
|
||||
return false;
|
||||
fileBuffer[file.m_length] = '\0';
|
||||
|
||||
rawFile->buffer = fileBuffer;
|
||||
const auto compressionBufferSize = static_cast<size_t>(file.m_length + COMPRESSED_BUFFER_SIZE_PADDING);
|
||||
auto* compressedBuffer = static_cast<char*>(memory->Alloc(compressionBufferSize));
|
||||
|
||||
z_stream_s zs{};
|
||||
|
||||
zs.zalloc = Z_NULL;
|
||||
zs.zfree = Z_NULL;
|
||||
zs.opaque = Z_NULL;
|
||||
zs.avail_in = static_cast<uInt>(file.m_length);
|
||||
zs.avail_out = compressionBufferSize;
|
||||
zs.next_in = reinterpret_cast<const Bytef*>(uncompressedBuffer.get());
|
||||
zs.next_out = reinterpret_cast<Bytef*>(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::cout << "Deflate failed for loading rawfile \"" << assetName << "\"" << std::endl;
|
||||
deflateEnd(&zs);
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto compressedSize = compressionBufferSize - zs.avail_out;
|
||||
|
||||
auto* rawFile = memory->Create<RawFile>();
|
||||
rawFile->name = memory->Dup(assetName.c_str());
|
||||
rawFile->compressedLen = static_cast<int>(compressedSize);
|
||||
rawFile->len = static_cast<int>(file.m_length);
|
||||
rawFile->buffer = static_cast<const char*>(compressedBuffer);
|
||||
|
||||
manager->AddAsset(ASSET_TYPE_RAWFILE, assetName, rawFile);
|
||||
|
||||
return true;
|
||||
|
@ -8,6 +8,8 @@ namespace IW5
|
||||
{
|
||||
class AssetLoaderRawFile final : public BasicAssetLoader<ASSET_TYPE_RAWFILE, RawFile>
|
||||
{
|
||||
static constexpr size_t COMPRESSED_BUFFER_SIZE_PADDING = 64;
|
||||
|
||||
public:
|
||||
_NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override;
|
||||
_NODISCARD bool CanLoadFromRaw() const override;
|
||||
|
@ -37,10 +37,10 @@ std::string AssetDumperRawFile::GetFileNameForAsset(Zone* zone, XAssetInfo<RawFi
|
||||
void AssetDumperRawFile::DumpRaw(AssetDumpingContext& context, XAssetInfo<RawFile>* asset, std::ostream& stream)
|
||||
{
|
||||
const auto* rawFile = asset->Asset();
|
||||
if (rawFile->compressedLen > 0)
|
||||
{
|
||||
z_stream_s zs{};
|
||||
if (rawFile->compressedLen <= 0)
|
||||
return;
|
||||
|
||||
z_stream_s zs{};
|
||||
zs.zalloc = Z_NULL;
|
||||
zs.zfree = Z_NULL;
|
||||
zs.opaque = Z_NULL;
|
||||
@ -76,9 +76,4 @@ void AssetDumperRawFile::DumpRaw(AssetDumpingContext& context, XAssetInfo<RawFil
|
||||
}
|
||||
|
||||
inflateEnd(&zs);
|
||||
}
|
||||
else if (rawFile->len > 0)
|
||||
{
|
||||
stream.write(rawFile->buffer, rawFile->len);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user