2022-02-27 07:53:44 -05:00
|
|
|
#include <STDInclude.hpp>
|
2022-06-22 11:36:06 -04:00
|
|
|
#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);
|
|
|
|
|
2022-11-08 20:31:46 -05:00
|
|
|
if (!rawFile.exists())
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
2022-11-08 20:31:46 -05:00
|
|
|
return;
|
|
|
|
}
|
2017-01-19 16:23:59 -05:00
|
|
|
|
2022-11-08 20:31:46 -05:00
|
|
|
auto* asset = builder->getAllocator()->allocate<Game::RawFile>();
|
|
|
|
if (!asset)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2017-01-19 16:23:59 -05:00
|
|
|
|
2022-11-08 20:31:46 -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-11-08 20:31:46 -05:00
|
|
|
|
2022-12-01 09:46:18 -05:00
|
|
|
const auto compressedData = Utils::Compression::ZLib::Compress(rawFile.getBuffer());
|
|
|
|
// Only save the compressed buffer if we gained space
|
|
|
|
if (compressedData.size() < rawFile.getBuffer().size())
|
2022-11-08 20:31:46 -05:00
|
|
|
{
|
2022-12-01 09:46:18 -05:00
|
|
|
asset->buffer = builder->getAllocator()->duplicateString(compressedData);
|
|
|
|
std::memcpy(const_cast<char*>(asset->buffer), compressedData.data(), compressedData.size());
|
|
|
|
asset->compressedLen = static_cast<int>(compressedData.size());
|
2022-11-08 20:31:46 -05:00
|
|
|
}
|
|
|
|
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());
|
2022-11-08 20:31:46 -05:00
|
|
|
asset->compressedLen = 0;
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
2022-11-08 20:31:46 -05:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2022-11-08 20:31:46 -05:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|