mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-10-22 22:35:51 +00:00
refactor: streamline rawfile dumping
This commit is contained in:
121
src/ObjWriting/Game/T5/RawFile/RawFileDumperT5.cpp
Normal file
121
src/ObjWriting/Game/T5/RawFile/RawFileDumperT5.cpp
Normal 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
|
Reference in New Issue
Block a user