mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-20 00:02:55 +00:00
Merge pull request #59 from JezuzLizard/t6-animtree-decompression-compression
T6 animtree decompression compression
This commit is contained in:
commit
9e275f37d2
@ -4,9 +4,15 @@
|
|||||||
#include "Pool/GlobalAssetPool.h"
|
#include "Pool/GlobalAssetPool.h"
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <iostream>
|
||||||
|
#include <zlib.h>
|
||||||
|
#include <zutil.h>
|
||||||
|
|
||||||
using namespace T6;
|
using namespace T6;
|
||||||
|
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
void* AssetLoaderRawFile::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory)
|
void* AssetLoaderRawFile::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory)
|
||||||
{
|
{
|
||||||
auto* rawFile = memory->Create<RawFile>();
|
auto* rawFile = memory->Create<RawFile>();
|
||||||
@ -20,13 +26,60 @@ bool AssetLoaderRawFile::CanLoadFromRaw() const
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AssetLoaderRawFile::LoadFromRaw(
|
bool AssetLoaderRawFile::LoadAnimtree(
|
||||||
const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const
|
const SearchPathOpenFile& file, const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager)
|
||||||
{
|
{
|
||||||
const auto file = searchPath->Open(assetName);
|
const auto uncompressedBuffer = std::make_unique<char[]>(static_cast<size_t>(file.m_length));
|
||||||
if (!file.IsOpen())
|
file.m_stream->read(uncompressedBuffer.get(), file.m_length);
|
||||||
|
if (file.m_stream->gcount() != file.m_length)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
const auto compressionBufferSize = static_cast<size_t>(file.m_length + sizeof(uint32_t) + COMPRESSED_BUFFER_SIZE_PADDING);
|
||||||
|
auto* compressedBuffer = static_cast<char*>(memory->Alloc(compressionBufferSize));
|
||||||
|
|
||||||
|
z_stream_s zs{};
|
||||||
|
|
||||||
|
zs.zalloc = Z_NULL;
|
||||||
|
zs.zfree = Z_NULL;
|
||||||
|
zs.opaque = Z_NULL;
|
||||||
|
zs.avail_in = static_cast<uInt>(file.m_length);
|
||||||
|
zs.avail_out = compressionBufferSize;
|
||||||
|
zs.next_in = reinterpret_cast<const Bytef*>(uncompressedBuffer.get());
|
||||||
|
zs.next_out = reinterpret_cast<Bytef*>(&compressedBuffer[sizeof(uint32_t)]);
|
||||||
|
|
||||||
|
int ret = deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -DEF_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
|
||||||
|
|
||||||
|
if (ret != Z_OK)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Initializing deflate failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = deflate(&zs, Z_FINISH);
|
||||||
|
|
||||||
|
if (ret != Z_STREAM_END)
|
||||||
|
{
|
||||||
|
std::cerr << "Deflate failed for loading animtree file \"" << assetName << "\"" << std::endl;
|
||||||
|
deflateEnd(&zs);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto compressedSize = compressionBufferSize + sizeof(uint32_t) - zs.avail_out;
|
||||||
|
|
||||||
|
reinterpret_cast<uint32_t*>(compressedBuffer)[0] = static_cast<uint32_t>(file.m_length); // outLen
|
||||||
|
|
||||||
|
auto* rawFile = memory->Create<RawFile>();
|
||||||
|
rawFile->name = memory->Dup(assetName.c_str());
|
||||||
|
rawFile->len = static_cast<int>(compressedSize);
|
||||||
|
rawFile->buffer = static_cast<const char*>(compressedBuffer);
|
||||||
|
|
||||||
|
manager->AddAsset(ASSET_TYPE_RAWFILE, assetName, rawFile);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AssetLoaderRawFile::LoadDefault(
|
||||||
|
const SearchPathOpenFile& file, const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager)
|
||||||
|
{
|
||||||
auto* rawFile = memory->Create<RawFile>();
|
auto* rawFile = memory->Create<RawFile>();
|
||||||
rawFile->name = memory->Dup(assetName.c_str());
|
rawFile->name = memory->Dup(assetName.c_str());
|
||||||
rawFile->len = static_cast<int>(file.m_length);
|
rawFile->len = static_cast<int>(file.m_length);
|
||||||
@ -42,3 +95,19 @@ bool AssetLoaderRawFile::LoadFromRaw(
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool AssetLoaderRawFile::LoadFromRaw(
|
||||||
|
const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const
|
||||||
|
{
|
||||||
|
const auto file = searchPath->Open(assetName);
|
||||||
|
if (!file.IsOpen())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
const fs::path rawFilePath(assetName);
|
||||||
|
const auto extension = rawFilePath.extension().string();
|
||||||
|
|
||||||
|
if (extension == ".atr")
|
||||||
|
return LoadAnimtree(file, assetName, searchPath, memory, manager);
|
||||||
|
|
||||||
|
return LoadDefault(file, assetName, searchPath, memory, manager);
|
||||||
|
}
|
||||||
|
@ -8,6 +8,13 @@ namespace T6
|
|||||||
{
|
{
|
||||||
class AssetLoaderRawFile final : public BasicAssetLoader<ASSET_TYPE_RAWFILE, RawFile>
|
class AssetLoaderRawFile final : public BasicAssetLoader<ASSET_TYPE_RAWFILE, RawFile>
|
||||||
{
|
{
|
||||||
|
static constexpr size_t COMPRESSED_BUFFER_SIZE_PADDING = 64;
|
||||||
|
|
||||||
|
static bool LoadAnimtree(
|
||||||
|
const SearchPathOpenFile& file, const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager);
|
||||||
|
static bool LoadDefault(
|
||||||
|
const SearchPathOpenFile& file, const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
_NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override;
|
_NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override;
|
||||||
_NODISCARD bool CanLoadFromRaw() const override;
|
_NODISCARD bool CanLoadFromRaw() const override;
|
||||||
|
@ -1,12 +1,78 @@
|
|||||||
#include "AssetDumperRawFile.h"
|
#include "AssetDumperRawFile.h"
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <zlib.h>
|
||||||
|
#include <zutil.h>
|
||||||
|
|
||||||
using namespace T6;
|
using namespace T6;
|
||||||
|
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
bool AssetDumperRawFile::ShouldDump(XAssetInfo<RawFile>* asset)
|
bool AssetDumperRawFile::ShouldDump(XAssetInfo<RawFile>* asset)
|
||||||
{
|
{
|
||||||
return true;
|
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 << "\"" << std::endl;
|
||||||
|
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 << 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 = 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 << "\"" << std::endl;
|
||||||
|
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)
|
void AssetDumperRawFile::DumpAsset(AssetDumpingContext& context, XAssetInfo<RawFile>* asset)
|
||||||
{
|
{
|
||||||
const auto* rawFile = asset->Asset();
|
const auto* rawFile = asset->Asset();
|
||||||
@ -16,5 +82,15 @@ void AssetDumperRawFile::DumpAsset(AssetDumpingContext& context, XAssetInfo<RawF
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
auto& stream = *assetFile;
|
auto& stream = *assetFile;
|
||||||
stream.write(rawFile->buffer, rawFile->len);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,10 @@ namespace T6
|
|||||||
{
|
{
|
||||||
class AssetDumperRawFile final : public AbstractAssetDumper<RawFile>
|
class AssetDumperRawFile final : public AbstractAssetDumper<RawFile>
|
||||||
{
|
{
|
||||||
|
constexpr static size_t ANIMTREE_MAX_SIZE = 0xC000000;
|
||||||
|
|
||||||
|
void DumpAnimtree(AssetDumpingContext& context, XAssetInfo<RawFile>* asset, std::ostream& stream);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool ShouldDump(XAssetInfo<RawFile>* asset) override;
|
bool ShouldDump(XAssetInfo<RawFile>* asset) override;
|
||||||
void DumpAsset(AssetDumpingContext& context, XAssetInfo<RawFile>* asset) override;
|
void DumpAsset(AssetDumpingContext& context, XAssetInfo<RawFile>* asset) override;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user