2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-08-30 21:53:15 +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

@@ -7,7 +7,7 @@
#include "Localize/LocalizeDumperIW3.h"
#include "Maps/MapEntsDumperIW3.h"
#include "ObjWriting.h"
#include "RawFile/AssetDumperRawFile.h"
#include "RawFile/RawFileDumperIW3.h"
#include "Sound/AssetDumperLoadedSound.h"
#include "StringTable/AssetDumperStringTable.h"
#include "Weapon/AssetDumperWeapon.h"
@@ -49,7 +49,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)
return true;

View File

@@ -1,20 +0,0 @@
#include "AssetDumperRawFile.h"
using namespace IW3;
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;
stream.write(rawFile->buffer, rawFile->len);
}

View File

@@ -0,0 +1,23 @@
#include "RawFileDumperIW3.h"
using namespace IW3;
namespace IW3::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;
stream.write(rawFile->buffer, rawFile->len);
}
} // namespace IW3::raw_file

View File

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

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

@@ -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

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

@@ -1,29 +1,31 @@
#include "AssetDumperRawFile.h"
#include "RawFileDumperIW5.h"
#include <format>
#include <stdexcept>
#include <zlib.h>
using namespace IW4;
using namespace IW5;
bool AssetDumperRawFile::ShouldDump(XAssetInfo<RawFile>* asset)
namespace IW5::raw_file
{
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)
bool Dumper::ShouldDump(XAssetInfo<RawFile>* asset)
{
z_stream_s zs{};
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;
@@ -37,7 +39,7 @@ void AssetDumperRawFile::DumpAsset(AssetDumpingContext& context, XAssetInfo<RawF
throw std::runtime_error("Initializing inflate failed");
}
zs.next_in = reinterpret_cast<const Bytef*>(rawFile->data.compressedBuffer);
zs.next_in = reinterpret_cast<const Bytef*>(rawFile->buffer);
zs.avail_in = rawFile->compressedLen;
Bytef buffer[0x1000];
@@ -60,8 +62,4 @@ void AssetDumperRawFile::DumpAsset(AssetDumpingContext& context, XAssetInfo<RawF
inflateEnd(&zs);
}
else if (rawFile->len > 0)
{
stream.write(rawFile->data.buffer, rawFile->len);
}
}
} // 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

View File

@@ -6,7 +6,7 @@
#include "Image/ImageDumperT5.h"
#include "Localize/LocalizeDumperT5.h"
#include "ObjWriting.h"
#include "RawFile/AssetDumperRawFile.h"
#include "RawFile/RawFileDumperT5.h"
#include "Sound/AssetDumperSndBank.h"
#include "StringTable/AssetDumperStringTable.h"
#include "Weapon/AssetDumperWeapon.h"
@@ -49,7 +49,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(AssetDumperPackIndex, m_pack_index, ASSET_TYPE_PACK_INDEX)
// DUMP_ASSET_POOL(AssetDumperXGlobals, m_xglobals, ASSET_TYPE_XGLOBALS)

View File

@@ -1,114 +0,0 @@
#include "AssetDumperRawFile.h"
#include <cassert>
#include <filesystem>
#include <zlib.h>
using namespace T5;
namespace fs = std::filesystem;
void AssetDumperRawFile::DumpGsc(AssetDumpingContext& context, XAssetInfo<RawFile>* asset, std::ostream& stream)
{
const auto* rawFile = asset->Asset();
if (rawFile->len <= 8)
{
std::cout << "Invalid len of gsc file \"" << rawFile->name << "\"\n";
return;
}
const auto outLen = reinterpret_cast<const uint32_t*>(rawFile->buffer)[0];
const auto inLen = reinterpret_cast<const uint32_t*>(rawFile->buffer)[1];
assert(inLen == static_cast<unsigned>(rawFile->len) - 8);
if (inLen > static_cast<unsigned>(rawFile->len - 8) + 1)
{
std::cout << "Invalid compression of gsc file \"" << rawFile->name << "\": " << inLen << "\n";
return;
}
if (outLen > GSC_MAX_SIZE)
{
std::cout << "Invalid size of gsc file \"" << rawFile->name << "\": " << outLen << "\n";
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[8]);
zs.avail_in = inLen;
Bytef buffer[0x1000];
size_t writtenSize = 0;
while (zs.avail_in > 0)
{
zs.next_out = buffer;
zs.avail_out = sizeof(buffer);
ret = inflate(&zs, Z_SYNC_FLUSH);
if (ret < 0)
{
std::cout << "Inflate failed for dumping gsc file \"" << rawFile->name << "\"\n";
inflateEnd(&zs);
return;
}
const auto inflateOutSize = sizeof(buffer) - zs.avail_out;
if (writtenSize + inflateOutSize >= outLen)
{
// Last byte is a \0 byte. Skip it.
stream.write(reinterpret_cast<char*>(buffer), inflateOutSize - 1);
}
else
{
stream.write(reinterpret_cast<char*>(buffer), inflateOutSize);
}
writtenSize += inflateOutSize;
}
inflateEnd(&zs);
}
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;
const fs::path rawFilePath(rawFile->name);
const auto extension = rawFilePath.extension().string();
if (extension == ".gsc" || extension == ".csc")
{
DumpGsc(context, asset, stream);
}
else
{
stream.write(rawFile->buffer, rawFile->len);
}
}

