2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-11-30 16:27:47 +00:00

feat: load iw5 materials from json

This commit is contained in:
Jan
2024-09-22 16:59:31 +02:00
parent 28a4fbd0d6
commit 7b28b574d2
4 changed files with 509 additions and 5 deletions

View File

@@ -1,18 +1,57 @@
#include "AssetLoaderMaterial.h"
#include "Game/IW5/IW5.h"
#include "ObjLoading.h"
#include "Game/IW5/Material/JsonMaterialLoader.h"
#include "Pool/GlobalAssetPool.h"
#include <cstring>
#include <format>
#include <iostream>
using namespace IW5;
void* AssetLoaderMaterial::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory)
{
auto* material = memory->Create<Material>();
memset(material, 0, sizeof(Material));
auto* asset = memory->Alloc<AssetMaterial::Type>();
asset->info.name = memory->Dup(assetName.c_str());
return asset;
}
bool AssetLoaderMaterial::CanLoadFromRaw() const
{
return true;
}
std::string AssetLoaderMaterial::GetFileNameForAsset(const std::string& assetName)
{
std::string sanitizedFileName(assetName);
if (sanitizedFileName[0] == '*')
{
std::ranges::replace(sanitizedFileName, '*', '_');
const auto parenthesisPos = sanitizedFileName.find('(');
if (parenthesisPos != std::string::npos)
sanitizedFileName.erase(parenthesisPos);
sanitizedFileName = "generated/" + sanitizedFileName;
}
return std::format("materials/{}.json", sanitizedFileName);
}
bool AssetLoaderMaterial::LoadFromRaw(
const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const
{
const auto file = searchPath->Open(GetFileNameForAsset(assetName));
if (!file.IsOpen())
return false;
auto* material = memory->Alloc<Material>();
material->info.name = memory->Dup(assetName.c_str());
return material;
std::vector<XAssetInfoGeneric*> dependencies;
if (LoadMaterialAsJson(*file.m_stream, *material, memory, manager, dependencies))
manager->AddAsset<AssetMaterial>(assetName, material, std::move(dependencies));
else
std::cerr << std::format("Failed to load material \"{}\"\n", assetName);
return true;
}

View File

@@ -1,6 +1,6 @@
#pragma once
#include "AssetLoading/BasicAssetLoader.h"
#include "AssetLoading/IAssetLoadingManager.h"
#include "Game/IW5/IW5.h"
#include "SearchPath/ISearchPath.h"
@@ -8,7 +8,12 @@ namespace IW5
{
class AssetLoaderMaterial final : public BasicAssetLoader<AssetMaterial>
{
static std::string GetFileNameForAsset(const std::string& assetName);
public:
_NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override;
_NODISCARD bool CanLoadFromRaw() const override;
bool
LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const override;
};
} // namespace IW5