mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-10 23:08:05 -05:00
chore: implement base loading and writing of xmodel json
This commit is contained in:
44
src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderXModel.cpp
Normal file
44
src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderXModel.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
#include "AssetLoaderXModel.h"
|
||||
|
||||
#include "Game/T6/XModel/JsonXModelLoader.h"
|
||||
#include "Game/T6/T6.h"
|
||||
#include "Pool/GlobalAssetPool.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
|
||||
using namespace T6;
|
||||
|
||||
void* AssetLoaderXModel::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory)
|
||||
{
|
||||
auto* xmodel = memory->Create<XModel>();
|
||||
memset(xmodel, 0, sizeof(XModel));
|
||||
xmodel->name = memory->Dup(assetName.c_str());
|
||||
|
||||
return xmodel;
|
||||
}
|
||||
|
||||
bool AssetLoaderXModel::CanLoadFromRaw() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AssetLoaderXModel::LoadFromRaw(
|
||||
const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const
|
||||
{
|
||||
const auto file = searchPath->Open(std::format("xmodel/{}.json", assetName));
|
||||
if (!file.IsOpen())
|
||||
return false;
|
||||
|
||||
auto* xmodel = memory->Alloc<XModel>();
|
||||
xmodel->name = memory->Dup(assetName.c_str());
|
||||
|
||||
std::vector<XAssetInfoGeneric*> dependencies;
|
||||
if (LoadXModelAsJson(*file.m_stream, *xmodel, memory, manager, dependencies))
|
||||
manager->AddAsset<AssetXModel>(assetName, xmodel, std::move(dependencies));
|
||||
else
|
||||
std::cerr << "Failed to load xmodel \"" << assetName << "\"\n";
|
||||
|
||||
return true;
|
||||
}
|
19
src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderXModel.h
Normal file
19
src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderXModel.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 AssetLoaderXModel final : public BasicAssetLoader<ASSET_TYPE_XMODEL, XModel>
|
||||
{
|
||||
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
|
116
src/ObjLoading/Game/T6/XModel/JsonXModelLoader.cpp
Normal file
116
src/ObjLoading/Game/T6/XModel/JsonXModelLoader.cpp
Normal file
@ -0,0 +1,116 @@
|
||||
#include "JsonXModelLoader.h"
|
||||
|
||||
#include "Game/T6/CommonT6.h"
|
||||
#include "Game/T6/Json/JsonXModel.h"
|
||||
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using namespace nlohmann;
|
||||
using namespace T6;
|
||||
|
||||
namespace
|
||||
{
|
||||
class JsonLoader
|
||||
{
|
||||
public:
|
||||
JsonLoader(std::istream& stream, MemoryManager& memory, IAssetLoadingManager& manager, std::set<XAssetInfoGeneric*>& dependencies)
|
||||
: m_stream(stream),
|
||||
m_memory(memory),
|
||||
m_manager(manager),
|
||||
m_dependencies(dependencies)
|
||||
|
||||
{
|
||||
}
|
||||
|
||||
bool Load(XModel& xmodel) const
|
||||
{
|
||||
const auto jRoot = json::parse(m_stream);
|
||||
std::string type;
|
||||
unsigned version;
|
||||
|
||||
jRoot.at("_type").get_to(type);
|
||||
jRoot.at("_version").get_to(version);
|
||||
|
||||
if (type != "xmodel" || version != 1u)
|
||||
{
|
||||
std::cerr << "Tried to load xmodel \"" << xmodel.name << "\" but did not find expected type material of version 1\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto jXModel = jRoot.get<JsonXModel>();
|
||||
return CreateXModelFromJson(jXModel, xmodel);
|
||||
}
|
||||
|
||||
private:
|
||||
static void PrintError(const XModel& xmodel, const std::string& message)
|
||||
{
|
||||
std::cerr << "Cannot load xmodel \"" << xmodel.name << "\": " << message << "\n";
|
||||
}
|
||||
|
||||
bool CreateXModelFromJson(const JsonXModel& jXModel, XModel& xmodel) const
|
||||
{
|
||||
xmodel.collLod = static_cast<uint16_t>(jXModel.collLod);
|
||||
|
||||
if (jXModel.physPreset)
|
||||
{
|
||||
auto* physPreset = m_manager.LoadDependency<AssetPhysPreset>(jXModel.physPreset.value());
|
||||
if (!physPreset)
|
||||
{
|
||||
PrintError(xmodel, "Could not find phys preset");
|
||||
return false;
|
||||
}
|
||||
m_dependencies.emplace(physPreset);
|
||||
xmodel.physPreset = physPreset->Asset();
|
||||
}
|
||||
else
|
||||
{
|
||||
xmodel.physPreset = nullptr;
|
||||
}
|
||||
|
||||
if (jXModel.physConstraints)
|
||||
{
|
||||
auto* physConstraints = m_manager.LoadDependency<AssetPhysConstraints>(jXModel.physConstraints.value());
|
||||
if (!physConstraints)
|
||||
{
|
||||
PrintError(xmodel, "Could not find phys constraints");
|
||||
return false;
|
||||
}
|
||||
m_dependencies.emplace(physConstraints);
|
||||
xmodel.physConstraints = physConstraints->Asset();
|
||||
}
|
||||
else
|
||||
{
|
||||
xmodel.physConstraints = nullptr;
|
||||
}
|
||||
|
||||
xmodel.flags = jXModel.flags;
|
||||
xmodel.lightingOriginOffset.x = jXModel.lightingOriginOffset.x;
|
||||
xmodel.lightingOriginOffset.y = jXModel.lightingOriginOffset.y;
|
||||
xmodel.lightingOriginOffset.z = jXModel.lightingOriginOffset.z;
|
||||
xmodel.lightingOriginRange = jXModel.lightingOriginRange;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::istream& m_stream;
|
||||
MemoryManager& m_memory;
|
||||
IAssetLoadingManager& m_manager;
|
||||
std::set<XAssetInfoGeneric*>& m_dependencies;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
namespace T6
|
||||
{
|
||||
bool LoadXModelAsJson(
|
||||
std::istream& stream, XModel& xmodel, MemoryManager* memory, IAssetLoadingManager* manager, std::vector<XAssetInfoGeneric*>& dependencies)
|
||||
{
|
||||
std::set<XAssetInfoGeneric*> dependenciesSet;
|
||||
const JsonLoader loader(stream, *memory, *manager, dependenciesSet);
|
||||
|
||||
dependencies.assign_range(dependenciesSet);
|
||||
|
||||
return loader.Load(xmodel);
|
||||
}
|
||||
} // namespace T6
|
13
src/ObjLoading/Game/T6/XModel/JsonXModelLoader.h
Normal file
13
src/ObjLoading/Game/T6/XModel/JsonXModelLoader.h
Normal file
@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "AssetLoading/IAssetLoadingManager.h"
|
||||
#include "Game/T6/T6.h"
|
||||
#include "Utils/MemoryManager.h"
|
||||
|
||||
#include <istream>
|
||||
|
||||
namespace T6
|
||||
{
|
||||
bool LoadXModelAsJson(
|
||||
std::istream& stream, XModel& xmodel, MemoryManager* memory, IAssetLoadingManager* manager, std::vector<XAssetInfoGeneric*>& dependencies);
|
||||
} // namespace T6
|
Reference in New Issue
Block a user