File size: 8,688 Bytes
7fd553e | 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 | // Copyright Epic Games, Inc. All Rights Reserved.
#include "LyraAssetManager.h"
#include "LyraLogChannels.h"
#include "LyraGameplayTags.h"
#include "LyraGameData.h"
#include "AbilitySystemGlobals.h"
#include "Character/LyraPawnData.h"
#include "Misc/App.h"
#include "Stats/StatsMisc.h"
#include "Engine/Engine.h"
#include "AbilitySystem/LyraGameplayCueManager.h"
#include "Misc/ScopedSlowTask.h"
#include "System/LyraAssetManagerStartupJob.h"
#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraAssetManager)
const FName FLyraBundles::Equipped("Equipped");
//////////////////////////////////////////////////////////////////////
static FAutoConsoleCommand CVarDumpLoadedAssets(
TEXT("Lyra.DumpLoadedAssets"),
TEXT("Shows all assets that were loaded via the asset manager and are currently in memory."),
FConsoleCommandDelegate::CreateStatic(ULyraAssetManager::DumpLoadedAssets)
);
//////////////////////////////////////////////////////////////////////
#define STARTUP_JOB_WEIGHTED(JobFunc, JobWeight) StartupJobs.Add(FLyraAssetManagerStartupJob(#JobFunc, [this](const FLyraAssetManagerStartupJob& StartupJob, TSharedPtr<FStreamableHandle>& LoadHandle){JobFunc;}, JobWeight))
#define STARTUP_JOB(JobFunc) STARTUP_JOB_WEIGHTED(JobFunc, 1.f)
//////////////////////////////////////////////////////////////////////
ULyraAssetManager::ULyraAssetManager()
{
DefaultPawnData = nullptr;
}
ULyraAssetManager& ULyraAssetManager::Get()
{
check(GEngine);
if (ULyraAssetManager* Singleton = Cast<ULyraAssetManager>(GEngine->AssetManager))
{
return *Singleton;
}
UE_LOG(LogLyra, Fatal, TEXT("Invalid AssetManagerClassName in DefaultEngine.ini. It must be set to LyraAssetManager!"));
// Fatal error above prevents this from being called.
return *NewObject<ULyraAssetManager>();
}
UObject* ULyraAssetManager::SynchronousLoadAsset(const FSoftObjectPath& AssetPath)
{
if (AssetPath.IsValid())
{
TUniquePtr<FScopeLogTime> LogTimePtr;
if (ShouldLogAssetLoads())
{
LogTimePtr = MakeUnique<FScopeLogTime>(*FString::Printf(TEXT("Synchronously loaded asset [%s]"), *AssetPath.ToString()), nullptr, FScopeLogTime::ScopeLog_Seconds);
}
if (UAssetManager::IsInitialized())
{
return UAssetManager::GetStreamableManager().LoadSynchronous(AssetPath, false);
}
// Use LoadObject if asset manager isn't ready yet.
return AssetPath.TryLoad();
}
return nullptr;
}
bool ULyraAssetManager::ShouldLogAssetLoads()
{
static bool bLogAssetLoads = FParse::Param(FCommandLine::Get(), TEXT("LogAssetLoads"));
return bLogAssetLoads;
}
void ULyraAssetManager::AddLoadedAsset(const UObject* Asset)
{
if (ensureAlways(Asset))
{
FScopeLock LoadedAssetsLock(&LoadedAssetsCritical);
LoadedAssets.Add(Asset);
}
}
void ULyraAssetManager::DumpLoadedAssets()
{
UE_LOG(LogLyra, Log, TEXT("========== Start Dumping Loaded Assets =========="));
for (const UObject* LoadedAsset : Get().LoadedAssets)
{
UE_LOG(LogLyra, Log, TEXT(" %s"), *GetNameSafe(LoadedAsset));
}
UE_LOG(LogLyra, Log, TEXT("... %d assets in loaded pool"), Get().LoadedAssets.Num());
UE_LOG(LogLyra, Log, TEXT("========== Finish Dumping Loaded Assets =========="));
}
void ULyraAssetManager::StartInitialLoading()
{
SCOPED_BOOT_TIMING("ULyraAssetManager::StartInitialLoading");
// This does all of the scanning, need to do this now even if loads are deferred
Super::StartInitialLoading();
STARTUP_JOB(InitializeGameplayCueManager());
{
// Load base game data asset
STARTUP_JOB_WEIGHTED(GetGameData(), 25.f);
}
// Run all the queued up startup jobs
DoAllStartupJobs();
}
void ULyraAssetManager::InitializeGameplayCueManager()
{
SCOPED_BOOT_TIMING("ULyraAssetManager::InitializeGameplayCueManager");
ULyraGameplayCueManager* GCM = ULyraGameplayCueManager::Get();
check(GCM);
GCM->LoadAlwaysLoadedCues();
}
const ULyraGameData& ULyraAssetManager::GetGameData()
{
return GetOrLoadTypedGameData<ULyraGameData>(LyraGameDataPath);
}
const ULyraPawnData* ULyraAssetManager::GetDefaultPawnData() const
{
return GetAsset(DefaultPawnData);
}
UPrimaryDataAsset* ULyraAssetManager::LoadGameDataOfClass(TSubclassOf<UPrimaryDataAsset> DataClass, const TSoftObjectPtr<UPrimaryDataAsset>& DataClassPath, FPrimaryAssetType PrimaryAssetType)
{
UPrimaryDataAsset* Asset = nullptr;
DECLARE_SCOPE_CYCLE_COUNTER(TEXT("Loading GameData Object"), STAT_GameData, STATGROUP_LoadTime);
if (!DataClassPath.IsNull())
{
#if WITH_EDITOR
FScopedSlowTask SlowTask(0, FText::Format(NSLOCTEXT("LyraEditor", "BeginLoadingGameDataTask", "Loading GameData {0}"), FText::FromName(DataClass->GetFName())));
const bool bShowCancelButton = false;
const bool bAllowInPIE = true;
SlowTask.MakeDialog(bShowCancelButton, bAllowInPIE);
#endif
UE_LOG(LogLyra, Log, TEXT("Loading GameData: %s ..."), *DataClassPath.ToString());
SCOPE_LOG_TIME_IN_SECONDS(TEXT(" ... GameData loaded!"), nullptr);
// This can be called recursively in the editor because it is called on demand from PostLoad so force a sync load for primary asset and async load the rest in that case
if (GIsEditor)
{
Asset = DataClassPath.LoadSynchronous();
LoadPrimaryAssetsWithType(PrimaryAssetType);
}
else
{
TSharedPtr<FStreamableHandle> Handle = LoadPrimaryAssetsWithType(PrimaryAssetType);
if (Handle.IsValid())
{
Handle->WaitUntilComplete(0.0f, false);
// This should always work
Asset = Cast<UPrimaryDataAsset>(Handle->GetLoadedAsset());
}
}
}
if (Asset)
{
GameDataMap.Add(DataClass, Asset);
}
else
{
// It is not acceptable to fail to load any GameData asset. It will result in soft failures that are hard to diagnose.
UE_LOG(LogLyra, Fatal, TEXT("Failed to load GameData asset at %s. Type %s. This is not recoverable and likely means you do not have the correct data to run %s."), *DataClassPath.ToString(), *PrimaryAssetType.ToString(), FApp::GetProjectName());
}
return Asset;
}
void ULyraAssetManager::DoAllStartupJobs()
{
SCOPED_BOOT_TIMING("ULyraAssetManager::DoAllStartupJobs");
const double AllStartupJobsStartTime = FPlatformTime::Seconds();
if (IsRunningDedicatedServer())
{
// No need for periodic progress updates, just run the jobs
for (const FLyraAssetManagerStartupJob& StartupJob : StartupJobs)
{
StartupJob.DoJob();
}
}
else
{
if (StartupJobs.Num() > 0)
{
float TotalJobValue = 0.0f;
for (const FLyraAssetManagerStartupJob& StartupJob : StartupJobs)
{
TotalJobValue += StartupJob.JobWeight;
}
float AccumulatedJobValue = 0.0f;
for (FLyraAssetManagerStartupJob& StartupJob : StartupJobs)
{
const float JobValue = StartupJob.JobWeight;
StartupJob.SubstepProgressDelegate.BindLambda([This = this, AccumulatedJobValue, JobValue, TotalJobValue](float NewProgress)
{
const float SubstepAdjustment = FMath::Clamp(NewProgress, 0.0f, 1.0f) * JobValue;
const float OverallPercentWithSubstep = (AccumulatedJobValue + SubstepAdjustment) / TotalJobValue;
This->UpdateInitialGameContentLoadPercent(OverallPercentWithSubstep);
});
StartupJob.DoJob();
StartupJob.SubstepProgressDelegate.Unbind();
AccumulatedJobValue += JobValue;
UpdateInitialGameContentLoadPercent(AccumulatedJobValue / TotalJobValue);
}
}
else
{
UpdateInitialGameContentLoadPercent(1.0f);
}
}
StartupJobs.Empty();
UE_LOG(LogLyra, Display, TEXT("All startup jobs took %.2f seconds to complete"), FPlatformTime::Seconds() - AllStartupJobsStartTime);
}
void ULyraAssetManager::UpdateInitialGameContentLoadPercent(float GameContentPercent)
{
// Could route this to the early startup loading screen
}
#if WITH_EDITOR
void ULyraAssetManager::PreBeginPIE(bool bStartSimulate)
{
Super::PreBeginPIE(bStartSimulate);
{
FScopedSlowTask SlowTask(0, NSLOCTEXT("LyraEditor", "BeginLoadingPIEData", "Loading PIE Data"));
const bool bShowCancelButton = false;
const bool bAllowInPIE = true;
SlowTask.MakeDialog(bShowCancelButton, bAllowInPIE);
const ULyraGameData& LocalGameDataCommon = GetGameData();
// Intentionally after GetGameData to avoid counting GameData time in this timer
SCOPE_LOG_TIME_IN_SECONDS(TEXT("PreBeginPIE asset preloading complete"), nullptr);
// You could add preloading of anything else needed for the experience we'll be using here
// (e.g., by grabbing the default experience from the world settings + the experience override in developer settings)
}
}
#endif
|