2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-06-27 23:01:55 +00:00

Dump iw4 images

This commit is contained in:
Jan
2020-10-16 15:57:39 +02:00
parent b8b8e4c267
commit a1c965a4c0
13 changed files with 689 additions and 230 deletions

View File

@ -0,0 +1,109 @@
#include "ObjLoaderIW4.h"
#include "Game/IW4/GameIW4.h"
#include "Game/IW4/GameAssetPoolIW4.h"
#include "ObjContainer/IPak/IPak.h"
#include "ObjLoading.h"
#include "Image/Texture.h"
#include "Image/IwiLoader.h"
namespace IW4
{
bool ObjLoader::SupportsZone(Zone* zone) const
{
return zone->m_game == &g_GameIW4;
}
bool ObjLoader::IsMpZone(Zone* zone)
{
return zone->m_name.compare(0, 3, "mp_") == 0
|| zone->m_name.compare(zone->m_name.length() - 3, 3, "_mp") == 0;
}
bool ObjLoader::IsZmZone(Zone* zone)
{
return zone->m_name.compare(0, 3, "zm_") == 0
|| zone->m_name.compare(zone->m_name.length() - 3, 3, "_zm") == 0;
}
void ObjLoader::LoadReferencedContainersForZone(ISearchPath* searchPath, Zone* zone) const
{
}
void ObjLoader::UnloadContainersOfZone(Zone* zone) const
{
}
void ObjLoader::LoadImageFromLoadDef(GfxImage* image, Zone* zone)
{
// TODO: Load Texture from LoadDef here
}
void ObjLoader::LoadImageFromIwi(GfxImage* image, ISearchPath* searchPath, Zone* zone)
{
Texture* loadedTexture = nullptr;
IwiLoader loader(zone->GetMemory());
const std::string imageFileName = "images/" + std::string(image->name) + ".iwi";
auto* filePathImage = searchPath->Open(imageFileName);
if (filePathImage != nullptr)
{
loadedTexture = loader.LoadIwi(filePathImage);
filePathImage->Close();
delete filePathImage;
}
if (loadedTexture != nullptr)
{
image->texture.texture = loadedTexture;
image->cardMemory.platform[0] = 0;
const int textureMipCount = loadedTexture->GetMipMapCount();
for (int mipLevel = 0; mipLevel < textureMipCount; mipLevel++)
image->cardMemory.platform[0] += static_cast<int>(loadedTexture->GetSizeOfMipLevel(mipLevel) * loadedTexture->GetFaceCount());
}
else
{
printf("Could not find data for image \"%s\"\n", image->name);
}
}
void ObjLoader::LoadImageData(ISearchPath* searchPath, Zone* zone)
{
auto* assetPool = dynamic_cast<GameAssetPoolIW4*>(zone->GetPools());
if (assetPool && assetPool->m_image != nullptr)
{
for (auto* imageEntry : *assetPool->m_image)
{
auto* image = imageEntry->Asset();
if (image->cardMemory.platform[0] > 0)
{
continue;
}
// Do not load linked assets
if (image->name && image->name[0] == ',')
{
continue;
}
if (image->texture.loadDef && image->texture.loadDef->resourceSize > 0)
{
LoadImageFromLoadDef(image, zone);
}
else
{
LoadImageFromIwi(image, searchPath, zone);
}
}
}
}
void ObjLoader::LoadObjDataForZone(ISearchPath* searchPath, Zone* zone) const
{
LoadImageData(searchPath, zone);
}
}

View File

