RealtimeStyleTransferRuntime/Source/LyraGame/System/LyraSystemStatics.cpp

106 lines
3.9 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "LyraSystemStatics.h"
#include "Engine/Engine.h"
#include "Engine/World.h"
#include "Engine/AssetManager.h"
#include "LyraLogChannels.h"
#include "Components/MeshComponent.h"
#include "GameModes/LyraUserFacingExperienceDefinition.h"
TSoftObjectPtr<UObject> ULyraSystemStatics::GetTypedSoftObjectReferenceFromPrimaryAssetId(FPrimaryAssetId PrimaryAssetId, TSubclassOf<UObject> ExpectedAssetType)
{
if (UAssetManager* Manager = UAssetManager::GetIfValid())
{
FPrimaryAssetTypeInfo Info;
if (Manager->GetPrimaryAssetTypeInfo(PrimaryAssetId.PrimaryAssetType, Info) && !Info.bHasBlueprintClasses)
{
if (UClass* AssetClass = Info.AssetBaseClassLoaded)
{
if ((ExpectedAssetType == nullptr) || !AssetClass->IsChildOf(ExpectedAssetType))
{
return nullptr;
}
}
else
{
UE_LOG(LogLyra, Warning, TEXT("GetTypedSoftObjectReferenceFromPrimaryAssetId(%s, %s) - AssetBaseClassLoaded was unset so we couldn't validate it, returning null"),
*PrimaryAssetId.ToString(),
*GetPathNameSafe(*ExpectedAssetType));
}
return TSoftObjectPtr<UObject>(Manager->GetPrimaryAssetPath(PrimaryAssetId));
}
}
return nullptr;
}
FPrimaryAssetId ULyraSystemStatics::GetPrimaryAssetIdFromUserFacingExperienceName(const FString& AdvertisedExperienceID)
{
const FPrimaryAssetType Type(ULyraUserFacingExperienceDefinition::StaticClass()->GetFName());
return FPrimaryAssetId(Type, FName(*AdvertisedExperienceID));
}
void ULyraSystemStatics::PlayNextGame(const UObject* WorldContextObject)
{
UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
if (World == nullptr)
{
return;
}
const FWorldContext& WorldContext = GEngine->GetWorldContextFromWorldChecked(World);
FURL LastURL = WorldContext.LastURL;
#if WITH_EDITOR
// To transition during PIE we need to strip the PIE prefix from maps.
LastURL.Map = UWorld::StripPIEPrefixFromPackageName(LastURL.Map, WorldContext.World()->StreamingLevelsPrefix);
#endif
FString URL = LastURL.ToString();
// If we don't remove the host/port info the server travel will fail.
URL.RemoveFromStart(LastURL.GetHostPortString());
const bool bAbsolute = false; // we want to use TRAVEL_Relative
const bool bShouldSkipGameNotify = false;
World->ServerTravel(URL, bAbsolute, bShouldSkipGameNotify);
}
void ULyraSystemStatics::SetScalarParameterValueOnAllMeshComponents(AActor* TargetActor, const FName ParameterName, const float ParameterValue, bool bIncludeChildActors)
{
if (TargetActor != nullptr)
{
TargetActor->ForEachComponent<UMeshComponent>(bIncludeChildActors, [=](UMeshComponent* InComponent)
{
InComponent->SetScalarParameterValueOnMaterials(ParameterName, ParameterValue);
});
}
}
void ULyraSystemStatics::SetVectorParameterValueOnAllMeshComponents(AActor* TargetActor, const FName ParameterName, const FVector ParameterValue, bool bIncludeChildActors)
{
if (TargetActor != nullptr)
{
TargetActor->ForEachComponent<UMeshComponent>(bIncludeChildActors, [=](UMeshComponent* InComponent)
{
InComponent->SetVectorParameterValueOnMaterials(ParameterName, ParameterValue);
});
}
}
void ULyraSystemStatics::SetColorParameterValueOnAllMeshComponents(AActor* TargetActor, const FName ParameterName, const FLinearColor ParameterValue, bool bIncludeChildActors)
{
SetVectorParameterValueOnAllMeshComponents(TargetActor, ParameterName, FVector(ParameterValue), bIncludeChildActors);
}
TArray<UActorComponent*> ULyraSystemStatics::FindComponentsByClass(AActor* TargetActor, TSubclassOf<UActorComponent> ComponentClass, bool bIncludeChildActors)
{
TArray<UActorComponent*> Components;
if (TargetActor != nullptr)
{
TargetActor->GetComponents(ComponentClass, /*out*/ Components, bIncludeChildActors);
}
return MoveTemp(Components);
}