mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2026-03-17 10:23:02 +00:00
feat: add external image loader for every supported game
This commit is contained in:
@@ -1,5 +1,43 @@
|
||||
#include "ImageLoaderCommon.h"
|
||||
|
||||
#include "Image/ImageCommon.h"
|
||||
#include "Image/IwiLoader.h"
|
||||
#include "Image/Texture.h"
|
||||
#include "Utils/Logging/Log.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <zlib.h>
|
||||
|
||||
using namespace image;
|
||||
|
||||
namespace
|
||||
{
|
||||
CommonImageLoaderResult Success(size_t iwiSize, CommonIwiMetaData meta, std::unique_ptr<Texture> texture, CommonImageLoaderHash hash)
|
||||
{
|
||||
return CommonImageLoaderResult{
|
||||
.m_failure = false,
|
||||
.m_iwi_size = iwiSize,
|
||||
.m_meta = meta,
|
||||
.m_texture = std::move(texture),
|
||||
.m_hash = hash,
|
||||
};
|
||||
}
|
||||
|
||||
CommonImageLoaderResult NoAction()
|
||||
{
|
||||
return CommonImageLoaderResult{
|
||||
.m_failure = false,
|
||||
};
|
||||
}
|
||||
|
||||
CommonImageLoaderResult Failure()
|
||||
{
|
||||
return CommonImageLoaderResult{
|
||||
.m_failure = true,
|
||||
};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
namespace image
|
||||
{
|
||||
std::optional<AssetCreationResult> CommonImageLoaderResult::GetResultIfCancelled() const
|
||||
@@ -12,4 +50,40 @@ namespace image
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
CommonImageLoaderResult
|
||||
LoadImageCommon(const std::string& imageName, ISearchPath& searchPath, IwiVersion expectedIwiVersion, CommonImageLoaderHashType hashType)
|
||||
{
|
||||
const auto fileName = image::GetFileNameForAsset(imageName, ".iwi");
|
||||
const auto file = searchPath.Open(fileName);
|
||||
if (!file.IsOpen())
|
||||
return NoAction();
|
||||
|
||||
const auto fileSize = static_cast<size_t>(file.m_length);
|
||||
|
||||
std::optional<IwiLoaderResult> loaderResult;
|
||||
CommonImageLoaderHash hash{};
|
||||
if (hashType == CommonImageLoaderHashType::NONE)
|
||||
{
|
||||
loaderResult = image::LoadIwi(*file.m_stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto fileData = std::make_unique<char[]>(fileSize);
|
||||
file.m_stream->read(fileData.get(), static_cast<std::streamsize>(fileSize));
|
||||
|
||||
hash.crc32 = static_cast<unsigned>(crc32(0u, reinterpret_cast<const Bytef*>(fileData.get()), static_cast<unsigned>(fileSize)));
|
||||
|
||||
std::istringstream inMemory(std::string(fileData.get(), fileSize));
|
||||
loaderResult = image::LoadIwi(inMemory);
|
||||
}
|
||||
|
||||
if (!loaderResult)
|
||||
{
|
||||
con::error("Failed to load texture from: {}", fileName);
|
||||
return Failure();
|
||||
}
|
||||
|
||||
return Success(fileSize, loaderResult->m_meta, std::move(loaderResult->m_texture), hash);
|
||||
}
|
||||
} // namespace image
|
||||
|
||||
@@ -3,21 +3,37 @@
|
||||
#include "Asset/AssetCreationResult.h"
|
||||
#include "Image/IwiTypes.h"
|
||||
#include "Image/Texture.h"
|
||||
#include "SearchPath/ISearchPath.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
namespace image
|
||||
{
|
||||
enum class CommonImageLoaderHashType
|
||||
{
|
||||
NONE,
|
||||
CRC32
|
||||
};
|
||||
|
||||
union CommonImageLoaderHash
|
||||
{
|
||||
uint32_t crc32;
|
||||
};
|
||||
|
||||
struct CommonImageLoaderResult
|
||||
{
|
||||
std::optional<AssetCreationResult> GetResultIfCancelled() const;
|
||||
|
||||
bool m_failure;
|
||||
std::string m_iwi_path;
|
||||
size_t m_iwi_size;
|
||||
CommonIwiMetaData m_meta;
|
||||
std::unique_ptr<Texture> m_texture;
|
||||
CommonImageLoaderHash m_hash;
|
||||
};
|
||||
|
||||
CommonImageLoaderResult LoadImageCommon();
|
||||
CommonImageLoaderResult
|
||||
LoadImageCommon(const std::string& imageName, ISearchPath& searchPath, IwiVersion expectedIwiVersion, CommonImageLoaderHashType hashType);
|
||||
} // namespace image
|
||||
|
||||
111
src/ObjLoading/Image/ImageLoaderExternal.cpp.template
Normal file
111
src/ObjLoading/Image/ImageLoaderExternal.cpp.template
Normal file
@@ -0,0 +1,111 @@
|
||||
#options GAME (IW3, IW4, IW5, T5, T6)
|
||||
|
||||
#filename "Game/" + GAME + "/Image/ImageLoaderExternal" + GAME + ".cpp"
|
||||
|
||||
#if GAME == "IW3"
|
||||
#define FEATURE_IW3
|
||||
#elif GAME == "IW4"
|
||||
#define FEATURE_IW4
|
||||
#elif GAME == "IW5"
|
||||
#define FEATURE_IW5
|
||||
#elif GAME == "T5"
|
||||
#define FEATURE_T5
|
||||
#elif GAME == "T6"
|
||||
#define FEATURE_T6
|
||||
#endif
|
||||
|
||||
#if defined(FEATURE_IW3)
|
||||
#define IWI_VERSION IWI_6
|
||||
#elif defined(FEATURE_IW4) || defined(FEATURE_IW5)
|
||||
#define IWI_VERSION IWI_8
|
||||
#elif defined(FEATURE_T5)
|
||||
#define IWI_VERSION IWI_13
|
||||
#elif defined(FEATURE_T6)
|
||||
#define IWI_VERSION IWI_27
|
||||
#endif
|
||||
|
||||
#if defined(FEATURE_T6)
|
||||
#define HASH_TYPE CRC32
|
||||
#else
|
||||
#define HASH_TYPE NONE
|
||||
#endif
|
||||
|
||||
|
||||
// This file was templated.
|
||||
// See ImageLoaderExternal.cpp.template.
|
||||
// Do not modify, changes will be lost.
|
||||
|
||||
#set LOADER_HEADER "\"ImageLoaderExternal" + GAME + ".h\""
|
||||
#include LOADER_HEADER
|
||||
|
||||
#set COMMON_HEADER "\"Game/" + GAME + "/Common" + GAME + ".h\""
|
||||
#include COMMON_HEADER
|
||||
#include "Image/ImageLoaderCommon.h"
|
||||
#include "Utils/Logging/Log.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
using namespace GAME;
|
||||
using namespace image;
|
||||
|
||||
namespace
|
||||
{
|
||||
#set LOADER_CLASS "ImageLoader" + GAME
|
||||
class LOADER_CLASS final : public AssetCreator<AssetImage>
|
||||
{
|
||||
public:
|
||||
LOADER_CLASS(MemoryManager& memory, ISearchPath& searchPath)
|
||||
: m_memory(memory),
|
||||
m_search_path(searchPath)
|
||||
{
|
||||
}
|
||||
|
||||
AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override
|
||||
{
|
||||
const auto loadingResult = LoadImageCommon(assetName, m_search_path, IwiVersion::IWI_VERSION, CommonImageLoaderHashType::HASH_TYPE);
|
||||
const auto earlyReturn = loadingResult.GetResultIfCancelled();
|
||||
if (earlyReturn)
|
||||
return *earlyReturn;
|
||||
|
||||
const auto* texture = loadingResult.m_texture.get();
|
||||
|
||||
auto* image = m_memory.Alloc<GfxImage>();
|
||||
image->name = m_memory.Dup(assetName.c_str());
|
||||
#ifdef FEATURE_t6
|
||||
image->hash = Common::R_HashString(image->name, 0);
|
||||
#endif
|
||||
#ifndef FEATURE_IW5
|
||||
image->delayLoadPixels = true;
|
||||
#endif
|
||||
|
||||
image->noPicmip = loadingResult.m_meta.m_no_picmip;
|
||||
image->width = static_cast<uint16_t>(texture->GetWidth());
|
||||
image->height = static_cast<uint16_t>(texture->GetHeight());
|
||||
image->depth = static_cast<uint16_t>(texture->GetDepth());
|
||||
|
||||
#ifdef FEATURE_T6
|
||||
image->streaming = 1;
|
||||
image->streamedParts[0].levelCount = 1;
|
||||
image->streamedParts[0].levelSize = static_cast<uint32_t>(loadingResult.m_iwi_size);
|
||||
image->streamedParts[0].hash = loadingResult.m_hash.crc32 & 0x1FFFFFFF;
|
||||
image->streamedPartCount = 1;
|
||||
#endif
|
||||
|
||||
image->texture.loadDef = m_memory.Alloc<GfxImageLoadDef>();
|
||||
|
||||
return AssetCreationResult::Success(context.AddAsset<AssetImage>(assetName, image));
|
||||
}
|
||||
|
||||
MemoryManager& m_memory;
|
||||
ISearchPath& m_search_path;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
namespace image
|
||||
{
|
||||
#set LOADER_METHOD "CreateLoaderExternal" + GAME
|
||||
std::unique_ptr<AssetCreator<AssetImage>> LOADER_METHOD(MemoryManager& memory, ISearchPath& searchPath)
|
||||
{
|
||||
return std::make_unique<LOADER_CLASS>(memory, searchPath);
|
||||
}
|
||||
} // namespace image
|
||||
23
src/ObjLoading/Image/ImageLoaderExternal.h.template
Normal file
23
src/ObjLoading/Image/ImageLoaderExternal.h.template
Normal file
@@ -0,0 +1,23 @@
|
||||
#options GAME (IW3, IW4, IW5, T5, T6)
|
||||
|
||||
#filename "Game/" + GAME + "/Image/ImageLoaderExternal" + GAME + ".h"
|
||||
|
||||
// This file was templated.
|
||||
// See ImageLoaderExternal.h.template.
|
||||
// Do not modify, changes will be lost.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Asset/IAssetCreator.h"
|
||||
#set GAME_HEADER "\"Game/" + GAME + "/" + GAME + ".h\""
|
||||
#include GAME_HEADER
|
||||
#include "SearchPath/ISearchPath.h"
|
||||
#include "Utils/MemoryManager.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace image
|
||||
{
|
||||
#set LOADER_METHOD "CreateLoaderExternal" + GAME
|
||||
std::unique_ptr<AssetCreator<GAME::AssetImage>> LOADER_METHOD(MemoryManager& memory, ISearchPath& searchPath);
|
||||
} // namespace image
|
||||
Reference in New Issue
Block a user