From 24c9e08046e82ff62a4bb2efe44e0018c7cd4a32 Mon Sep 17 00:00:00 2001 From: Jan Laupetin Date: Wed, 30 Jul 2025 18:48:59 +0100 Subject: [PATCH] refactor: streamline phys preset dumping --- src/ObjCommon/PhysPreset/PhysPresetCommon.cpp | 11 ++ src/ObjCommon/PhysPreset/PhysPresetCommon.h | 8 ++ .../IW4/PhysPreset/RawLoaderPhysPresetIW4.cpp | 3 +- .../T6/PhysPreset/RawLoaderPhysPresetT6.cpp | 3 +- src/ObjWriting/Game/IW4/ObjWriterIW4.cpp | 4 +- .../IW4/PhysPreset/AssetDumperPhysPreset.cpp | 107 ---------------- .../PhysPresetInfoStringDumperIW4.cpp | 112 +++++++++++++++++ ...eset.h => PhysPresetInfoStringDumperIW4.h} | 7 +- src/ObjWriting/Game/T5/ObjWriterT5.cpp | 1 - .../T5/PhysPreset/AssetDumperPhysPreset.cpp | 0 .../T5/PhysPreset/AssetDumperPhysPreset.h | 0 src/ObjWriting/Game/T6/ObjWriterT6.cpp | 4 +- .../T6/PhysPreset/AssetDumperPhysPreset.cpp | 109 ----------------- .../PhysPresetInfoStringDumperT6.cpp | 114 ++++++++++++++++++ ...reset.h => PhysPresetInfoStringDumperT6.h} | 9 +- 15 files changed, 258 insertions(+), 234 deletions(-) create mode 100644 src/ObjCommon/PhysPreset/PhysPresetCommon.cpp create mode 100644 src/ObjCommon/PhysPreset/PhysPresetCommon.h delete mode 100644 src/ObjWriting/Game/IW4/PhysPreset/AssetDumperPhysPreset.cpp create mode 100644 src/ObjWriting/Game/IW4/PhysPreset/PhysPresetInfoStringDumperIW4.cpp rename src/ObjWriting/Game/IW4/PhysPreset/{AssetDumperPhysPreset.h => PhysPresetInfoStringDumperIW4.h} (54%) delete mode 100644 src/ObjWriting/Game/T5/PhysPreset/AssetDumperPhysPreset.cpp delete mode 100644 src/ObjWriting/Game/T5/PhysPreset/AssetDumperPhysPreset.h delete mode 100644 src/ObjWriting/Game/T6/PhysPreset/AssetDumperPhysPreset.cpp create mode 100644 src/ObjWriting/Game/T6/PhysPreset/PhysPresetInfoStringDumperT6.cpp rename src/ObjWriting/Game/T6/PhysPreset/{AssetDumperPhysPreset.h => PhysPresetInfoStringDumperT6.h} (51%) diff --git a/src/ObjCommon/PhysPreset/PhysPresetCommon.cpp b/src/ObjCommon/PhysPreset/PhysPresetCommon.cpp new file mode 100644 index 00000000..66c08396 --- /dev/null +++ b/src/ObjCommon/PhysPreset/PhysPresetCommon.cpp @@ -0,0 +1,11 @@ +#include "PhysPresetCommon.h" + +#include + +namespace phys_preset +{ + std::string GetFileNameForAssetName(const std::string& assetName) + { + return std::format("physic/{}", assetName); + } +} // namespace phys_preset diff --git a/src/ObjCommon/PhysPreset/PhysPresetCommon.h b/src/ObjCommon/PhysPreset/PhysPresetCommon.h new file mode 100644 index 00000000..d627051c --- /dev/null +++ b/src/ObjCommon/PhysPreset/PhysPresetCommon.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +namespace phys_preset +{ + std::string GetFileNameForAssetName(const std::string& assetName); +} diff --git a/src/ObjLoading/Game/IW4/PhysPreset/RawLoaderPhysPresetIW4.cpp b/src/ObjLoading/Game/IW4/PhysPreset/RawLoaderPhysPresetIW4.cpp index ada031ac..11bd6813 100644 --- a/src/ObjLoading/Game/IW4/PhysPreset/RawLoaderPhysPresetIW4.cpp +++ b/src/ObjLoading/Game/IW4/PhysPreset/RawLoaderPhysPresetIW4.cpp @@ -4,6 +4,7 @@ #include "Game/IW4/ObjConstantsIW4.h" #include "InfoString/InfoString.h" #include "InfoStringLoaderPhysPresetIW4.h" +#include "PhysPreset/PhysPresetCommon.h" #include #include @@ -19,7 +20,7 @@ RawLoaderPhysPreset::RawLoaderPhysPreset(MemoryManager& memory, ISearchPath& sea AssetCreationResult RawLoaderPhysPreset::CreateAsset(const std::string& assetName, AssetCreationContext& context) { - const auto fileName = std::format("physic/{}", assetName); + const auto fileName = phys_preset::GetFileNameForAssetName(assetName); const auto file = m_search_path.Open(fileName); if (!file.IsOpen()) return AssetCreationResult::NoAction(); diff --git a/src/ObjLoading/Game/T6/PhysPreset/RawLoaderPhysPresetT6.cpp b/src/ObjLoading/Game/T6/PhysPreset/RawLoaderPhysPresetT6.cpp index 4fe78db8..237ccbc8 100644 --- a/src/ObjLoading/Game/T6/PhysPreset/RawLoaderPhysPresetT6.cpp +++ b/src/ObjLoading/Game/T6/PhysPreset/RawLoaderPhysPresetT6.cpp @@ -4,6 +4,7 @@ #include "Game/T6/T6.h" #include "InfoString/InfoString.h" #include "InfoStringLoaderPhysPresetT6.h" +#include "PhysPreset/PhysPresetCommon.h" #include #include @@ -24,7 +25,7 @@ namespace AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override { - const auto fileName = std::format("physic/{}", assetName); + const auto fileName = phys_preset::GetFileNameForAssetName(assetName); const auto file = m_search_path.Open(fileName); if (!file.IsOpen()) return AssetCreationResult::NoAction(); diff --git a/src/ObjWriting/Game/IW4/ObjWriterIW4.cpp b/src/ObjWriting/Game/IW4/ObjWriterIW4.cpp index d14504d6..4dd466d9 100644 --- a/src/ObjWriting/Game/IW4/ObjWriterIW4.cpp +++ b/src/ObjWriting/Game/IW4/ObjWriterIW4.cpp @@ -13,7 +13,7 @@ #include "Menu/AssetDumperMenuList.h" #include "ObjWriting.h" #include "PhysCollmap/AssetDumperPhysCollmap.h" -#include "PhysPreset/AssetDumperPhysPreset.h" +#include "PhysPreset/PhysPresetInfoStringDumperIW4.h" #include "RawFile/AssetDumperRawFile.h" #include "Shader/AssetDumperPixelShader.h" #include "Shader/AssetDumperVertexShader.h" @@ -39,7 +39,7 @@ bool ObjWriter::DumpZone(AssetDumpingContext& context) const const auto* assetPools = dynamic_cast(context.m_zone.m_pools.get()); - DUMP_ASSET_POOL(AssetDumperPhysPreset, m_phys_preset, ASSET_TYPE_PHYSPRESET) + DUMP_ASSET_POOL(phys_preset::InfoStringDumper, m_phys_preset, ASSET_TYPE_PHYSPRESET) DUMP_ASSET_POOL(AssetDumperPhysCollmap, m_phys_collmap, ASSET_TYPE_PHYSCOLLMAP) // DUMP_ASSET_POOL(AssetDumperXAnimParts, m_xanim_parts, ASSET_TYPE_XANIMPARTS) DUMP_ASSET_POOL(AssetDumperXModel, m_xmodel, ASSET_TYPE_XMODEL) diff --git a/src/ObjWriting/Game/IW4/PhysPreset/AssetDumperPhysPreset.cpp b/src/ObjWriting/Game/IW4/PhysPreset/AssetDumperPhysPreset.cpp deleted file mode 100644 index bfd6d990..00000000 --- a/src/ObjWriting/Game/IW4/PhysPreset/AssetDumperPhysPreset.cpp +++ /dev/null @@ -1,107 +0,0 @@ -#include "AssetDumperPhysPreset.h" - -#include "Game/IW4/InfoString/InfoStringFromStructConverter.h" -#include "Game/IW4/ObjConstantsIW4.h" -#include "Game/IW4/PhysPreset/PhysPresetFields.h" - -#include -#include -#include -#include - -using namespace IW4; - -namespace IW4 -{ - 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)) - { - } - }; -} // namespace IW4 - -void AssetDumperPhysPreset::CopyToPhysPresetInfo(const PhysPreset* physPreset, PhysPresetInfo* physPresetInfo) -{ - physPresetInfo->mass = std::clamp(physPreset->mass * 1000.0f, 1.0f, 2000.0f); - physPresetInfo->bounce = physPreset->bounce; - - if (std::isinf(physPreset->friction)) - { - physPresetInfo->isFrictionInfinity = 1; - physPresetInfo->friction = 0; - } - else - { - physPresetInfo->isFrictionInfinity = 0; - 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->tempDefaultToCylinder = physPreset->tempDefaultToCylinder ? 1 : 0; - physPresetInfo->perSurfaceSndAlias = physPreset->perSurfaceSndAlias ? 1 : 0; -} - -InfoString AssetDumperPhysPreset::CreateInfoString(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(); -} - -bool AssetDumperPhysPreset::ShouldDump(XAssetInfo* asset) -{ - return true; -} - -void AssetDumperPhysPreset::DumpAsset(AssetDumpingContext& context, XAssetInfo* asset) -{ - // Only dump raw when no gdt available - if (context.m_gdt) - { - const auto infoString = CreateInfoString(asset); - GdtEntry gdtEntry(asset->m_name, ObjConstants::GDF_FILENAME_PHYS_PRESET); - infoString.ToGdtProperties(ObjConstants::INFO_STRING_PREFIX_PHYS_PRESET, gdtEntry); - context.m_gdt->WriteEntry(gdtEntry); - } - else - { - const auto assetFile = context.OpenAssetFile("physic/" + asset->m_name); - - if (!assetFile) - return; - - auto& stream = *assetFile; - const auto infoString = CreateInfoString(asset); - const auto stringValue = infoString.ToString(ObjConstants::INFO_STRING_PREFIX_PHYS_PRESET); - stream.write(stringValue.c_str(), stringValue.size()); - } -} diff --git a/src/ObjWriting/Game/IW4/PhysPreset/PhysPresetInfoStringDumperIW4.cpp b/src/ObjWriting/Game/IW4/PhysPreset/PhysPresetInfoStringDumperIW4.cpp new file mode 100644 index 00000000..734bd8d8 --- /dev/null +++ b/src/ObjWriting/Game/IW4/PhysPreset/PhysPresetInfoStringDumperIW4.cpp @@ -0,0 +1,112 @@ +#include "PhysPresetInfoStringDumperIW4.h" + +#include "Game/IW4/InfoString/InfoStringFromStructConverter.h" +#include "Game/IW4/ObjConstantsIW4.h" +#include "Game/IW4/PhysPreset/PhysPresetFields.h" +#include "PhysPreset/PhysPresetCommon.h" + +#include +#include +#include +#include + +using namespace IW4; +using namespace ::phys_preset; + +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 (std::isinf(physPreset->friction)) + { + physPresetInfo->isFrictionInfinity = 1; + physPresetInfo->friction = 0; + } + else + { + physPresetInfo->isFrictionInfinity = 0; + 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->tempDefaultToCylinder = physPreset->tempDefaultToCylinder ? 1 : 0; + physPresetInfo->perSurfaceSndAlias = physPreset->perSurfaceSndAlias ? 1 : 0; + } + + InfoString CreateInfoString(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 IW4::phys_preset +{ + bool InfoStringDumper::ShouldDump(XAssetInfo* asset) + { + return true; + } + + void InfoStringDumper::DumpAsset(AssetDumpingContext& context, XAssetInfo* asset) + { + // Only dump raw when no gdt available + if (context.m_gdt) + { + const auto infoString = CreateInfoString(asset); + GdtEntry gdtEntry(asset->m_name, ObjConstants::GDF_FILENAME_PHYS_PRESET); + infoString.ToGdtProperties(ObjConstants::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(ObjConstants::INFO_STRING_PREFIX_PHYS_PRESET); + stream.write(stringValue.c_str(), stringValue.size()); + } + } +} // namespace IW4::phys_preset diff --git a/src/ObjWriting/Game/IW4/PhysPreset/AssetDumperPhysPreset.h b/src/ObjWriting/Game/IW4/PhysPreset/PhysPresetInfoStringDumperIW4.h similarity index 54% rename from src/ObjWriting/Game/IW4/PhysPreset/AssetDumperPhysPreset.h rename to src/ObjWriting/Game/IW4/PhysPreset/PhysPresetInfoStringDumperIW4.h index 5de816cc..98b6a366 100644 --- a/src/ObjWriting/Game/IW4/PhysPreset/AssetDumperPhysPreset.h +++ b/src/ObjWriting/Game/IW4/PhysPreset/PhysPresetInfoStringDumperIW4.h @@ -4,13 +4,10 @@ #include "Game/IW4/IW4.h" #include "InfoString/InfoString.h" -namespace IW4 +namespace IW4::phys_preset { - class AssetDumperPhysPreset final : public AbstractAssetDumper + class InfoStringDumper final : public AbstractAssetDumper { - static void CopyToPhysPresetInfo(const PhysPreset* physPreset, PhysPresetInfo* physPresetInfo); - static InfoString CreateInfoString(XAssetInfo* asset); - protected: bool ShouldDump(XAssetInfo* asset) override; void DumpAsset(AssetDumpingContext& context, XAssetInfo* asset) override; diff --git a/src/ObjWriting/Game/T5/ObjWriterT5.cpp b/src/ObjWriting/Game/T5/ObjWriterT5.cpp index b12857e8..baaf5a32 100644 --- a/src/ObjWriting/Game/T5/ObjWriterT5.cpp +++ b/src/ObjWriting/Game/T5/ObjWriterT5.cpp @@ -6,7 +6,6 @@ #include "Image/ImageDumperT5.h" #include "Localize/LocalizeDumperT5.h" #include "ObjWriting.h" -#include "PhysPreset/AssetDumperPhysPreset.h" #include "RawFile/AssetDumperRawFile.h" #include "Sound/AssetDumperSndBank.h" #include "StringTable/AssetDumperStringTable.h" diff --git a/src/ObjWriting/Game/T5/PhysPreset/AssetDumperPhysPreset.cpp b/src/ObjWriting/Game/T5/PhysPreset/AssetDumperPhysPreset.cpp deleted file mode 100644 index e69de29b..00000000 diff --git a/src/ObjWriting/Game/T5/PhysPreset/AssetDumperPhysPreset.h b/src/ObjWriting/Game/T5/PhysPreset/AssetDumperPhysPreset.h deleted file mode 100644 index e69de29b..00000000 diff --git a/src/ObjWriting/Game/T6/ObjWriterT6.cpp b/src/ObjWriting/Game/T6/ObjWriterT6.cpp index 3aeaf091..3e8de73d 100644 --- a/src/ObjWriting/Game/T6/ObjWriterT6.cpp +++ b/src/ObjWriting/Game/T6/ObjWriterT6.cpp @@ -10,7 +10,7 @@ #include "Maps/MapEntsDumperT6.h" #include "ObjWriting.h" #include "PhysConstraints/PhysConstraintsInfoStringDumperT6.h" -#include "PhysPreset/AssetDumperPhysPreset.h" +#include "PhysPreset/PhysPresetInfoStringDumperT6.h" #include "Qdb/AssetDumperQdb.h" #include "RawFile/AssetDumperRawFile.h" #include "Script/AssetDumperScriptParseTree.h" @@ -46,7 +46,7 @@ bool ObjWriter::DumpZone(AssetDumpingContext& context) const const auto* assetPools = dynamic_cast(context.m_zone.m_pools.get()); - DUMP_ASSET_POOL(AssetDumperPhysPreset, m_phys_preset, ASSET_TYPE_PHYSPRESET) + DUMP_ASSET_POOL(phys_preset::InfoStringDumper, m_phys_preset, ASSET_TYPE_PHYSPRESET) DUMP_ASSET_POOL(phys_constraints::InfoStringDumper, m_phys_constraints, ASSET_TYPE_PHYSCONSTRAINTS) // DUMP_ASSET_POOL(AssetDumperDestructibleDef, m_destructible_def, ASSET_TYPE_DESTRUCTIBLEDEF) // DUMP_ASSET_POOL(AssetDumperXAnimParts, m_xanim_parts, ASSET_TYPE_XANIMPARTS) diff --git a/src/ObjWriting/Game/T6/PhysPreset/AssetDumperPhysPreset.cpp b/src/ObjWriting/Game/T6/PhysPreset/AssetDumperPhysPreset.cpp deleted file mode 100644 index d11819f4..00000000 --- a/src/ObjWriting/Game/T6/PhysPreset/AssetDumperPhysPreset.cpp +++ /dev/null @@ -1,109 +0,0 @@ -#include "AssetDumperPhysPreset.h" - -#include "Game/T6/InfoString/InfoStringFromStructConverter.h" -#include "Game/T6/ObjConstantsT6.h" -#include "Game/T6/PhysPreset/PhysPresetFields.h" - -#include -#include -#include -#include - -using namespace T6; - -namespace T6 -{ - 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)) - { - } - }; -} // namespace T6 - -void AssetDumperPhysPreset::CopyToPhysPresetInfo(const PhysPreset* physPreset, PhysPresetInfo* physPresetInfo) -{ - physPresetInfo->mass = std::clamp(physPreset->mass * 1000.0f, 1.0f, 2000.0f); - physPresetInfo->bounce = physPreset->bounce; - - if (std::isinf(physPreset->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 AssetDumperPhysPreset::CreateInfoString(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(); -} - -bool AssetDumperPhysPreset::ShouldDump(XAssetInfo* asset) -{ - return true; -} - -void AssetDumperPhysPreset::DumpAsset(AssetDumpingContext& context, XAssetInfo* asset) -{ - // Only dump raw when no gdt available - if (context.m_gdt) - { - const auto infoString = CreateInfoString(asset); - GdtEntry gdtEntry(asset->m_name, ObjConstants::GDF_FILENAME_PHYS_PRESET); - infoString.ToGdtProperties(ObjConstants::INFO_STRING_PREFIX_PHYS_PRESET, gdtEntry); - context.m_gdt->WriteEntry(gdtEntry); - } - else - { - const auto assetFile = context.OpenAssetFile("physic/" + asset->m_name); - - if (!assetFile) - return; - - auto& stream = *assetFile; - const auto infoString = CreateInfoString(asset); - const auto stringValue = infoString.ToString(ObjConstants::INFO_STRING_PREFIX_PHYS_PRESET); - stream.write(stringValue.c_str(), stringValue.size()); - } -} diff --git a/src/ObjWriting/Game/T6/PhysPreset/PhysPresetInfoStringDumperT6.cpp b/src/ObjWriting/Game/T6/PhysPreset/PhysPresetInfoStringDumperT6.cpp new file mode 100644 index 00000000..8941af2d --- /dev/null +++ b/src/ObjWriting/Game/T6/PhysPreset/PhysPresetInfoStringDumperT6.cpp @@ -0,0 +1,114 @@ +#include "PhysPresetInfoStringDumperT6.h" + +#include "Game/T6/InfoString/InfoStringFromStructConverter.h" +#include "Game/T6/ObjConstantsT6.h" +#include "Game/T6/PhysPreset/PhysPresetFields.h" +#include "PhysPreset/PhysPresetCommon.h" + +#include +#include +#include +#include + +using namespace T6; +using namespace ::phys_preset; + +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 (std::isinf(physPreset->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(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 T6::phys_preset +{ + bool InfoStringDumper::ShouldDump(XAssetInfo* asset) + { + return true; + } + + void InfoStringDumper::DumpAsset(AssetDumpingContext& context, XAssetInfo* asset) + { + // Only dump raw when no gdt available + if (context.m_gdt) + { + const auto infoString = CreateInfoString(asset); + GdtEntry gdtEntry(asset->m_name, ObjConstants::GDF_FILENAME_PHYS_PRESET); + infoString.ToGdtProperties(ObjConstants::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(ObjConstants::INFO_STRING_PREFIX_PHYS_PRESET); + stream.write(stringValue.c_str(), stringValue.size()); + } + } +} // namespace T6::phys_preset diff --git a/src/ObjWriting/Game/T6/PhysPreset/AssetDumperPhysPreset.h b/src/ObjWriting/Game/T6/PhysPreset/PhysPresetInfoStringDumperT6.h similarity index 51% rename from src/ObjWriting/Game/T6/PhysPreset/AssetDumperPhysPreset.h rename to src/ObjWriting/Game/T6/PhysPreset/PhysPresetInfoStringDumperT6.h index a760ef45..26ce2296 100644 --- a/src/ObjWriting/Game/T6/PhysPreset/AssetDumperPhysPreset.h +++ b/src/ObjWriting/Game/T6/PhysPreset/PhysPresetInfoStringDumperT6.h @@ -4,15 +4,12 @@ #include "Game/T6/T6.h" #include "InfoString/InfoString.h" -namespace T6 +namespace T6::phys_preset { - class AssetDumperPhysPreset final : public AbstractAssetDumper + class InfoStringDumper final : public AbstractAssetDumper { - static void CopyToPhysPresetInfo(const PhysPreset* physPreset, PhysPresetInfo* physPresetInfo); - static InfoString CreateInfoString(XAssetInfo* asset); - protected: bool ShouldDump(XAssetInfo* asset) override; void DumpAsset(AssetDumpingContext& context, XAssetInfo* asset) override; }; -} // namespace T6 +} // namespace T6::phys_preset