2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2026-01-11 03:01:49 +00:00

refactor: streamline rawfile dumping

This commit is contained in:
Jan Laupetin
2025-07-30 19:55:57 +01:00
parent 24c9e08046
commit bcb52391dc
20 changed files with 374 additions and 358 deletions

View File

@@ -14,7 +14,7 @@
#include "ObjWriting.h"
#include "PhysCollmap/AssetDumperPhysCollmap.h"
#include "PhysPreset/PhysPresetInfoStringDumperIW4.h"
#include "RawFile/AssetDumperRawFile.h"
#include "RawFile/RawFileDumperIW4.h"
#include "Shader/AssetDumperPixelShader.h"
#include "Shader/AssetDumperVertexShader.h"
#include "Sound/AssetDumperLoadedSound.h"
@@ -70,7 +70,7 @@ bool ObjWriter::DumpZone(AssetDumpingContext& context) const
// DUMP_ASSET_POOL(AssetDumperSndDriverGlobals, m_snd_driver_globals, ASSET_TYPE_SNDDRIVER_GLOBALS)
// DUMP_ASSET_POOL(AssetDumperFxEffectDef, m_fx, ASSET_TYPE_FX)
// DUMP_ASSET_POOL(AssetDumperFxImpactTable, m_fx_impact_table, ASSET_TYPE_IMPACT_FX)
DUMP_ASSET_POOL(AssetDumperRawFile, m_raw_file, ASSET_TYPE_RAWFILE)
DUMP_ASSET_POOL(raw_file::Dumper, m_raw_file, ASSET_TYPE_RAWFILE)
DUMP_ASSET_POOL(AssetDumperStringTable, m_string_table, ASSET_TYPE_STRINGTABLE)
DUMP_ASSET_POOL(leaderboard::JsonDumper, m_leaderboard, ASSET_TYPE_LEADERBOARD)
DUMP_ASSET_POOL(AssetDumperStructuredDataDefSet, m_structed_data_def_set, ASSET_TYPE_STRUCTURED_DATA_DEF)

View File

@@ -1,67 +0,0 @@
#include "AssetDumperRawFile.h"
#include <format>
#include <stdexcept>
#include <zlib.h>
using namespace IW4;
bool AssetDumperRawFile::ShouldDump(XAssetInfo<RawFile>* asset)
{
return true;
}
void AssetDumperRawFile::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);
}
}

View 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

View File

@@ -3,12 +3,12 @@
#include "Dumping/AbstractAssetDumper.h"
#include "Game/IW4/IW4.h"
namespace IW4
namespace IW4::raw_file
{
class AssetDumperRawFile final : public AbstractAssetDumper<RawFile>
class Dumper final : public AbstractAssetDumper<RawFile>
{
protected:
bool ShouldDump(XAssetInfo<RawFile>* asset) override;
void DumpAsset(AssetDumpingContext& context, XAssetInfo<RawFile>* asset) override;
};
} // namespace IW4
} // namespace IW4::raw_file