2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-09-04 07:47:25 +00:00

chore: fix loading and writing code for T6

This commit is contained in:
Jan
2024-12-31 12:38:01 +01:00
parent d8bc156ffd
commit 83d13aa166
193 changed files with 4129 additions and 4208 deletions

View File

@@ -15,11 +15,11 @@ namespace
class JsonLoader
{
public:
JsonLoader(std::istream& stream, MemoryManager& memory, IAssetLoadingManager& manager, std::vector<XAssetInfoGeneric*>& dependencies)
JsonLoader(std::istream& stream, MemoryManager& memory, AssetCreationContext& context, AssetRegistration<AssetMaterial>& registration)
: m_stream(stream),
m_memory(memory),
m_manager(manager),
m_dependencies(dependencies)
m_context(context),
m_registration(registration)
{
}
@@ -35,7 +35,7 @@ namespace
if (type != "material" || version != 1u)
{
std::cerr << "Tried to load material \"" << material.info.name << "\" but did not find expected type material of version 1\n";
std::cerr << std::format("Tried to load material \"{}\" but did not find expected type material of version 1\n", material.info.name);
return false;
}
@@ -55,7 +55,7 @@ namespace
private:
static void PrintError(const Material& material, const std::string& message)
{
std::cerr << "Cannot load material \"" << material.info.name << "\": " << message << "\n";
std::cerr << std::format("Cannot load material \"{}\": {}\n", material.info.name, message);
}
static bool CreateGameFlagsFromJson(const JsonMaterial& jMaterial, unsigned& gameFlags)
@@ -108,13 +108,13 @@ namespace
textureDef.semantic = jTexture.semantic;
textureDef.isMatureContent = jTexture.isMatureContent;
auto* image = m_manager.LoadDependency<AssetImage>(jTexture.image);
auto* image = m_context.LoadDependency<AssetImage>(jTexture.image);
if (!image)
{
PrintError(material, std::format("Could not find textureDef image: {}", jTexture.image));
return false;
}
m_dependencies.push_back(image);
m_registration.AddDependency(image);
textureDef.image = image->Asset();
return true;
@@ -284,13 +284,13 @@ namespace
material.cameraRegion = jMaterial.cameraRegion;
material.probeMipBits = jMaterial.probeMipBits;
auto* techniqueSet = m_manager.LoadDependency<AssetTechniqueSet>(jMaterial.techniqueSet);
auto* techniqueSet = m_context.LoadDependency<AssetTechniqueSet>(jMaterial.techniqueSet);
if (!techniqueSet)
{
PrintError(material, "Could not find technique set");
return false;
}
m_dependencies.push_back(techniqueSet);
m_registration.AddDependency(techniqueSet);
material.techniqueSet = techniqueSet->Asset();
if (!jMaterial.textures.empty())
@@ -346,13 +346,13 @@ namespace
if (jMaterial.thermalMaterial)
{
auto* thermalMaterial = m_manager.LoadDependency<AssetMaterial>(jMaterial.thermalMaterial.value());
auto* thermalMaterial = m_context.LoadDependency<AssetMaterial>(jMaterial.thermalMaterial.value());
if (!thermalMaterial)
{
PrintError(material, "Could not find thermal material");
return false;
}
m_dependencies.push_back(thermalMaterial);
m_registration.AddDependency(thermalMaterial);
material.thermalMaterial = thermalMaterial->Asset();
}
else
@@ -365,17 +365,17 @@ namespace
std::istream& m_stream;
MemoryManager& m_memory;
IAssetLoadingManager& m_manager;
std::vector<XAssetInfoGeneric*>& m_dependencies;
AssetCreationContext& m_context;
AssetRegistration<AssetMaterial>& m_registration;
};
} // namespace
namespace T6
{
bool LoadMaterialAsJson(
std::istream& stream, Material& material, MemoryManager* memory, IAssetLoadingManager* manager, std::vector<XAssetInfoGeneric*>& dependencies)
std::istream& stream, Material& material, MemoryManager& memory, AssetCreationContext& context, AssetRegistration<AssetMaterial>& registration)
{
const JsonLoader loader(stream, *memory, *manager, dependencies);
const JsonLoader loader(stream, memory, context, registration);
return loader.Load(material);
}

View File

@@ -1,6 +1,7 @@
#pragma once
#include "AssetLoading/IAssetLoadingManager.h"
#include "Asset/AssetCreationContext.h"
#include "Asset/AssetRegistration.h"
#include "Game/T6/T6.h"
#include "Utils/MemoryManager.h"
@@ -9,5 +10,5 @@
namespace T6
{
bool LoadMaterialAsJson(
std::istream& stream, Material& material, MemoryManager* memory, IAssetLoadingManager* manager, std::vector<XAssetInfoGeneric*>& dependencies);
std::istream& stream, Material& material, MemoryManager& memory, AssetCreationContext& context, AssetRegistration<AssetMaterial>& registration);
} // namespace T6

View File

@@ -0,0 +1,70 @@
#include "LoaderMaterialT6.h"
#include "Game/T6/Material/JsonMaterialLoader.h"
#include "Game/T6/T6.h"
#include <algorithm>
#include <cstring>
#include <format>
#include <iostream>
using namespace T6;
namespace
{
class MaterialLoader final : public AssetCreator<AssetMaterial>
{
public:
MaterialLoader(MemoryManager& memory, ISearchPath& searchPath)
: m_memory(memory),
m_search_path(searchPath)
{
}
AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override
{
const auto file = m_search_path.Open(GetFileNameForAsset(assetName));
if (!file.IsOpen())
return AssetCreationResult::NoAction();
auto* material = m_memory.Alloc<Material>();
material->info.name = m_memory.Dup(assetName.c_str());
AssetRegistration<AssetMaterial> registration(assetName, material);
if (!LoadMaterialAsJson(*file.m_stream, *material, m_memory, context, registration))
{
std::cerr << std::format("Failed to load material \"{}\"\n", assetName);
return AssetCreationResult::Failure();
}
return AssetCreationResult::Success(context.AddAsset(std::move(registration)));
}
private:
static std::string 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);
}
MemoryManager& m_memory;
ISearchPath& m_search_path;
};
} // namespace
namespace T6
{
std::unique_ptr<AssetCreator<AssetMaterial>> CreateMaterialLoader(MemoryManager& memory, ISearchPath& searchPath)
{
return std::make_unique<MaterialLoader>(memory, searchPath);
}
} // namespace T6

View File

@@ -0,0 +1,13 @@
#pragma once
#include "Asset/IAssetCreator.h"
#include "Game/T6/T6.h"
#include "SearchPath/ISearchPath.h"
#include "Utils/MemoryManager.h"
#include <memory>
namespace T6
{
std::unique_ptr<AssetCreator<AssetMaterial>> CreateMaterialLoader(MemoryManager& memory, ISearchPath& searchPath);
} // namespace T6