2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-09-02 15:07:26 +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

@@ -10,7 +10,7 @@
#include "Menu/AssetDumperMenuDef.h"
#include "Menu/AssetDumperMenuList.h"
#include "ObjWriting.h"
#include "RawFile/AssetDumperRawFile.h"
#include "RawFile/RawFileDumperIW5.h"
#include "Script/AssetDumperScriptFile.h"
#include "Sound/AssetDumperLoadedSound.h"
#include "StringTable/AssetDumperStringTable.h"
@@ -61,7 +61,7 @@ bool ObjWriter::DumpZone(AssetDumpingContext& context) const
// DUMP_ASSET_POOL(AssetDumperFxEffectDef, m_fx, ASSET_TYPE_FX)
// DUMP_ASSET_POOL(AssetDumperFxImpactTable, m_fx_impact_table, ASSET_TYPE_IMPACT_FX)
// DUMP_ASSET_POOL(AssetDumperSurfaceFxTable, m_surface_fx_table, ASSET_TYPE_SURFACE_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(AssetDumperScriptFile, m_script_file, ASSET_TYPE_SCRIPTFILE)
DUMP_ASSET_POOL(AssetDumperStringTable, m_string_table, ASSET_TYPE_STRINGTABLE)
DUMP_ASSET_POOL(leaderboard::JsonDumper, m_leaderboard, ASSET_TYPE_LEADERBOARD)

View File

@@ -1,62 +0,0 @@
#include "AssetDumperRawFile.h"
#include <format>
#include <stdexcept>
#include <zlib.h>
using namespace IW5;
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)
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)
{
throw std::runtime_error("Initializing inflate failed");
}
zs.next_in = reinterpret_cast<const Bytef*>(rawFile->buffer);
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);
}

View File

@@ -0,0 +1,65 @@
#include "RawFileDumperIW5.h"
#include <format>
#include <stdexcept>
#include <zlib.h>
using namespace IW5;
namespace IW5::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)
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)
{
throw std::runtime_error("Initializing inflate failed");
}
zs.next_in = reinterpret_cast<const Bytef*>(rawFile->buffer);
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);
}
} // namespace IW5::raw_file

View File

@@ -3,12 +3,12 @@
#include "Dumping/AbstractAssetDumper.h"
#include "Game/IW5/IW5.h"
namespace IW5
namespace IW5::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 IW5
} // namespace IW5::raw_file