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:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user