From d75827b05ef3ff8186efd35793844e951ac09d4c Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Mon, 29 Aug 2022 08:58:07 +0100 Subject: [PATCH] parse: Add dummy fallback KV for when parsing fails There are lots of really broken models floating about, with complete garbage KV contents. If we fail to parse the KV, use a dummy fallback instead of returning a NULL parser, which always just crashes. --- vphysics_jolt/vjolt_parse.cpp | 41 ++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/vphysics_jolt/vjolt_parse.cpp b/vphysics_jolt/vjolt_parse.cpp index 9e26dfd..e9e12e5 100644 --- a/vphysics_jolt/vjolt_parse.cpp +++ b/vphysics_jolt/vjolt_parse.cpp @@ -475,6 +475,36 @@ void JoltPhysicsParseKV::NextBlock() //------------------------------------------------------------------------------------------------- +static constexpr const char* DummyParserKeyValues = R"( +"PhysProps_Fallback" +{ + "solid" + { + "dummy" "1" + } + "vehicle" + { + "dummy" "1" + } + "vehicle_sounds" + { + "dummy" "1" + } + "vehicle_view" + { + "dummy" "1" + } + "ragdollconstraint" + { + "dummy" "1" + } + "collisionrules" + { + "dummy" "1" + } +} +)"; + IVPhysicsKeyParser *CreateVPhysicsKeyParser( const char *pKeyData, bool bIsPacked ) { VJoltAssertMsg( !bIsPacked, "Packed VPhysics KV not supported. You should not get here anyway as we do not emit it." ); @@ -483,8 +513,17 @@ IVPhysicsKeyParser *CreateVPhysicsKeyParser( const char *pKeyData, bool bIsPacke KeyValues *pszKV = HeaderlessKVBufferToKeyValues( pKeyData, "VPhysicsKeyParse" ); + // Josh: Ideally we would return nullptr here, but that breaks a lot of things. + // If we fail to parse the KV, simply just fall-back to a dummy KV that will cause things + // to get zero-initialized. + // In the future, we may want to add a KV patching pass to fix up broken model and vehicle data. if ( !pszKV ) - return nullptr; + { + Log_Warning( LOG_VJolt, "CreateVPhysicsKeyParser: Encountered invalid KV data. Falling back to a dummy KV. You may notice a broken prop/vehicle.\n" ); + + pszKV = new KeyValues( "VPhysicsKeyParse_Fallback" ); + pszKV->LoadFromBuffer( "VPhysicsKeyParse_Fallback", DummyParserKeyValues ); + } return new JoltPhysicsParseKV( pszKV ); }