@ -0,0 +1,26 @@
#pragma once
#include "IObjLoader.h"
#include "SearchPath/ISearchPath.h"
#include "Game/IW4/IW4.h"
namespace IW4
{
class ObjLoader final : public IObjLoader
{
static void LoadImageFromIwi(GfxImage* image, ISearchPath* searchPath, Zone* zone);
static void LoadImageFromLoadDef(GfxImage* image, Zone* zone);
static void LoadImageData(ISearchPath* searchPath, Zone* zone);
static bool IsMpZone(Zone* zone);
static bool IsZmZone(Zone* zone);
public:
bool SupportsZone(Zone* zone) const override;
void LoadReferencedContainersForZone(ISearchPath* searchPath, Zone* zone) const override;
void UnloadContainersOfZone(Zone* zone) const override;
void LoadObjDataForZone(ISearchPath* searchPath, Zone* zone) const override;
};
}

View File

@ -7,226 +7,229 @@
#include "Image/IwiLoader.h"
#include "Game/T6/CommonT6.h"
const int ObjLoaderT6::IPAK_READ_HASH = CommonT6::Com_HashKey("ipak_read", 64);
const int ObjLoaderT6::GLOBAL_HASH = CommonT6::Com_HashKey("GLOBAL", 64);
bool ObjLoaderT6::SupportsZone(Zone* zone) const
namespace T6
{
return zone->m_game == &g_GameT6;
}
const int ObjLoader::IPAK_READ_HASH = CommonT6::Com_HashKey("ipak_read", 64);
const int ObjLoader::GLOBAL_HASH = CommonT6::Com_HashKey("GLOBAL", 64);
void ObjLoaderT6::LoadIPakForZone(ISearchPath* searchPath, const std::string& ipakName, Zone* zone)
{
if(ObjLoading::Configuration.Verbose)
printf("Trying to load ipak '%s' for zone '%s'\n", ipakName.c_str(), zone->m_name.c_str());
IPak* existingIPak = IPak::Repository.GetContainerByName(ipakName);
if(existingIPak != nullptr)
bool ObjLoader::SupportsZone(Zone* zone) const
{
if (ObjLoading::Configuration.Verbose)
printf("Referencing loaded ipak '%s'.\n", ipakName.c_str());
IPak::Repository.AddContainer(existingIPak, zone);
return;
return zone->m_game == &g_GameT6;
}
const std::string ipakFilename = ipakName + ".ipak";
auto* file = searchPath->Open(ipakFilename);
if(file && file->IsOpen())
void ObjLoader::LoadIPakForZone(ISearchPath* searchPath, const std::string& ipakName, Zone* zone)
{
IPak* ipak = new IPak(ipakFilename, file);
if (ObjLoading::Configuration.Verbose)
printf("Trying to load ipak '%s' for zone '%s'\n", ipakName.c_str(), zone->m_name.c_str());
if(ipak->Initialize())
IPak* existingIPak = IPak::Repository.GetContainerByName(ipakName);
if (existingIPak != nullptr)
{
IPak::Repository.AddContainer(ipak, zone);
if (ObjLoading::Configuration.Verbose)
printf("Found and loaded ipak '%s'.\n", ipakFilename.c_str());
printf("Referencing loaded ipak '%s'.\n", ipakName.c_str());
IPak::Repository.AddContainer(existingIPak, zone);
return;
}
else
const std::string ipakFilename = ipakName + ".ipak";
auto* file = searchPath->Open(ipakFilename);
if (file && file->IsOpen())
{
delete ipak;
file->Close();
delete file;
IPak* ipak = new IPak(ipakFilename, file);
printf("Failed to load ipak '%s'!\n", ipakFilename.c_str());
}
}
}
bool ObjLoaderT6::IsMpZone(Zone* zone)
{
return zone->m_name.compare(0, 3, "mp_") == 0
|| zone->m_name.compare(zone->m_name.length() - 3, 3, "_mp") == 0;
}
bool ObjLoaderT6::IsZmZone(Zone* zone)
{
return zone->m_name.compare(0, 3, "zm_") == 0
|| zone->m_name.compare(zone->m_name.length() - 3, 3, "_zm") == 0;
}
void ObjLoaderT6::LoadCommonIPaks(ISearchPath* searchPath, Zone* zone)
{
if(ObjLoading::Configuration.Verbose)
printf("Loading common ipaks for zone \"%s\"\n", zone->m_name.c_str());
LoadIPakForZone(searchPath, "base", zone);
auto languagePrefixes = g_GameT6.GetLanguagePrefixes();
for (const auto& languagePrefix : languagePrefixes)
{
LoadIPakForZone(searchPath, languagePrefix.m_prefix + "base", zone);
}
if (IsMpZone(zone))
{
if (ObjLoading::Configuration.Verbose)
printf("Loading multiplayer ipaks for zone \"%s\"\n", zone->m_name.c_str());
LoadIPakForZone(searchPath, "mp", zone);
LoadIPakForZone(searchPath, "so", zone);
}
else if (IsZmZone(zone))
{
if (ObjLoading::Configuration.Verbose)
printf("Loading zombie ipak for zone \"%s\"\n", zone->m_name.c_str());
LoadIPakForZone(searchPath, "zm", zone);
}
else
{
if (ObjLoading::Configuration.Verbose)
printf("Loading singleplayer ipak for zone \"%s\"\n", zone->m_name.c_str());
LoadIPakForZone(searchPath, "sp", zone);
}
}
void ObjLoaderT6::LoadReferencedContainersForZone(ISearchPath* searchPath, Zone* zone) const
{
auto* assetPoolT6 = dynamic_cast<GameAssetPoolT6*>(zone->GetPools());
const int zoneNameHash = CommonT6::Com_HashKey(zone->m_name.c_str(), 64);
LoadCommonIPaks(searchPath, zone);
if(assetPoolT6->m_key_value_pairs != nullptr)
{
for(auto* keyValuePairsEntry : *assetPoolT6->m_key_value_pairs)
{
auto* keyValuePairs = keyValuePairsEntry->Asset();
for(int variableIndex = 0; variableIndex < keyValuePairs->numVariables; variableIndex++)
if (ipak->Initialize())
{
T6::KeyValuePair* variable = &keyValuePairs->keyValuePairs[variableIndex];
if(variable->namespaceHash == zoneNameHash && variable->keyHash == IPAK_READ_HASH)
{
LoadIPakForZone(searchPath, variable->value, zone);
}
}
}
}
}
IPak::Repository.AddContainer(ipak, zone);
void ObjLoaderT6::UnloadContainersOfZone(Zone* zone) const
{
IPak::Repository.RemoveContainerReferences(zone);
}
void ObjLoaderT6::LoadImageFromLoadDef(T6::GfxImage* image, Zone* zone)
{
// TODO: Load Texture from LoadDef here
}
void ObjLoaderT6::LoadImageFromIwi(T6::GfxImage* image, ISearchPath* searchPath, Zone* zone)
{
Texture* loadedTexture = nullptr;
IwiLoader loader(zone->GetMemory());
if (image->streamedPartCount > 0)
{
for (auto* ipak : IPak::Repository)
{
auto* ipakStream = ipak->GetEntryStream(image->hash, image->streamedParts[0].hash);
if (ipakStream != nullptr)
{
loadedTexture = loader.LoadIwi(ipakStream);
ipakStream->Close();
delete ipakStream;
if (loadedTexture != nullptr)
{
break;
}
}
}
}
if(loadedTexture == nullptr)
{
const std::string imageFileName = "images/" + std::string(image->name) + ".iwi";
auto* filePathImage = searchPath->Open(imageFileName);
if (filePathImage != nullptr)
{
loadedTexture = loader.LoadIwi(filePathImage);
filePathImage->Close();
delete filePathImage;
}
}
if(loadedTexture != nullptr)
{
image->texture.texture = loadedTexture;
image->loadedSize = 0;
const int textureMipCount = loadedTexture->GetMipMapCount();
for(int mipLevel = 0; mipLevel < textureMipCount; mipLevel++)
image->loadedSize += loadedTexture->GetSizeOfMipLevel(mipLevel) * loadedTexture->GetFaceCount();
}
else
{
printf("Could not find data for image \"%s\"\n", image->name);
}
}
void ObjLoaderT6::LoadImageData(ISearchPath* searchPath, Zone* zone)
{
auto* assetPoolT6 = dynamic_cast<GameAssetPoolT6*>(zone->GetPools());
if (assetPoolT6 && assetPoolT6->m_image != nullptr)
{
for (auto* imageEntry : *assetPoolT6->m_image)
{
auto* image = imageEntry->Asset();
if(image->loadedSize > 0)
{
continue;
}
// Do not load linked assets
if(image->name && image->name[0] == ',')
{
continue;
}
if(image->texture.loadDef && image->texture.loadDef->resourceSize > 0)
{
LoadImageFromLoadDef(image, zone);
if (ObjLoading::Configuration.Verbose)
printf("Found and loaded ipak '%s'.\n", ipakFilename.c_str());
}
else
{
LoadImageFromIwi(image, searchPath, zone);
delete ipak;
file->Close();
delete file;
printf("Failed to load ipak '%s'!\n", ipakFilename.c_str());
}
}
}
}
void ObjLoaderT6::LoadObjDataForZone(ISearchPath* searchPath, Zone* zone) const
{
LoadImageData(searchPath, zone);
bool ObjLoader::IsMpZone(Zone* zone)
{
return zone->m_name.compare(0, 3, "mp_") == 0
|| zone->m_name.compare(zone->m_name.length() - 3, 3, "_mp") == 0;
}
bool ObjLoader::IsZmZone(Zone* zone)
{
return zone->m_name.compare(0, 3, "zm_") == 0
|| zone->m_name.compare(zone->m_name.length() - 3, 3, "_zm") == 0;
}
void ObjLoader::LoadCommonIPaks(ISearchPath* searchPath, Zone* zone)
{
if (ObjLoading::Configuration.Verbose)
printf("Loading common ipaks for zone \"%s\"\n", zone->m_name.c_str());
LoadIPakForZone(searchPath, "base", zone);
auto languagePrefixes = g_GameT6.GetLanguagePrefixes();
for (const auto& languagePrefix : languagePrefixes)
{
LoadIPakForZone(searchPath, languagePrefix.m_prefix + "base", zone);
}
if (IsMpZone(zone))
{
if (ObjLoading::Configuration.Verbose)
printf("Loading multiplayer ipaks for zone \"%s\"\n", zone->m_name.c_str());
LoadIPakForZone(searchPath, "mp", zone);
LoadIPakForZone(searchPath, "so", zone);
}
else if (IsZmZone(zone))
{
if (ObjLoading::Configuration.Verbose)
printf("Loading zombie ipak for zone \"%s\"\n", zone->m_name.c_str());
LoadIPakForZone(searchPath, "zm", zone);
}
else
{
if (ObjLoading::Configuration.Verbose)
printf("Loading singleplayer ipak for zone \"%s\"\n", zone->m_name.c_str());
LoadIPakForZone(searchPath, "sp", zone);
}
}
void ObjLoader::LoadReferencedContainersForZone(ISearchPath* searchPath, Zone* zone) const
{
auto* assetPoolT6 = dynamic_cast<GameAssetPoolT6*>(zone->GetPools());
const int zoneNameHash = CommonT6::Com_HashKey(zone->m_name.c_str(), 64);
LoadCommonIPaks(searchPath, zone);
if (assetPoolT6->m_key_value_pairs != nullptr)
{
for (auto* keyValuePairsEntry : *assetPoolT6->m_key_value_pairs)
{
auto* keyValuePairs = keyValuePairsEntry->Asset();
for (int variableIndex = 0; variableIndex < keyValuePairs->numVariables; variableIndex++)
{
KeyValuePair* variable = &keyValuePairs->keyValuePairs[variableIndex];
if (variable->namespaceHash == zoneNameHash && variable->keyHash == IPAK_READ_HASH)
{
LoadIPakForZone(searchPath, variable->value, zone);
}
}
}
}
}
void ObjLoader::UnloadContainersOfZone(Zone* zone) const
{
IPak::Repository.RemoveContainerReferences(zone);
}
void ObjLoader::LoadImageFromLoadDef(GfxImage* image, Zone* zone)
{
// TODO: Load Texture from LoadDef here
}
void ObjLoader::LoadImageFromIwi(GfxImage* image, ISearchPath* searchPath, Zone* zone)
{
Texture* loadedTexture = nullptr;
IwiLoader loader(zone->GetMemory());
if (image->streamedPartCount > 0)
{
for (auto* ipak : IPak::Repository)
{
auto* ipakStream = ipak->GetEntryStream(image->hash, image->streamedParts[0].hash);
if (ipakStream != nullptr)
{
loadedTexture = loader.LoadIwi(ipakStream);
ipakStream->Close();
delete ipakStream;
if (loadedTexture != nullptr)
{
break;
}
}
}
}
if (loadedTexture == nullptr)
{
const std::string imageFileName = "images/" + std::string(image->name) + ".iwi";
auto* filePathImage = searchPath->Open(imageFileName);
if (filePathImage != nullptr)
{
loadedTexture = loader.LoadIwi(filePathImage);
filePathImage->Close();
delete filePathImage;
}
}
if (loadedTexture != nullptr)
{
image->texture.texture = loadedTexture;
image->loadedSize = 0;
const int textureMipCount = loadedTexture->GetMipMapCount();
for (int mipLevel = 0; mipLevel < textureMipCount; mipLevel++)
image->loadedSize += loadedTexture->GetSizeOfMipLevel(mipLevel) * loadedTexture->GetFaceCount();
}
else
{
printf("Could not find data for image \"%s\"\n", image->name);
}
}
void ObjLoader::LoadImageData(ISearchPath* searchPath, Zone* zone)
{
auto* assetPoolT6 = dynamic_cast<GameAssetPoolT6*>(zone->GetPools());
if (assetPoolT6 && assetPoolT6->m_image != nullptr)
{
for (auto* imageEntry : *assetPoolT6->m_image)
{
auto* image = imageEntry->Asset();
if (image->loadedSize > 0)
{
continue;
}
// Do not load linked assets
if (image->name && image->name[0] == ',')
{
continue;
}
if (image->texture.loadDef && image->texture.loadDef->resourceSize > 0)
{
LoadImageFromLoadDef(image, zone);
}
else
{
LoadImageFromIwi(image, searchPath, zone);
}
}
}
}
void ObjLoader::LoadObjDataForZone(ISearchPath* searchPath, Zone* zone) const
{
LoadImageData(searchPath, zone);
}
}