View File

@@ -1,18 +0,0 @@
#pragma once
#include "Dumping/AbstractAssetDumper.h"
#include "Game/T5/T5.h"
namespace T5
{
class AssetDumperRawFile final : public AbstractAssetDumper<RawFile>
{
constexpr static size_t GSC_MAX_SIZE = 0xC000000;
static void DumpGsc(AssetDumpingContext& context, XAssetInfo<RawFile>* asset, std::ostream& stream);
protected:
bool ShouldDump(XAssetInfo<RawFile>* asset) override;
void DumpAsset(AssetDumpingContext& context, XAssetInfo<RawFile>* asset) override;
};
} // namespace T5

View File

@@ -0,0 +1,121 @@
#include "RawFileDumperT5.h"
#include "Utils/StringUtils.h"
#include <cassert>
#include <filesystem>
#include <zlib.h>
using namespace T5;
namespace fs = std::filesystem;
namespace
{
constexpr static size_t GSC_MAX_SIZE = 0xC000000;
void DumpGsc(AssetDumpingContext& context, XAssetInfo<RawFile>* asset, std::ostream& stream)
{
const auto* rawFile = asset->Asset();
if (rawFile->len <= 8)
{
std::cout << "Invalid len of gsc file \"" << rawFile->name << "\"\n";
return;
}
const auto outLen = reinterpret_cast<const uint32_t*>(rawFile->buffer)[0];
const auto inLen = reinterpret_cast<const uint32_t*>(rawFile->buffer)[1];
assert(inLen == static_cast<unsigned>(rawFile->len) - 8);
if (inLen > static_cast<unsigned>(rawFile->len - 8) + 1)
{
std::cout << "Invalid compression of gsc file \"" << rawFile->name << "\": " << inLen << "\n";
return;
}
if (outLen > GSC_MAX_SIZE)
{
std::cout << "Invalid size of gsc file \"" << rawFile->name << "\": " << outLen << "\n";
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[8]);
zs.avail_in = inLen;
Bytef buffer[0x1000];
size_t writtenSize = 0;
while (zs.avail_in > 0)
{
zs.next_out = buffer;
zs.avail_out = sizeof(buffer);
ret = inflate(&zs, Z_SYNC_FLUSH);
if (ret < 0)
{
std::cout << "Inflate failed for dumping gsc file \"" << rawFile->name << "\"\n";
inflateEnd(&zs);
return;
}
const auto inflateOutSize = sizeof(buffer) - zs.avail_out;
if (writtenSize + inflateOutSize >= outLen)
{
// Last byte is a \0 byte. Skip it.
stream.write(reinterpret_cast<char*>(buffer), inflateOutSize - 1);
}
else
{
stream.write(reinterpret_cast<char*>(buffer), inflateOutSize);
}
writtenSize += inflateOutSize;
}
inflateEnd(&zs);
}
} // namespace
namespace T5::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;
const fs::path rawFilePath(rawFile->name);
auto extension = rawFilePath.extension().string();
utils::MakeStringLowerCase(extension);
if (extension == ".gsc" || extension == ".csc")
DumpGsc(context, asset, stream);
else
stream.write(rawFile->buffer, rawFile->len);
}
} // namespace T5::raw_file

View File

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

View File

@@ -12,7 +12,7 @@
#include "PhysConstraints/PhysConstraintsInfoStringDumperT6.h"
#include "PhysPreset/PhysPresetInfoStringDumperT6.h"
#include "Qdb/AssetDumperQdb.h"
#include "RawFile/AssetDumperRawFile.h"
#include "RawFile/RawFileDumperT6.h"
#include "Script/AssetDumperScriptParseTree.h"
#include "Slug/AssetDumperSlug.h"
#include "Sound/AssetDumperSndBank.h"
@@ -75,7 +75,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(AssetDumperXGlobals, m_xglobals, ASSET_TYPE_XGLOBALS)

