mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-13 00:08:26 -05:00
feat: load materials from json
This commit is contained in:
59
src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderMaterial.cpp
Normal file
59
src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderMaterial.cpp
Normal 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;
|
||||
}
|
19
src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderMaterial.h
Normal file
19
src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderMaterial.h
Normal 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
|
Reference in New Issue
Block a user