View File

@ -4,26 +4,29 @@
#include "SearchPath/ISearchPath.h"
#include "Game/T6/T6.h"
class ObjLoaderT6 final : public IObjLoader
namespace T6
{
static const int IPAK_READ_HASH;
static const int GLOBAL_HASH;
class ObjLoader final : public IObjLoader
{
static const int IPAK_READ_HASH;
static const int GLOBAL_HASH;
static void LoadIPakForZone(ISearchPath* searchPath, const std::string& ipakName, Zone* zone);
static void LoadIPakForZone(ISearchPath* searchPath, const std::string& ipakName, Zone* zone);
static void LoadImageFromIwi(T6::GfxImage* image, ISearchPath* searchPath, Zone* zone);
static void LoadImageFromLoadDef(T6::GfxImage* image, Zone* zone);
static void LoadImageData(ISearchPath* searchPath, Zone* zone);
static void LoadImageFromIwi(GfxImage* image, ISearchPath* searchPath, Zone* zone);
static void LoadImageFromLoadDef(GfxImage* image, Zone* zone);
static void LoadImageData(ISearchPath* searchPath, Zone* zone);
static bool IsMpZone(Zone* zone);
static bool IsZmZone(Zone* zone);
static void LoadCommonIPaks(ISearchPath* searchPath, Zone* zone);
static bool IsMpZone(Zone* zone);
static bool IsZmZone(Zone* zone);
static void LoadCommonIPaks(ISearchPath* searchPath, Zone* zone);
public:
bool SupportsZone(Zone* zone) const override;
public:
bool SupportsZone(Zone* zone) const override;
void LoadReferencedContainersForZone(ISearchPath* searchPath, Zone* zone) const override;
void UnloadContainersOfZone(Zone* zone) const override;
void LoadReferencedContainersForZone(ISearchPath* searchPath, Zone* zone) const override;
void UnloadContainersOfZone(Zone* zone) const override;
void LoadObjDataForZone(ISearchPath* searchPath, Zone* zone) const override;
};
void LoadObjDataForZone(ISearchPath* searchPath, Zone* zone) const override;
};
}

