From d8bd6ef407e88d5c4029cb9607d698c4409a3bab Mon Sep 17 00:00:00 2001 From: Chris Burel Date: Fri, 21 May 2021 08:39:10 -0700 Subject: [PATCH] Preserve asset ids for assets that fail to load, when deserializing from json (#847) Sometimes deserializing a Json document happens when asset handlers are not registered. In that case, `FindOrCreateAsset` will fail to create the asset, since there's no handler registered to create it. When this happens, `FindOrCreateAsset` returns an Asset instance with a null asset id. This effectively causes the json deserializer to lose that data, even in situations where the the actual asset data doesn't need to be loaded, but the asset id needs to be preserved. --- .../AzCore/AzCore/Asset/AssetJsonSerializer.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Code/Framework/AzCore/AzCore/Asset/AssetJsonSerializer.cpp b/Code/Framework/AzCore/AzCore/Asset/AssetJsonSerializer.cpp index 555eedf034..a72fe4e013 100644 --- a/Code/Framework/AzCore/AzCore/Asset/AssetJsonSerializer.cpp +++ b/Code/Framework/AzCore/AzCore/Asset/AssetJsonSerializer.cpp @@ -133,7 +133,15 @@ namespace AZ if (!id.m_guid.IsNull()) { *instance = AssetManager::Instance().FindOrCreateAsset(id, instance->GetType(), instance->GetAutoLoadBehavior()); - + if (!instance->GetId().IsValid()) + { + // If the asset failed to be created, FindOrCreateAsset returns an asset instance with a null + // id. To preserve the asset id in the source json, reset the asset to an empty one, but with + // the right id. + const auto loadBehavior = instance->GetAutoLoadBehavior(); + *instance = Asset(id, instance->GetType()); + instance->SetAutoLoadBehavior(loadBehavior); + } result.Combine(context.Report(result, "Successfully created Asset with id.")); }