// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "Kismet/GameplayStatics.h" #include "ProjectT/ProjectT.h" #include "Kismet/BlueprintFunctionLibrary.h" #include "Engine/AssetManager.h" #include "Engine/StreamableManager.h" #include "ProjectT/Data/Gen/GenerateEnumContentType.h" #include "GlobalUtilsLibrary.generated.h" struct FNActionData; enum class EDataTableKey : uint8; UENUM(BlueprintType) enum class EFindAngleAxis :uint8 { EForward UMETA(DisplayName="EForward"), EUp UMETA(DisplayName="EUp"), ERight UMETA(DisplayName="ERight") }; UCLASS() class PROJECTT_API UGlobalUtilsLibrary : public UBlueprintFunctionLibrary { GENERATED_BODY() public: static UWorld* GetValidWorld(UObject* Object); static class UDataDam* GetSharedDataDam(UObject* Object); static class UStageManager* GetSharedLevelManager(UObject* Object); static class UOptionManager* GetSharedOptionManager(UObject* Object); static class USoundManager* GetSharedSoundManager(UObject* Object); static class UCoreCheatManager* GetSharedCoreCheatManager(UObject* Object); static class UNotificationCenter* GetSharedNotificationCenter(UObject* Object); static bool IsStandalonePlay(UObject* Object); static TArray ParseStringIndices(const TCHAR* Delimiter, const FString& Indices); static TArray ParseStringIndices(const FString& Indices, const FString& Delimeter); UFUNCTION(BlueprintCallable) static FString DivideString(const int Count, FString& InString); static int32 GetEnumIndexFromString(EDataTableKey InEnum, FString InString); static FString Uint32ToBase64String(uint32 Value); template static FString GetEnumToString(EnumType FindEnumData) { const UEnum* FindPtr = StaticEnum(); if (!FindPtr) return FString(); const int32 Idx = static_cast(FindEnumData); if (Idx == 0) return FString(); FString Ret = FindPtr->GetNameStringByIndex(Idx); return Ret; } template static EnumType GetStringToEnum(const FString& FindEnumTypeString) { EnumType Ret = StaticCast(0); for (EnumType e : TEnumRange()) { FString Name = UGlobalUtilsLibrary::GetEnumToString(e); if (FindEnumTypeString.Equals(Name)) return e; } return Ret; } template static TSoftClassPtr GetStringToSoftClass(FString& InString) { FSoftClassPath SoftClassPath(InString.Append("_C")); return TSoftClassPtr(SoftClassPath); } template static void AsyncLoadClassAsset(FString AssetPath, TFunction Callback) { FStreamableManager& Streamable = UAssetManager::GetStreamableManager(); FSoftObjectPath AssetRef(AssetPath); Streamable.RequestAsyncLoad(AssetRef,[AssetRef, Callback]() { T* LoadedAsset = Cast(AssetRef.TryLoad()); LoadedAsset = LoadedAsset ? LoadedAsset : nullptr; Callback(LoadedAsset); }); } template static void AsyncLoadSubClass(TSoftClassPtr SoftClassPtr, TFunction)> Callback) { if (SoftClassPtr.IsValid()) { Callback(SoftClassPtr.Get()); return; } FStreamableManager& Streamable = UAssetManager::GetStreamableManager(); Streamable.RequestAsyncLoad(SoftClassPtr.ToSoftObjectPath(), [SoftClassPtr, Callback]() { TSubclassOf LoadedClass = SoftClassPtr.Get(); Callback(LoadedClass); } ); } template static T* FindObject(const FString& FilePath) { T* Object = ConstructorHelpers::FObjectFinder(*FilePath).Object; NMT_ENSURE(Object); return Object; } template static ConstructorHelpers::FClassFinder FindClass(const FString& FilePath) { ConstructorHelpers::FClassFinder ClassFinder(*FilePath); NMT_ENSURE(ClassFinder.Succeeded()); return ClassFinder; } template static T* FindActorInLevel(ULevelStreaming* Level) { if(!NMT_ENSURE(Level && Level->IsLevelLoaded())) return nullptr; ULevel* LV = Level->GetLoadedLevel(); if(!NMT_ENSURE(LV)) return nullptr; for(AActor* Actor : LV->Actors) if(T* Ret = Cast(Actor)) return Ret; return nullptr; } template static T* GetGameModeChecked(const UObject* WorldContextObject) { return CastChecked(UGameplayStatics::GetGameMode(WorldContextObject)); } template static T* GetGameStateChecked(const UObject* WorldContextObject) { return CastChecked(UGameplayStatics::GetGameState(WorldContextObject)); } template static T* GetLocalPlayerChecked(const UObject* WorldContextObject) { return CastChecked(UGameplayStatics::GetPlayerCharacter(WorldContextObject, 0)); } template static T* GetLocalPlayerUnChecked(const UObject* WorldContextObject) { return Cast(UGameplayStatics::GetPlayerCharacter(WorldContextObject, 0)); } template static T* GetLocalPlayerControllerChecked(const UObject* WorldContextObject) { return CastChecked(UGameplayStatics::GetPlayerController(WorldContextObject, 0)); } template static T* GetLocalPlayerControllerUnChecked(const UObject* WorldContextObject) { return Cast(UGameplayStatics::GetPlayerController(WorldContextObject, 0)); } template static void ShuffleArray(const TArray& TargetArray) { int32 LastIndex = TargetArray.Num() - 1; for (int32 i = 0; i < LastIndex; ++i) { int32 Index = FMath::RandRange(i, LastIndex); if (i != Index) { const_cast*>(&TargetArray)->Swap(i, Index); } } } static FString GetScriptTypeByRefPath(const FString& RefPath); static EContentType GetContentTypeFromIndex(FName InRowName); template static FString GetStringFieldInStruct(T* StructInstance, const FName& FieldName) { UStruct* StructType = StructInstance->StaticStruct(); FProperty* Prop = StructType->FindPropertyByName(FieldName); const FStrProperty* PropType = CastField(Prop); FString Ret = PropType->GetPropertyValue_InContainer(StructInstance); return Ret; } template static float GetFloatFieldInStruct(T* StructInstance, const FName& FieldName) { UStruct* StructType = StructInstance->StaticStruct(); FProperty* Prop = StructType->FindPropertyByName(FieldName); const FFloatProperty* PropType = CastField(Prop); float Ret = PropType->GetPropertyValue_InContainer(StructInstance); return Ret; } template static int32 GetIntFieldInStruct(T* StructInstance, const FName& FieldName) { UStruct* StructType = StructInstance->StaticStruct(); FProperty* Prop = StructType->FindPropertyByName(FieldName); const FIntProperty* PropType = CastField(Prop); int32 Ret = PropType->GetPropertyValue_InContainer(StructInstance); return Ret; } template static bool GetBoolFieldInStruct(T* StructInstance, const FName& FieldName) { UStruct* StructType = StructInstance->StaticStruct(); FProperty* Prop = StructType->FindPropertyByName(FieldName); const FBoolProperty* PropType = CastField(Prop); bool Ret = PropType->GetPropertyValue_InContainer(StructInstance); return Ret; } static TArray GetFilteredCurrentActors(const TArray& InActors, const TArray& IgnoreClasses, const TArray& IncludeClasses); UFUNCTION(BlueprintCallable) static FString ReferencePathToFilePath(const FString& ReferencePath); UFUNCTION(BlueprintPure) static bool IsValueInRange(const int32 Value, const int32 A, const int32 B, bool GreaterEqual, bool LessEqual); UFUNCTION(BlueprintPure) static FVector GetCenterPointBetweenTwoVectors(FVector A, FVector B); UFUNCTION(BlueprintPure) static double FindAngleBetweenTwoVectors2D(FVector2D A, FVector2D B); UFUNCTION(BlueprintPure) static double FindAngleBetweenTwoVectors3D(FVector A, FVector B, EFindAngleAxis EFindAxis); UFUNCTION(BlueprintCallable, meta = (WorldContext = "WorldContextObject")) static bool SuggestProjectileVelocityAtLocation(const UObject* WorldContextObject, FVector& OutLaunchVelocity, const FVector& ProjectileStartLocation, const FVector& TargetLocation, const float DesiredArcHeight, double GravityZOverride, double TimeToTarget); UFUNCTION(BlueprintCallable) static FVector StringToVector3D( FString& InString); UFUNCTION(BlueprintCallable) static void DebugBreak(const FString& MessageString = TEXT("An intentional crash has occurred at the current node.")); UFUNCTION(BlueprintPure) static TSoftClassPtr GetSoftWidgetClassFromPath(FString Path); UFUNCTION(BlueprintPure) static bool IsEditorPlay(); UFUNCTION(BlueprintCallable) static void EnableComponent(UActorComponent* Component, bool bEnable); UFUNCTION(BlueprintCallable) static FString GetLocalizedStringFromTable(const FName& TableRow); UFUNCTION(BlueprintCallable) static FVector GetBezierPointByDeCasteljau(const TArray& Points, float t); };