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

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