From 759a3ccf0e83d0938f0fc840f06029ba59562570 Mon Sep 17 00:00:00 2001 From: Paging Red Date: Sat, 2 May 2026 06:01:59 -0400 Subject: [PATCH] Merge pull request #753 from pagingred/iw5_physpreset_dumper feat: phys presets dumping and loading for IW3, IW5, T5 --- docs/SupportedAssetTypes.md | 6 +- src/Common/Game/IW5/IW5.h | 7 + src/Common/Game/IW5/IW5_Assets.h | 20 ++ src/Common/Game/T5/T5_Assets.h | 17 ++ .../Game/IW5/InfoString/EnumStrings.h | 9 + .../Game/IW5/PhysPreset/PhysPresetFields.h | 25 +++ src/ObjCommon/Game/T5/ObjConstantsT5.h | 10 + .../Game/T5/PhysPreset/PhysPresetFields.h | 27 +++ .../InfoStringToStructConverter.cpp | 169 ++++++++++++++++ .../InfoString/InfoStringToStructConverter.h | 28 +++ src/ObjLoading/Game/IW3/ObjLoaderIW3.cpp | 9 +- .../IW3/PhysPreset/GdtLoaderPhysPresetIW3.cpp | 53 +++++ .../IW3/PhysPreset/GdtLoaderPhysPresetIW3.h | 14 ++ .../InfoStringLoaderPhysPresetIW3.cpp | 90 +++++++++ .../InfoStringLoaderPhysPresetIW3.h | 20 ++ .../IW3/PhysPreset/RawLoaderPhysPresetIW3.cpp | 55 ++++++ .../IW3/PhysPreset/RawLoaderPhysPresetIW3.h | 13 ++ src/ObjLoading/Game/IW5/ObjLoaderIW5.cpp | 5 +- .../IW5/PhysPreset/GdtLoaderPhysPresetIW5.cpp | 53 +++++ .../IW5/PhysPreset/GdtLoaderPhysPresetIW5.h | 14 ++ .../InfoStringLoaderPhysPresetIW5.cpp | 97 ++++++++++ .../InfoStringLoaderPhysPresetIW5.h | 20 ++ .../IW5/PhysPreset/RawLoaderPhysPresetIW5.cpp | 52 +++++ .../IW5/PhysPreset/RawLoaderPhysPresetIW5.h | 13 ++ .../InfoStringToStructConverter.cpp | 181 ++++++++++++++++++ .../InfoString/InfoStringToStructConverter.h | 28 +++ src/ObjLoading/Game/T5/ObjLoaderT5.cpp | 9 +- .../T5/PhysPreset/GdtLoaderPhysPresetT5.cpp | 54 ++++++ .../T5/PhysPreset/GdtLoaderPhysPresetT5.h | 14 ++ .../InfoStringLoaderPhysPresetT5.cpp | 100 ++++++++++ .../PhysPreset/InfoStringLoaderPhysPresetT5.h | 20 ++ .../T5/PhysPreset/RawLoaderPhysPresetT5.cpp | 56 ++++++ .../T5/PhysPreset/RawLoaderPhysPresetT5.h | 13 ++ src/ObjWriting/Game/IW5/ObjWriterIW5.cpp | 3 +- .../PhysPresetInfoStringDumperIW5.cpp | 112 +++++++++++ .../PhysPresetInfoStringDumperIW5.h | 13 ++ .../InfoStringFromStructConverter.cpp | 133 +++++++++++++ .../InfoStringFromStructConverter.h | 24 +++ src/ObjWriting/Game/T5/ObjWriterT5.cpp | 3 +- .../PhysPresetInfoStringDumperT5.cpp | 108 +++++++++++ .../PhysPreset/PhysPresetInfoStringDumperT5.h | 13 ++ 41 files changed, 1698 insertions(+), 12 deletions(-) create mode 100644 src/ObjCommon/Game/IW5/InfoString/EnumStrings.h create mode 100644 src/ObjCommon/Game/IW5/PhysPreset/PhysPresetFields.h create mode 100644 src/ObjCommon/Game/T5/PhysPreset/PhysPresetFields.h create mode 100644 src/ObjLoading/Game/IW3/PhysPreset/GdtLoaderPhysPresetIW3.cpp create mode 100644 src/ObjLoading/Game/IW3/PhysPreset/GdtLoaderPhysPresetIW3.h create mode 100644 src/ObjLoading/Game/IW3/PhysPreset/InfoStringLoaderPhysPresetIW3.cpp create mode 100644 src/ObjLoading/Game/IW3/PhysPreset/InfoStringLoaderPhysPresetIW3.h create mode 100644 src/ObjLoading/Game/IW3/PhysPreset/RawLoaderPhysPresetIW3.cpp create mode 100644 src/ObjLoading/Game/IW3/PhysPreset/RawLoaderPhysPresetIW3.h create mode 100644 src/ObjLoading/Game/IW5/PhysPreset/GdtLoaderPhysPresetIW5.cpp create mode 100644 src/ObjLoading/Game/IW5/PhysPreset/GdtLoaderPhysPresetIW5.h create mode 100644 src/ObjLoading/Game/IW5/PhysPreset/InfoStringLoaderPhysPresetIW5.cpp create mode 100644 src/ObjLoading/Game/IW5/PhysPreset/InfoStringLoaderPhysPresetIW5.h create mode 100644 src/ObjLoading/Game/IW5/PhysPreset/RawLoaderPhysPresetIW5.cpp create mode 100644 src/ObjLoading/Game/IW5/PhysPreset/RawLoaderPhysPresetIW5.h create mode 100644 src/ObjLoading/Game/T5/InfoString/InfoStringToStructConverter.cpp create mode 100644 src/ObjLoading/Game/T5/InfoString/InfoStringToStructConverter.h create mode 100644 src/ObjLoading/Game/T5/PhysPreset/GdtLoaderPhysPresetT5.cpp create mode 100644 src/ObjLoading/Game/T5/PhysPreset/GdtLoaderPhysPresetT5.h create mode 100644 src/ObjLoading/Game/T5/PhysPreset/InfoStringLoaderPhysPresetT5.cpp create mode 100644 src/ObjLoading/Game/T5/PhysPreset/InfoStringLoaderPhysPresetT5.h create mode 100644 src/ObjLoading/Game/T5/PhysPreset/RawLoaderPhysPresetT5.cpp create mode 100644 src/ObjLoading/Game/T5/PhysPreset/RawLoaderPhysPresetT5.h create mode 100644 src/ObjWriting/Game/IW5/PhysPreset/PhysPresetInfoStringDumperIW5.cpp create mode 100644 src/ObjWriting/Game/IW5/PhysPreset/PhysPresetInfoStringDumperIW5.h create mode 100644 src/ObjWriting/Game/T5/PhysPreset/PhysPresetInfoStringDumperT5.cpp create mode 100644 src/ObjWriting/Game/T5/PhysPreset/PhysPresetInfoStringDumperT5.h diff --git a/docs/SupportedAssetTypes.md b/docs/SupportedAssetTypes.md index 3d94d237..7bbf4154 100644 --- a/docs/SupportedAssetTypes.md +++ b/docs/SupportedAssetTypes.md @@ -11,7 +11,7 @@ The following section specify which assets are supported to be dumped to disk (u | Asset Type | Dumping Support | Loading Support | Notes | | -------------------- | --------------- | --------------- | ---------------------------------------------------------------------------- | -| PhysPreset | ✅ | ❌ | | +| PhysPreset | ✅ | ✅ | | | XAnimParts | ❌ | ❌ | | | XModel | ✅ | ✅ | Model data can be exported to `XMODEL_EXPORT/XMODEL_BIN`, `OBJ`, `GLB/GLTF`. | | Material | ✅ | ✅ | | @@ -81,7 +81,7 @@ The following section specify which assets are supported to be dumped to disk (u | Asset Type | Dumping Support | Loading Support | Notes | | ------------------------- | --------------- | --------------- | ------------------------------------------------------------------------------------------------------------- | -| PhysPreset | ❌ | ❌ | | +| PhysPreset | ✅ | ✅ | | | PhysCollmap | ❌ | ❌ | | | XAnimParts | ❌ | ❌ | | | XModelSurfs | ❌ | ❌ | | @@ -126,7 +126,7 @@ The following section specify which assets are supported to be dumped to disk (u | Asset Type | Dumping Support | Loading Support | Notes | | -------------------- | --------------- | --------------- | ---------------------------------------------------------------------------- | -| PhysPreset | ❌ | ❌ | | +| PhysPreset | ✅ | ✅ | | | PhysConstraints | ❌ | ❌ | | | DestructibleDef | ❌ | ❌ | | | XAnimParts | ❌ | ❌ | | diff --git a/src/Common/Game/IW5/IW5.h b/src/Common/Game/IW5/IW5.h index 38e6c7bc..3984d481 100644 --- a/src/Common/Game/IW5/IW5.h +++ b/src/Common/Game/IW5/IW5.h @@ -197,6 +197,13 @@ namespace IW5 WAFT_NUM_FIELD_TYPES, }; + enum physPresetFieldType_t + { + PPFT_SCALING = CSPFT_NUM_BASE_FIELD_TYPES, + + PPFT_NUM_FIELD_TYPES, + }; + using AssetPhysPreset = Asset; using AssetPhysCollMap = Asset; using AssetXAnim = Asset; diff --git a/src/Common/Game/IW5/IW5_Assets.h b/src/Common/Game/IW5/IW5_Assets.h index 647586f8..7355def7 100644 --- a/src/Common/Game/IW5/IW5_Assets.h +++ b/src/Common/Game/IW5/IW5_Assets.h @@ -201,6 +201,26 @@ namespace IW5 bool perSurfaceSndAlias; }; + struct PhysPresetInfo + { + float mass; + float bounce; + float friction; + float bulletForceScale; + float explosiveForceScale; + const char* sndAliasPrefix; + float piecesSpreadFraction; + float piecesUpwardVelocity; + float minMomentum; + float maxMomentum; + float minPitch; + float maxPitch; + PhysPresetScaling volumeType; + PhysPresetScaling pitchType; + int tempDefaultToCylinder; + int perSurfaceSndAlias; + }; + struct Bounds { vec3_t midPoint; diff --git a/src/Common/Game/T5/T5_Assets.h b/src/Common/Game/T5/T5_Assets.h index 077a82ef..13f25115 100644 --- a/src/Common/Game/T5/T5_Assets.h +++ b/src/Common/Game/T5/T5_Assets.h @@ -177,6 +177,23 @@ namespace T5 vec3_t buoyancyBoxMax; }; + struct PhysPresetInfo + { + float mass; + float bounce; + float friction; + int isFrictionInfinity; + float bulletForceScale; + float explosiveForceScale; + float piecesSpreadFraction; + float piecesUpwardVelocity; + int canFloat; + float gravityScale; + vec3_t centerOfMassOffset; + vec3_t buoyancyBoxMin; + vec3_t buoyancyBoxMax; + }; + enum ConstraintType { CONSTRAINT_NONE = 0x0, diff --git a/src/ObjCommon/Game/IW5/InfoString/EnumStrings.h b/src/ObjCommon/Game/IW5/InfoString/EnumStrings.h new file mode 100644 index 00000000..3d5c9721 --- /dev/null +++ b/src/ObjCommon/Game/IW5/InfoString/EnumStrings.h @@ -0,0 +1,9 @@ +#pragma once + +namespace IW5 +{ + inline const char* szPhysPresetScalingNames[]{ + "linear", + "quadratic", + }; +} // namespace IW5 diff --git a/src/ObjCommon/Game/IW5/PhysPreset/PhysPresetFields.h b/src/ObjCommon/Game/IW5/PhysPreset/PhysPresetFields.h new file mode 100644 index 00000000..b54e175f --- /dev/null +++ b/src/ObjCommon/Game/IW5/PhysPreset/PhysPresetFields.h @@ -0,0 +1,25 @@ +#pragma once + +#include "Game/IW5/IW5.h" + +namespace IW5 +{ + inline cspField_t phys_preset_fields[]{ + {"mass", offsetof(PhysPresetInfo, mass), CSPFT_FLOAT }, + {"bounce", offsetof(PhysPresetInfo, bounce), CSPFT_FLOAT }, + {"friction", offsetof(PhysPresetInfo, friction), CSPFT_FLOAT }, + {"bulletForceScale", offsetof(PhysPresetInfo, bulletForceScale), CSPFT_FLOAT }, + {"explosiveForceScale", offsetof(PhysPresetInfo, explosiveForceScale), CSPFT_FLOAT }, + {"sndAliasPrefix", offsetof(PhysPresetInfo, sndAliasPrefix), CSPFT_STRING }, + {"piecesSpreadFraction", offsetof(PhysPresetInfo, piecesSpreadFraction), CSPFT_FLOAT }, + {"piecesUpwardVelocity", offsetof(PhysPresetInfo, piecesUpwardVelocity), CSPFT_FLOAT }, + {"minMomentum", offsetof(PhysPresetInfo, minMomentum), CSPFT_FLOAT }, + {"maxMomentum", offsetof(PhysPresetInfo, maxMomentum), CSPFT_FLOAT }, + {"minPitch", offsetof(PhysPresetInfo, minPitch), CSPFT_FLOAT }, + {"maxPitch", offsetof(PhysPresetInfo, maxPitch), CSPFT_FLOAT }, + {"volumeType", offsetof(PhysPresetInfo, volumeType), PPFT_SCALING }, + {"pitchType", offsetof(PhysPresetInfo, pitchType), PPFT_SCALING }, + {"tempDefaultToCylinder", offsetof(PhysPresetInfo, tempDefaultToCylinder), CSPFT_QBOOLEAN}, + {"perSurfaceSndAlias", offsetof(PhysPresetInfo, perSurfaceSndAlias), CSPFT_QBOOLEAN}, + }; +} diff --git a/src/ObjCommon/Game/T5/ObjConstantsT5.h b/src/ObjCommon/Game/T5/ObjConstantsT5.h index e69de29b..0b073b48 100644 --- a/src/ObjCommon/Game/T5/ObjConstantsT5.h +++ b/src/ObjCommon/Game/T5/ObjConstantsT5.h @@ -0,0 +1,10 @@ +#pragma once + +namespace T5 +{ + static constexpr auto INFO_STRING_PREFIX_PHYS_PRESET = "PHYSIC"; + + static constexpr auto GDF_FILENAME_PHYS_PRESET = "physpreset.gdf"; + + static constexpr float MAX_FRICTION = 1e+10; +} // namespace T5 diff --git a/src/ObjCommon/Game/T5/PhysPreset/PhysPresetFields.h b/src/ObjCommon/Game/T5/PhysPreset/PhysPresetFields.h new file mode 100644 index 00000000..5a754168 --- /dev/null +++ b/src/ObjCommon/Game/T5/PhysPreset/PhysPresetFields.h @@ -0,0 +1,27 @@ +#pragma once +#include "Game/T5/T5.h" + +namespace T5 +{ + inline cspField_t phys_preset_fields[]{ + {"mass", offsetof(PhysPresetInfo, mass), CSPFT_FLOAT }, + {"bounce", offsetof(PhysPresetInfo, bounce), CSPFT_FLOAT }, + {"friction", offsetof(PhysPresetInfo, friction), CSPFT_FLOAT }, + {"isFrictionInfinity", offsetof(PhysPresetInfo, isFrictionInfinity), CSPFT_QBOOLEAN}, + {"bulletForceScale", offsetof(PhysPresetInfo, bulletForceScale), CSPFT_FLOAT }, + {"explosiveForceScale", offsetof(PhysPresetInfo, explosiveForceScale), CSPFT_FLOAT }, + {"piecesSpreadFraction", offsetof(PhysPresetInfo, piecesSpreadFraction), CSPFT_FLOAT }, + {"piecesUpwardVelocity", offsetof(PhysPresetInfo, piecesUpwardVelocity), CSPFT_FLOAT }, + {"canFloat", offsetof(PhysPresetInfo, canFloat), CSPFT_QBOOLEAN}, + {"gravityScale", offsetof(PhysPresetInfo, gravityScale), CSPFT_FLOAT }, + {"massOffsetX", offsetof(PhysPresetInfo, centerOfMassOffset.x), CSPFT_FLOAT }, + {"massOffsetY", offsetof(PhysPresetInfo, centerOfMassOffset.y), CSPFT_FLOAT }, + {"massOffsetZ", offsetof(PhysPresetInfo, centerOfMassOffset.z), CSPFT_FLOAT }, + {"buoyancyMinX", offsetof(PhysPresetInfo, buoyancyBoxMin.x), CSPFT_FLOAT }, + {"buoyancyMinY", offsetof(PhysPresetInfo, buoyancyBoxMin.y), CSPFT_FLOAT }, + {"buoyancyMinZ", offsetof(PhysPresetInfo, buoyancyBoxMin.z), CSPFT_FLOAT }, + {"buoyancyMaxX", offsetof(PhysPresetInfo, buoyancyBoxMax.x), CSPFT_FLOAT }, + {"buoyancyMaxY", offsetof(PhysPresetInfo, buoyancyBoxMax.y), CSPFT_FLOAT }, + {"buoyancyMaxZ", offsetof(PhysPresetInfo, buoyancyBoxMax.z), CSPFT_FLOAT }, + }; +} diff --git a/src/ObjLoading/Game/IW3/InfoString/InfoStringToStructConverter.cpp b/src/ObjLoading/Game/IW3/InfoString/InfoStringToStructConverter.cpp index e69de29b..8bc058a6 100644 --- a/src/ObjLoading/Game/IW3/InfoString/InfoStringToStructConverter.cpp +++ b/src/ObjLoading/Game/IW3/InfoString/InfoStringToStructConverter.cpp @@ -0,0 +1,169 @@ +#include "InfoStringToStructConverter.h" + +#include "Utils/Logging/Log.h" + +#include +#include +#include + +using namespace IW3; + +InfoStringToStructConverter::InfoStringToStructConverter(const InfoString& infoString, + void* structure, + ZoneScriptStrings& zoneScriptStrings, + MemoryManager& memory, + AssetCreationContext& context, + GenericAssetRegistration& registration, + const cspField_t* fields, + const size_t fieldCount) + : InfoStringToStructConverterBase(infoString, structure, zoneScriptStrings, memory, context, registration), + m_fields(fields), + m_field_count(fieldCount) +{ +} + +bool InfoStringToStructConverter::ConvertBaseField(const cspField_t& field, const std::string& value) +{ + switch (static_cast(field.iFieldType)) + { + case CSPFT_STRING: + return ConvertString(value, field.iOffset); + + case CSPFT_STRING_MAX_STRING_CHARS: + return ConvertStringBuffer(value, field.iOffset, 1024); + + case CSPFT_STRING_MAX_QPATH: + return ConvertStringBuffer(value, field.iOffset, 64); + + case CSPFT_STRING_MAX_OSPATH: + return ConvertStringBuffer(value, field.iOffset, 256); + + case CSPFT_INT: + return ConvertInt(value, field.iOffset); + + case CSPFT_QBOOLEAN: + return ConvertQBoolean(value, field.iOffset); + + case CSPFT_FLOAT: + return ConvertFloat(value, field.iOffset); + + case CSPFT_MILLISECONDS: + return ConvertMilliseconds(value, field.iOffset); + + case CSPFT_FX: + { + if (value.empty()) + { + *reinterpret_cast(reinterpret_cast(m_structure) + field.iOffset) = nullptr; + return true; + } + + auto* fx = m_context.LoadDependency(value); + + if (fx == nullptr) + { + con::error("Failed to load fx asset \"{}\"", value); + return false; + } + + m_registration.AddDependency(fx); + *reinterpret_cast(reinterpret_cast(m_structure) + field.iOffset) = fx->Asset(); + + return true; + } + + case CSPFT_XMODEL: + { + if (value.empty()) + { + *reinterpret_cast(reinterpret_cast(m_structure) + field.iOffset) = nullptr; + return true; + } + + auto* xmodel = m_context.LoadDependency(value); + + if (xmodel == nullptr) + { + con::error("Failed to load xmodel asset \"{}\"", value); + return false; + } + + m_registration.AddDependency(xmodel); + *reinterpret_cast(reinterpret_cast(m_structure) + field.iOffset) = xmodel->Asset(); + + return true; + } + + case CSPFT_MATERIAL: + { + if (value.empty()) + { + *reinterpret_cast(reinterpret_cast(m_structure) + field.iOffset) = nullptr; + return true; + } + + auto* material = m_context.LoadDependency(value); + + if (material == nullptr) + { + con::error("Failed to load material asset \"{}\"", value); + return false; + } + + m_registration.AddDependency(material); + *reinterpret_cast(reinterpret_cast(m_structure) + field.iOffset) = material->Asset(); + + return true; + } + + case CSPFT_SOUND: + { + if (value.empty()) + { + reinterpret_cast(reinterpret_cast(m_structure) + field.iOffset)->name = nullptr; + return true; + } + + auto* name = m_memory.Alloc(); + name->soundName = m_memory.Dup(value.c_str()); + + reinterpret_cast(reinterpret_cast(m_structure) + field.iOffset)->name = name; + + m_registration.AddIndirectAssetReference(m_context.LoadIndirectAssetReference(value)); + return true; + } + + case CSPFT_NUM_BASE_FIELD_TYPES: + default: + assert(false); + return false; + } +} + +bool InfoStringToStructConverter::Convert() +{ + for (auto fieldIndex = 0u; fieldIndex < m_field_count; fieldIndex++) + { + const auto& field = m_fields[fieldIndex]; + assert(field.iFieldType >= 0); + + auto foundValue = false; + const auto& value = m_info_string.GetValueForKey(std::string(field.szName), &foundValue); + + if (foundValue) + { + if (field.iFieldType < CSPFT_NUM_BASE_FIELD_TYPES) + { + if (!ConvertBaseField(field, value)) + return false; + } + else + { + if (!ConvertExtensionField(field, value)) + return false; + } + } + } + + return true; +} diff --git a/src/ObjLoading/Game/IW3/InfoString/InfoStringToStructConverter.h b/src/ObjLoading/Game/IW3/InfoString/InfoStringToStructConverter.h index e69de29b..11a80529 100644 --- a/src/ObjLoading/Game/IW3/InfoString/InfoStringToStructConverter.h +++ b/src/ObjLoading/Game/IW3/InfoString/InfoStringToStructConverter.h @@ -0,0 +1,28 @@ +#pragma once + +#include "Game/IW3/IW3.h" +#include "InfoString/InfoStringToStructConverterBase.h" + +namespace IW3 +{ + class InfoStringToStructConverter : public InfoStringToStructConverterBase + { + public: + InfoStringToStructConverter(const InfoString& infoString, + void* structure, + ZoneScriptStrings& zoneScriptStrings, + MemoryManager& memory, + AssetCreationContext& context, + GenericAssetRegistration& registration, + const cspField_t* fields, + size_t fieldCount); + bool Convert() override; + + protected: + virtual bool ConvertExtensionField(const cspField_t& field, const std::string& value) = 0; + bool ConvertBaseField(const cspField_t& field, const std::string& value); + + const cspField_t* m_fields; + size_t m_field_count; + }; +} // namespace IW3 diff --git a/src/ObjLoading/Game/IW3/ObjLoaderIW3.cpp b/src/ObjLoading/Game/IW3/ObjLoaderIW3.cpp index d8656beb..b36c4e5a 100644 --- a/src/ObjLoading/Game/IW3/ObjLoaderIW3.cpp +++ b/src/ObjLoading/Game/IW3/ObjLoaderIW3.cpp @@ -12,6 +12,8 @@ #include "Localize/AssetLoaderLocalizeIW3.h" #include "Material/LoaderMaterialIW3.h" #include "ObjLoading.h" +#include "PhysPreset/GdtLoaderPhysPresetIW3.h" +#include "PhysPreset/RawLoaderPhysPresetIW3.h" #include "RawFile/AssetLoaderRawFileIW3.h" #include "StringTable/AssetLoaderStringTableIW3.h" @@ -89,11 +91,12 @@ namespace collection.AddAssetCreator(std::make_unique>(zone)); } - void ConfigureLoaders(AssetCreatorCollection& collection, Zone& zone, ISearchPath& searchPath) + void ConfigureLoaders(AssetCreatorCollection& collection, Zone& zone, ISearchPath& searchPath, IGdtQueryable& gdt) { auto& memory = zone.Memory(); - // collection.AddAssetCreator(std::make_unique(memory)); + collection.AddAssetCreator(phys_preset::CreateRawLoaderIW3(memory, searchPath, zone)); + collection.AddAssetCreator(phys_preset::CreateGdtLoaderIW3(memory, gdt, zone)); // collection.AddAssetCreator(std::make_unique(memory)); collection.AddAssetCreator(xmodel::CreateLoaderIW3(memory, searchPath, zone)); collection.AddAssetCreator(material::CreateLoaderIW3(memory, searchPath)); @@ -129,6 +132,6 @@ namespace void ObjLoader::ConfigureCreatorCollection(AssetCreatorCollection& collection, Zone& zone, ISearchPath& searchPath, IGdtQueryable& gdt) const { ConfigureDefaultCreators(collection, zone); - ConfigureLoaders(collection, zone, searchPath); + ConfigureLoaders(collection, zone, searchPath, gdt); ConfigureGlobalAssetPoolsLoaders(collection, zone); } diff --git a/src/ObjLoading/Game/IW3/PhysPreset/GdtLoaderPhysPresetIW3.cpp b/src/ObjLoading/Game/IW3/PhysPreset/GdtLoaderPhysPresetIW3.cpp new file mode 100644 index 00000000..16fca79b --- /dev/null +++ b/src/ObjLoading/Game/IW3/PhysPreset/GdtLoaderPhysPresetIW3.cpp @@ -0,0 +1,53 @@ +#include "GdtLoaderPhysPresetIW3.h" + +#include "Game/IW3/IW3.h" +#include "Game/IW3/ObjConstantsIW3.h" +#include "InfoString/InfoString.h" +#include "InfoStringLoaderPhysPresetIW3.h" +#include "Utils/Logging/Log.h" + +#include +#include + +using namespace IW3; + +namespace +{ + class GdtLoaderPhysPreset final : public AssetCreator + { + public: + GdtLoaderPhysPreset(MemoryManager& memory, IGdtQueryable& gdt, Zone& zone) + : m_gdt(gdt), + m_info_string_loader(memory, zone) + { + } + + AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override + { + const auto* gdtEntry = m_gdt.GetGdtEntryByGdfAndName(GDF_FILENAME_PHYS_PRESET, assetName); + if (gdtEntry == nullptr) + return AssetCreationResult::NoAction(); + + InfoString infoString; + if (!infoString.FromGdtProperties(*gdtEntry)) + { + con::error("Failed to read phys preset gdt entry: \"{}\"", assetName); + return AssetCreationResult::Failure(); + } + + return m_info_string_loader.CreateAsset(assetName, infoString, context); + } + + private: + IGdtQueryable& m_gdt; + phys_preset::InfoStringLoaderIW3 m_info_string_loader; + }; +} // namespace + +namespace phys_preset +{ + std::unique_ptr> CreateGdtLoaderIW3(MemoryManager& memory, IGdtQueryable& gdt, Zone& zone) + { + return std::make_unique(memory, gdt, zone); + } +} // namespace phys_preset diff --git a/src/ObjLoading/Game/IW3/PhysPreset/GdtLoaderPhysPresetIW3.h b/src/ObjLoading/Game/IW3/PhysPreset/GdtLoaderPhysPresetIW3.h new file mode 100644 index 00000000..42bcb2e0 --- /dev/null +++ b/src/ObjLoading/Game/IW3/PhysPreset/GdtLoaderPhysPresetIW3.h @@ -0,0 +1,14 @@ +#pragma once + +#include "Asset/IAssetCreator.h" +#include "Game/IW3/IW3.h" +#include "Gdt/IGdtQueryable.h" +#include "SearchPath/ISearchPath.h" +#include "Utils/MemoryManager.h" + +#include + +namespace phys_preset +{ + std::unique_ptr> CreateGdtLoaderIW3(MemoryManager& memory, IGdtQueryable& gdt, Zone& zone); +} // namespace phys_preset diff --git a/src/ObjLoading/Game/IW3/PhysPreset/InfoStringLoaderPhysPresetIW3.cpp b/src/ObjLoading/Game/IW3/PhysPreset/InfoStringLoaderPhysPresetIW3.cpp new file mode 100644 index 00000000..540083ac --- /dev/null +++ b/src/ObjLoading/Game/IW3/PhysPreset/InfoStringLoaderPhysPresetIW3.cpp @@ -0,0 +1,90 @@ +#include "InfoStringLoaderPhysPresetIW3.h" + +#include "Game/IW3/IW3.h" +#include "Game/IW3/InfoString/InfoStringToStructConverter.h" +#include "Game/IW3/PhysPreset/PhysPresetFields.h" +#include "Utils/Logging/Log.h" + +#include +#include +#include +#include +#include +#include + +using namespace IW3; + +namespace +{ + class InfoStringToPhysPresetConverter final : public InfoStringToStructConverter + { + public: + InfoStringToPhysPresetConverter(const InfoString& infoString, + void* structure, + ZoneScriptStrings& zoneScriptStrings, + MemoryManager& memory, + AssetCreationContext& context, + GenericAssetRegistration& registration, + const cspField_t* fields, + size_t fieldCount) + : InfoStringToStructConverter(infoString, structure, zoneScriptStrings, memory, context, registration, fields, fieldCount) + { + } + + protected: + bool ConvertExtensionField(const cspField_t& field, const std::string& value) override + { + assert(false); + return false; + } + }; + + void CopyFromPhysPresetInfo(const PhysPresetInfo& physPresetInfo, PhysPreset& physPreset) + { + physPreset.mass = physPresetInfo.mass; + physPreset.bounce = physPresetInfo.bounce; + + if (physPresetInfo.isFrictionInfinity != 0) + physPreset.friction = std::numeric_limits::max(); + else + physPreset.friction = physPresetInfo.friction; + + physPreset.bulletForceScale = physPresetInfo.bulletForceScale; + physPreset.explosiveForceScale = physPresetInfo.explosiveForceScale; + physPreset.sndAliasPrefix = physPresetInfo.sndAliasPrefix; + physPreset.piecesSpreadFraction = physPresetInfo.piecesSpreadFraction; + physPreset.piecesUpwardVelocity = physPresetInfo.piecesUpwardVelocity; + physPreset.tempDefaultToCylinder = physPresetInfo.tempDefaultToCylinder != 0; + } +} // namespace + +namespace phys_preset +{ + InfoStringLoaderIW3::InfoStringLoaderIW3(MemoryManager& memory, Zone& zone) + : m_memory(memory), + m_zone(zone) + { + } + + AssetCreationResult InfoStringLoaderIW3::CreateAsset(const std::string& assetName, const InfoString& infoString, AssetCreationContext& context) + { + PhysPresetInfo presetInfo; + std::memset(&presetInfo, 0, sizeof(presetInfo)); + + auto* physPreset = m_memory.Alloc(); + AssetRegistration registration(assetName, physPreset); + + InfoStringToPhysPresetConverter converter( + infoString, &presetInfo, m_zone.m_script_strings, m_memory, context, registration, phys_preset_fields, std::extent_v); + if (!converter.Convert()) + { + con::error("Failed to parse phys preset: \"{}\"", assetName); + return AssetCreationResult::Failure(); + } + + CopyFromPhysPresetInfo(presetInfo, *physPreset); + physPreset->name = m_memory.Dup(assetName.c_str()); + + return AssetCreationResult::Success(context.AddAsset(std::move(registration))); + } +} // namespace phys_preset diff --git a/src/ObjLoading/Game/IW3/PhysPreset/InfoStringLoaderPhysPresetIW3.h b/src/ObjLoading/Game/IW3/PhysPreset/InfoStringLoaderPhysPresetIW3.h new file mode 100644 index 00000000..3a0c28ae --- /dev/null +++ b/src/ObjLoading/Game/IW3/PhysPreset/InfoStringLoaderPhysPresetIW3.h @@ -0,0 +1,20 @@ +#pragma once + +#include "Asset/AssetCreationContext.h" +#include "Asset/AssetCreationResult.h" +#include "InfoString/InfoString.h" + +namespace phys_preset +{ + class InfoStringLoaderIW3 + { + public: + InfoStringLoaderIW3(MemoryManager& memory, Zone& zone); + + AssetCreationResult CreateAsset(const std::string& assetName, const InfoString& infoString, AssetCreationContext& context); + + private: + MemoryManager& m_memory; + Zone& m_zone; + }; +} // namespace phys_preset diff --git a/src/ObjLoading/Game/IW3/PhysPreset/RawLoaderPhysPresetIW3.cpp b/src/ObjLoading/Game/IW3/PhysPreset/RawLoaderPhysPresetIW3.cpp new file mode 100644 index 00000000..7eb1a285 --- /dev/null +++ b/src/ObjLoading/Game/IW3/PhysPreset/RawLoaderPhysPresetIW3.cpp @@ -0,0 +1,55 @@ +#include "RawLoaderPhysPresetIW3.h" + +#include "Game/IW3/IW3.h" +#include "Game/IW3/ObjConstantsIW3.h" +#include "InfoString/InfoString.h" +#include "InfoStringLoaderPhysPresetIW3.h" +#include "PhysPreset/PhysPresetCommon.h" +#include "Utils/Logging/Log.h" + +#include +#include + +using namespace IW3; + +namespace +{ + class RawLoaderPhysPreset final : public AssetCreator + { + public: + RawLoaderPhysPreset(MemoryManager& memory, ISearchPath& searchPath, Zone& zone) + : m_search_path(searchPath), + m_info_string_loader(memory, zone) + { + } + + AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override + { + const auto fileName = phys_preset::GetFileNameForAssetName(assetName); + const auto file = m_search_path.Open(fileName); + if (!file.IsOpen()) + return AssetCreationResult::NoAction(); + + InfoString infoString; + if (!infoString.FromStream(INFO_STRING_PREFIX_PHYS_PRESET, *file.m_stream)) + { + con::error("Could not parse as info string file: \"{}\"", fileName); + return AssetCreationResult::Failure(); + } + + return m_info_string_loader.CreateAsset(assetName, infoString, context); + } + + private: + ISearchPath& m_search_path; + phys_preset::InfoStringLoaderIW3 m_info_string_loader; + }; +} // namespace + +namespace phys_preset +{ + std::unique_ptr> CreateRawLoaderIW3(MemoryManager& memory, ISearchPath& searchPath, Zone& zone) + { + return std::make_unique(memory, searchPath, zone); + } +} // namespace phys_preset diff --git a/src/ObjLoading/Game/IW3/PhysPreset/RawLoaderPhysPresetIW3.h b/src/ObjLoading/Game/IW3/PhysPreset/RawLoaderPhysPresetIW3.h new file mode 100644 index 00000000..49b22843 --- /dev/null +++ b/src/ObjLoading/Game/IW3/PhysPreset/RawLoaderPhysPresetIW3.h @@ -0,0 +1,13 @@ +#pragma once + +#include "Asset/IAssetCreator.h" +#include "Game/IW3/IW3.h" +#include "SearchPath/ISearchPath.h" +#include "Utils/MemoryManager.h" + +#include + +namespace phys_preset +{ + std::unique_ptr> CreateRawLoaderIW3(MemoryManager& memory, ISearchPath& searchPath, Zone& zone); +} // namespace phys_preset diff --git a/src/ObjLoading/Game/IW5/ObjLoaderIW5.cpp b/src/ObjLoading/Game/IW5/ObjLoaderIW5.cpp index 59a936c8..8023011f 100644 --- a/src/ObjLoading/Game/IW5/ObjLoaderIW5.cpp +++ b/src/ObjLoading/Game/IW5/ObjLoaderIW5.cpp @@ -14,6 +14,8 @@ #include "Material/LoaderMaterialIW5.h" #include "Menu/LoaderMenuListIW5.h" #include "ObjLoading.h" +#include "PhysPreset/GdtLoaderPhysPresetIW5.h" +#include "PhysPreset/RawLoaderPhysPresetIW5.h" #include "RawFile/LoaderRawFileIW5.h" #include "Script/LoaderScriptFileIW5.h" #include "StringTable/LoaderStringTableIW5.h" @@ -125,7 +127,8 @@ namespace { auto& memory = zone.Memory(); - // collection.AddAssetCreator(std::make_unique(memory)); + collection.AddAssetCreator(phys_preset::CreateRawLoaderIW5(memory, searchPath, zone)); + collection.AddAssetCreator(phys_preset::CreateGdtLoaderIW5(memory, gdt, zone)); // collection.AddAssetCreator(std::make_unique(memory)); // collection.AddAssetCreator(std::make_unique(memory)); // collection.AddAssetCreator(std::make_unique(memory)); diff --git a/src/ObjLoading/Game/IW5/PhysPreset/GdtLoaderPhysPresetIW5.cpp b/src/ObjLoading/Game/IW5/PhysPreset/GdtLoaderPhysPresetIW5.cpp new file mode 100644 index 00000000..1275aa64 --- /dev/null +++ b/src/ObjLoading/Game/IW5/PhysPreset/GdtLoaderPhysPresetIW5.cpp @@ -0,0 +1,53 @@ +#include "GdtLoaderPhysPresetIW5.h" + +#include "Game/IW5/IW5.h" +#include "Game/IW5/ObjConstantsIW5.h" +#include "InfoString/InfoString.h" +#include "InfoStringLoaderPhysPresetIW5.h" +#include "Utils/Logging/Log.h" + +#include +#include + +using namespace IW5; + +namespace +{ + class GdtLoaderPhysPreset final : public AssetCreator + { + public: + GdtLoaderPhysPreset(MemoryManager& memory, IGdtQueryable& gdt, Zone& zone) + : m_gdt(gdt), + m_info_string_loader(memory, zone) + { + } + + AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override + { + const auto* gdtEntry = m_gdt.GetGdtEntryByGdfAndName(GDF_FILENAME_PHYS_PRESET, assetName); + if (gdtEntry == nullptr) + return AssetCreationResult::NoAction(); + + InfoString infoString; + if (!infoString.FromGdtProperties(*gdtEntry)) + { + con::error("Failed to read phys preset gdt entry: \"{}\"", assetName); + return AssetCreationResult::Failure(); + } + + return m_info_string_loader.CreateAsset(assetName, infoString, context); + } + + private: + IGdtQueryable& m_gdt; + phys_preset::InfoStringLoaderIW5 m_info_string_loader; + }; +} // namespace + +namespace phys_preset +{ + std::unique_ptr> CreateGdtLoaderIW5(MemoryManager& memory, IGdtQueryable& gdt, Zone& zone) + { + return std::make_unique(memory, gdt, zone); + } +} // namespace phys_preset diff --git a/src/ObjLoading/Game/IW5/PhysPreset/GdtLoaderPhysPresetIW5.h b/src/ObjLoading/Game/IW5/PhysPreset/GdtLoaderPhysPresetIW5.h new file mode 100644 index 00000000..b4f68a8b --- /dev/null +++ b/src/ObjLoading/Game/IW5/PhysPreset/GdtLoaderPhysPresetIW5.h @@ -0,0 +1,14 @@ +#pragma once + +#include "Asset/IAssetCreator.h" +#include "Game/IW5/IW5.h" +#include "Gdt/IGdtQueryable.h" +#include "SearchPath/ISearchPath.h" +#include "Utils/MemoryManager.h" + +#include + +namespace phys_preset +{ + std::unique_ptr> CreateGdtLoaderIW5(MemoryManager& memory, IGdtQueryable& gdt, Zone& zone); +} // namespace phys_preset diff --git a/src/ObjLoading/Game/IW5/PhysPreset/InfoStringLoaderPhysPresetIW5.cpp b/src/ObjLoading/Game/IW5/PhysPreset/InfoStringLoaderPhysPresetIW5.cpp new file mode 100644 index 00000000..dcad4533 --- /dev/null +++ b/src/ObjLoading/Game/IW5/PhysPreset/InfoStringLoaderPhysPresetIW5.cpp @@ -0,0 +1,97 @@ +#include "InfoStringLoaderPhysPresetIW5.h" + +#include "Game/IW5/IW5.h" +#include "Game/IW5/InfoString/EnumStrings.h" +#include "Game/IW5/InfoString/InfoStringToStructConverter.h" +#include "Game/IW5/PhysPreset/PhysPresetFields.h" +#include "Utils/Logging/Log.h" + +#include +#include +#include + +using namespace IW5; + +namespace +{ + class InfoStringToPhysPresetConverter final : public InfoStringToStructConverter + { + public: + InfoStringToPhysPresetConverter(const InfoString& infoString, + void* structure, + ZoneScriptStrings& zoneScriptStrings, + MemoryManager& memory, + AssetCreationContext& context, + GenericAssetRegistration& registration, + const cspField_t* fields, + size_t fieldCount) + : InfoStringToStructConverter(infoString, structure, zoneScriptStrings, memory, context, registration, fields, fieldCount) + { + } + + protected: + bool ConvertExtensionField(const cspField_t& field, const std::string& value) override + { + switch (static_cast(field.iFieldType)) + { + case PPFT_SCALING: + return ConvertEnumInt(field.szName, value, field.iOffset, szPhysPresetScalingNames, std::extent_v); + + default: + assert(false); + return false; + } + } + }; + + void CopyFromPhysPresetInfo(const PhysPresetInfo& physPresetInfo, PhysPreset& physPreset) + { + physPreset.mass = physPresetInfo.mass; + physPreset.bounce = physPresetInfo.bounce; + physPreset.friction = physPresetInfo.friction; + physPreset.bulletForceScale = physPresetInfo.bulletForceScale; + physPreset.explosiveForceScale = physPresetInfo.explosiveForceScale; + physPreset.sndAliasPrefix = physPresetInfo.sndAliasPrefix; + physPreset.piecesSpreadFraction = physPresetInfo.piecesSpreadFraction; + physPreset.piecesUpwardVelocity = physPresetInfo.piecesUpwardVelocity; + physPreset.minMomentum = physPresetInfo.minMomentum; + physPreset.maxMomentum = physPresetInfo.maxMomentum; + physPreset.minPitch = physPresetInfo.minPitch; + physPreset.maxPitch = physPresetInfo.maxPitch; + physPreset.volumeType = physPresetInfo.volumeType; + physPreset.pitchType = physPresetInfo.pitchType; + physPreset.tempDefaultToCylinder = physPresetInfo.tempDefaultToCylinder != 0; + physPreset.perSurfaceSndAlias = physPresetInfo.perSurfaceSndAlias != 0; + } +} // namespace + +namespace phys_preset +{ + InfoStringLoaderIW5::InfoStringLoaderIW5(MemoryManager& memory, Zone& zone) + : m_memory(memory), + m_zone(zone) + { + } + + AssetCreationResult InfoStringLoaderIW5::CreateAsset(const std::string& assetName, const InfoString& infoString, AssetCreationContext& context) + { + PhysPresetInfo presetInfo; + std::memset(&presetInfo, 0, sizeof(presetInfo)); + + auto* physPreset = m_memory.Alloc(); + AssetRegistration registration(assetName, physPreset); + + InfoStringToPhysPresetConverter converter( + infoString, &presetInfo, m_zone.m_script_strings, m_memory, context, registration, phys_preset_fields, std::extent_v); + if (!converter.Convert()) + { + con::error("Failed to parse phys preset: \"{}\"", assetName); + return AssetCreationResult::Failure(); + } + + CopyFromPhysPresetInfo(presetInfo, *physPreset); + physPreset->name = m_memory.Dup(assetName.c_str()); + + return AssetCreationResult::Success(context.AddAsset(std::move(registration))); + } +} // namespace phys_preset diff --git a/src/ObjLoading/Game/IW5/PhysPreset/InfoStringLoaderPhysPresetIW5.h b/src/ObjLoading/Game/IW5/PhysPreset/InfoStringLoaderPhysPresetIW5.h new file mode 100644 index 00000000..393763dc --- /dev/null +++ b/src/ObjLoading/Game/IW5/PhysPreset/InfoStringLoaderPhysPresetIW5.h @@ -0,0 +1,20 @@ +#pragma once + +#include "Asset/AssetCreationContext.h" +#include "Asset/AssetCreationResult.h" +#include "InfoString/InfoString.h" + +namespace phys_preset +{ + class InfoStringLoaderIW5 + { + public: + InfoStringLoaderIW5(MemoryManager& memory, Zone& zone); + + AssetCreationResult CreateAsset(const std::string& assetName, const InfoString& infoString, AssetCreationContext& context); + + private: + MemoryManager& m_memory; + Zone& m_zone; + }; +} // namespace phys_preset diff --git a/src/ObjLoading/Game/IW5/PhysPreset/RawLoaderPhysPresetIW5.cpp b/src/ObjLoading/Game/IW5/PhysPreset/RawLoaderPhysPresetIW5.cpp new file mode 100644 index 00000000..d9d7506f --- /dev/null +++ b/src/ObjLoading/Game/IW5/PhysPreset/RawLoaderPhysPresetIW5.cpp @@ -0,0 +1,52 @@ +#include "RawLoaderPhysPresetIW5.h" + +#include "Game/IW5/IW5.h" +#include "Game/IW5/ObjConstantsIW5.h" +#include "InfoString/InfoString.h" +#include "InfoStringLoaderPhysPresetIW5.h" +#include "PhysPreset/PhysPresetCommon.h" +#include "Utils/Logging/Log.h" + +using namespace IW5; + +namespace +{ + class RawLoaderPhysPreset final : public AssetCreator + { + public: + RawLoaderPhysPreset(MemoryManager& memory, ISearchPath& searchPath, Zone& zone) + : m_search_path(searchPath), + m_info_string_loader(memory, zone) + { + } + + AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override + { + const auto fileName = phys_preset::GetFileNameForAssetName(assetName); + const auto file = m_search_path.Open(fileName); + if (!file.IsOpen()) + return AssetCreationResult::NoAction(); + + InfoString infoString; + if (!infoString.FromStream(INFO_STRING_PREFIX_PHYS_PRESET, *file.m_stream)) + { + con::error("Could not parse as info string file: \"{}\"", fileName); + return AssetCreationResult::Failure(); + } + + return m_info_string_loader.CreateAsset(assetName, infoString, context); + } + + private: + ISearchPath& m_search_path; + phys_preset::InfoStringLoaderIW5 m_info_string_loader; + }; +} // namespace + +namespace phys_preset +{ + std::unique_ptr> CreateRawLoaderIW5(MemoryManager& memory, ISearchPath& searchPath, Zone& zone) + { + return std::make_unique(memory, searchPath, zone); + } +} // namespace phys_preset diff --git a/src/ObjLoading/Game/IW5/PhysPreset/RawLoaderPhysPresetIW5.h b/src/ObjLoading/Game/IW5/PhysPreset/RawLoaderPhysPresetIW5.h new file mode 100644 index 00000000..42bca424 --- /dev/null +++ b/src/ObjLoading/Game/IW5/PhysPreset/RawLoaderPhysPresetIW5.h @@ -0,0 +1,13 @@ +#pragma once + +#include "Asset/IAssetCreator.h" +#include "Game/IW5/IW5.h" +#include "SearchPath/ISearchPath.h" +#include "Utils/MemoryManager.h" + +#include + +namespace phys_preset +{ + std::unique_ptr> CreateRawLoaderIW5(MemoryManager& memory, ISearchPath& searchPath, Zone& zone); +} // namespace phys_preset diff --git a/src/ObjLoading/Game/T5/InfoString/InfoStringToStructConverter.cpp b/src/ObjLoading/Game/T5/InfoString/InfoStringToStructConverter.cpp new file mode 100644 index 00000000..4c21539e --- /dev/null +++ b/src/ObjLoading/Game/T5/InfoString/InfoStringToStructConverter.cpp @@ -0,0 +1,181 @@ +#include "InfoStringToStructConverter.h" + +#include "Game/T5/CommonT5.h" +#include "Utils/Logging/Log.h" + +#include +#include + +using namespace T5; + +InfoStringToStructConverter::InfoStringToStructConverter(const InfoString& infoString, + void* structure, + ZoneScriptStrings& zoneScriptStrings, + MemoryManager& memory, + AssetCreationContext& context, + GenericAssetRegistration& registration, + const cspField_t* fields, + const size_t fieldCount) + : InfoStringToStructConverterBase(infoString, structure, zoneScriptStrings, memory, context, registration), + m_fields(fields), + m_field_count(fieldCount) +{ +} + +bool InfoStringToStructConverter::ConvertBaseField(const cspField_t& field, const std::string& value) +{ + switch (static_cast(field.iFieldType)) + { + case CSPFT_STRING: + return ConvertString(value, field.iOffset); + + case CSPFT_STRING_MAX_STRING_CHARS: + return ConvertStringBuffer(value, field.iOffset, 1024); + + case CSPFT_STRING_MAX_QPATH: + return ConvertStringBuffer(value, field.iOffset, 64); + + case CSPFT_STRING_MAX_OSPATH: + return ConvertStringBuffer(value, field.iOffset, 256); + + case CSPFT_INT: + return ConvertInt(value, field.iOffset); + + case CSPFT_BOOL: + return ConvertBool(value, field.iOffset); + + case CSPFT_QBOOLEAN: + return ConvertQBoolean(value, field.iOffset); + + case CSPFT_FLOAT: + return ConvertFloat(value, field.iOffset); + + case CSPFT_MILLISECONDS: + return ConvertMilliseconds(value, field.iOffset); + + case CSPFT_FX: + { + if (value.empty()) + { + *reinterpret_cast(reinterpret_cast(m_structure) + field.iOffset) = nullptr; + return true; + } + + auto* fx = m_context.LoadDependency(value); + + if (fx == nullptr) + { + con::error("Failed to load fx asset \"{}\"", value); + return false; + } + + m_registration.AddDependency(fx); + *reinterpret_cast(reinterpret_cast(m_structure) + field.iOffset) = fx->Asset(); + + return true; + } + + case CSPFT_XMODEL: + { + if (value.empty()) + { + *reinterpret_cast(reinterpret_cast(m_structure) + field.iOffset) = nullptr; + return true; + } + + auto* xmodel = m_context.LoadDependency(value); + + if (xmodel == nullptr) + { + con::error("Failed to load xmodel asset \"{}\"", value); + return false; + } + + m_registration.AddDependency(xmodel); + *reinterpret_cast(reinterpret_cast(m_structure) + field.iOffset) = xmodel->Asset(); + + return true; + } + + case CSPFT_MATERIAL: + case CSPFT_MATERIAL_STREAM: + { + if (value.empty()) + { + *reinterpret_cast(reinterpret_cast(m_structure) + field.iOffset) = nullptr; + return true; + } + + auto* material = m_context.LoadDependency(value); + + if (material == nullptr) + { + con::error("Failed to load material asset \"{}\"", value); + return false; + } + + m_registration.AddDependency(material); + *reinterpret_cast(reinterpret_cast(m_structure) + field.iOffset) = material->Asset(); + + return true; + } + + case CSPFT_PHYS_PRESET: + { + if (value.empty()) + { + *reinterpret_cast(reinterpret_cast(m_structure) + field.iOffset) = nullptr; + return true; + } + + auto* physPreset = m_context.LoadDependency(value); + + if (physPreset == nullptr) + { + con::error("Failed to load physpreset asset \"{}\"", value); + return false; + } + + m_registration.AddDependency(physPreset); + *reinterpret_cast(reinterpret_cast(m_structure) + field.iOffset) = physPreset->Asset(); + + return true; + } + + case CSPFT_SCRIPT_STRING: + return ConvertScriptString(value, field.iOffset); + + case CSPFT_NUM_BASE_FIELD_TYPES: + default: + assert(false); + return false; + } +} + +bool InfoStringToStructConverter::Convert() +{ + for (auto fieldIndex = 0u; fieldIndex < m_field_count; fieldIndex++) + { + const auto& field = m_fields[fieldIndex]; + assert(field.iFieldType >= 0); + + auto foundValue = false; + const auto& value = m_info_string.GetValueForKey(std::string(field.szName), &foundValue); + + if (foundValue) + { + if (field.iFieldType < CSPFT_NUM_BASE_FIELD_TYPES) + { + if (!ConvertBaseField(field, value)) + return false; + } + else + { + if (!ConvertExtensionField(field, value)) + return false; + } + } + } + + return true; +} diff --git a/src/ObjLoading/Game/T5/InfoString/InfoStringToStructConverter.h b/src/ObjLoading/Game/T5/InfoString/InfoStringToStructConverter.h new file mode 100644 index 00000000..065a80da --- /dev/null +++ b/src/ObjLoading/Game/T5/InfoString/InfoStringToStructConverter.h @@ -0,0 +1,28 @@ +#pragma once + +#include "Game/T5/T5.h" +#include "InfoString/InfoStringToStructConverterBase.h" + +namespace T5 +{ + class InfoStringToStructConverter : public InfoStringToStructConverterBase + { + public: + InfoStringToStructConverter(const InfoString& infoString, + void* structure, + ZoneScriptStrings& zoneScriptStrings, + MemoryManager& memory, + AssetCreationContext& context, + GenericAssetRegistration& registration, + const cspField_t* fields, + size_t fieldCount); + bool Convert() override; + + protected: + virtual bool ConvertExtensionField(const cspField_t& field, const std::string& value) = 0; + bool ConvertBaseField(const cspField_t& field, const std::string& value); + + const cspField_t* m_fields; + size_t m_field_count; + }; +} // namespace T5 diff --git a/src/ObjLoading/Game/T5/ObjLoaderT5.cpp b/src/ObjLoading/Game/T5/ObjLoaderT5.cpp index b50a9a53..2c72f5c1 100644 --- a/src/ObjLoading/Game/T5/ObjLoaderT5.cpp +++ b/src/ObjLoading/Game/T5/ObjLoaderT5.cpp @@ -12,6 +12,8 @@ #include "Localize/LoaderLocalizeT5.h" #include "Material/LoaderMaterialT5.h" #include "ObjLoading.h" +#include "PhysPreset/GdtLoaderPhysPresetT5.h" +#include "PhysPreset/RawLoaderPhysPresetT5.h" #include "RawFile/LoaderRawFileT5.h" #include "StringTable/LoaderStringTableT5.h" @@ -101,11 +103,12 @@ namespace collection.AddAssetCreator(std::make_unique>(zone)); } - void ConfigureLoaders(AssetCreatorCollection& collection, Zone& zone, ISearchPath& searchPath) + void ConfigureLoaders(AssetCreatorCollection& collection, Zone& zone, ISearchPath& searchPath, IGdtQueryable& gdt) { auto& memory = zone.Memory(); - // collection.AddAssetCreator(std::make_unique(memory)); + collection.AddAssetCreator(phys_preset::CreateRawLoaderT5(memory, searchPath, zone)); + collection.AddAssetCreator(phys_preset::CreateGdtLoaderT5(memory, gdt, zone)); // collection.AddAssetCreator(std::make_unique(memory)); // collection.AddAssetCreator(std::make_unique(memory)); // collection.AddAssetCreator(std::make_unique(memory)); @@ -147,6 +150,6 @@ namespace void ObjLoader::ConfigureCreatorCollection(AssetCreatorCollection& collection, Zone& zone, ISearchPath& searchPath, IGdtQueryable& gdt) const { ConfigureDefaultCreators(collection, zone); - ConfigureLoaders(collection, zone, searchPath); + ConfigureLoaders(collection, zone, searchPath, gdt); ConfigureGlobalAssetPoolsLoaders(collection, zone); } diff --git a/src/ObjLoading/Game/T5/PhysPreset/GdtLoaderPhysPresetT5.cpp b/src/ObjLoading/Game/T5/PhysPreset/GdtLoaderPhysPresetT5.cpp new file mode 100644 index 00000000..b0412494 --- /dev/null +++ b/src/ObjLoading/Game/T5/PhysPreset/GdtLoaderPhysPresetT5.cpp @@ -0,0 +1,54 @@ +#include "GdtLoaderPhysPresetT5.h" + +#include "Game/T5/ObjConstantsT5.h" +#include "Game/T5/T5.h" +#include "InfoString/InfoString.h" +#include "InfoStringLoaderPhysPresetT5.h" +#include "Utils/Logging/Log.h" + +#include +#include +#include + +using namespace T5; + +namespace +{ + class GdtLoaderPhysPreset final : public AssetCreator + { + public: + GdtLoaderPhysPreset(MemoryManager& memory, IGdtQueryable& gdt, Zone& zone) + : m_gdt(gdt), + m_info_string_loader(memory, zone) + { + } + + AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override + { + const auto* gdtEntry = m_gdt.GetGdtEntryByGdfAndName(GDF_FILENAME_PHYS_PRESET, assetName); + if (gdtEntry == nullptr) + return AssetCreationResult::NoAction(); + + InfoString infoString; + if (!infoString.FromGdtProperties(*gdtEntry)) + { + con::error("Failed to read phys preset gdt entry: \"{}\"", assetName); + return AssetCreationResult::Failure(); + } + + return m_info_string_loader.CreateAsset(assetName, infoString, context); + } + + private: + IGdtQueryable& m_gdt; + phys_preset::InfoStringLoaderT5 m_info_string_loader; + }; +} // namespace + +namespace phys_preset +{ + std::unique_ptr> CreateGdtLoaderT5(MemoryManager& memory, IGdtQueryable& gdt, Zone& zone) + { + return std::make_unique(memory, gdt, zone); + } +} // namespace phys_preset diff --git a/src/ObjLoading/Game/T5/PhysPreset/GdtLoaderPhysPresetT5.h b/src/ObjLoading/Game/T5/PhysPreset/GdtLoaderPhysPresetT5.h new file mode 100644 index 00000000..77eee81c --- /dev/null +++ b/src/ObjLoading/Game/T5/PhysPreset/GdtLoaderPhysPresetT5.h @@ -0,0 +1,14 @@ +#pragma once + +#include "Asset/IAssetCreator.h" +#include "Game/T5/T5.h" +#include "Gdt/IGdtQueryable.h" +#include "SearchPath/ISearchPath.h" +#include "Utils/MemoryManager.h" + +#include + +namespace phys_preset +{ + std::unique_ptr> CreateGdtLoaderT5(MemoryManager& memory, IGdtQueryable& gdt, Zone& zone); +} // namespace phys_preset diff --git a/src/ObjLoading/Game/T5/PhysPreset/InfoStringLoaderPhysPresetT5.cpp b/src/ObjLoading/Game/T5/PhysPreset/InfoStringLoaderPhysPresetT5.cpp new file mode 100644 index 00000000..42c1d22d --- /dev/null +++ b/src/ObjLoading/Game/T5/PhysPreset/InfoStringLoaderPhysPresetT5.cpp @@ -0,0 +1,100 @@ +#include "InfoStringLoaderPhysPresetT5.h" + +#include "Game/T5/InfoString/InfoStringToStructConverter.h" +#include "Game/T5/ObjConstantsT5.h" +#include "Game/T5/PhysPreset/PhysPresetFields.h" +#include "Game/T5/T5.h" +#include "Utils/Logging/Log.h" + +#include +#include +#include +#include +#include +#include + +using namespace T5; + +namespace +{ + class InfoStringToPhysPresetConverter final : public InfoStringToStructConverter + { + protected: + bool ConvertExtensionField(const cspField_t& field, const std::string& value) override + { + assert(false); + return false; + } + + public: + InfoStringToPhysPresetConverter(const InfoString& infoString, + PhysPresetInfo& physPreset, + ZoneScriptStrings& zoneScriptStrings, + MemoryManager& memory, + AssetCreationContext& context, + AssetRegistration& registration, + const cspField_t* fields, + const size_t fieldCount) + : InfoStringToStructConverter(infoString, &physPreset, zoneScriptStrings, memory, context, registration, fields, fieldCount) + { + } + }; + + void CopyFromPhysPresetInfo(const PhysPresetInfo& physPresetInfo, PhysPreset& physPreset) + { + physPreset.mass = std::clamp(physPresetInfo.mass, 1.0f, 2000.0f) * 0.001f; + physPreset.bounce = physPresetInfo.bounce; + + if (physPresetInfo.isFrictionInfinity != 0) + physPreset.friction = MAX_FRICTION; + else + physPreset.friction = physPresetInfo.friction; + + physPreset.bulletForceScale = physPresetInfo.bulletForceScale; + physPreset.explosiveForceScale = physPresetInfo.explosiveForceScale; + physPreset.piecesSpreadFraction = physPresetInfo.piecesSpreadFraction; + physPreset.piecesUpwardVelocity = physPresetInfo.piecesUpwardVelocity; + physPreset.canFloat = physPresetInfo.canFloat; + physPreset.gravityScale = std::clamp(physPresetInfo.gravityScale, 0.01f, 10.0f); + physPreset.centerOfMassOffset = physPresetInfo.centerOfMassOffset; + physPreset.buoyancyBoxMin = physPresetInfo.buoyancyBoxMin; + physPreset.buoyancyBoxMax = physPresetInfo.buoyancyBoxMax; + } +} // namespace + +namespace phys_preset +{ + InfoStringLoaderT5::InfoStringLoaderT5(MemoryManager& memory, Zone& zone) + : m_memory(memory), + m_zone(zone) + { + } + + AssetCreationResult InfoStringLoaderT5::CreateAsset(const std::string& assetName, const InfoString& infoString, AssetCreationContext& context) + { + auto* physPreset = m_memory.Alloc(); + physPreset->name = m_memory.Dup(assetName.c_str()); + + AssetRegistration registration(assetName, physPreset); + + PhysPresetInfo physPresetInfo; + memset(&physPresetInfo, 0, sizeof(physPresetInfo)); + InfoStringToPhysPresetConverter converter(infoString, + physPresetInfo, + m_zone.m_script_strings, + m_memory, + context, + registration, + phys_preset_fields, + std::extent_v); + if (!converter.Convert()) + { + con::error("Failed to parse phys preset: \"{}\"", assetName); + return AssetCreationResult::Failure(); + } + + CopyFromPhysPresetInfo(physPresetInfo, *physPreset); + + return AssetCreationResult::Success(context.AddAsset(std::move(registration))); + } +} // namespace phys_preset diff --git a/src/ObjLoading/Game/T5/PhysPreset/InfoStringLoaderPhysPresetT5.h b/src/ObjLoading/Game/T5/PhysPreset/InfoStringLoaderPhysPresetT5.h new file mode 100644 index 00000000..0c96ff4e --- /dev/null +++ b/src/ObjLoading/Game/T5/PhysPreset/InfoStringLoaderPhysPresetT5.h @@ -0,0 +1,20 @@ +#pragma once + +#include "Asset/AssetCreationContext.h" +#include "Asset/AssetCreationResult.h" +#include "InfoString/InfoString.h" + +namespace phys_preset +{ + class InfoStringLoaderT5 + { + public: + InfoStringLoaderT5(MemoryManager& memory, Zone& zone); + + AssetCreationResult CreateAsset(const std::string& assetName, const InfoString& infoString, AssetCreationContext& context); + + private: + MemoryManager& m_memory; + Zone& m_zone; + }; +} // namespace phys_preset diff --git a/src/ObjLoading/Game/T5/PhysPreset/RawLoaderPhysPresetT5.cpp b/src/ObjLoading/Game/T5/PhysPreset/RawLoaderPhysPresetT5.cpp new file mode 100644 index 00000000..2eedb60e --- /dev/null +++ b/src/ObjLoading/Game/T5/PhysPreset/RawLoaderPhysPresetT5.cpp @@ -0,0 +1,56 @@ +#include "RawLoaderPhysPresetT5.h" + +#include "Game/T5/ObjConstantsT5.h" +#include "Game/T5/T5.h" +#include "InfoString/InfoString.h" +#include "InfoStringLoaderPhysPresetT5.h" +#include "PhysPreset/PhysPresetCommon.h" +#include "Utils/Logging/Log.h" + +#include +#include +#include + +using namespace T5; + +namespace +{ + class RawLoaderPhysPreset final : public AssetCreator + { + public: + RawLoaderPhysPreset(MemoryManager& memory, ISearchPath& searchPath, Zone& zone) + : m_search_path(searchPath), + m_info_string_loader(memory, zone) + { + } + + AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override + { + const auto fileName = phys_preset::GetFileNameForAssetName(assetName); + const auto file = m_search_path.Open(fileName); + if (!file.IsOpen()) + return AssetCreationResult::NoAction(); + + InfoString infoString; + if (!infoString.FromStream(INFO_STRING_PREFIX_PHYS_PRESET, *file.m_stream)) + { + con::error("Could not parse as info string file: \"{}\"", fileName); + return AssetCreationResult::Failure(); + } + + return m_info_string_loader.CreateAsset(assetName, infoString, context); + } + + private: + ISearchPath& m_search_path; + phys_preset::InfoStringLoaderT5 m_info_string_loader; + }; +} // namespace + +namespace phys_preset +{ + std::unique_ptr> CreateRawLoaderT5(MemoryManager& memory, ISearchPath& searchPath, Zone& zone) + { + return std::make_unique(memory, searchPath, zone); + } +} // namespace phys_preset diff --git a/src/ObjLoading/Game/T5/PhysPreset/RawLoaderPhysPresetT5.h b/src/ObjLoading/Game/T5/PhysPreset/RawLoaderPhysPresetT5.h new file mode 100644 index 00000000..1ad2db7d --- /dev/null +++ b/src/ObjLoading/Game/T5/PhysPreset/RawLoaderPhysPresetT5.h @@ -0,0 +1,13 @@ +#pragma once + +#include "Asset/IAssetCreator.h" +#include "Game/T5/T5.h" +#include "SearchPath/ISearchPath.h" +#include "Utils/MemoryManager.h" + +#include + +namespace phys_preset +{ + std::unique_ptr> CreateRawLoaderT5(MemoryManager& memory, ISearchPath& searchPath, Zone& zone); +} // namespace phys_preset diff --git a/src/ObjWriting/Game/IW5/ObjWriterIW5.cpp b/src/ObjWriting/Game/IW5/ObjWriterIW5.cpp index 74a3bcdd..d720cecd 100644 --- a/src/ObjWriting/Game/IW5/ObjWriterIW5.cpp +++ b/src/ObjWriting/Game/IW5/ObjWriterIW5.cpp @@ -11,6 +11,7 @@ #include "Maps/AddonMapEntsDumperIW5.h" #include "Menu/MenuDumperIW5.h" #include "Menu/MenuListDumperIW5.h" +#include "PhysPreset/PhysPresetInfoStringDumperIW5.h" #include "RawFile/RawFileDumperIW5.h" #include "Script/ScriptDumperIW5.h" #include "Sound/LoadedSoundDumperIW5.h" @@ -22,7 +23,7 @@ using namespace IW5; void ObjWriter::RegisterAssetDumpers(AssetDumpingContext& context) { - // REGISTER_DUMPER(AssetDumperPhysPreset) + RegisterAssetDumper(std::make_unique()); // REGISTER_DUMPER(AssetDumperPhysCollmap) // REGISTER_DUMPER(AssetDumperXAnimParts) // REGISTER_DUMPER(AssetDumperXModelSurfs) diff --git a/src/ObjWriting/Game/IW5/PhysPreset/PhysPresetInfoStringDumperIW5.cpp b/src/ObjWriting/Game/IW5/PhysPreset/PhysPresetInfoStringDumperIW5.cpp new file mode 100644 index 00000000..fa0a77d4 --- /dev/null +++ b/src/ObjWriting/Game/IW5/PhysPreset/PhysPresetInfoStringDumperIW5.cpp @@ -0,0 +1,112 @@ +#include "PhysPresetInfoStringDumperIW5.h" + +#include "Game/IW5/InfoString/EnumStrings.h" +#include "Game/IW5/InfoString/InfoStringFromStructConverter.h" +#include "Game/IW5/ObjConstantsIW5.h" +#include "Game/IW5/PhysPreset/PhysPresetFields.h" +#include "PhysPreset/PhysPresetCommon.h" + +#include +#include +#include +#include + +using namespace IW5; + +namespace +{ + class InfoStringFromPhysPresetConverter final : public InfoStringFromStructConverter + { + protected: + void FillFromExtensionField(const cspField_t& field) override + { + switch (static_cast(field.iFieldType)) + { + case PPFT_SCALING: + FillFromEnumInt(std::string(field.szName), field.iOffset, szPhysPresetScalingNames, std::extent_v); + break; + + case PPFT_NUM_FIELD_TYPES: + default: + assert(false); + break; + } + } + + public: + InfoStringFromPhysPresetConverter(const PhysPresetInfo* structure, + const cspField_t* fields, + const size_t fieldCount, + std::function scriptStringValueCallback) + : InfoStringFromStructConverter(structure, fields, fieldCount, std::move(scriptStringValueCallback)) + { + } + }; + + void CopyToPhysPresetInfo(const PhysPreset* physPreset, PhysPresetInfo* physPresetInfo) + { + physPresetInfo->mass = physPreset->mass; + physPresetInfo->bounce = physPreset->bounce; + physPresetInfo->friction = physPreset->friction; + physPresetInfo->bulletForceScale = physPreset->bulletForceScale; + physPresetInfo->explosiveForceScale = physPreset->explosiveForceScale; + physPresetInfo->sndAliasPrefix = physPreset->sndAliasPrefix; + physPresetInfo->piecesSpreadFraction = physPreset->piecesSpreadFraction; + physPresetInfo->piecesUpwardVelocity = physPreset->piecesUpwardVelocity; + physPresetInfo->minMomentum = physPreset->minMomentum; + physPresetInfo->maxMomentum = physPreset->maxMomentum; + physPresetInfo->minPitch = physPreset->minPitch; + physPresetInfo->maxPitch = physPreset->maxPitch; + physPresetInfo->volumeType = physPreset->volumeType; + physPresetInfo->pitchType = physPreset->pitchType; + physPresetInfo->tempDefaultToCylinder = physPreset->tempDefaultToCylinder ? 1 : 0; + physPresetInfo->perSurfaceSndAlias = physPreset->perSurfaceSndAlias ? 1 : 0; + } + + InfoString CreateInfoString(const XAssetInfo& asset) + { + auto* physPresetInfo = new PhysPresetInfo; + CopyToPhysPresetInfo(asset.Asset(), physPresetInfo); + + InfoStringFromPhysPresetConverter converter(physPresetInfo, + phys_preset_fields, + std::extent_v, + [asset](const scr_string_t scrStr) -> std::string + { + assert(scrStr < asset.m_zone->m_script_strings.Count()); + if (scrStr >= asset.m_zone->m_script_strings.Count()) + return ""; + + return asset.m_zone->m_script_strings[scrStr]; + }); + + return converter.Convert(); + } +} // namespace + +namespace phys_preset +{ + void InfoStringDumperIW5::DumpAsset(AssetDumpingContext& context, const XAssetInfo& asset) + { + // Only dump raw when no gdt available + if (context.m_gdt) + { + const auto infoString = CreateInfoString(asset); + GdtEntry gdtEntry(asset.m_name, GDF_FILENAME_PHYS_PRESET); + infoString.ToGdtProperties(INFO_STRING_PREFIX_PHYS_PRESET, gdtEntry); + context.m_gdt->WriteEntry(gdtEntry); + } + else + { + const auto assetFile = context.OpenAssetFile(GetFileNameForAssetName(asset.m_name)); + + if (!assetFile) + return; + + auto& stream = *assetFile; + const auto infoString = CreateInfoString(asset); + const auto stringValue = infoString.ToString(INFO_STRING_PREFIX_PHYS_PRESET); + stream.write(stringValue.c_str(), stringValue.size()); + } + } +} // namespace phys_preset diff --git a/src/ObjWriting/Game/IW5/PhysPreset/PhysPresetInfoStringDumperIW5.h b/src/ObjWriting/Game/IW5/PhysPreset/PhysPresetInfoStringDumperIW5.h new file mode 100644 index 00000000..c79008af --- /dev/null +++ b/src/ObjWriting/Game/IW5/PhysPreset/PhysPresetInfoStringDumperIW5.h @@ -0,0 +1,13 @@ +#pragma once + +#include "Dumping/AbstractAssetDumper.h" +#include "Game/IW5/IW5.h" + +namespace phys_preset +{ + class InfoStringDumperIW5 final : public AbstractAssetDumper + { + protected: + void DumpAsset(AssetDumpingContext& context, const XAssetInfo& asset) override; + }; +} // namespace phys_preset diff --git a/src/ObjWriting/Game/T5/InfoString/InfoStringFromStructConverter.cpp b/src/ObjWriting/Game/T5/InfoString/InfoStringFromStructConverter.cpp index e69de29b..647e18fd 100644 --- a/src/ObjWriting/Game/T5/InfoString/InfoStringFromStructConverter.cpp +++ b/src/ObjWriting/Game/T5/InfoString/InfoStringFromStructConverter.cpp @@ -0,0 +1,133 @@ +#include "InfoStringFromStructConverter.h" + +#include +#include + +using namespace T5; + +void InfoStringFromStructConverter::FillFromBaseField(const cspField_t& field) +{ + switch (static_cast(field.iFieldType)) + { + case CSPFT_STRING: + FillFromString(std::string(field.szName), field.iOffset); + break; + + case CSPFT_STRING_MAX_STRING_CHARS: + FillFromStringBuffer(std::string(field.szName), field.iOffset, 1024); + break; + + case CSPFT_STRING_MAX_QPATH: + FillFromStringBuffer(std::string(field.szName), field.iOffset, 64); + break; + + case CSPFT_STRING_MAX_OSPATH: + FillFromStringBuffer(std::string(field.szName), field.iOffset, 256); + break; + + case CSPFT_INT: + FillFromInt(std::string(field.szName), field.iOffset); + break; + + case CSPFT_BOOL: + FillFromBool(std::string(field.szName), field.iOffset); + break; + + case CSPFT_QBOOLEAN: + FillFromQBoolean(std::string(field.szName), field.iOffset); + break; + + case CSPFT_FLOAT: + FillFromFloat(std::string(field.szName), field.iOffset); + break; + + case CSPFT_MILLISECONDS: + FillFromMilliseconds(std::string(field.szName), field.iOffset); + break; + + case CSPFT_FX: + { + const auto* fx = *reinterpret_cast(reinterpret_cast(m_structure) + field.iOffset); + + if (fx) + m_info_string.SetValueForKey(std::string(field.szName), std::string(AssetName(fx->name))); + else + m_info_string.SetValueForKey(std::string(field.szName), ""); + break; + } + + case CSPFT_XMODEL: + { + const auto* model = *reinterpret_cast(reinterpret_cast(m_structure) + field.iOffset); + + if (model) + m_info_string.SetValueForKey(std::string(field.szName), std::string(AssetName(model->name))); + else + m_info_string.SetValueForKey(std::string(field.szName), ""); + break; + } + + case CSPFT_MATERIAL: + case CSPFT_MATERIAL_STREAM: + { + const auto* material = *reinterpret_cast(reinterpret_cast(m_structure) + field.iOffset); + + if (material) + m_info_string.SetValueForKey(std::string(field.szName), std::string(AssetName(material->info.name))); + else + m_info_string.SetValueForKey(std::string(field.szName), ""); + break; + } + + case CSPFT_PHYS_PRESET: + { + const auto* physPreset = *reinterpret_cast(reinterpret_cast(m_structure) + field.iOffset); + + if (physPreset) + m_info_string.SetValueForKey(std::string(field.szName), std::string(AssetName(physPreset->name))); + else + m_info_string.SetValueForKey(std::string(field.szName), ""); + break; + } + + case CSPFT_SCRIPT_STRING: + FillFromScriptString(std::string(field.szName), field.iOffset); + break; + + case CSPFT_NUM_BASE_FIELD_TYPES: + default: + assert(false); + break; + } +} + +void InfoStringFromStructConverter::FillInfoString() +{ + for (auto fieldIndex = 0u; fieldIndex < m_field_count; fieldIndex++) + { + const auto& field = m_fields[fieldIndex]; + assert(field.iFieldType >= 0); + + if (field.iFieldType < CSPFT_NUM_BASE_FIELD_TYPES) + FillFromBaseField(field); + else + FillFromExtensionField(field); + } +} + +InfoStringFromStructConverter::InfoStringFromStructConverter(const void* structure, const cspField_t* fields, const size_t fieldCount) + : InfoStringFromStructConverterBase(structure), + m_fields(fields), + m_field_count(fieldCount) +{ +} + +InfoStringFromStructConverter::InfoStringFromStructConverter(const void* structure, + const cspField_t* fields, + const size_t fieldCount, + std::function scriptStringValueCallback) + : InfoStringFromStructConverterBase(structure, std::move(scriptStringValueCallback)), + m_fields(fields), + m_field_count(fieldCount) +{ +} diff --git a/src/ObjWriting/Game/T5/InfoString/InfoStringFromStructConverter.h b/src/ObjWriting/Game/T5/InfoString/InfoStringFromStructConverter.h index e69de29b..e4a89d13 100644 --- a/src/ObjWriting/Game/T5/InfoString/InfoStringFromStructConverter.h +++ b/src/ObjWriting/Game/T5/InfoString/InfoStringFromStructConverter.h @@ -0,0 +1,24 @@ +#pragma once +#include "Game/T5/T5.h" +#include "InfoString/InfoStringFromStructConverterBase.h" + +namespace T5 +{ + class InfoStringFromStructConverter : public InfoStringFromStructConverterBase + { + protected: + const cspField_t* m_fields; + size_t m_field_count; + + virtual void FillFromExtensionField(const cspField_t& field) = 0; + void FillFromBaseField(const cspField_t& field); + void FillInfoString() override; + + public: + InfoStringFromStructConverter(const void* structure, const cspField_t* fields, size_t fieldCount); + InfoStringFromStructConverter(const void* structure, + const cspField_t* fields, + size_t fieldCount, + std::function scriptStringValueCallback); + }; +} // namespace T5 diff --git a/src/ObjWriting/Game/T5/ObjWriterT5.cpp b/src/ObjWriting/Game/T5/ObjWriterT5.cpp index 19c5e277..4e23d39d 100644 --- a/src/ObjWriting/Game/T5/ObjWriterT5.cpp +++ b/src/ObjWriting/Game/T5/ObjWriterT5.cpp @@ -5,6 +5,7 @@ #include "Game/T5/XModel/XModelDumperT5.h" #include "Image/ImageDumperT5.h" #include "Localize/LocalizeDumperT5.h" +#include "PhysPreset/PhysPresetInfoStringDumperT5.h" #include "RawFile/RawFileDumperT5.h" #include "StringTable/StringTableDumperT5.h" @@ -12,7 +13,7 @@ using namespace T5; void ObjWriter::RegisterAssetDumpers(AssetDumpingContext& context) { - // REGISTER_DUMPER(AssetDumperPhysPreset, m_phys_preset) + RegisterAssetDumper(std::make_unique()); // REGISTER_DUMPER(AssetDumperPhysConstraints, m_phys_constraints) // REGISTER_DUMPER(AssetDumperDestructibleDef, m_destructible_def) // REGISTER_DUMPER(AssetDumperXAnimParts, m_xanim_parts) diff --git a/src/ObjWriting/Game/T5/PhysPreset/PhysPresetInfoStringDumperT5.cpp b/src/ObjWriting/Game/T5/PhysPreset/PhysPresetInfoStringDumperT5.cpp new file mode 100644 index 00000000..9de34a25 --- /dev/null +++ b/src/ObjWriting/Game/T5/PhysPreset/PhysPresetInfoStringDumperT5.cpp @@ -0,0 +1,108 @@ +#include "PhysPresetInfoStringDumperT5.h" + +#include "Game/T5/InfoString/InfoStringFromStructConverter.h" +#include "Game/T5/ObjConstantsT5.h" +#include "Game/T5/PhysPreset/PhysPresetFields.h" +#include "PhysPreset/PhysPresetCommon.h" + +#include +#include +#include +#include + +using namespace T5; + +namespace +{ + class InfoStringFromPhysPresetConverter final : public InfoStringFromStructConverter + { + protected: + void FillFromExtensionField(const cspField_t& field) override + { + assert(false); + } + + public: + InfoStringFromPhysPresetConverter(const PhysPresetInfo* structure, + const cspField_t* fields, + const size_t fieldCount, + std::function scriptStringValueCallback) + : InfoStringFromStructConverter(structure, fields, fieldCount, std::move(scriptStringValueCallback)) + { + } + }; + + void CopyToPhysPresetInfo(const PhysPreset* physPreset, PhysPresetInfo* physPresetInfo) + { + physPresetInfo->mass = std::clamp(physPreset->mass * 1000.0f, 1.0f, 2000.0f); + physPresetInfo->bounce = physPreset->bounce; + + if (physPreset->friction >= MAX_FRICTION) + { + physPresetInfo->isFrictionInfinity = 1; + physPresetInfo->friction = 0; + } + else + { + physPresetInfo->isFrictionInfinity = 0; + physPresetInfo->friction = physPreset->friction; + } + + physPresetInfo->bulletForceScale = physPreset->bulletForceScale; + physPresetInfo->explosiveForceScale = physPreset->explosiveForceScale; + physPresetInfo->piecesSpreadFraction = physPreset->piecesSpreadFraction; + physPresetInfo->piecesUpwardVelocity = physPreset->piecesUpwardVelocity; + physPresetInfo->canFloat = physPreset->canFloat; + physPresetInfo->gravityScale = std::clamp(physPreset->gravityScale, 0.01f, 10.0f); + physPresetInfo->centerOfMassOffset = physPreset->centerOfMassOffset; + physPresetInfo->buoyancyBoxMin = physPreset->buoyancyBoxMin; + physPresetInfo->buoyancyBoxMax = physPreset->buoyancyBoxMax; + } + + InfoString CreateInfoString(const XAssetInfo& asset) + { + auto* physPresetInfo = new PhysPresetInfo; + CopyToPhysPresetInfo(asset.Asset(), physPresetInfo); + + InfoStringFromPhysPresetConverter converter(physPresetInfo, + phys_preset_fields, + std::extent_v, + [asset](const scr_string_t scrStr) -> std::string + { + assert(scrStr < asset.m_zone->m_script_strings.Count()); + if (scrStr >= asset.m_zone->m_script_strings.Count()) + return ""; + + return asset.m_zone->m_script_strings[scrStr]; + }); + + return converter.Convert(); + } +} // namespace + +namespace phys_preset +{ + void InfoStringDumperT5::DumpAsset(AssetDumpingContext& context, const XAssetInfo& asset) + { + // Only dump raw when no gdt available + if (context.m_gdt) + { + const auto infoString = CreateInfoString(asset); + GdtEntry gdtEntry(asset.m_name, GDF_FILENAME_PHYS_PRESET); + infoString.ToGdtProperties(INFO_STRING_PREFIX_PHYS_PRESET, gdtEntry); + context.m_gdt->WriteEntry(gdtEntry); + } + else + { + const auto assetFile = context.OpenAssetFile(GetFileNameForAssetName(asset.m_name)); + + if (!assetFile) + return; + + auto& stream = *assetFile; + const auto infoString = CreateInfoString(asset); + const auto stringValue = infoString.ToString(INFO_STRING_PREFIX_PHYS_PRESET); + stream.write(stringValue.c_str(), stringValue.size()); + } + } +} // namespace phys_preset diff --git a/src/ObjWriting/Game/T5/PhysPreset/PhysPresetInfoStringDumperT5.h b/src/ObjWriting/Game/T5/PhysPreset/PhysPresetInfoStringDumperT5.h new file mode 100644 index 00000000..b353143d --- /dev/null +++ b/src/ObjWriting/Game/T5/PhysPreset/PhysPresetInfoStringDumperT5.h @@ -0,0 +1,13 @@ +#pragma once + +#include "Dumping/AbstractAssetDumper.h" +#include "Game/T5/T5.h" + +namespace phys_preset +{ + class InfoStringDumperT5 final : public AbstractAssetDumper + { + protected: + void DumpAsset(AssetDumpingContext& context, const XAssetInfo& asset) override; + }; +} // namespace phys_preset