mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-10 23:08:05 -05:00
refactor: image and obj data loading
This commit is contained in:
@ -35,10 +35,8 @@ bool AssetLoaderGfxImage::LoadFromRaw(
|
||||
const auto fileData = std::make_unique<char[]>(fileSize);
|
||||
file.m_stream->read(fileData.get(), fileSize);
|
||||
|
||||
MemoryManager tempMemory;
|
||||
IwiLoader iwiLoader(&tempMemory);
|
||||
std::istringstream ss(std::string(fileData.get(), fileSize));
|
||||
const auto texture = iwiLoader.LoadIwi(ss);
|
||||
const auto texture = iwi::LoadIwi(ss);
|
||||
if (!texture)
|
||||
{
|
||||
std::cerr << std::format("Failed to load texture from: {}\n", fileName);
|
||||
|
@ -227,7 +227,7 @@ bool AssetLoaderMenuList::LoadFromRaw(
|
||||
return true;
|
||||
}
|
||||
|
||||
void AssetLoaderMenuList::FinalizeAssetsForZone(AssetLoadingContext* context) const
|
||||
void AssetLoaderMenuList::FinalizeAssetsForZone(AssetLoadingContext& context) const
|
||||
{
|
||||
context->GetZoneAssetLoaderState<MenuConversionZoneState>()->FinalizeSupportingData();
|
||||
context.GetZoneAssetLoaderState<MenuConversionZoneState>()->FinalizeSupportingData();
|
||||
}
|
||||
|
@ -14,6 +14,6 @@ namespace IW5
|
||||
_NODISCARD bool CanLoadFromRaw() const override;
|
||||
bool
|
||||
LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const override;
|
||||
void FinalizeAssetsForZone(AssetLoadingContext* context) const override;
|
||||
void FinalizeAssetsForZone(AssetLoadingContext& context) const override;
|
||||
};
|
||||
} // namespace IW5
|
||||
|
@ -41,14 +41,9 @@
|
||||
#include "AssetLoaders/AssetLoaderXModel.h"
|
||||
#include "AssetLoaders/AssetLoaderXModelSurfs.h"
|
||||
#include "AssetLoading/AssetLoadingManager.h"
|
||||
#include "Game/IW5/GameAssetPoolIW5.h"
|
||||
#include "Game/IW5/GameIW5.h"
|
||||
#include "Image/Dx9TextureLoader.h"
|
||||
#include "Image/IwiLoader.h"
|
||||
#include "Image/IwiTypes.h"
|
||||
#include "Image/Texture.h"
|
||||
#include "ObjContainer/IPak/IPak.h"
|
||||
#include "ObjLoading.h"
|
||||
|
||||
using namespace IW5;
|
||||
|
||||
@ -104,129 +99,32 @@ ObjLoader::ObjLoader()
|
||||
#undef REGISTER_ASSET_LOADER
|
||||
}
|
||||
|
||||
bool ObjLoader::SupportsZone(Zone* zone) const
|
||||
bool ObjLoader::SupportsZone(const Zone& zone) const
|
||||
{
|
||||
return zone->m_game == &g_GameIW5;
|
||||
return zone.m_game == &g_GameIW5;
|
||||
}
|
||||
|
||||
bool ObjLoader::IsMpZone(Zone* zone)
|
||||
bool ObjLoader::IsMpZone(const Zone& zone)
|
||||
{
|
||||
return zone->m_name.compare(0, 3, "mp_") == 0 || zone->m_name.compare(zone->m_name.length() - 3, 3, "_mp") == 0;
|
||||
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)
|
||||
bool ObjLoader::IsZmZone(const Zone& zone)
|
||||
{
|
||||
return zone->m_name.compare(0, 3, "zm_") == 0 || zone->m_name.compare(zone->m_name.length() - 3, 3, "_zm") == 0;
|
||||
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::LoadReferencedContainersForZone(ISearchPath& searchPath, Zone& zone) const {}
|
||||
|
||||
void ObjLoader::UnloadContainersOfZone(Zone* zone) const {}
|
||||
void ObjLoader::UnloadContainersOfZone(Zone& zone) const {}
|
||||
|
||||
void ObjLoader::LoadImageFromLoadDef(GfxImage* image, Zone* zone)
|
||||
bool ObjLoader::LoadAssetForZone(AssetLoadingContext& context, const asset_type_t assetType, const std::string& assetName) const
|
||||
{
|
||||
const auto* loadDef = image->texture.loadDef;
|
||||
Dx9TextureLoader textureLoader(zone->GetMemory());
|
||||
|
||||
textureLoader.Width(image->width).Height(image->height).Depth(image->depth);
|
||||
|
||||
if ((loadDef->flags & iwi8::IMG_FLAG_MAPTYPE_MASK) == iwi8::IMG_FLAG_MAPTYPE_3D)
|
||||
textureLoader.Type(TextureType::T_3D);
|
||||
else if ((loadDef->flags & iwi8::IMG_FLAG_MAPTYPE_MASK) == iwi8::IMG_FLAG_MAPTYPE_CUBE)
|
||||
textureLoader.Type(TextureType::T_CUBE);
|
||||
else
|
||||
textureLoader.Type(TextureType::T_2D);
|
||||
|
||||
textureLoader.Format(static_cast<oat::D3DFORMAT>(loadDef->format));
|
||||
textureLoader.HasMipMaps(!(loadDef->flags & iwi8::IMG_FLAG_NOMIPMAPS));
|
||||
Texture* loadedTexture = textureLoader.LoadTexture(image->texture.loadDef->data);
|
||||
|
||||
if (loadedTexture != nullptr)
|
||||
{
|
||||
image->texture.texture = loadedTexture;
|
||||
image->cardMemory.platform[0] = 0;
|
||||
|
||||
const auto textureMipCount = loadedTexture->GetMipMapCount();
|
||||
for (auto mipLevel = 0; mipLevel < textureMipCount; mipLevel++)
|
||||
image->cardMemory.platform[0] += static_cast<int>(loadedTexture->GetSizeOfMipLevel(mipLevel) * loadedTexture->GetFaceCount());
|
||||
}
|
||||
}
|
||||
|
||||
void ObjLoader::LoadImageFromIwi(GfxImage* image, ISearchPath* searchPath, Zone* zone)
|
||||
{
|
||||
Texture* loadedTexture = nullptr;
|
||||
IwiLoader loader(zone->GetMemory());
|
||||
|
||||
const auto imageFileName = "images/" + std::string(image->name) + ".iwi";
|
||||
|
||||
{
|
||||
const auto filePathImage = searchPath->Open(imageFileName);
|
||||
if (filePathImage.IsOpen())
|
||||
{
|
||||
loadedTexture = loader.LoadIwi(*filePathImage.m_stream);
|
||||
}
|
||||
}
|
||||
|
||||
if (loadedTexture != nullptr)
|
||||
{
|
||||
image->texture.texture = loadedTexture;
|
||||
image->cardMemory.platform[0] = 0;
|
||||
|
||||
const auto textureMipCount = loadedTexture->GetMipMapCount();
|
||||
for (auto 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<GameAssetPoolIW5*>(zone->m_pools.get());
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
bool ObjLoader::LoadAssetForZone(AssetLoadingContext* context, asset_type_t assetType, const std::string& assetName) const
|
||||
{
|
||||
AssetLoadingManager assetLoadingManager(m_asset_loaders_by_type, *context);
|
||||
AssetLoadingManager assetLoadingManager(m_asset_loaders_by_type, context);
|
||||
return assetLoadingManager.LoadAssetFromLoader(assetType, assetName);
|
||||
}
|
||||
|
||||
void ObjLoader::FinalizeAssetsForZone(AssetLoadingContext* context) const
|
||||
void ObjLoader::FinalizeAssetsForZone(AssetLoadingContext& context) const
|
||||
{
|
||||
for (const auto& [type, loader] : m_asset_loaders_by_type)
|
||||
loader->FinalizeAssetsForZone(context);
|
||||
|
@ -1,37 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include "AssetLoading/IAssetLoader.h"
|
||||
#include "Game/IW5/IW5.h"
|
||||
#include "IObjLoader.h"
|
||||
#include "SearchPath/ISearchPath.h"
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace IW5
|
||||
{
|
||||
class ObjLoader final : public IObjLoader
|
||||
{
|
||||
std::map<asset_type_t, std::unique_ptr<IAssetLoader>> m_asset_loaders_by_type;
|
||||
|
||||
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:
|
||||
ObjLoader();
|
||||
|
||||
bool SupportsZone(Zone* zone) const override;
|
||||
[[nodiscard]] bool SupportsZone(const 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;
|
||||
bool LoadAssetForZone(AssetLoadingContext& context, asset_type_t assetType, const std::string& assetName) const override;
|
||||
void FinalizeAssetsForZone(AssetLoadingContext& context) const override;
|
||||
|
||||
bool LoadAssetForZone(AssetLoadingContext* context, asset_type_t assetType, const std::string& assetName) const override;
|
||||
void FinalizeAssetsForZone(AssetLoadingContext* context) const override;
|
||||
private:
|
||||
static bool IsMpZone(const Zone& zone);
|
||||
static bool IsZmZone(const Zone& zone);
|
||||
|
||||
std::unordered_map<asset_type_t, std::unique_ptr<IAssetLoader>> m_asset_loaders_by_type;
|
||||
};
|
||||
} // namespace IW5
|
||||
|
Reference in New Issue
Block a user