2017-01-20 08:36:52 -05:00
|
|
|
#include "STDInclude.hpp"
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
#define IW4X_IMG_VERSION "0"
|
|
|
|
|
|
|
|
namespace Assets
|
|
|
|
{
|
2018-12-17 08:29:18 -05:00
|
|
|
void IGfxImage::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder)
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
|
|
|
Game::GfxImage* image = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_IMAGE, name.data()).image;
|
|
|
|
if (image && name[0] != '*') return;
|
|
|
|
|
|
|
|
image = builder->getAllocator()->allocate<Game::GfxImage>();
|
|
|
|
if (!image)
|
|
|
|
{
|
|
|
|
Components::Logger::Error("Failed to allocate GfxImage structure!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
image->name = builder->getAllocator()->duplicateString(name);
|
|
|
|
image->semantic = 2;
|
|
|
|
|
|
|
|
const char* tempName = image->name;
|
|
|
|
if (tempName[0] == '*') tempName++;
|
|
|
|
|
|
|
|
Components::FileSystem::File imageFile(Utils::String::VA("images/%s.iw4xImage", tempName));
|
|
|
|
if (imageFile.exists())
|
|
|
|
{
|
|
|
|
Utils::Stream::Reader reader(builder->getAllocator(), imageFile.getBuffer());
|
|
|
|
|
2017-01-20 16:41:03 -05:00
|
|
|
__int64 magic = reader.read<__int64>();
|
|
|
|
if (std::memcmp(&magic, "IW4xImg" IW4X_IMG_VERSION, 8))
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
|
|
|
Components::Logger::Error(0, "Reading image '%s' failed, header is invalid!", name.data());
|
|
|
|
}
|
|
|
|
|
2018-05-09 08:33:52 -04:00
|
|
|
image->mapType = reader.read<char>();
|
2017-01-19 16:23:59 -05:00
|
|
|
image->semantic = reader.read<char>();
|
|
|
|
image->category = reader.read<char>();
|
|
|
|
|
2018-05-10 07:01:11 -04:00
|
|
|
int dataLength = reader.read<int>();
|
|
|
|
image->cardMemory.platform[0] = dataLength;
|
|
|
|
image->cardMemory.platform[1] = dataLength;
|
2017-01-19 16:23:59 -05:00
|
|
|
|
2018-05-10 07:01:11 -04:00
|
|
|
Game::GfxImageLoadDefIW3 loadDef;
|
|
|
|
image->texture.loadDef = reinterpret_cast<Game::GfxImageLoadDef*>(reader.readArray<char>(dataLength + 16));
|
|
|
|
std::memcpy(&loadDef, image->texture.loadDef, sizeof(loadDef));
|
2017-01-19 16:23:59 -05:00
|
|
|
|
2018-05-10 07:01:11 -04:00
|
|
|
image->texture.loadDef->levelCount = loadDef.levelCount;
|
|
|
|
image->texture.loadDef->flags = loadDef.flags;
|
|
|
|
image->texture.loadDef->format = loadDef.format;
|
|
|
|
image->texture.loadDef->resourceSize = loadDef.resourceSize;
|
|
|
|
ZeroMemory(image->texture.loadDef->pad, 3);
|
2017-01-19 16:23:59 -05:00
|
|
|
|
2018-05-10 07:01:11 -04:00
|
|
|
if (image->texture.loadDef->resourceSize != dataLength)
|
2017-03-20 16:07:55 -04:00
|
|
|
{
|
|
|
|
Components::Logger::Error("Resource size doesn't match the data length (%s)!\n", name.data());
|
|
|
|
}
|
|
|
|
|
2018-05-10 07:01:11 -04:00
|
|
|
image->width = loadDef.dimensions[0];
|
|
|
|
image->height = loadDef.dimensions[1];
|
|
|
|
image->depth = loadDef.dimensions[2];
|
2018-05-09 09:14:25 -04:00
|
|
|
|
|
|
|
image->delayLoadPixels = true;
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
header->image = image;
|
|
|
|
}
|
2017-06-14 06:06:04 -04:00
|
|
|
else if (name[0] != '*')
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
|
|
|
char nameBuffer[MAX_PATH] = { 0 };
|
|
|
|
Components::Materials::FormatImagePath(nameBuffer, sizeof(nameBuffer), 0, 0, name.data());
|
|
|
|
Components::FileSystem::File iwi(nameBuffer);
|
|
|
|
|
|
|
|
if (!iwi.exists())
|
|
|
|
{
|
|
|
|
Components::Logger::Error("Loading image '%s' failed!", iwi.getName().data());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto iwiBuffer = iwi.getBuffer();
|
|
|
|
|
|
|
|
const Game::GfxImageFileHeader* iwiHeader = reinterpret_cast<const Game::GfxImageFileHeader*>(iwiBuffer.data());
|
|
|
|
|
|
|
|
if (std::memcmp(iwiHeader->tag, "IWi", 3) && iwiHeader->version == 8)
|
|
|
|
{
|
|
|
|
Components::Logger::Error("Image is not a valid IWi!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
image->mapType = Game::MAPTYPE_2D;
|
2018-05-10 07:01:11 -04:00
|
|
|
image->cardMemory.platform[0] = iwiHeader->fileSizeForPicmip[0] - 32;
|
|
|
|
image->cardMemory.platform[1] = iwiHeader->fileSizeForPicmip[0] - 32;
|
2017-01-19 16:23:59 -05:00
|
|
|
|
2018-05-09 08:33:52 -04:00
|
|
|
image->texture.loadDef = builder->getAllocator()->allocate<Game::GfxImageLoadDef>();
|
|
|
|
if (!image->texture.loadDef)
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
|
|
|
Components::Logger::Error("Failed to allocate GfxImageLoadDef structure!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-10 07:01:11 -04:00
|
|
|
image->texture.loadDef->flags = iwiHeader->flags;
|
2018-05-09 08:33:52 -04:00
|
|
|
image->texture.loadDef->levelCount = 0;
|
2017-01-19 16:23:59 -05:00
|
|
|
|
2018-05-10 07:01:11 -04:00
|
|
|
image->width = iwiHeader->dimensions[0];
|
|
|
|
image->height = iwiHeader->dimensions[1];
|
2018-05-09 08:33:52 -04:00
|
|
|
image->depth = iwiHeader->dimensions[2];
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
switch (iwiHeader->format)
|
|
|
|
{
|
2018-05-09 08:33:52 -04:00
|
|
|
case Game::IMG_FORMAT_BITMAP_RGBA:
|
2017-06-14 06:06:04 -04:00
|
|
|
{
|
2018-05-09 08:33:52 -04:00
|
|
|
image->texture.loadDef->format = 21;
|
2017-06-14 06:06:04 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-09 08:33:52 -04:00
|
|
|
case Game::IMG_FORMAT_BITMAP_RGB:
|
2017-06-14 06:06:04 -04:00
|
|
|
{
|
2018-05-09 08:33:52 -04:00
|
|
|
image->texture.loadDef->format = 20;
|
2017-06-14 06:06:04 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-09 08:33:52 -04:00
|
|
|
case Game::IMG_FORMAT_DXT1:
|
2017-06-14 06:06:04 -04:00
|
|
|
{
|
2018-05-09 08:33:52 -04:00
|
|
|
image->texture.loadDef->format = 0x31545844;
|
2017-06-14 06:06:04 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-09 08:33:52 -04:00
|
|
|
case Game::IMG_FORMAT_DXT3:
|
2017-06-14 06:06:04 -04:00
|
|
|
{
|
2018-05-09 08:33:52 -04:00
|
|
|
image->texture.loadDef->format = 0x33545844;
|
2017-06-14 06:06:04 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-09 08:33:52 -04:00
|
|
|
case Game::IMG_FORMAT_DXT5:
|
2017-06-14 06:06:04 -04:00
|
|
|
{
|
2018-05-09 08:33:52 -04:00
|
|
|
image->texture.loadDef->format = 0x35545844;
|
2017-06-14 06:06:04 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
header->image = image;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IGfxImage::save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder)
|
|
|
|
{
|
|
|
|
AssertSize(Game::GfxImage, 32);
|
|
|
|
|
|
|
|
Utils::Stream* buffer = builder->getBuffer();
|
|
|
|
Game::GfxImage* asset = header.image;
|
|
|
|
Game::GfxImage* dest = buffer->dest<Game::GfxImage>();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer->pushBlock(Game::XFILE_BLOCK_TEMP);
|
|
|
|
|
2018-05-09 08:33:52 -04:00
|
|
|
if (asset->texture.loadDef)
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
|
|
|
buffer->align(Utils::Stream::ALIGN_4);
|
|
|
|
|
|
|
|
Game::GfxImageLoadDef* destTexture = buffer->dest<Game::GfxImageLoadDef>();
|
2018-05-09 08:33:52 -04:00
|
|
|
buffer->save(asset->texture.loadDef, 16, 1);
|
2017-01-19 16:23:59 -05:00
|
|
|
|
2018-05-09 08:33:52 -04:00
|
|
|
builder->incrementExternalSize(asset->texture.loadDef->resourceSize);
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
if (destTexture->resourceSize > 0)
|
|
|
|
{
|
2018-05-09 08:33:52 -04:00
|
|
|
buffer->save(asset->texture.loadDef->data, asset->texture.loadDef->resourceSize);
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
|
|
|
|
2018-05-09 08:33:52 -04:00
|
|
|
Utils::Stream::ClearPointer(&dest->texture.loadDef);
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer->popBlock();
|
|
|
|
buffer->popBlock();
|
|
|
|
}
|
|
|
|
}
|