Address review comments.

This commit is contained in:
JezuzLizard 2023-12-14 13:48:44 -08:00
parent 96e67dc1fa
commit 8b62bc0bc0
2 changed files with 9 additions and 20 deletions

View File

@ -29,13 +29,12 @@ bool AssetLoaderRawFile::CanLoadFromRaw() const
bool AssetLoaderRawFile::LoadAnimtree( bool AssetLoaderRawFile::LoadAnimtree(
const SearchPathOpenFile& file, const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager) const SearchPathOpenFile& file, const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager)
{ {
const auto uncompressedBuffer = std::make_unique<char[]>(static_cast<size_t>(file.m_length + 1)); const auto uncompressedBuffer = std::make_unique<char[]>(static_cast<size_t>(file.m_length));
file.m_stream->read(uncompressedBuffer.get(), file.m_length); file.m_stream->read(uncompressedBuffer.get(), file.m_length);
if (file.m_stream->gcount() != 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) + COMPRESSED_BUFFER_SIZE_PADDING); 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)); auto* compressedBuffer = static_cast<char*>(memory->Alloc(compressionBufferSize));
z_stream_s zs{}; z_stream_s zs{};
@ -43,7 +42,7 @@ bool AssetLoaderRawFile::LoadAnimtree(
zs.zalloc = Z_NULL; zs.zalloc = Z_NULL;
zs.zfree = Z_NULL; zs.zfree = Z_NULL;
zs.opaque = Z_NULL; zs.opaque = Z_NULL;
zs.avail_in = static_cast<uInt>(file.m_length + 1); zs.avail_in = static_cast<uInt>(file.m_length);
zs.avail_out = compressionBufferSize; zs.avail_out = compressionBufferSize;
zs.next_in = reinterpret_cast<const Bytef*>(uncompressedBuffer.get()); zs.next_in = reinterpret_cast<const Bytef*>(uncompressedBuffer.get());
zs.next_out = reinterpret_cast<Bytef*>(&compressedBuffer[sizeof(uint32_t)]); zs.next_out = reinterpret_cast<Bytef*>(&compressedBuffer[sizeof(uint32_t)]);
@ -59,14 +58,14 @@ bool AssetLoaderRawFile::LoadAnimtree(
if (ret != Z_STREAM_END) if (ret != Z_STREAM_END)
{ {
std::cout << "Deflate failed for loading animtree file \"" << assetName << "\"" << std::endl; std::cerr << "Deflate failed for loading animtree file \"" << assetName << "\"" << std::endl;
deflateEnd(&zs); deflateEnd(&zs);
return false; return false;
} }
const auto compressedSize = compressionBufferSize + sizeof(uint32_t) - zs.avail_out; const auto compressedSize = compressionBufferSize + sizeof(uint32_t) - zs.avail_out;
reinterpret_cast<uint32_t*>(compressedBuffer)[0] = static_cast<uint32_t>(file.m_length + 1); // outLen reinterpret_cast<uint32_t*>(compressedBuffer)[0] = static_cast<uint32_t>(file.m_length); // outLen
auto* rawFile = memory->Create<RawFile>(); auto* rawFile = memory->Create<RawFile>();
rawFile->name = memory->Dup(assetName.c_str()); rawFile->name = memory->Dup(assetName.c_str());

View File

@ -19,7 +19,7 @@ void AssetDumperRawFile::DumpAnimtree(AssetDumpingContext& context, XAssetInfo<R
if (rawFile->len <= 4) if (rawFile->len <= 4)
{ {
std::cout << "Invalid len of animtree file \"" << rawFile->name << "\"" << std::endl; std::cerr << "Invalid len of animtree file \"" << rawFile->name << "\"" << std::endl;
return; return;
} }
@ -28,7 +28,7 @@ void AssetDumperRawFile::DumpAnimtree(AssetDumpingContext& context, XAssetInfo<R
if (outLen > ANIMTREE_MAX_SIZE) if (outLen > ANIMTREE_MAX_SIZE)
{ {
std::cout << "Invalid size of animtree file \"" << rawFile->name << "\": " << outLen << std::endl; std::cerr << "Invalid size of animtree file \"" << rawFile->name << "\": " << outLen << std::endl;
return; return;
} }
@ -52,7 +52,6 @@ void AssetDumperRawFile::DumpAnimtree(AssetDumpingContext& context, XAssetInfo<R
Bytef buffer[0x1000]; Bytef buffer[0x1000];
size_t writtenSize = 0;
while (zs.avail_in > 0) while (zs.avail_in > 0)
{ {
zs.next_out = buffer; zs.next_out = buffer;
@ -61,23 +60,14 @@ void AssetDumperRawFile::DumpAnimtree(AssetDumpingContext& context, XAssetInfo<R
if (ret < 0) if (ret < 0)
{ {
std::cout << "Inflate failed for dumping animtree file \"" << rawFile->name << "\"" << std::endl; std::cerr << "Inflate failed for dumping animtree file \"" << rawFile->name << "\"" << std::endl;
inflateEnd(&zs); inflateEnd(&zs);
return; return;
} }
const auto inflateOutSize = sizeof buffer - zs.avail_out; const auto inflateOutSize = sizeof buffer - zs.avail_out;
if (writtenSize + inflateOutSize >= outLen) stream.write(reinterpret_cast<char*>(buffer), inflateOutSize);
{
// 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); inflateEnd(&zs);