Load T5 fastfiles

This commit is contained in:
Jan
2021-05-12 21:47:34 +02:00
parent e3a3d012e6
commit bb3e7d9e88
18 changed files with 711 additions and 6 deletions

View File

@ -1,7 +1,91 @@
#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 << "\"" << std::endl;
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 << std::endl;
return;
}
if(outLen > GSC_MAX_SIZE)
{
std::cout << "Invalid size of gsc file \"" << rawFile->name << "\": " << outLen << std::endl;
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 << "\"" << std::endl;
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;
@ -20,5 +104,16 @@ std::string AssetDumperRawFile::GetFileNameForAsset(Zone* zone, XAssetInfo<RawFi
void AssetDumperRawFile::DumpRaw(AssetDumpingContext& context, XAssetInfo<RawFile>* asset, std::ostream& stream)
{
const auto* rawFile = asset->Asset();
stream.write(rawFile->buffer, rawFile->len);
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

@ -7,6 +7,10 @@ namespace T5
{
class AssetDumperRawFile final : public AbstractAssetDumper<RawFile>
{
constexpr static size_t GSC_MAX_SIZE = 0xC000000;
void DumpGsc(AssetDumpingContext& context, XAssetInfo<RawFile>* asset, std::ostream& stream);
protected:
bool ShouldDump(XAssetInfo<RawFile>* asset) override;
bool CanDumpAsRaw() override;

View File

@ -2,6 +2,7 @@
#include "Dumping/IZoneDumper.h"
#include "Game/IW3/ZoneDumperIW3.h"
#include "Game/IW4/ZoneDumperIW4.h"
#include "Game/T5/ZoneDumperT5.h"
#include "Game/T6/ZoneDumperT6.h"
ObjWriting::Configuration_t ObjWriting::Configuration;
@ -10,6 +11,7 @@ const IZoneDumper* const ZONE_DUMPER[]
{
new IW3::ZoneDumper(),
new IW4::ZoneDumper(),
new T5::ZoneDumper(),
new T6::ZoneDumper()
};