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

75 lines
1.6 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
const auto data = Utils::Compression::ZLib::Compress(rawFile.getBuffer());
2017-01-19 16:23:59 -05:00
asset->name = builder->getAllocator()->duplicateString(name);
if (data.size() < rawFile.getBuffer().size())
{
asset->buffer = builder->getAllocator()->duplicateString(data);
asset->compressedLen = static_cast<int>(data.size());
}
else
{
asset->buffer = builder->getAllocator()->duplicateString(rawFile.getBuffer());
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();
}
}