iw4x-client/src/Components/Modules/AssetInterfaces/IRawFile.cpp

78 lines
2.0 KiB
C++
Raw Normal View History

2022-02-27 07:53:44 -05:00
#include <STDInclude.hpp>
#include "IRawFile.hpp"
2017-01-19 16:23:59 -05:00
namespace Assets
{
2018-12-17 08:29:18 -05:00
void IRawFile::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder)
2017-01-19 16:23:59 -05:00
{
Components::FileSystem::File rawFile(name);
if (!rawFile.exists())
2017-01-19 16:23:59 -05:00
{
return;
}
2017-01-19 16:23:59 -05:00
auto* asset = builder->getAllocator()->allocate<Game::RawFile>();
if (!asset)
{
return;
}
2017-01-19 16:23:59 -05:00
2022-12-01 09:04:30 -05:00
auto compressed_size = compressBound(rawFile.getBuffer().size());
2017-01-19 16:23:59 -05:00
asset->name = builder->getAllocator()->duplicateString(name);
2022-12-01 09:04:30 -05:00
asset->len = static_cast<int>(rawFile.getBuffer().size());
2022-12-01 09:04:30 -05:00
if (asset->len < static_cast<int>(compressed_size))
{
2022-12-01 09:04:30 -05:00
asset->buffer = builder->getAllocator()->allocateArray<char>(compressed_size);
compress2((Bytef*)(asset->buffer), &compressed_size, (const Bytef*)(rawFile.getBuffer().data()), rawFile.getBuffer().size(), Z_BEST_COMPRESSION);
asset->compressedLen = static_cast<int>(compressed_size);
}
else
{
2022-12-01 09:04:30 -05:00
asset->buffer = builder->getAllocator()->allocateArray<char>(rawFile.getBuffer().size() + 1);
std::memcpy(const_cast<char*>(asset->buffer), rawFile.getBuffer().data(), rawFile.getBuffer().size());
asset->compressedLen = 0;
2017-01-19 16:23:59 -05:00
}
asset->len = static_cast<int>(rawFile.getBuffer().size());
header->rawfile = asset;
2017-01-19 16:23:59 -05:00
}
void IRawFile::save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder)
{
AssertSize(Game::RawFile, 16);
auto* buffer = builder->getBuffer();
auto* asset = header.rawfile;
auto* dest = buffer->dest<Game::RawFile>();
2017-01-19 16:23:59 -05:00
buffer->save(asset);
buffer->pushBlock(Game::XFILE_BLOCK_VIRTUAL);
if (asset->name)
{
buffer->saveString(builder->getAssetName(this->getType(), asset->name));
Utils::Stream::ClearPointer(&dest->name);
}
2018-05-09 08:33:52 -04:00
if (asset->buffer)
2017-01-19 16:23:59 -05:00
{
2018-05-09 08:33:52 -04:00
if (asset->compressedLen)
2017-01-19 16:23:59 -05:00
{
2018-05-09 08:33:52 -04:00
buffer->save(asset->buffer, asset->compressedLen);
2017-01-19 16:23:59 -05:00
}
else
{
2018-05-09 08:33:52 -04:00
buffer->save(asset->buffer, asset->len + 1);
2017-01-19 16:23:59 -05:00
}
2018-05-09 08:33:52 -04:00
Utils::Stream::ClearPointer(&dest->buffer);
2017-01-19 16:23:59 -05:00
}
buffer->popBlock();
}
}