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

82 lines
2.8 KiB
C++
Raw Normal View History

2022-02-27 07:53:44 -05:00
#include <STDInclude.hpp>
2016-01-22 05:59:43 -05:00
#define IW4X_TECHSET_VERSION "0"
2016-01-22 05:59:43 -05:00
namespace Assets
{
void IMaterialPixelShader::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder)
{
if (!header->data) this->loadNative(header, name, builder); // Check if there is a native one
if (!header->data) this->loadBinary(header, name, builder); // Check if we need to import a new one into the game
}
void IMaterialPixelShader::loadNative(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* /*builder*/)
{
header->pixelShader = Components::AssetHandler::FindOriginalAsset(this->getType(), name.data()).pixelShader;
}
void IMaterialPixelShader::loadBinary(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder)
{
Components::FileSystem::File psFile(Utils::String::VA("ps/%s.iw4xPS", name.data()));
if (!psFile.exists()) return;
Utils::Stream::Reader reader(builder->getAllocator(), psFile.getBuffer());
char* magic = reader.readArray<char>(8);
if (std::memcmp(magic, "IW4xPIXL", 8))
{
2022-06-12 17:07:53 -04:00
Components::Logger::Error(Game::ERR_FATAL, "Reading pixel shader '{}' failed, header is invalid!", name);
}
std::string version;
version.push_back(reader.read<char>());
if (version != IW4X_TECHSET_VERSION)
{
2022-06-12 17:07:53 -04:00
Components::Logger::Error(Game::ERR_FATAL,
"Reading pixel shader '{}' failed, expected version is {}, but it was {}!", name, IW4X_TECHSET_VERSION, version);
}
Game::MaterialPixelShader* asset = reader.readObject<Game::MaterialPixelShader>();
if (asset->name)
{
asset->name = reader.readCString();
}
if (asset->prog.loadDef.program)
{
asset->prog.loadDef.program = reader.readArray<unsigned int>(asset->prog.loadDef.programSize);
}
header->pixelShader = asset;
}
void IMaterialPixelShader::save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder)
2016-01-22 05:59:43 -05:00
{
AssertSize(Game::MaterialPixelShader, 16);
2016-01-22 05:59:43 -05:00
Utils::Stream* buffer = builder->getBuffer();
2016-01-22 05:59:43 -05:00
Game::MaterialPixelShader* asset = header.pixelShader;
Game::MaterialPixelShader* dest = buffer->dest<Game::MaterialPixelShader>();
buffer->save(asset);
2016-01-22 05:59:43 -05:00
buffer->pushBlock(Game::XFILE_BLOCK_VIRTUAL);
2016-01-22 05:59:43 -05:00
if (asset->name)
{
buffer->saveString(builder->getAssetName(this->getType(), asset->name));
Utils::Stream::ClearPointer(&dest->name);
2016-01-22 05:59:43 -05:00
}
2017-04-22 15:47:04 -04:00
if (asset->prog.loadDef.program)
2016-01-22 05:59:43 -05:00
{
buffer->align(Utils::Stream::ALIGN_4);
2017-04-22 15:47:04 -04:00
buffer->saveArray(asset->prog.loadDef.program, asset->prog.loadDef.programSize);
Utils::Stream::ClearPointer(&dest->prog.loadDef.program);
2016-01-22 05:59:43 -05:00
}
buffer->popBlock();
2016-01-22 05:59:43 -05:00
}
}