feat: load materials from json

This commit is contained in:
Jan
2024-02-24 20:33:26 +01:00
parent 1f6d0ab51a
commit 0bb17a33bd
8 changed files with 481 additions and 17 deletions

View File

@ -0,0 +1,59 @@
#include "AssetLoaderMaterial.h"
#include "Game/T6/Material/JsonMaterialLoader.h"
#include "Game/T6/T6.h"
#include "Pool/GlobalAssetPool.h"
#include <format>
#include <iostream>
using namespace T6;
void* AssetLoaderMaterial::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory)
{
auto* material = memory->Create<Material>();
memset(material, 0, sizeof(Material));
material->info.name = memory->Dup(assetName.c_str());
return material;
}
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 = static_cast<Material*>(memory->Alloc(sizeof(Material)));
memset(material, 0, sizeof(Material));
material->info.name = memory->Dup(assetName.c_str());
std::vector<XAssetInfoGeneric*> dependencies;
if (LoadMaterialAsJson(*file.m_stream, *material, memory, manager, dependencies))
manager->AddAsset(ASSET_TYPE_MATERIAL, assetName, material, std::move(dependencies));
else
std::cerr << "Failed to load material \"" << assetName << "\"\n";
return true;
}

View File

@ -0,0 +1,19 @@
#pragma once
#include "AssetLoading/BasicAssetLoader.h"
#include "AssetLoading/IAssetLoadingManager.h"
#include "Game/T6/T6.h"
#include "SearchPath/ISearchPath.h"
namespace T6
{
class AssetLoaderMaterial final : public BasicAssetLoader<ASSET_TYPE_MATERIAL, Material>
{
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 T6