2016-07-11 11:14:58 -04:00
|
|
|
#include <STDInclude.hpp>
|
|
|
|
|
2016-12-25 19:44:45 -05:00
|
|
|
#define IW4X_IMG_VERSION "0"
|
|
|
|
|
2016-07-11 11:14:58 -04:00
|
|
|
namespace Assets
|
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
void IGfxImage::load(Game::XAssetHeader* header, std::string name, Components::ZoneBuilder::Zone* builder)
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
|
|
|
Game::GfxImage* image = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_IMAGE, name.data()).image;
|
2016-12-28 20:21:45 -05:00
|
|
|
if (image && name[0] != '*') return;
|
2016-07-11 11:14:58 -04:00
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
image = builder->getAllocator()->allocate<Game::GfxImage>();
|
2016-07-11 11:14:58 -04:00
|
|
|
if (!image)
|
|
|
|
{
|
|
|
|
Components::Logger::Error("Failed to allocate GfxImage structure!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
image->name = builder->getAllocator()->duplicateString(name);
|
2016-07-11 11:14:58 -04:00
|
|
|
image->semantic = 2;
|
|
|
|
image->category = 0;
|
|
|
|
image->cardMemory = 0;
|
|
|
|
|
2016-12-25 19:44:45 -05:00
|
|
|
const char* tempName = image->name;
|
|
|
|
if (tempName[0] == '*') tempName++;
|
2016-07-11 11:14:58 -04:00
|
|
|
|
2016-12-25 19:44:45 -05:00
|
|
|
Components::FileSystem::File imageFile(fmt::sprintf("images/%s.iw4xImage", tempName));
|
|
|
|
if (imageFile.exists())
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
2016-12-25 19:44:45 -05:00
|
|
|
Utils::Stream::Reader reader(builder->getAllocator(), imageFile.getBuffer());
|
2016-07-11 11:14:58 -04:00
|
|
|
|
2016-12-25 19:44:45 -05:00
|
|
|
if (reader.read<__int64>() != *reinterpret_cast<__int64*>("IW4xImg" IW4X_IMG_VERSION))
|
|
|
|
{
|
|
|
|
Components::Logger::Error(0, "Reading image '%s' failed, header is invalid!", name.data());
|
|
|
|
}
|
2016-07-11 11:14:58 -04:00
|
|
|
|
2016-12-25 19:44:45 -05:00
|
|
|
AssertSize(Game::MapType, 1);
|
|
|
|
image->mapType = reader.read<Game::MapType>();
|
|
|
|
image->semantic = reader.read<char>();
|
|
|
|
image->category = reader.read<char>();
|
2016-07-11 11:14:58 -04:00
|
|
|
|
2016-12-25 19:44:45 -05:00
|
|
|
image->dataLen1 = reader.read<int>();
|
|
|
|
image->dataLen2 = image->dataLen1;
|
2016-07-11 11:14:58 -04:00
|
|
|
|
2016-12-25 19:44:45 -05:00
|
|
|
image->loadDef = reinterpret_cast<Game::GfxImageLoadDef*>(reader.readArray<char>(image->dataLen1 + 16));
|
2016-12-28 20:21:45 -05:00
|
|
|
|
|
|
|
image->height = image->loadDef->dimensions[0];
|
|
|
|
image->width = image->loadDef->dimensions[1];
|
|
|
|
image->depth = image->loadDef->dimensions[2];
|
|
|
|
|
|
|
|
image->loaded = true;
|
2016-12-30 09:54:25 -05:00
|
|
|
image->loadDef->flags = 0;
|
|
|
|
|
|
|
|
if (Utils::String::StartsWith(name, "*lightmap"))
|
|
|
|
{
|
|
|
|
image->loadDef->dimensions[0] = 0;
|
|
|
|
image->loadDef->dimensions[1] = 2;
|
|
|
|
image->loadDef->dimensions[2] = 0;
|
|
|
|
}
|
2016-12-29 09:59:11 -05:00
|
|
|
|
|
|
|
header->image = image;
|
2016-12-25 19:44:45 -05:00
|
|
|
}
|
2016-12-29 09:59:11 -05:00
|
|
|
else if(name[0] != '*')
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
2016-12-25 19:44:45 -05:00
|
|
|
char nameBuffer[MAX_PATH] = { 0 };
|
|
|
|
Components::Materials::FormatImagePath(nameBuffer, sizeof(nameBuffer), 0, 0, name.data());
|
|
|
|
Components::FileSystem::File iwi(nameBuffer);
|
2016-07-11 11:14:58 -04:00
|
|
|
|
2016-12-25 19:44:45 -05:00
|
|
|
if (!iwi.exists())
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
2016-12-25 19:44:45 -05:00
|
|
|
Components::Logger::Error("Loading image '%s' failed!", iwi.getName().data());
|
|
|
|
return;
|
2016-07-11 11:14:58 -04:00
|
|
|
}
|
|
|
|
|
2016-12-25 19:44:45 -05:00
|
|
|
auto iwiBuffer = iwi.getBuffer();
|
|
|
|
|
|
|
|
const Game::GfxImageFileHeader* iwiHeader = reinterpret_cast<const Game::GfxImageFileHeader*>(iwiBuffer.data());
|
2016-07-11 11:14:58 -04:00
|
|
|
|
2016-12-26 16:28:46 -05:00
|
|
|
if (std::memcmp(iwiHeader->tag, "IWi", 3) && iwiHeader->version == 8)
|
|
|
|
{
|
|
|
|
Components::Logger::Error("Image is not a valid IWi!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-25 19:44:45 -05:00
|
|
|
image->mapType = Game::MAPTYPE_2D;
|
|
|
|
image->dataLen1 = iwiHeader->fileSizeForPicmip[0] - 32;
|
|
|
|
image->dataLen2 = iwiHeader->fileSizeForPicmip[0] - 32;
|
|
|
|
|
2016-12-26 16:28:46 -05:00
|
|
|
image->loadDef = builder->getAllocator()->allocate<Game::GfxImageLoadDef>();
|
|
|
|
if (!image->loadDef)
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
2016-12-26 16:28:46 -05:00
|
|
|
Components::Logger::Error("Failed to allocate GfxImageLoadDef structure!");
|
2016-12-25 19:44:45 -05:00
|
|
|
return;
|
2016-07-11 11:14:58 -04:00
|
|
|
}
|
|
|
|
|
2016-12-25 19:44:45 -05:00
|
|
|
std::memcpy(image->loadDef->dimensions, iwiHeader->dimensions, 6);
|
|
|
|
image->loadDef->flags = 0;
|
|
|
|
image->loadDef->levelCount = 0;
|
|
|
|
|
2016-12-28 20:21:45 -05:00
|
|
|
image->height = image->loadDef->dimensions[0];
|
|
|
|
image->width = image->loadDef->dimensions[1];
|
|
|
|
image->depth = image->loadDef->dimensions[2];
|
|
|
|
|
2016-12-25 19:44:45 -05:00
|
|
|
switch (iwiHeader->format)
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
2016-12-25 19:44:45 -05:00
|
|
|
case Game::IWI_COMPRESSION::IWI_ARGB:
|
|
|
|
{
|
|
|
|
image->loadDef->format = 21;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Game::IWI_COMPRESSION::IWI_RGB8:
|
|
|
|
{
|
|
|
|
image->loadDef->format = 20;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Game::IWI_COMPRESSION::IWI_DXT1:
|
|
|
|
{
|
|
|
|
image->loadDef->format = 0x31545844;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Game::IWI_COMPRESSION::IWI_DXT3:
|
|
|
|
{
|
|
|
|
image->loadDef->format = 0x33545844;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Game::IWI_COMPRESSION::IWI_DXT5:
|
|
|
|
{
|
|
|
|
image->loadDef->format = 0x35545844;
|
|
|
|
break;
|
|
|
|
}
|
2016-07-11 11:14:58 -04:00
|
|
|
}
|
|
|
|
|
2016-12-29 09:59:11 -05:00
|
|
|
header->image = image;
|
|
|
|
}
|
2016-07-11 11:14:58 -04:00
|
|
|
}
|
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
void IGfxImage::save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder)
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
AssertSize(Game::GfxImage, 32);
|
2016-12-21 20:48:00 -05:00
|
|
|
AssertSize(Game::MapType, 1);
|
2016-07-11 11:14:58 -04:00
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
Utils::Stream* buffer = builder->getBuffer();
|
2016-07-11 11:14:58 -04:00
|
|
|
Game::GfxImage* asset = header.image;
|
2016-11-20 08:09:07 -05:00
|
|
|
Game::GfxImage* dest = buffer->dest<Game::GfxImage>();
|
|
|
|
buffer->save(asset);
|
2016-07-11 11:14:58 -04:00
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
buffer->pushBlock(Game::XFILE_BLOCK_VIRTUAL);
|
2016-07-11 11:14:58 -04:00
|
|
|
|
|
|
|
if (asset->name)
|
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
buffer->saveString(builder->getAssetName(this->getType(), asset->name));
|
2016-07-11 11:14:58 -04:00
|
|
|
Utils::Stream::ClearPointer(&dest->name);
|
|
|
|
}
|
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
buffer->pushBlock(Game::XFILE_BLOCK_TEMP);
|
2016-07-11 11:14:58 -04:00
|
|
|
|
2016-12-21 20:48:00 -05:00
|
|
|
if (asset->loadDef)
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
buffer->align(Utils::Stream::ALIGN_4);
|
2016-07-11 11:14:58 -04:00
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
Game::GfxImageLoadDef* destTexture = buffer->dest<Game::GfxImageLoadDef>();
|
2016-12-21 20:48:00 -05:00
|
|
|
buffer->save(asset->loadDef, 16, 1);
|
2016-11-20 14:41:38 -05:00
|
|
|
|
2016-12-21 20:48:00 -05:00
|
|
|
builder->incrementExternalSize(asset->loadDef->resourceSize);
|
2016-07-11 11:14:58 -04:00
|
|
|
|
2016-12-21 20:48:00 -05:00
|
|
|
if (destTexture->resourceSize > 0)
|
2016-11-20 14:41:38 -05:00
|
|
|
{
|
2016-12-21 20:48:00 -05:00
|
|
|
buffer->save(asset->loadDef->data, asset->loadDef->resourceSize);
|
2016-11-20 14:41:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Utils::Stream::ClearPointer(&dest->loadDef);
|
2016-07-11 11:14:58 -04:00
|
|
|
}
|
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
buffer->popBlock();
|
|
|
|
buffer->popBlock();
|
2016-07-11 11:14:58 -04:00
|
|
|
}
|
|
|
|
}
|