From 7d4029b21fe0d46fc3a5987e8d4eb3f038a1ed6d Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 27 Mar 2021 14:00:55 +0100 Subject: [PATCH] Add physconstraints loading --- .../AssetLoaderPhysConstraints.cpp | 119 ++++++++++++++++++ .../AssetLoaders/AssetLoaderPhysConstraints.h | 18 +++ src/ObjLoading/Game/T6/ObjLoaderT6.cpp | 3 +- 3 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderPhysConstraints.cpp create mode 100644 src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderPhysConstraints.h diff --git a/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderPhysConstraints.cpp b/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderPhysConstraints.cpp new file mode 100644 index 00000000..dcf3d893 --- /dev/null +++ b/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderPhysConstraints.cpp @@ -0,0 +1,119 @@ +#include "AssetLoaderPhysConstraints.h" + +#include +#include +#include + +#include "Game/T6/ObjConstantsT6.h" +#include "Game/T6/T6.h" +#include "Game/T6/InfoString/EnumStrings.h" +#include "Game/T6/InfoString/InfoStringToStructConverter.h" +#include "Game/T6/InfoString/PhysConstraintsFields.h" +#include "InfoString/InfoString.h" + +using namespace T6; + +namespace T6 +{ + class InfoStringToPhysConstraintsConverter final : public InfoStringToStructConverter + { + protected: + bool ConvertExtensionField(const cspField_t& field, const std::string& value) override + { + switch (static_cast(field.iFieldType)) + { + case CFT_TYPE: + return ConvertEnumInt(value, field.iOffset, s_constraintTypeNames, std::extent::value); + + default: + assert(false); + return false; + } + } + + public: + InfoStringToPhysConstraintsConverter(const InfoString& infoString, PhysConstraints* physConstraints, ZoneScriptStrings& zoneScriptStrings, MemoryManager* memory, IAssetLoadingManager* manager, + const cspField_t* fields, const size_t fieldCount) + : InfoStringToStructConverter(infoString, physConstraints, zoneScriptStrings, memory, manager, fields, fieldCount) + { + } + }; +} + +void AssetLoaderPhysConstraints::CalculatePhysConstraintsFields(PhysConstraints* physConstraints, Zone* zone) +{ + for(auto& constraint : physConstraints->data) + { + constraint.targetname = zone->m_script_strings.AddOrGetScriptString(""); + constraint.target_ent1 = zone->m_script_strings.AddOrGetScriptString(""); + constraint.target_ent2 = zone->m_script_strings.AddOrGetScriptString(""); + constraint.attach_point_type1 = ATTACH_POINT_BONE; + constraint.attach_point_type2 = ATTACH_POINT_BONE; + constraint.target_bone1 = ""; + constraint.target_bone2 = ""; + } + + // count + { + auto foundEnd = false; + for (auto i = 0u; i < std::extent::value; i++) + { + if (physConstraints->data[i].type == CONSTRAINT_NONE) + { + foundEnd = true; + physConstraints->count = i; + break; + } + } + + if (!foundEnd) + physConstraints->count = std::extent::value; + } +} + +void* AssetLoaderPhysConstraints::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) +{ + auto* physConstraints = memory->Create(); + memset(physConstraints, 0, sizeof(PhysConstraints)); + physConstraints->name = memory->Dup(assetName.c_str()); + return physConstraints; +} + +bool AssetLoaderPhysConstraints::CanLoadFromRaw() const +{ + return true; +} + +bool AssetLoaderPhysConstraints::LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const +{ + const auto fileName = "physconstraints/" + assetName; + const auto file = searchPath->Open(fileName); + if (!file.IsOpen()) + return false; + + InfoString infoString; + if (!infoString.FromStream(ObjConstants::INFO_STRING_PREFIX_PHYS_CONSTRAINTS, *file.m_stream)) + { + std::cout << "Failed to read phys constraints raw file: \"" << fileName << "\"" << std::endl; + return true; + } + + auto* physConstraints = memory->Create(); + memset(physConstraints, 0, sizeof(PhysConstraints)); + + InfoStringToPhysConstraintsConverter converter(infoString, physConstraints, zone->m_script_strings, memory, manager, phys_constraints_fields, std::extent::value); + if (!converter.Convert()) + { + std::cout << "Failed to parse phys constraints raw file: \"" << fileName << "\"" << std::endl; + return true; + } + + CalculatePhysConstraintsFields(physConstraints, zone); + physConstraints->name = memory->Dup(assetName.c_str()); + + auto scrStrings = converter.GetUsedScriptStrings(); + scrStrings.push_back(zone->m_script_strings.AddOrGetScriptString("")); + manager->AddAsset(ASSET_TYPE_PHYSCONSTRAINTS, assetName, physConstraints, converter.GetDependencies(), scrStrings); + + return true; +} diff --git a/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderPhysConstraints.h b/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderPhysConstraints.h new file mode 100644 index 00000000..91c39c38 --- /dev/null +++ b/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderPhysConstraints.h @@ -0,0 +1,18 @@ +#pragma once +#include "Game/T6/T6.h" +#include "AssetLoading/BasicAssetLoader.h" +#include "AssetLoading/IAssetLoadingManager.h" +#include "SearchPath/ISearchPath.h" + +namespace T6 +{ + class AssetLoaderPhysConstraints final : public BasicAssetLoader + { + static void CalculatePhysConstraintsFields(PhysConstraints* physConstraints, Zone* zone); + + 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; + }; +} diff --git a/src/ObjLoading/Game/T6/ObjLoaderT6.cpp b/src/ObjLoading/Game/T6/ObjLoaderT6.cpp index 354da947..9ef53c25 100644 --- a/src/ObjLoading/Game/T6/ObjLoaderT6.cpp +++ b/src/ObjLoading/Game/T6/ObjLoaderT6.cpp @@ -6,6 +6,7 @@ #include "ObjLoading.h" #include "AssetLoaders/AssetLoaderFontIcon.h" #include "AssetLoaders/AssetLoaderLocalizeEntry.h" +#include "AssetLoaders/AssetLoaderPhysConstraints.h" #include "AssetLoaders/AssetLoaderPhysPreset.h" #include "AssetLoaders/AssetLoaderQdb.h" #include "AssetLoaders/AssetLoaderRawFile.h" @@ -34,7 +35,7 @@ namespace T6 #define BASIC_LOADER(assetType, assetClass) BasicAssetLoader REGISTER_ASSET_LOADER(AssetLoaderPhysPreset) - REGISTER_ASSET_LOADER(BASIC_LOADER(ASSET_TYPE_PHYSCONSTRAINTS, PhysConstraints)) + REGISTER_ASSET_LOADER(AssetLoaderPhysConstraints) REGISTER_ASSET_LOADER(BASIC_LOADER(ASSET_TYPE_DESTRUCTIBLEDEF, DestructibleDef)) REGISTER_ASSET_LOADER(BASIC_LOADER(ASSET_TYPE_XANIMPARTS, XAnimParts)) REGISTER_ASSET_LOADER(BASIC_LOADER(ASSET_TYPE_XMODEL, XModel))