File size: 13,766 Bytes
1e67697 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | // Copyright Epic Games, Inc. All Rights Reserved.
#include "LyraGameplayCueManager.h"
#include "Engine/AssetManager.h"
#include "LyraLogChannels.h"
#include "GameplayCueSet.h"
#include "AbilitySystemGlobals.h"
#include "GameplayTagsManager.h"
#include "UObject/UObjectThreadContext.h"
#include "Async/Async.h"
#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraGameplayCueManager)
//////////////////////////////////////////////////////////////////////
enum class ELyraEditorLoadMode
{
// Loads all cues upfront; longer loading speed in the editor but short PIE times and effects never fail to play
LoadUpfront,
// Outside of editor: Async loads as cue tag are registered
// In editor: Async loads when cues are invoked
// Note: This can cause some 'why didn't I see the effect for X' issues in PIE and is good for iteration speed but otherwise bad for designers
PreloadAsCuesAreReferenced_GameOnly,
// Async loads as cue tag are registered
PreloadAsCuesAreReferenced
};
namespace LyraGameplayCueManagerCvars
{
static FAutoConsoleCommand CVarDumpGameplayCues(
TEXT("Lyra.DumpGameplayCues"),
TEXT("Shows all assets that were loaded via LyraGameplayCueManager and are currently in memory."),
FConsoleCommandWithArgsDelegate::CreateStatic(ULyraGameplayCueManager::DumpGameplayCues));
static ELyraEditorLoadMode LoadMode = ELyraEditorLoadMode::LoadUpfront;
}
const bool bPreloadEvenInEditor = true;
//////////////////////////////////////////////////////////////////////
struct FGameplayCueTagThreadSynchronizeGraphTask : public FAsyncGraphTaskBase
{
TFunction<void()> TheTask;
FGameplayCueTagThreadSynchronizeGraphTask(TFunction<void()>&& Task) : TheTask(MoveTemp(Task)) { }
void DoTask(ENamedThreads::Type CurrentThread, const FGraphEventRef& MyCompletionGraphEvent) { TheTask(); }
ENamedThreads::Type GetDesiredThread() { return ENamedThreads::GameThread; }
};
//////////////////////////////////////////////////////////////////////
ULyraGameplayCueManager::ULyraGameplayCueManager(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
}
ULyraGameplayCueManager* ULyraGameplayCueManager::Get()
{
return Cast<ULyraGameplayCueManager>(UAbilitySystemGlobals::Get().GetGameplayCueManager());
}
void ULyraGameplayCueManager::OnCreated()
{
Super::OnCreated();
UpdateDelayLoadDelegateListeners();
}
void ULyraGameplayCueManager::LoadAlwaysLoadedCues()
{
if (ShouldDelayLoadGameplayCues())
{
UGameplayTagsManager& TagManager = UGameplayTagsManager::Get();
//@TODO: Try to collect these by filtering GameplayCue. tags out of native gameplay tags?
TArray<FName> AdditionalAlwaysLoadedCueTags;
for (const FName& CueTagName : AdditionalAlwaysLoadedCueTags)
{
FGameplayTag CueTag = TagManager.RequestGameplayTag(CueTagName, /*ErrorIfNotFound=*/ false);
if (CueTag.IsValid())
{
ProcessTagToPreload(CueTag, nullptr);
}
else
{
UE_LOG(LogLyra, Warning, TEXT("ULyraGameplayCueManager::AdditionalAlwaysLoadedCueTags contains invalid tag %s"), *CueTagName.ToString());
}
}
}
}
bool ULyraGameplayCueManager::ShouldAsyncLoadRuntimeObjectLibraries() const
{
switch (LyraGameplayCueManagerCvars::LoadMode)
{
case ELyraEditorLoadMode::LoadUpfront:
return true;
case ELyraEditorLoadMode::PreloadAsCuesAreReferenced_GameOnly:
#if WITH_EDITOR
if (GIsEditor)
{
return false;
}
#endif
break;
case ELyraEditorLoadMode::PreloadAsCuesAreReferenced:
break;
}
return !ShouldDelayLoadGameplayCues();
}
bool ULyraGameplayCueManager::ShouldSyncLoadMissingGameplayCues() const
{
return false;
}
bool ULyraGameplayCueManager::ShouldAsyncLoadMissingGameplayCues() const
{
return true;
}
void ULyraGameplayCueManager::DumpGameplayCues(const TArray<FString>& Args)
{
ULyraGameplayCueManager* GCM = Cast<ULyraGameplayCueManager>(UAbilitySystemGlobals::Get().GetGameplayCueManager());
if (!GCM)
{
UE_LOG(LogLyra, Error, TEXT("DumpGameplayCues failed. No ULyraGameplayCueManager found."));
return;
}
const bool bIncludeRefs = Args.Contains(TEXT("Refs"));
UE_LOG(LogLyra, Log, TEXT("=========== Dumping Always Loaded Gameplay Cue Notifies ==========="));
for (UClass* CueClass : GCM->AlwaysLoadedCues)
{
UE_LOG(LogLyra, Log, TEXT(" %s"), *GetPathNameSafe(CueClass));
}
UE_LOG(LogLyra, Log, TEXT("=========== Dumping Preloaded Gameplay Cue Notifies ==========="));
for (UClass* CueClass : GCM->PreloadedCues)
{
TSet<FObjectKey>* ReferencerSet = GCM->PreloadedCueReferencers.Find(CueClass);
int32 NumRefs = ReferencerSet ? ReferencerSet->Num() : 0;
UE_LOG(LogLyra, Log, TEXT(" %s (%d refs)"), *GetPathNameSafe(CueClass), NumRefs);
if (bIncludeRefs && ReferencerSet)
{
for (const FObjectKey& Ref : *ReferencerSet)
{
UObject* RefObject = Ref.ResolveObjectPtr();
UE_LOG(LogLyra, Log, TEXT(" ^- %s"), *GetPathNameSafe(RefObject));
}
}
}
UE_LOG(LogLyra, Log, TEXT("=========== Dumping Gameplay Cue Notifies loaded on demand ==========="));
int32 NumMissingCuesLoaded = 0;
if (GCM->RuntimeGameplayCueObjectLibrary.CueSet)
{
for (const FGameplayCueNotifyData& CueData : GCM->RuntimeGameplayCueObjectLibrary.CueSet->GameplayCueData)
{
if (CueData.LoadedGameplayCueClass && !GCM->AlwaysLoadedCues.Contains(CueData.LoadedGameplayCueClass) && !GCM->PreloadedCues.Contains(CueData.LoadedGameplayCueClass))
{
NumMissingCuesLoaded++;
UE_LOG(LogLyra, Log, TEXT(" %s"), *CueData.LoadedGameplayCueClass->GetPathName());
}
}
}
UE_LOG(LogLyra, Log, TEXT("=========== Gameplay Cue Notify summary ==========="));
UE_LOG(LogLyra, Log, TEXT(" ... %d cues in always loaded list"), GCM->AlwaysLoadedCues.Num());
UE_LOG(LogLyra, Log, TEXT(" ... %d cues in preloaded list"), GCM->PreloadedCues.Num());
UE_LOG(LogLyra, Log, TEXT(" ... %d cues loaded on demand"), NumMissingCuesLoaded);
UE_LOG(LogLyra, Log, TEXT(" ... %d cues in total"), GCM->AlwaysLoadedCues.Num() + GCM->PreloadedCues.Num() + NumMissingCuesLoaded);
}
void ULyraGameplayCueManager::OnGameplayTagLoaded(const FGameplayTag& Tag)
{
FScopeLock ScopeLock(&LoadedGameplayTagsToProcessCS);
bool bStartTask = LoadedGameplayTagsToProcess.Num() == 0;
FUObjectSerializeContext* LoadContext = FUObjectThreadContext::Get().GetSerializeContext();
UObject* OwningObject = LoadContext ? LoadContext->SerializedObject : nullptr;
LoadedGameplayTagsToProcess.Emplace(Tag, OwningObject);
if (bStartTask)
{
TGraphTask<FGameplayCueTagThreadSynchronizeGraphTask>::CreateTask().ConstructAndDispatchWhenReady([]()
{
if (GIsRunning)
{
if (ULyraGameplayCueManager* StrongThis = Get())
{
// If we are garbage collecting we cannot call StaticFindObject (or a few other static uobject functions), so we'll just wait until the GC is over and process the tags then
if (IsGarbageCollecting())
{
StrongThis->bProcessLoadedTagsAfterGC = true;
}
else
{
StrongThis->ProcessLoadedTags();
}
}
}
});
}
}
void ULyraGameplayCueManager::HandlePostGarbageCollect()
{
if (bProcessLoadedTagsAfterGC)
{
ProcessLoadedTags();
}
bProcessLoadedTagsAfterGC = false;
}
void ULyraGameplayCueManager::ProcessLoadedTags()
{
TArray<FLoadedGameplayTagToProcessData> TaskLoadedGameplayTagsToProcess;
{
// Lock LoadedGameplayTagsToProcess just long enough to make a copy and clear
FScopeLock TaskScopeLock(&LoadedGameplayTagsToProcessCS);
TaskLoadedGameplayTagsToProcess = LoadedGameplayTagsToProcess;
LoadedGameplayTagsToProcess.Empty();
}
// This might return during shutdown, and we don't want to proceed if that is the case
if (GIsRunning)
{
if (RuntimeGameplayCueObjectLibrary.CueSet)
{
for (const FLoadedGameplayTagToProcessData& LoadedTagData : TaskLoadedGameplayTagsToProcess)
{
if (RuntimeGameplayCueObjectLibrary.CueSet->GameplayCueDataMap.Contains(LoadedTagData.Tag))
{
if (!LoadedTagData.WeakOwner.IsStale())
{
ProcessTagToPreload(LoadedTagData.Tag, LoadedTagData.WeakOwner.Get());
}
}
}
}
else
{
UE_LOG(LogLyra, Warning, TEXT("ULyraGameplayCueManager::OnGameplayTagLoaded processed loaded tag(s) but RuntimeGameplayCueObjectLibrary.CueSet was null. Skipping processing."));
}
}
}
void ULyraGameplayCueManager::ProcessTagToPreload(const FGameplayTag& Tag, UObject* OwningObject)
{
switch (LyraGameplayCueManagerCvars::LoadMode)
{
case ELyraEditorLoadMode::LoadUpfront:
return;
case ELyraEditorLoadMode::PreloadAsCuesAreReferenced_GameOnly:
#if WITH_EDITOR
if (GIsEditor)
{
return;
}
#endif
break;
case ELyraEditorLoadMode::PreloadAsCuesAreReferenced:
break;
}
check(RuntimeGameplayCueObjectLibrary.CueSet);
int32* DataIdx = RuntimeGameplayCueObjectLibrary.CueSet->GameplayCueDataMap.Find(Tag);
if (DataIdx && RuntimeGameplayCueObjectLibrary.CueSet->GameplayCueData.IsValidIndex(*DataIdx))
{
const FGameplayCueNotifyData& CueData = RuntimeGameplayCueObjectLibrary.CueSet->GameplayCueData[*DataIdx];
UClass* LoadedGameplayCueClass = FindObject<UClass>(nullptr, *CueData.GameplayCueNotifyObj.ToString());
if (LoadedGameplayCueClass)
{
RegisterPreloadedCue(LoadedGameplayCueClass, OwningObject);
}
else
{
bool bAlwaysLoadedCue = OwningObject == nullptr;
TWeakObjectPtr<UObject> WeakOwner = OwningObject;
StreamableManager.RequestAsyncLoad(CueData.GameplayCueNotifyObj, FStreamableDelegate::CreateUObject(this, &ThisClass::OnPreloadCueComplete, CueData.GameplayCueNotifyObj, WeakOwner, bAlwaysLoadedCue), FStreamableManager::DefaultAsyncLoadPriority, false, false, TEXT("GameplayCueManager"));
}
}
}
void ULyraGameplayCueManager::OnPreloadCueComplete(FSoftObjectPath Path, TWeakObjectPtr<UObject> OwningObject, bool bAlwaysLoadedCue)
{
if (bAlwaysLoadedCue || OwningObject.IsValid())
{
if (UClass* LoadedGameplayCueClass = Cast<UClass>(Path.ResolveObject()))
{
RegisterPreloadedCue(LoadedGameplayCueClass, OwningObject.Get());
}
}
}
void ULyraGameplayCueManager::RegisterPreloadedCue(UClass* LoadedGameplayCueClass, UObject* OwningObject)
{
check(LoadedGameplayCueClass);
const bool bAlwaysLoadedCue = OwningObject == nullptr;
if (bAlwaysLoadedCue)
{
AlwaysLoadedCues.Add(LoadedGameplayCueClass);
PreloadedCues.Remove(LoadedGameplayCueClass);
PreloadedCueReferencers.Remove(LoadedGameplayCueClass);
}
else if ((OwningObject != LoadedGameplayCueClass) && (OwningObject != LoadedGameplayCueClass->GetDefaultObject()) && !AlwaysLoadedCues.Contains(LoadedGameplayCueClass))
{
PreloadedCues.Add(LoadedGameplayCueClass);
TSet<FObjectKey>& ReferencerSet = PreloadedCueReferencers.FindOrAdd(LoadedGameplayCueClass);
ReferencerSet.Add(OwningObject);
}
}
void ULyraGameplayCueManager::HandlePostLoadMap(UWorld* NewWorld)
{
if (RuntimeGameplayCueObjectLibrary.CueSet)
{
for (UClass* CueClass : AlwaysLoadedCues)
{
RuntimeGameplayCueObjectLibrary.CueSet->RemoveLoadedClass(CueClass);
}
for (UClass* CueClass : PreloadedCues)
{
RuntimeGameplayCueObjectLibrary.CueSet->RemoveLoadedClass(CueClass);
}
}
for (auto CueIt = PreloadedCues.CreateIterator(); CueIt; ++CueIt)
{
TSet<FObjectKey>& ReferencerSet = PreloadedCueReferencers.FindChecked(*CueIt);
for (auto RefIt = ReferencerSet.CreateIterator(); RefIt; ++RefIt)
{
if (!RefIt->ResolveObjectPtr())
{
RefIt.RemoveCurrent();
}
}
if (ReferencerSet.Num() == 0)
{
PreloadedCueReferencers.Remove(*CueIt);
CueIt.RemoveCurrent();
}
}
}
void ULyraGameplayCueManager::UpdateDelayLoadDelegateListeners()
{
UGameplayTagsManager::Get().OnGameplayTagLoadedDelegate.RemoveAll(this);
FCoreUObjectDelegates::GetPostGarbageCollect().RemoveAll(this);
FCoreUObjectDelegates::PostLoadMapWithWorld.RemoveAll(this);
switch (LyraGameplayCueManagerCvars::LoadMode)
{
case ELyraEditorLoadMode::LoadUpfront:
return;
case ELyraEditorLoadMode::PreloadAsCuesAreReferenced_GameOnly:
#if WITH_EDITOR
if (GIsEditor)
{
return;
}
#endif
break;
case ELyraEditorLoadMode::PreloadAsCuesAreReferenced:
break;
}
UGameplayTagsManager::Get().OnGameplayTagLoadedDelegate.AddUObject(this, &ThisClass::OnGameplayTagLoaded);
FCoreUObjectDelegates::GetPostGarbageCollect().AddUObject(this, &ThisClass::HandlePostGarbageCollect);
FCoreUObjectDelegates::PostLoadMapWithWorld.AddUObject(this, &ThisClass::HandlePostLoadMap);
}
bool ULyraGameplayCueManager::ShouldDelayLoadGameplayCues() const
{
const bool bClientDelayLoadGameplayCues = true;
return !IsRunningDedicatedServer() && bClientDelayLoadGameplayCues;
}
const FPrimaryAssetType UFortAssetManager_GameplayCueRefsType = TEXT("GameplayCueRefs");
const FName UFortAssetManager_GameplayCueRefsName = TEXT("GameplayCueReferences");
const FName UFortAssetManager_LoadStateClient = FName(TEXT("Client"));
void ULyraGameplayCueManager::RefreshGameplayCuePrimaryAsset()
{
TArray<FSoftObjectPath> CuePaths;
UGameplayCueSet* RuntimeGameplayCueSet = GetRuntimeCueSet();
if (RuntimeGameplayCueSet)
{
RuntimeGameplayCueSet->GetSoftObjectPaths(CuePaths);
}
FAssetBundleData BundleData;
BundleData.AddBundleAssetsTruncated(UFortAssetManager_LoadStateClient, CuePaths);
FPrimaryAssetId PrimaryAssetId = FPrimaryAssetId(UFortAssetManager_GameplayCueRefsType, UFortAssetManager_GameplayCueRefsName);
UAssetManager::Get().AddDynamicAsset(PrimaryAssetId, FSoftObjectPath(), BundleData);
}
|