@@ -1052,7 +1052,10 @@ namespace AZ
1052
1052
// =========================================================================
1053
1053
// SliceComponent::SliceReference::Instantiate
1054
1054
// =========================================================================
1055
- bool SliceComponent::SliceReference::Instantiate (const AZ::ObjectStream::FilterDescriptor& filterDesc)
1055
+ bool SliceComponent::SliceReference::Instantiate (
1056
+ const AZ::ObjectStream::FilterDescriptor& filterDesc,
1057
+ AZ::SerializeContext* serializeContext,
1058
+ AZStd::unordered_map<AZStd::string, AZStd::string>* relativeToAbsoluteSlicePaths)
1056
1059
{
1057
1060
AZ_PROFILE_FUNCTION (AzCore);
1058
1061
@@ -1061,41 +1064,64 @@ namespace AZ
1061
1064
return true ;
1062
1065
}
1063
1066
1064
- AZ::Data::AssetInfo assetInfo;
1065
- AZ::Data::AssetCatalogRequestBus::BroadcastResult (assetInfo, &AZ::Data::AssetCatalogRequestBus::Events::GetAssetInfoById, m_asset.GetId ());
1066
- bool isAssetStillOnDisk = assetInfo.m_assetId .IsValid ();
1067
-
1068
- bool isCachedAssetReady = m_asset.IsReady ();
1069
-
1070
- if (!(isCachedAssetReady && isAssetStillOnDisk))
1067
+ SliceComponent* dependentSlice = nullptr ;
1068
+ const bool useAssetCatalog = !serializeContext || !relativeToAbsoluteSlicePaths;
1069
+ if (useAssetCatalog)
1071
1070
{
1072
- // If the asset has been queued for async loading but hasn't completed, we've reached the point where it is required
1073
- // to be complete, so block until it finishes loading.
1074
- if (m_asset.IsLoading ())
1075
- {
1076
- m_asset.BlockUntilLoadComplete ();
1077
- }
1071
+ AZ::Data::AssetInfo assetInfo;
1072
+ AZ::Data::AssetCatalogRequestBus::BroadcastResult (assetInfo, &AZ::Data::AssetCatalogRequestBus::Events::GetAssetInfoById, m_asset.GetId ());
1078
1073
1079
- // If the asset still isn't ready, an unexpected error has occurred.
1080
- if (!m_asset.IsReady ())
1074
+ const bool isAssetStillOnDisk = assetInfo.m_assetId .IsValid ();
1075
+ const bool isCachedAssetReady = m_asset.IsReady ();
1076
+ if (!(isCachedAssetReady && isAssetStillOnDisk))
1081
1077
{
1078
+ // If the asset has been queued for async loading but hasn't completed, we've reached the point where it is required
1079
+ // to be complete, so block until it finishes loading.
1080
+ if (m_asset.IsLoading ())
1081
+ {
1082
+ m_asset.BlockUntilLoadComplete ();
1083
+ }
1084
+
1085
+ // If the asset still isn't ready, an unexpected error has occurred.
1086
+ if (!m_asset.IsReady ())
1087
+ {
1082
1088
#if defined(AZ_ENABLE_TRACING)
1083
- const Data::Asset<SliceAsset> owningAsset = m_component->m_myAsset ?
1084
- Data::Asset<SliceAsset>(Data::AssetManager::Instance ().FindAsset (m_component->m_myAsset ->GetId (), AZ::Data::AssetLoadBehavior::Default)) :
1085
- Data::Asset<SliceAsset>();
1086
- AZ_Error (" Slice" , false ,
1087
- " Instantiation of %d slice instance(s) of asset %s failed - asset not ready or not found during instantiation of owning slice %s!"
1088
- " Saving owning slice will lose data for these instances." ,
1089
- m_instances.size (),
1090
- m_asset.ToString <AZStd::string>().c_str (),
1091
- m_component->m_myAsset ? owningAsset.ToString <AZStd::string>().c_str () : " [Could not find owning slice]" );
1089
+ const Data::Asset<SliceAsset> owningAsset = m_component->m_myAsset ?
1090
+ Data::Asset<SliceAsset>(Data::AssetManager::Instance ().FindAsset (m_component->m_myAsset ->GetId (), AZ::Data::AssetLoadBehavior::Default)) :
1091
+ Data::Asset<SliceAsset>();
1092
+ AZ_Error (" Slice" , false ,
1093
+ " Instantiation of %d slice instance(s) of asset %s failed - asset not ready or not found during instantiation of owning slice %s!"
1094
+ " Saving owning slice will lose data for these instances." ,
1095
+ m_instances.size (),
1096
+ m_asset.ToString <AZStd::string>().c_str (),
1097
+ m_component->m_myAsset ? owningAsset.ToString <AZStd::string>().c_str () : " [Could not find owning slice]" );
1092
1098
#endif // AZ_ENABLE_TRACING
1099
+ return false ;
1100
+ }
1101
+ }
1102
+ dependentSlice = m_asset.Get ()->GetComponent ();
1103
+ }
1104
+ else
1105
+ {
1106
+ const AZStd::string& path = (*relativeToAbsoluteSlicePaths)[m_asset.GetHint ()];
1107
+ if (path.empty ())
1108
+ {
1109
+ AZ_Error (" SliceReference::Instantiate" , false , " Failed to find Slice relative path from %s" , m_asset.GetHint ().c_str ());
1093
1110
return false ;
1094
1111
}
1112
+
1113
+ AZ::Entity* sliceRootEntity = AZ::EntityUtils::LoadRootEntityFromSlicePath (path.c_str (), serializeContext);
1114
+ dependentSlice = AZ::EntityUtils::FindFirstDerivedComponent<SliceComponent>(sliceRootEntity);
1115
+ m_asset.Get ()->SetData (sliceRootEntity, dependentSlice, false );
1095
1116
}
1096
1117
1097
- SliceComponent* dependentSlice = m_asset.Get ()->GetComponent ();
1098
- InstantiateResult instantiationResult = dependentSlice->Instantiate ();
1118
+ if (!dependentSlice)
1119
+ {
1120
+ AZ_Error (" SliceReference::Instantiate" , false , " Failed to get SliceComponent from %s" , m_asset.GetHint ().c_str ());
1121
+ return false ;
1122
+ }
1123
+
1124
+ InstantiateResult instantiationResult = dependentSlice->Instantiate (serializeContext, relativeToAbsoluteSlicePaths);
1099
1125
if (instantiationResult != InstantiateResult::Success)
1100
1126
{
1101
1127
#if defined(AZ_ENABLE_TRACING)
@@ -1653,7 +1679,9 @@ namespace AZ
1653
1679
// =========================================================================
1654
1680
// SliceComponent::Instantiate
1655
1681
// =========================================================================
1656
- SliceComponent::InstantiateResult SliceComponent::Instantiate ()
1682
+ SliceComponent::InstantiateResult SliceComponent::Instantiate (
1683
+ AZ::SerializeContext* serializeContext,
1684
+ AZStd::unordered_map<AZStd::string, AZStd::string>* relativeToAbsoluteSlicePaths)
1657
1685
{
1658
1686
AZ_PROFILE_FUNCTION (AzCore);
1659
1687
AZStd::unique_lock<AZStd::recursive_mutex> lock (m_instantiateMutex);
@@ -1684,7 +1712,8 @@ namespace AZ
1684
1712
}
1685
1713
else
1686
1714
{
1687
- bool instantiateSuccess = slice.Instantiate (AZ::ObjectStream::FilterDescriptor (m_assetLoadFilterCB, m_filterFlags));
1715
+ bool instantiateSuccess = slice.Instantiate (
1716
+ AZ::ObjectStream::FilterDescriptor (m_assetLoadFilterCB, m_filterFlags), serializeContext, relativeToAbsoluteSlicePaths);
1688
1717
if (instantiateSuccess)
1689
1718
{
1690
1719
// Prune empty slice instances.
@@ -3956,4 +3985,40 @@ namespace AZ
3956
3985
;
3957
3986
}
3958
3987
}
3988
+
3989
+ namespace EntityUtils
3990
+ {
3991
+ AZ::Entity* LoadRootEntityFromSlicePath (const char * filePath, SerializeContext* context)
3992
+ {
3993
+ AZ::Entity* sliceRootEntity = nullptr ;
3994
+ auto callback = [&sliceRootEntity](void * classPtr, const Uuid& classId, SerializeContext* serializeContext)
3995
+ {
3996
+ sliceRootEntity = serializeContext->Cast <AZ::Entity*>(classPtr, classId);
3997
+ if (!sliceRootEntity)
3998
+ {
3999
+ AZ_Warning (" LoadRootEntityFromSlicePath" , false , " File not opened: Slice root is not an entity.\n " );
4000
+ return false ;
4001
+ }
4002
+ return true ;
4003
+ };
4004
+
4005
+ if (!AZ::Utils::InspectSerializedFile (
4006
+ filePath,
4007
+ context,
4008
+ callback,
4009
+ [](const AZ::Data::AssetFilterInfo& filterInfo)
4010
+ {
4011
+ return (filterInfo.m_assetType == azrtti_typeid<AZ::SliceAsset>());
4012
+ }))
4013
+ {
4014
+ AZ_Warning (" LoadRootEntityFromSlicePath" , false , " Failed to load '%s'. File may not contain an object stream." , filePath);
4015
+ return nullptr ;
4016
+ }
4017
+
4018
+ return sliceRootEntity;
4019
+ }
4020
+ }
4021
+
3959
4022
} // namespace AZ
4023
+
4024
+
0 commit comments