View File

@ -7,6 +7,120 @@ IwiLoader::IwiLoader(MemoryManager* memoryManager)
m_memory_manager = memoryManager;
}
const ImageFormat* IwiLoader::GetFormat8(int8_t format)
{
switch (static_cast<iwi8::IwiFormat>(format))
{
case iwi8::IwiFormat::IMG_FORMAT_BITMAP_RGBA:
return &ImageFormat::FORMAT_R8_G8_B8_A8;
case iwi8::IwiFormat::IMG_FORMAT_BITMAP_RGB:
return &ImageFormat::FORMAT_R8_G8_B8;
case iwi8::IwiFormat::IMG_FORMAT_BITMAP_ALPHA:
return &ImageFormat::FORMAT_A8;
case iwi8::IwiFormat::IMG_FORMAT_DXT1:
return &ImageFormat::FORMAT_BC1;
case iwi8::IwiFormat::IMG_FORMAT_DXT3:
return &ImageFormat::FORMAT_BC2;
case iwi8::IwiFormat::IMG_FORMAT_DXT5:
return &ImageFormat::FORMAT_BC3;
case iwi8::IwiFormat::IMG_FORMAT_DXN:
return &ImageFormat::FORMAT_BC5;
case iwi8::IwiFormat::IMG_FORMAT_BITMAP_LUMINANCE_ALPHA: // used
case iwi8::IwiFormat::IMG_FORMAT_BITMAP_LUMINANCE: // used
case iwi8::IwiFormat::IMG_FORMAT_WAVELET_RGBA: // used
case iwi8::IwiFormat::IMG_FORMAT_WAVELET_RGB: // used
case iwi8::IwiFormat::IMG_FORMAT_WAVELET_LUMINANCE_ALPHA:
case iwi8::IwiFormat::IMG_FORMAT_WAVELET_LUMINANCE:
case iwi8::IwiFormat::IMG_FORMAT_WAVELET_ALPHA:
case iwi8::IwiFormat::IMG_FORMAT_DXT3A_AS_LUMINANCE:
case iwi8::IwiFormat::IMG_FORMAT_DXT5A_AS_LUMINANCE:
case iwi8::IwiFormat::IMG_FORMAT_DXT3A_AS_ALPHA:
case iwi8::IwiFormat::IMG_FORMAT_DXT5A_AS_ALPHA:
case iwi8::IwiFormat::IMG_FORMAT_DXT1_AS_LUMINANCE_ALPHA:
case iwi8::IwiFormat::IMG_FORMAT_DXN_AS_LUMINANCE_ALPHA:
case iwi8::IwiFormat::IMG_FORMAT_DXT1_AS_LUMINANCE:
case iwi8::IwiFormat::IMG_FORMAT_DXT1_AS_ALPHA:
printf("Unsupported IWI format: %i\n", format);
break;
default:
printf("Unknown IWI format: %i\n", format);
break;
}
return nullptr;
}
Texture* IwiLoader::LoadIwi8(FileAPI::IFile* file)
{
iwi8::IwiHeader header{};
if (file->Read(&header, sizeof header, 1) != 1)
return nullptr;
const ImageFormat* format = GetFormat8(header.format);
if (format == nullptr)
return nullptr;
uint16_t width = header.dimensions[0];
uint16_t height = header.dimensions[1];
uint16_t depth = header.dimensions[2];
bool hasMipMaps = !(header.flags & iwi8::IwiFlags::IMG_FLAG_NOMIPMAPS);
Texture* texture;
if ((header.flags & iwi8::IwiFlags::IMG_FLAG_MAPTYPE_MASK) == iwi8::IwiFlags::IMG_FLAG_MAPTYPE_CUBE)
{
texture = m_memory_manager->Create<TextureCube>(format, width, height, hasMipMaps);
}
else if ((header.flags & iwi8::IwiFlags::IMG_FLAG_MAPTYPE_MASK) == iwi8::IwiFlags::IMG_FLAG_MAPTYPE_3D)
{
texture = m_memory_manager->Create<Texture3D>(format, width, height, depth, hasMipMaps);
}
else if ((header.flags & iwi8::IwiFlags::IMG_FLAG_MAPTYPE_MASK) == iwi8::IwiFlags::IMG_FLAG_MAPTYPE_2D)
{
texture = m_memory_manager->Create<Texture2D>(format, width, height, hasMipMaps);
}
else if ((header.flags & iwi8::IwiFlags::IMG_FLAG_MAPTYPE_MASK) == iwi8::IwiFlags::IMG_FLAG_MAPTYPE_1D)
{
printf("Iwi has unsupported map type 1D\n");
return nullptr;
}
else
{
printf("Iwi has unsupported map type\n");
return nullptr;
}
texture->Allocate();
size_t currentFileSize = sizeof iwi8::IwiHeader + sizeof IwiVersion;
const int mipMapCount = hasMipMaps ? texture->GetMipMapCount() : 1;
for (int currentMipLevel = mipMapCount - 1; currentMipLevel >= 0; currentMipLevel--)
{
const size_t sizeOfMipLevel = texture->GetSizeOfMipLevel(currentMipLevel) * texture->GetFaceCount();
currentFileSize += sizeOfMipLevel;
if (currentMipLevel < static_cast<int>(_countof(iwi8::IwiHeader::fileSizeForPicmip))
&& currentFileSize != header.fileSizeForPicmip[currentMipLevel])
{
printf("Iwi has invalid file size for picmip %i\n", currentMipLevel);
m_memory_manager->Delete(texture);
return nullptr;
}
if (file->Read(texture->GetBufferForMipLevel(currentMipLevel), 1, sizeOfMipLevel) != sizeOfMipLevel)
{
printf("Unexpected eof of iwi in mip level %i\n", currentMipLevel);
m_memory_manager->Delete(texture);
return nullptr;
}
}
return texture;
}
const ImageFormat* IwiLoader::GetFormat27(int8_t format)
{
switch (static_cast<iwi27::IwiFormat>(format))
@ -65,11 +179,11 @@ Texture* IwiLoader::LoadIwi27(FileAPI::IFile* file)
bool hasMipMaps = !(header.flags & iwi27::IwiFlags::IMG_FLAG_NOMIPMAPS);
Texture* texture;
if(header.flags & iwi27::IwiFlags::IMG_FLAG_CUBEMAP)
if (header.flags & iwi27::IwiFlags::IMG_FLAG_CUBEMAP)
{
texture = m_memory_manager->Create<TextureCube>(format, width, height, hasMipMaps);
}
else if(header.flags & iwi27::IwiFlags::IMG_FLAG_VOLMAP)
else if (header.flags & iwi27::IwiFlags::IMG_FLAG_VOLMAP)
{
texture = m_memory_manager->Create<Texture3D>(format, width, height, depth, hasMipMaps);
}
@ -125,6 +239,9 @@ Texture* IwiLoader::LoadIwi(FileAPI::IFile* file)
switch (iwiVersion.version)
{
case 8:
return LoadIwi8(file);
case 27:
return LoadIwi27(file);

View File

@ -8,6 +8,9 @@ class IwiLoader
{
MemoryManager* m_memory_manager;
static const ImageFormat* GetFormat8(int8_t format);
Texture* LoadIwi8(FileAPI::IFile* file);
static const ImageFormat* GetFormat27(int8_t format);
Texture* LoadIwi27(FileAPI::IFile* file);

View File

@ -53,7 +53,7 @@ public:
{
const auto result = unzReadCurrentFile(m_container, buffer, elementSize * elementCount);
return result >= 0 ? static_cast<size_t>(result) : 0;
return result >= 0 ? static_cast<size_t>(result) / elementSize : 0;
}
size_t Write(const void* data, size_t elementSize, size_t elementCount) override
@ -113,7 +113,7 @@ public:
void Close() override
{
unzClose(m_container);
unzCloseCurrentFile(m_container);
m_open = false;
m_parent->OnIWDFileClose();
@ -232,6 +232,8 @@ public:
{
m_last_file = new IWDFile(this, m_unz_file, iwdEntry->m_size);
}
return m_last_file;
}
return nullptr;

View File

@ -1,5 +1,6 @@
#include "ObjLoading.h"
#include "IObjLoader.h"
#include "Game/IW4/ObjLoaderIW4.h"
#include "Game/T6/ObjLoaderT6.h"
#include "ObjContainer/IWD/IWD.h"
#include "SearchPath/SearchPaths.h"
@ -8,12 +9,13 @@ ObjLoading::Configuration_t ObjLoading::Configuration;
const IObjLoader* const OBJ_LOADERS[]
{
new ObjLoaderT6()
new IW4::ObjLoader(),
new T6::ObjLoader()
};
void ObjLoading::LoadReferencedContainersForZone(ISearchPath* searchPath, Zone* zone)
{
for (auto* loader : OBJ_LOADERS)
for (const auto* loader : OBJ_LOADERS)
{
if (loader->SupportsZone(zone))
{
@ -25,7 +27,7 @@ void ObjLoading::LoadReferencedContainersForZone(ISearchPath* searchPath, Zone*
void ObjLoading::LoadObjDataForZone(ISearchPath* searchPath, Zone* zone)
{
for (auto* loader : OBJ_LOADERS)
for (const auto* loader : OBJ_LOADERS)
{
if (loader->SupportsZone(zone))
{
@ -37,7 +39,7 @@ void ObjLoading::LoadObjDataForZone(ISearchPath* searchPath, Zone* zone)
void ObjLoading::UnloadContainersOfZone(Zone* zone)
{
for (auto* loader : OBJ_LOADERS)
for (const auto* loader : OBJ_LOADERS)
{
if (loader->SupportsZone(zone))
{
@ -56,7 +58,7 @@ void ObjLoading::LoadIWDsInSearchPath(ISearchPath* searchPath)
if (file.IsOpen())
{
const auto fileP = new FileAPI::File(std::move(file));
auto* fileP = new FileAPI::File(std::move(file));
IWD* iwd = new IWD(path, fileP);
if (iwd->Initialize())
@ -83,7 +85,7 @@ SearchPaths ObjLoading::GetIWDSearchPaths()
{
SearchPaths iwdPaths;
for(auto iwd : IWD::Repository)
for (auto* iwd : IWD::Repository)
{
iwdPaths.IncludeSearchPath(iwd);
}