mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-21 00:25:44 +00:00
fix t5 rawfile loader not compressing gsc and csc files as the loader expects it
This commit is contained in:
parent
b3029c0d28
commit
0492a87cbd
@ -1,12 +1,17 @@
|
|||||||
#include "AssetLoaderRawFile.h"
|
#include "AssetLoaderRawFile.h"
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <iostream>
|
||||||
|
#include <zlib.h>
|
||||||
|
|
||||||
#include "Game/T5/T5.h"
|
#include "Game/T5/T5.h"
|
||||||
#include "Pool/GlobalAssetPool.h"
|
#include "Pool/GlobalAssetPool.h"
|
||||||
|
|
||||||
using namespace T5;
|
using namespace T5;
|
||||||
|
|
||||||
|
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,12 +25,60 @@ bool AssetLoaderRawFile::CanLoadFromRaw() const
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AssetLoaderRawFile::LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const
|
bool AssetLoaderRawFile::LoadGsc(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 + 1));
|
||||||
if (!file.IsOpen())
|
file.m_stream->read(uncompressedBuffer.get(), file.m_length);
|
||||||
|
if (file.m_stream->gcount() != file.m_length)
|
||||||
return false;
|
return false;
|
||||||
|
uncompressedBuffer[static_cast<size_t>(file.m_length)] = '\0';
|
||||||
|
|
||||||
|
const auto compressionBufferSize = static_cast<size_t>(file.m_length + 1 + sizeof(uint32_t) + 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 + 1);
|
||||||
|
zs.avail_out = compressionBufferSize;
|
||||||
|
zs.next_in = reinterpret_cast<const Bytef*>(uncompressedBuffer.get());
|
||||||
|
zs.next_out = reinterpret_cast<Bytef*>(&compressedBuffer[sizeof(uint32_t) + sizeof(uint32_t)]);
|
||||||
|
|
||||||
|
int ret = deflateInit(&zs, Z_DEFAULT_COMPRESSION);
|
||||||
|
|
||||||
|
if (ret != Z_OK)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Initializing deflate failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = deflate(&zs, Z_FINISH);
|
||||||
|
|
||||||
|
if (ret != Z_STREAM_END)
|
||||||
|
{
|
||||||
|
std::cout << "Deflate failed for loading gsc file \"" << assetName << "\"" << std::endl;
|
||||||
|
deflateEnd(&zs);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto compressedSize = compressionBufferSize - zs.avail_out;
|
||||||
|
|
||||||
|
reinterpret_cast<uint32_t*>(compressedBuffer)[0] = static_cast<uint32_t>(file.m_length + 1); // outLen
|
||||||
|
reinterpret_cast<uint32_t*>(compressedBuffer)[1] = compressedSize; // inLen
|
||||||
|
|
||||||
|
auto* rawFile = memory->Create<RawFile>();
|
||||||
|
rawFile->name = memory->Dup(assetName.c_str());
|
||||||
|
rawFile->len = static_cast<int>(compressedSize + sizeof(uint32_t) + sizeof(uint32_t));
|
||||||
|
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);
|
||||||
@ -41,3 +94,18 @@ bool AssetLoaderRawFile::LoadFromRaw(const std::string& assetName, ISearchPath*
|
|||||||
|
|
||||||
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 == ".gsc" ||extension == ".csc")
|
||||||
|
return LoadGsc(file, assetName, searchPath, memory, manager);
|
||||||
|
|
||||||
|
return LoadDefault(file, assetName, searchPath, memory, manager);
|
||||||
|
}
|
||||||
|
@ -8,6 +8,11 @@ namespace T5
|
|||||||
{
|
{
|
||||||
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 LoadGsc(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;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user