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 "AssetLoaderRawFile.h"
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <iostream>
|
||||||
|
#include <zlib.h>
|
||||||
|
|
||||||
#include "Game/IW5/IW5.h"
|
#include "Game/IW5/IW5.h"
|
||||||
#include "Pool/GlobalAssetPool.h"
|
#include "Pool/GlobalAssetPool.h"
|
||||||
@ -26,17 +29,48 @@ bool AssetLoaderRawFile::LoadFromRaw(const std::string& assetName, ISearchPath*
|
|||||||
if (!file.IsOpen())
|
if (!file.IsOpen())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
auto* rawFile = memory->Create<RawFile>();
|
const auto uncompressedBuffer = std::make_unique<char[]>(static_cast<size_t>(file.m_length));
|
||||||
rawFile->name = memory->Dup(assetName.c_str());
|
file.m_stream->read(uncompressedBuffer.get(), file.m_length);
|
||||||
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);
|
|
||||||
if (file.m_stream->gcount() != file.m_length)
|
if (file.m_stream->gcount() != file.m_length)
|
||||||
return false;
|
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);
|
manager->AddAsset(ASSET_TYPE_RAWFILE, assetName, rawFile);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -8,6 +8,8 @@ namespace IW5
|
|||||||
{
|
{
|
||||||
class AssetLoaderRawFile final : public BasicAssetLoader<ASSET_TYPE_RAWFILE, RawFile>
|
class AssetLoaderRawFile final : public BasicAssetLoader<ASSET_TYPE_RAWFILE, RawFile>
|
||||||
{
|
{
|
||||||
|
static constexpr size_t COMPRESSED_BUFFER_SIZE_PADDING = 64;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
_NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override;
|
_NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override;
|
||||||
_NODISCARD bool CanLoadFromRaw() const override;
|
_NODISCARD bool CanLoadFromRaw() const override;
|
||||||
|
@ -37,48 +37,43 @@ std::string AssetDumperRawFile::GetFileNameForAsset(Zone* zone, XAssetInfo<RawFi
|
|||||||
void AssetDumperRawFile::DumpRaw(AssetDumpingContext& context, XAssetInfo<RawFile>* asset, std::ostream& stream)
|
void AssetDumperRawFile::DumpRaw(AssetDumpingContext& context, XAssetInfo<RawFile>* asset, std::ostream& stream)
|
||||||
{
|
{
|
||||||
const auto* rawFile = asset->Asset();
|
const auto* rawFile = asset->Asset();
|
||||||
if (rawFile->compressedLen > 0)
|
if (rawFile->compressedLen <= 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
z_stream_s zs{};
|
||||||
|
zs.zalloc = Z_NULL;
|
||||||
|
zs.zfree = Z_NULL;
|
||||||
|
zs.opaque = Z_NULL;
|
||||||
|
zs.avail_in = 0;
|
||||||
|
zs.next_in = Z_NULL;
|
||||||
|
|
||||||
|
int ret = inflateInit(&zs);
|
||||||
|
|
||||||
|
if (ret != Z_OK)
|
||||||
{
|
{
|
||||||
z_stream_s zs{};
|
throw std::runtime_error("Initializing inflate failed");
|
||||||
|
}
|
||||||
|
|
||||||
zs.zalloc = Z_NULL;
|
zs.next_in = reinterpret_cast<const Bytef*>(rawFile->buffer);
|
||||||
zs.zfree = Z_NULL;
|
zs.avail_in = rawFile->compressedLen;
|
||||||
zs.opaque = Z_NULL;
|
|
||||||
zs.avail_in = 0;
|
|
||||||
zs.next_in = Z_NULL;
|
|
||||||
|
|
||||||
int ret = inflateInit(&zs);
|
Bytef buffer[0x1000];
|
||||||
|
|
||||||
if (ret != Z_OK)
|
while (zs.avail_in > 0)
|
||||||
|
{
|
||||||
|
zs.next_out = buffer;
|
||||||
|
zs.avail_out = sizeof buffer;
|
||||||
|
ret = inflate(&zs, Z_SYNC_FLUSH);
|
||||||
|
|
||||||
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
throw std::runtime_error("Initializing inflate failed");
|
printf("Inflate failed for dumping rawfile '%s'\n", rawFile->name);
|
||||||
|
inflateEnd(&zs);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
zs.next_in = reinterpret_cast<const Bytef*>(rawFile->buffer);
|
stream.write(reinterpret_cast<char*>(buffer), sizeof buffer - zs.avail_out);
|
||||||
zs.avail_in = rawFile->compressedLen;
|
|
||||||
|
|
||||||
Bytef buffer[0x1000];
|
|
||||||
|
|
||||||
while (zs.avail_in > 0)
|
|
||||||
{
|
|
||||||
zs.next_out = buffer;
|
|
||||||
zs.avail_out = sizeof buffer;
|
|
||||||
ret = inflate(&zs, Z_SYNC_FLUSH);
|
|
||||||
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
printf("Inflate failed for dumping rawfile '%s'\n", rawFile->name);
|
|
||||||
inflateEnd(&zs);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
stream.write(reinterpret_cast<char*>(buffer), sizeof buffer - zs.avail_out);
|
|
||||||
}
|
|
||||||
|
|
||||||
inflateEnd(&zs);
|
|
||||||
}
|
|
||||||
else if (rawFile->len > 0)
|
|
||||||
{
|
|
||||||
stream.write(rawFile->buffer, rawFile->len);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inflateEnd(&zs);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user