|
| 1 | +// Pavel Gornostaev <https://github.com/Pavreally> |
| 2 | + |
| 3 | +#include "AsyncDataAssetManagerSubsystem.h" |
| 4 | + |
| 5 | +#include "Engine/AssetManager.h" |
| 6 | +#include "Engine/DataAsset.h" |
| 7 | +#include "AsyncTechnologiesSettings.h" |
| 8 | + |
| 9 | +TArray<FMirrorADAM> UAsyncDataAssetManagerSubsystem::GetDataADAM() |
| 10 | +{ |
| 11 | + int32 Length = DataADAM.Num(); |
| 12 | + TArray<FMirrorADAM> MirrorDataADAM; |
| 13 | + |
| 14 | + if (Length == 0) |
| 15 | + return MirrorDataADAM; |
| 16 | + |
| 17 | + for (int32 i = 0; i < Length; i++) |
| 18 | + { |
| 19 | + FMirrorADAM MirrorDataAsset; |
| 20 | + MirrorDataAsset.PrimaryDataAssetName = DataADAM[i].SoftReference.GetAssetName(); |
| 21 | + MirrorDataAsset.SoftReference = DataADAM[i].SoftReference; |
| 22 | + MirrorDataAsset.Tag = DataADAM[i].Tag; |
| 23 | + MirrorDataADAM.Add(MirrorDataAsset); |
| 24 | + } |
| 25 | + |
| 26 | + return MirrorDataADAM; |
| 27 | +} |
| 28 | + |
| 29 | +TMap<FName, int32> UAsyncDataAssetManagerSubsystem::GetCollectionByTagADAM() |
| 30 | +{ |
| 31 | + TMap<FName, int32> TagCollection; |
| 32 | + |
| 33 | + if (DataADAM.Num() == 0) |
| 34 | + return TagCollection; |
| 35 | + |
| 36 | + for (const FMemoryADAM& Data : DataADAM) |
| 37 | + { |
| 38 | + if (TagCollection.Contains(Data.Tag)) |
| 39 | + { |
| 40 | + TagCollection[Data.Tag]++; |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + TagCollection.Add(Data.Tag, 1); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return TagCollection; |
| 49 | +} |
| 50 | + |
| 51 | +UObject* UAsyncDataAssetManagerSubsystem::GetObjectDataADAM(TSoftObjectPtr<UPrimaryDataAsset> PrimaryDataAsset, bool& IsValid ) |
| 52 | +{ |
| 53 | + if (PrimaryDataAsset.IsNull()) |
| 54 | + { |
| 55 | + UE_LOG(LogTemp, Warning, TEXT("ADAM (Get Object): No reference is specified in function.")); |
| 56 | + |
| 57 | + return nullptr; |
| 58 | + } |
| 59 | + |
| 60 | + int32 ObjectIndex = GetIndexDataADAM(PrimaryDataAsset); |
| 61 | + |
| 62 | + if (ObjectIndex == -1) |
| 63 | + { |
| 64 | + UE_LOG(LogTemp, Warning, TEXT("ADAM (Get Object): The requested data asset \"%s\" is not in the memory of the ADAM subsystem."), *PrimaryDataAsset.GetAssetName()); |
| 65 | + |
| 66 | + return nullptr; |
| 67 | + } |
| 68 | + |
| 69 | + UObject* DataAsset = DataADAM[ObjectIndex].MemoryReference->GetLoadedAsset(); |
| 70 | + |
| 71 | + // Return bool value. Checking of Data Asset |
| 72 | + IsValid = DataAsset != nullptr; |
| 73 | + |
| 74 | + return DataAsset; |
| 75 | +} |
| 76 | + |
| 77 | +int32 UAsyncDataAssetManagerSubsystem::GetIndexDataADAM(TSoftObjectPtr<UPrimaryDataAsset> PrimaryDataAsset) |
| 78 | +{ |
| 79 | + for (int32 i = 0; i < DataADAM.Num(); i++) |
| 80 | + { |
| 81 | + if (DataADAM[i].SoftReference == PrimaryDataAsset) |
| 82 | + return i; |
| 83 | + } |
| 84 | + |
| 85 | + return -1; |
| 86 | +} |
| 87 | + |
| 88 | +TArray<TSoftObjectPtr<UPrimaryDataAsset>> UAsyncDataAssetManagerSubsystem::FindNestedAssets(UPrimaryDataAsset* DataAsset) |
| 89 | +{ |
| 90 | + TArray<TSoftObjectPtr<UPrimaryDataAsset>> NestedAssets; |
| 91 | + |
| 92 | + if (!DataAsset) |
| 93 | + { |
| 94 | + UE_LOG(LogTemp, Warning, TEXT("ADAM (Recursive Load): Received a null value.")); |
| 95 | + |
| 96 | + return NestedAssets; |
| 97 | + } |
| 98 | + |
| 99 | + // Storage of unique asset names for duplicate control. |
| 100 | + TArray<FString> UniqueAssetNames; |
| 101 | + |
| 102 | + // Use reflection to search for nested Data Assets |
| 103 | + for (TFieldIterator<FProperty> PropIterator(DataAsset->GetClass()); PropIterator; ++PropIterator) |
| 104 | + { |
| 105 | + const FProperty* Prop = *PropIterator; |
| 106 | + |
| 107 | + // Look for properties of type TSoftObjectPtr<UPrimaryDataAsset> |
| 108 | + if (const FSoftObjectProperty* SoftObjectProperty = CastField<FSoftObjectProperty>(Prop)) |
| 109 | + { |
| 110 | + if (SoftObjectProperty->PropertyClass->IsChildOf(UPrimaryDataAsset::StaticClass())) |
| 111 | + { |
| 112 | + TSoftObjectPtr<UPrimaryDataAsset> ChildAsset = *SoftObjectProperty->ContainerPtrToValuePtr<TSoftObjectPtr<UPrimaryDataAsset>>(DataAsset); |
| 113 | + FString ChildAssetName = ChildAsset.GetAssetName(); |
| 114 | + // Filtering and adding data |
| 115 | + if (!ChildAssetName.IsEmpty() && !UniqueAssetNames.Contains(ChildAssetName)) |
| 116 | + { |
| 117 | + UniqueAssetNames.AddUnique(ChildAssetName); |
| 118 | + NestedAssets.Add(ChildAsset); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // If the array of soft references |
| 124 | + if (const FArrayProperty* ArrayProp = CastField<FArrayProperty>(Prop)) |
| 125 | + { |
| 126 | + if (const FSoftObjectProperty* InnerSoftObjectProperty = CastField<FSoftObjectProperty>(ArrayProp->Inner)) |
| 127 | + { |
| 128 | + if (InnerSoftObjectProperty->PropertyClass->IsChildOf(UPrimaryDataAsset::StaticClass())) |
| 129 | + { |
| 130 | + FScriptArrayHelper ArrayHelper(ArrayProp, ArrayProp->ContainerPtrToValuePtr<void>(DataAsset)); |
| 131 | + |
| 132 | + for (int32 i = 0; i < ArrayHelper.Num(); ++i) |
| 133 | + { |
| 134 | + TSoftObjectPtr<UPrimaryDataAsset> ChildAsset = *reinterpret_cast<TSoftObjectPtr<UPrimaryDataAsset>*>(ArrayHelper.GetRawPtr(i)); |
| 135 | + FString ChildAssetName = ChildAsset.GetAssetName(); |
| 136 | + // Filtering and adding data |
| 137 | + if (!ChildAssetName.IsEmpty() && !UniqueAssetNames.Contains(ChildAssetName)) |
| 138 | + { |
| 139 | + UniqueAssetNames.AddUnique(ChildAssetName); |
| 140 | + NestedAssets.Add(ChildAsset); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + if (EnableLog && NestedAssets.Num() == 0) |
| 149 | + { |
| 150 | + UE_LOG(LogTemp, Display, TEXT("ADAM (Recursive data): Iteration is complete. All nested data is loaded!")); |
| 151 | + } |
| 152 | + |
| 153 | + return NestedAssets; |
| 154 | +} |
0 commit comments