View File

@@ -1,96 +0,0 @@
#include "AssetDumperRawFile.h"
#include <filesystem>
#include <zlib.h>
#include <zutil.h>
using namespace T6;
namespace fs = std::filesystem;
bool AssetDumperRawFile::ShouldDump(XAssetInfo<RawFile>* asset)
{
return true;
}
void AssetDumperRawFile::DumpAnimtree(AssetDumpingContext& context, XAssetInfo<RawFile>* asset, std::ostream& stream)
{
const auto* rawFile = asset->Asset();
if (rawFile->len <= 4)
{
std::cerr << "Invalid len of animtree file \"" << rawFile->name << "\"\n";
return;
}
const auto outLen = reinterpret_cast<const uint32_t*>(rawFile->buffer)[0];
const auto inLen = rawFile->len;
if (outLen > ANIMTREE_MAX_SIZE)
{
std::cerr << "Invalid size of animtree file \"" << rawFile->name << "\": " << outLen << "\n";
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 = inflateInit2(&zs, -DEF_WBITS);
if (ret != Z_OK)
{
throw std::runtime_error("Initializing inflate failed");
}
zs.next_in = reinterpret_cast<const Bytef*>(&rawFile->buffer[4]);
zs.avail_in = inLen - sizeof(uint32_t);
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 << "Inflate failed for dumping animtree file \"" << rawFile->name << "\"\n";
inflateEnd(&zs);
return;
}
const auto inflateOutSize = sizeof(buffer) - zs.avail_out;
stream.write(reinterpret_cast<char*>(buffer), inflateOutSize);
}
inflateEnd(&zs);
}
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;
const fs::path rawFilePath(rawFile->name);
const auto extension = rawFilePath.extension().string();
if (extension == ".atr")
{
DumpAnimtree(context, asset, stream);
}
else
{
stream.write(rawFile->buffer, rawFile->len);
}
}

View File

@@ -0,0 +1,104 @@
#include "RawFileDumperT6.h"
#include <filesystem>
#include <zlib.h>
#include <zutil.h>
using namespace T6;
namespace fs = std::filesystem;
namespace
{
constexpr size_t ANIMTREE_MAX_SIZE = 0xC000000;
void DumpAnimtree(AssetDumpingContext& context, XAssetInfo<RawFile>* asset, std::ostream& stream)
{
const auto* rawFile = asset->Asset();
if (rawFile->len <= 4)
{
std::cerr << "Invalid len of animtree file \"" << rawFile->name << "\"\n";
return;
}
const auto outLen = reinterpret_cast<const uint32_t*>(rawFile->buffer)[0];
const auto inLen = rawFile->len;
if (outLen > ANIMTREE_MAX_SIZE)
{
std::cerr << "Invalid size of animtree file \"" << rawFile->name << "\": " << outLen << "\n";
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 = inflateInit2(&zs, -DEF_WBITS);
if (ret != Z_OK)
{
throw std::runtime_error("Initializing inflate failed");
}
zs.next_in = reinterpret_cast<const Bytef*>(&rawFile->buffer[4]);
zs.avail_in = inLen - sizeof(uint32_t);
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 << "Inflate failed for dumping animtree file \"" << rawFile->name << "\"\n";
inflateEnd(&zs);
return;
}
const auto inflateOutSize = sizeof(buffer) - zs.avail_out;
stream.write(reinterpret_cast<char*>(buffer), inflateOutSize);
}
inflateEnd(&zs);
}
} // namespace
namespace T6::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;
const fs::path rawFilePath(rawFile->name);
const auto extension = rawFilePath.extension().string();
if (extension == ".atr")
{
DumpAnimtree(context, asset, stream);
}
else
{
stream.write(rawFile->buffer, rawFile->len);
}
}
} // namespace T6::raw_file

View File

@@ -3,14 +3,10 @@
#include "Dumping/AbstractAssetDumper.h"
#include "Game/T6/T6.h"
namespace T6
namespace T6::raw_file
{
class AssetDumperRawFile final : public AbstractAssetDumper<RawFile>
class Dumper final : public AbstractAssetDumper<RawFile>
{
constexpr static size_t ANIMTREE_MAX_SIZE = 0xC000000;
void DumpAnimtree(AssetDumpingContext& context, XAssetInfo<RawFile>* asset, std::ostream& stream);
protected:
bool ShouldDump(XAssetInfo<RawFile>* asset) override;
void DumpAsset(AssetDumpingContext& context, XAssetInfo<RawFile>* asset) override;