[IGfxImage] Non-streamed image importer

This commit is contained in:
momo5502 2016-12-26 01:44:45 +01:00
parent b381fbd238
commit 4f608b3a19

View File

@ -1,5 +1,7 @@
#include <STDInclude.hpp>
#define IW4X_IMG_VERSION "0"
namespace Assets
{
void IGfxImage::load(Game::XAssetHeader* header, std::string name, Components::ZoneBuilder::Zone* builder)
@ -19,71 +21,90 @@ namespace Assets
image->category = 0;
image->cardMemory = 0;
image->loadDef = builder->getAllocator()->allocate<Game::GfxImageLoadDef>();
if (!image->loadDef)
const char* tempName = image->name;
if (tempName[0] == '*') tempName++;
Components::FileSystem::File imageFile(fmt::sprintf("images/%s.iw4xImage", tempName));
if (imageFile.exists())
{
Components::Logger::Error("Failed to allocate GfxImageLoadDef structure!");
return;
Utils::Stream::Reader reader(builder->getAllocator(), imageFile.getBuffer());
if (reader.read<__int64>() != *reinterpret_cast<__int64*>("IW4xImg" IW4X_IMG_VERSION))
{
Components::Logger::Error(0, "Reading image '%s' failed, header is invalid!", name.data());
}
AssertSize(Game::MapType, 1);
image->mapType = reader.read<Game::MapType>();
image->semantic = reader.read<char>();
image->category = reader.read<char>();
image->dataLen1 = reader.read<int>();
image->dataLen2 = image->dataLen1;
image->loadDef = reinterpret_cast<Game::GfxImageLoadDef*>(reader.readArray<char>(image->dataLen1 + 16));
}
char nameBuffer[MAX_PATH] = { 0 };
Components::Materials::FormatImagePath(nameBuffer, sizeof(nameBuffer), 0, 0, name.data());
Components::FileSystem::File iwi(nameBuffer);
if (!iwi.exists())
else
{
Components::Logger::Error("Loading image '%s' failed!", iwi.getName().data());
return;
}
char nameBuffer[MAX_PATH] = { 0 };
Components::Materials::FormatImagePath(nameBuffer, sizeof(nameBuffer), 0, 0, name.data());
Components::FileSystem::File iwi(nameBuffer);
auto iwiBuffer = iwi.getBuffer();
const Game::GfxImageFileHeader* iwiHeader = reinterpret_cast<const Game::GfxImageFileHeader*>(iwiBuffer.data());
image->mapType = Game::MAPTYPE_2D;
image->dataLen1 = iwiHeader->fileSizeForPicmip[0] - 32;
image->dataLen2 = iwiHeader->fileSizeForPicmip[0] - 32;
if (std::memcmp(iwiHeader->tag, "IWi", 3))
{
Components::Logger::Error("Image is not a valid IWi!");
return;
}
std::memcpy(image->loadDef->dimensions, iwiHeader->dimensions, 6);
image->loadDef->flags = 0;
image->loadDef->levelCount = 0;
switch (iwiHeader->format)
{
case Game::IWI_COMPRESSION::IWI_ARGB:
if (!iwi.exists())
{
image->loadDef->format = 21;
break;
Components::Logger::Error("Loading image '%s' failed!", iwi.getName().data());
return;
}
case Game::IWI_COMPRESSION::IWI_RGB8:
auto iwiBuffer = iwi.getBuffer();
const Game::GfxImageFileHeader* iwiHeader = reinterpret_cast<const Game::GfxImageFileHeader*>(iwiBuffer.data());
image->mapType = Game::MAPTYPE_2D;
image->dataLen1 = iwiHeader->fileSizeForPicmip[0] - 32;
image->dataLen2 = iwiHeader->fileSizeForPicmip[0] - 32;
if (std::memcmp(iwiHeader->tag, "IWi", 3) && iwiHeader->version == 8)
{
image->loadDef->format = 20;
break;
Components::Logger::Error("Image is not a valid IWi!");
return;
}
case Game::IWI_COMPRESSION::IWI_DXT1:
{
image->loadDef->format = 0x31545844;
break;
}
std::memcpy(image->loadDef->dimensions, iwiHeader->dimensions, 6);
image->loadDef->flags = 0;
image->loadDef->levelCount = 0;
case Game::IWI_COMPRESSION::IWI_DXT3:
switch (iwiHeader->format)
{
image->loadDef->format = 0x33545844;
break;
}
case Game::IWI_COMPRESSION::IWI_ARGB:
{
image->loadDef->format = 21;
break;
}
case Game::IWI_COMPRESSION::IWI_DXT5:
{
image->loadDef->format = 0x35545844;
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;
}
}
}