mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2026-02-10 17:43:03 +00:00
refactor: streamline rawfile dumping
This commit is contained in:
70
src/ObjWriting/Game/IW4/RawFile/RawFileDumperIW4.cpp
Normal file
70
src/ObjWriting/Game/IW4/RawFile/RawFileDumperIW4.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
#include "RawFileDumperIW4.h"
|
||||
|
||||
#include <format>
|
||||
#include <stdexcept>
|
||||
#include <zlib.h>
|
||||
|
||||
using namespace IW4;
|
||||
|
||||
namespace IW4::raw_file
|
||||
{
|
||||
bool Dumper::ShouldDump(XAssetInfo<RawFile>* asset)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void Dumper::DumpAsset(AssetDumpingContext& context, XAssetInfo<RawFile>* asset)
|
||||
{
|
||||
const auto* rawFile = asset->Asset();
|
||||
const auto assetFile = context.OpenAssetFile(asset->m_name);
|
||||
|
||||
if (!assetFile)
|
||||
return;
|
||||
|
||||
auto& stream = *assetFile;
|
||||
if (rawFile->compressedLen > 0)
|
||||
{
|
||||
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)
|
||||
{
|
||||
throw std::runtime_error("Initializing inflate failed");
|
||||
}
|
||||
|
||||
zs.next_in = reinterpret_cast<const Bytef*>(rawFile->data.compressedBuffer);
|
||||
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)
|
||||
{
|
||||
std::cerr << std::format("Inflate failed when attempting to dump rawfile '{}'\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->data.buffer, rawFile->len);
|
||||
}
|
||||
}
|
||||
} // namespace IW4::raw_file
|
||||
Reference in New Issue
Block a user