|
|
|
|
| #include "LyraExperienceManagerComponent.h"
|
| #include "Engine/World.h"
|
| #include "Net/UnrealNetwork.h"
|
| #include "LyraExperienceDefinition.h"
|
| #include "LyraExperienceActionSet.h"
|
| #include "LyraExperienceManager.h"
|
| #include "GameFeaturesSubsystem.h"
|
| #include "System/LyraAssetManager.h"
|
| #include "GameFeatureAction.h"
|
| #include "GameFeaturesSubsystemSettings.h"
|
| #include "TimerManager.h"
|
| #include "Settings/LyraSettingsLocal.h"
|
| #include "LyraLogChannels.h"
|
|
|
| #include UE_INLINE_GENERATED_CPP_BY_NAME(LyraExperienceManagerComponent)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| namespace LyraConsoleVariables
|
| {
|
| static float ExperienceLoadRandomDelayMin = 0.0f;
|
| static FAutoConsoleVariableRef CVarExperienceLoadRandomDelayMin(
|
| TEXT("lyra.chaos.ExperienceDelayLoad.MinSecs"),
|
| ExperienceLoadRandomDelayMin,
|
| TEXT("This value (in seconds) will be added as a delay of load completion of the experience (along with the random value lyra.chaos.ExperienceDelayLoad.RandomSecs)"),
|
| ECVF_Default);
|
|
|
| static float ExperienceLoadRandomDelayRange = 0.0f;
|
| static FAutoConsoleVariableRef CVarExperienceLoadRandomDelayRange(
|
| TEXT("lyra.chaos.ExperienceDelayLoad.RandomSecs"),
|
| ExperienceLoadRandomDelayRange,
|
| TEXT("A random amount of time between 0 and this value (in seconds) will be added as a delay of load completion of the experience (along with the fixed value lyra.chaos.ExperienceDelayLoad.MinSecs)"),
|
| ECVF_Default);
|
|
|
| float GetExperienceLoadDelayDuration()
|
| {
|
| return FMath::Max(0.0f, ExperienceLoadRandomDelayMin + FMath::FRand() * ExperienceLoadRandomDelayRange);
|
| }
|
| }
|
|
|
| ULyraExperienceManagerComponent::ULyraExperienceManagerComponent(const FObjectInitializer& ObjectInitializer)
|
| : Super(ObjectInitializer)
|
| {
|
| SetIsReplicatedByDefault(true);
|
| }
|
|
|
| void ULyraExperienceManagerComponent::SetCurrentExperience(FPrimaryAssetId ExperienceId)
|
| {
|
| ULyraAssetManager& AssetManager = ULyraAssetManager::Get();
|
| FSoftObjectPath AssetPath = AssetManager.GetPrimaryAssetPath(ExperienceId);
|
| TSubclassOf<ULyraExperienceDefinition> AssetClass = Cast<UClass>(AssetPath.TryLoad());
|
| check(AssetClass);
|
| const ULyraExperienceDefinition* Experience = GetDefault<ULyraExperienceDefinition>(AssetClass);
|
|
|
| check(Experience != nullptr);
|
| check(CurrentExperience == nullptr);
|
| CurrentExperience = Experience;
|
| StartExperienceLoad();
|
| }
|
|
|
| void ULyraExperienceManagerComponent::CallOrRegister_OnExperienceLoaded_HighPriority(FOnLyraExperienceLoaded::FDelegate&& Delegate)
|
| {
|
| if (IsExperienceLoaded())
|
| {
|
| Delegate.Execute(CurrentExperience);
|
| }
|
| else
|
| {
|
| OnExperienceLoaded_HighPriority.Add(MoveTemp(Delegate));
|
| }
|
| }
|
|
|
| void ULyraExperienceManagerComponent::CallOrRegister_OnExperienceLoaded(FOnLyraExperienceLoaded::FDelegate&& Delegate)
|
| {
|
| if (IsExperienceLoaded())
|
| {
|
| Delegate.Execute(CurrentExperience);
|
| }
|
| else
|
| {
|
| OnExperienceLoaded.Add(MoveTemp(Delegate));
|
| }
|
| }
|
|
|
| void ULyraExperienceManagerComponent::CallOrRegister_OnExperienceLoaded_LowPriority(FOnLyraExperienceLoaded::FDelegate&& Delegate)
|
| {
|
| if (IsExperienceLoaded())
|
| {
|
| Delegate.Execute(CurrentExperience);
|
| }
|
| else
|
| {
|
| OnExperienceLoaded_LowPriority.Add(MoveTemp(Delegate));
|
| }
|
| }
|
|
|
| const ULyraExperienceDefinition* ULyraExperienceManagerComponent::GetCurrentExperienceChecked() const
|
| {
|
| check(LoadState == ELyraExperienceLoadState::Loaded);
|
| check(CurrentExperience != nullptr);
|
| return CurrentExperience;
|
| }
|
|
|
| bool ULyraExperienceManagerComponent::IsExperienceLoaded() const
|
| {
|
| return (LoadState == ELyraExperienceLoadState::Loaded) && (CurrentExperience != nullptr);
|
| }
|
|
|
| void ULyraExperienceManagerComponent::OnRep_CurrentExperience()
|
| {
|
| StartExperienceLoad();
|
| }
|
|
|
| void ULyraExperienceManagerComponent::StartExperienceLoad()
|
| {
|
| check(CurrentExperience != nullptr);
|
| check(LoadState == ELyraExperienceLoadState::Unloaded);
|
|
|
| UE_LOG(LogLyraExperience, Log, TEXT("EXPERIENCE: StartExperienceLoad(CurrentExperience = %s, %s)"),
|
| *CurrentExperience->GetPrimaryAssetId().ToString(),
|
| *GetClientServerContextString(this));
|
|
|
| LoadState = ELyraExperienceLoadState::Loading;
|
|
|
| ULyraAssetManager& AssetManager = ULyraAssetManager::Get();
|
|
|
| TSet<FPrimaryAssetId> BundleAssetList;
|
| TSet<FSoftObjectPath> RawAssetList;
|
|
|
| BundleAssetList.Add(CurrentExperience->GetPrimaryAssetId());
|
| for (const TObjectPtr<ULyraExperienceActionSet>& ActionSet : CurrentExperience->ActionSets)
|
| {
|
| if (ActionSet != nullptr)
|
| {
|
| BundleAssetList.Add(ActionSet->GetPrimaryAssetId());
|
| }
|
| }
|
|
|
|
|
|
|
| TArray<FName> BundlesToLoad;
|
| BundlesToLoad.Add(FLyraBundles::Equipped);
|
|
|
|
|
| const ENetMode OwnerNetMode = GetOwner()->GetNetMode();
|
| const bool bLoadClient = GIsEditor || (OwnerNetMode != NM_DedicatedServer);
|
| const bool bLoadServer = GIsEditor || (OwnerNetMode != NM_Client);
|
| if (bLoadClient)
|
| {
|
| BundlesToLoad.Add(UGameFeaturesSubsystemSettings::LoadStateClient);
|
| }
|
| if (bLoadServer)
|
| {
|
| BundlesToLoad.Add(UGameFeaturesSubsystemSettings::LoadStateServer);
|
| }
|
|
|
| TSharedPtr<FStreamableHandle> BundleLoadHandle = nullptr;
|
| if (BundleAssetList.Num() > 0)
|
| {
|
| BundleLoadHandle = AssetManager.ChangeBundleStateForPrimaryAssets(BundleAssetList.Array(), BundlesToLoad, {}, false, FStreamableDelegate(), FStreamableManager::AsyncLoadHighPriority);
|
| }
|
|
|
| TSharedPtr<FStreamableHandle> RawLoadHandle = nullptr;
|
| if (RawAssetList.Num() > 0)
|
| {
|
| RawLoadHandle = AssetManager.LoadAssetList(RawAssetList.Array(), FStreamableDelegate(), FStreamableManager::AsyncLoadHighPriority, TEXT("StartExperienceLoad()"));
|
| }
|
|
|
|
|
| TSharedPtr<FStreamableHandle> Handle = nullptr;
|
| if (BundleLoadHandle.IsValid() && RawLoadHandle.IsValid())
|
| {
|
| Handle = AssetManager.GetStreamableManager().CreateCombinedHandle({ BundleLoadHandle, RawLoadHandle });
|
| }
|
| else
|
| {
|
| Handle = BundleLoadHandle.IsValid() ? BundleLoadHandle : RawLoadHandle;
|
| }
|
|
|
| FStreamableDelegate OnAssetsLoadedDelegate = FStreamableDelegate::CreateUObject(this, &ThisClass::OnExperienceLoadComplete);
|
| if (!Handle.IsValid() || Handle->HasLoadCompleted())
|
| {
|
|
|
| FStreamableHandle::ExecuteDelegate(OnAssetsLoadedDelegate);
|
| }
|
| else
|
| {
|
| Handle->BindCompleteDelegate(OnAssetsLoadedDelegate);
|
|
|
| Handle->BindCancelDelegate(FStreamableDelegate::CreateLambda([OnAssetsLoadedDelegate]()
|
| {
|
| OnAssetsLoadedDelegate.ExecuteIfBound();
|
| }));
|
| }
|
|
|
|
|
| TSet<FPrimaryAssetId> PreloadAssetList;
|
|
|
| if (PreloadAssetList.Num() > 0)
|
| {
|
| AssetManager.ChangeBundleStateForPrimaryAssets(PreloadAssetList.Array(), BundlesToLoad, {});
|
| }
|
| }
|
|
|
| void ULyraExperienceManagerComponent::OnExperienceLoadComplete()
|
| {
|
| check(LoadState == ELyraExperienceLoadState::Loading);
|
| check(CurrentExperience != nullptr);
|
|
|
| UE_LOG(LogLyraExperience, Log, TEXT("EXPERIENCE: OnExperienceLoadComplete(CurrentExperience = %s, %s)"),
|
| *CurrentExperience->GetPrimaryAssetId().ToString(),
|
| *GetClientServerContextString(this));
|
|
|
|
|
| GameFeaturePluginURLs.Reset();
|
|
|
| auto CollectGameFeaturePluginURLs = [This=this](const UPrimaryDataAsset* Context, const TArray<FString>& FeaturePluginList)
|
| {
|
| for (const FString& PluginName : FeaturePluginList)
|
| {
|
| FString PluginURL;
|
| if (UGameFeaturesSubsystem::Get().GetPluginURLByName(PluginName, PluginURL))
|
| {
|
| This->GameFeaturePluginURLs.AddUnique(PluginURL);
|
| }
|
| else
|
| {
|
| ensureMsgf(false, TEXT("OnExperienceLoadComplete failed to find plugin URL from PluginName %s for experience %s - fix data, ignoring for this run"), *PluginName, *Context->GetPrimaryAssetId().ToString());
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| };
|
|
|
| CollectGameFeaturePluginURLs(CurrentExperience, CurrentExperience->GameFeaturesToEnable);
|
| for (const TObjectPtr<ULyraExperienceActionSet>& ActionSet : CurrentExperience->ActionSets)
|
| {
|
| if (ActionSet != nullptr)
|
| {
|
| CollectGameFeaturePluginURLs(ActionSet, ActionSet->GameFeaturesToEnable);
|
| }
|
| }
|
|
|
|
|
| NumGameFeaturePluginsLoading = GameFeaturePluginURLs.Num();
|
| if (NumGameFeaturePluginsLoading > 0)
|
| {
|
| LoadState = ELyraExperienceLoadState::LoadingGameFeatures;
|
| for (const FString& PluginURL : GameFeaturePluginURLs)
|
| {
|
| ULyraExperienceManager::NotifyOfPluginActivation(PluginURL);
|
| UGameFeaturesSubsystem::Get().LoadAndActivateGameFeaturePlugin(PluginURL, FGameFeaturePluginLoadComplete::CreateUObject(this, &ThisClass::OnGameFeaturePluginLoadComplete));
|
| }
|
| }
|
| else
|
| {
|
| OnExperienceFullLoadCompleted();
|
| }
|
| }
|
|
|
| void ULyraExperienceManagerComponent::OnGameFeaturePluginLoadComplete(const UE::GameFeatures::FResult& Result)
|
| {
|
|
|
| NumGameFeaturePluginsLoading--;
|
|
|
| if (NumGameFeaturePluginsLoading == 0)
|
| {
|
| OnExperienceFullLoadCompleted();
|
| }
|
| }
|
|
|
| void ULyraExperienceManagerComponent::OnExperienceFullLoadCompleted()
|
| {
|
| check(LoadState != ELyraExperienceLoadState::Loaded);
|
|
|
|
|
| if (LoadState != ELyraExperienceLoadState::LoadingChaosTestingDelay)
|
| {
|
| const float DelaySecs = LyraConsoleVariables::GetExperienceLoadDelayDuration();
|
| if (DelaySecs > 0.0f)
|
| {
|
| FTimerHandle DummyHandle;
|
|
|
| LoadState = ELyraExperienceLoadState::LoadingChaosTestingDelay;
|
| GetWorld()->GetTimerManager().SetTimer(DummyHandle, this, &ThisClass::OnExperienceFullLoadCompleted, DelaySecs, false);
|
|
|
| return;
|
| }
|
| }
|
|
|
| LoadState = ELyraExperienceLoadState::ExecutingActions;
|
|
|
|
|
| FGameFeatureActivatingContext Context;
|
|
|
|
|
| const FWorldContext* ExistingWorldContext = GEngine->GetWorldContextFromWorld(GetWorld());
|
| if (ExistingWorldContext)
|
| {
|
| Context.SetRequiredWorldContextHandle(ExistingWorldContext->ContextHandle);
|
| }
|
|
|
| auto ActivateListOfActions = [&Context](const TArray<UGameFeatureAction*>& ActionList)
|
| {
|
| for (UGameFeatureAction* Action : ActionList)
|
| {
|
| if (Action != nullptr)
|
| {
|
|
|
|
|
|
|
| Action->OnGameFeatureRegistering();
|
| Action->OnGameFeatureLoading();
|
| Action->OnGameFeatureActivating(Context);
|
| }
|
| }
|
| };
|
|
|
| ActivateListOfActions(CurrentExperience->Actions);
|
| for (const TObjectPtr<ULyraExperienceActionSet>& ActionSet : CurrentExperience->ActionSets)
|
| {
|
| if (ActionSet != nullptr)
|
| {
|
| ActivateListOfActions(ActionSet->Actions);
|
| }
|
| }
|
|
|
| LoadState = ELyraExperienceLoadState::Loaded;
|
|
|
| OnExperienceLoaded_HighPriority.Broadcast(CurrentExperience);
|
| OnExperienceLoaded_HighPriority.Clear();
|
|
|
| OnExperienceLoaded.Broadcast(CurrentExperience);
|
| OnExperienceLoaded.Clear();
|
|
|
| OnExperienceLoaded_LowPriority.Broadcast(CurrentExperience);
|
| OnExperienceLoaded_LowPriority.Clear();
|
|
|
|
|
| #if !UE_SERVER
|
| ULyraSettingsLocal::Get()->OnExperienceLoaded();
|
| #endif
|
| }
|
|
|
| void ULyraExperienceManagerComponent::OnActionDeactivationCompleted()
|
| {
|
| check(IsInGameThread());
|
| ++NumObservedPausers;
|
|
|
| if (NumObservedPausers == NumExpectedPausers)
|
| {
|
| OnAllActionsDeactivated();
|
| }
|
| }
|
|
|
| void ULyraExperienceManagerComponent::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
|
| {
|
| Super::GetLifetimeReplicatedProps(OutLifetimeProps);
|
|
|
| DOREPLIFETIME(ThisClass, CurrentExperience);
|
| }
|
|
|
| void ULyraExperienceManagerComponent::EndPlay(const EEndPlayReason::Type EndPlayReason)
|
| {
|
| Super::EndPlay(EndPlayReason);
|
|
|
|
|
|
|
| for (const FString& PluginURL : GameFeaturePluginURLs)
|
| {
|
| if (ULyraExperienceManager::RequestToDeactivatePlugin(PluginURL))
|
| {
|
| UGameFeaturesSubsystem::Get().DeactivateGameFeaturePlugin(PluginURL);
|
| }
|
| }
|
|
|
|
|
| if (LoadState == ELyraExperienceLoadState::Loaded)
|
| {
|
| LoadState = ELyraExperienceLoadState::Deactivating;
|
|
|
|
|
| NumExpectedPausers = INDEX_NONE;
|
| NumObservedPausers = 0;
|
|
|
|
|
| FGameFeatureDeactivatingContext Context(TEXT(""), [this](FStringView) { this->OnActionDeactivationCompleted(); });
|
|
|
| const FWorldContext* ExistingWorldContext = GEngine->GetWorldContextFromWorld(GetWorld());
|
| if (ExistingWorldContext)
|
| {
|
| Context.SetRequiredWorldContextHandle(ExistingWorldContext->ContextHandle);
|
| }
|
|
|
| auto DeactivateListOfActions = [&Context](const TArray<UGameFeatureAction*>& ActionList)
|
| {
|
| for (UGameFeatureAction* Action : ActionList)
|
| {
|
| if (Action)
|
| {
|
| Action->OnGameFeatureDeactivating(Context);
|
| Action->OnGameFeatureUnregistering();
|
| }
|
| }
|
| };
|
|
|
| DeactivateListOfActions(CurrentExperience->Actions);
|
| for (const TObjectPtr<ULyraExperienceActionSet>& ActionSet : CurrentExperience->ActionSets)
|
| {
|
| if (ActionSet != nullptr)
|
| {
|
| DeactivateListOfActions(ActionSet->Actions);
|
| }
|
| }
|
|
|
| NumExpectedPausers = Context.GetNumPausers();
|
|
|
| if (NumExpectedPausers > 0)
|
| {
|
| UE_LOG(LogLyraExperience, Error, TEXT("Actions that have asynchronous deactivation aren't fully supported yet in Lyra experiences"));
|
| }
|
|
|
| if (NumExpectedPausers == NumObservedPausers)
|
| {
|
| OnAllActionsDeactivated();
|
| }
|
| }
|
| }
|
|
|
| bool ULyraExperienceManagerComponent::ShouldShowLoadingScreen(FString& OutReason) const
|
| {
|
| if (LoadState != ELyraExperienceLoadState::Loaded)
|
| {
|
| OutReason = TEXT("Experience still loading");
|
| return true;
|
| }
|
| else
|
| {
|
| return false;
|
| }
|
| }
|
|
|
| void ULyraExperienceManagerComponent::OnAllActionsDeactivated()
|
| {
|
|
|
| LoadState = ELyraExperienceLoadState::Unloaded;
|
| CurrentExperience = nullptr;
|
|
|
| }
|
|
|
|
|