Skip to content

Commit beadcb2

Browse files
committed
- Removing _LevelInstance_N postfix when fixing up level reference names
1 parent 5cf9c43 commit beadcb2

2 files changed

Lines changed: 100 additions & 9 deletions

File tree

Source/RyRuntime/Private/RyRuntimeLevelHelpers.cpp

Lines changed: 90 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,20 @@ TSoftObjectPtr<UWorld> URyRuntimeLevelHelpers::GetWorldSoftReferenceFromPath(con
2626
{
2727
const FString fullFilePath = FPaths::GetPath(PathToWorld);
2828
FString fileName = FPaths::GetBaseFilename(PathToWorld);
29+
2930
if (fileName.StartsWith("UEDPIE_"))
3031
{
3132
// Strip off "UEDPIE_N_" prefix. N is a number, usually 0.
3233
fileName = fileName.Right(fileName.Len() - 9);
3334
}
35+
36+
// Strip off _LevelInstance_N postfix
37+
const int32 levelInstStart = fileName.Find(TEXT("_LevelInstance_"), ESearchCase::CaseSensitive, ESearchDir::FromEnd);
38+
if(levelInstStart != INDEX_NONE)
39+
{
40+
fileName = fileName.Left(levelInstStart);
41+
}
42+
3443
return TSoftObjectPtr<UWorld>(FString::Printf(TEXT("%s/%s.%s"), *fullFilePath, *fileName, *fileName));
3544
}
3645

@@ -63,13 +72,27 @@ FString URyRuntimeLevelHelpers::GetWorldSoftReferencePath(const TSoftObjectPtr<U
6372
FString worldRefPath = worldRef.ToString();
6473
if (!worldRefPath.IsEmpty())
6574
{
75+
bool shouldfix = false;
6676
FString worldPath;
6777
FString worldName;
6878
worldRefPath.Split("/", &worldPath, &worldName, ESearchCase::CaseSensitive, ESearchDir::FromEnd);
6979
if (worldName.StartsWith("UEDPIE_"))
7080
{
7181
// Strip off "UEDPIE_N_" prefix. N is a number, usually 0.
7282
worldName = worldName.Right(worldName.Len() - 9);
83+
shouldfix = true;
84+
}
85+
86+
// Strip off _LevelInstance_N postfix
87+
const int32 levelInstStart = worldName.Find(TEXT("_LevelInstance_"), ESearchCase::CaseSensitive, ESearchDir::FromEnd);
88+
if(levelInstStart != INDEX_NONE)
89+
{
90+
worldName = worldName.Left(levelInstStart);
91+
shouldfix = true;
92+
}
93+
94+
if(shouldfix)
95+
{
7396
worldRefPath = FString::Printf(TEXT("%s/%s"), *worldPath, *worldName);
7497
}
7598
}
@@ -85,13 +108,27 @@ TSoftObjectPtr<UWorld> URyRuntimeLevelHelpers::GetCleanWorldSoftReference(const
85108
FString worldRefPath = worldRef.ToString();
86109
if (!worldRefPath.IsEmpty())
87110
{
111+
bool shouldfix = false;
88112
FString worldPath;
89113
FString worldName;
90114
worldRefPath.Split("/", &worldPath, &worldName, ESearchCase::CaseSensitive, ESearchDir::FromEnd);
91115
if (worldName.StartsWith("UEDPIE_"))
92116
{
93117
// Strip off "UEDPIE_N_" prefix. N is a number, usually 0.
94118
worldName = worldName.Right(worldName.Len() - 9);
119+
shouldfix = true;
120+
}
121+
122+
// Strip off _LevelInstance_N postfix
123+
const int32 levelInstStart = worldName.Find(TEXT("_LevelInstance_"), ESearchCase::CaseSensitive, ESearchDir::FromEnd);
124+
if(levelInstStart != INDEX_NONE)
125+
{
126+
worldName = worldName.Left(levelInstStart);
127+
shouldfix = true;
128+
}
129+
130+
if(shouldfix)
131+
{
95132
worldRefPath = FString::Printf(TEXT("%s/%s"), *worldPath, *worldName);
96133
}
97134
}
@@ -148,12 +185,58 @@ bool URyRuntimeLevelHelpers::IsActorInLevel(const AActor* actorToCheck, const UL
148185
return GetActorLevel(actorToCheck) == levelToCheck;
149186
}
150187

188+
//---------------------------------------------------------------------------------------------------------------------
189+
/**
190+
*/
191+
FString URyRuntimeLevelHelpers::RemoveLevelPathDecorators(const FString& levelNamePath)
192+
{
193+
if (levelNamePath.IsEmpty())
194+
{
195+
return TEXT("");
196+
}
197+
198+
int32 pathSepIndex = INDEX_NONE;
199+
levelNamePath.FindLastChar('/', pathSepIndex);
200+
201+
FString pathPart;
202+
FString filePart;
203+
if(pathSepIndex != INDEX_NONE)
204+
{
205+
pathPart = levelNamePath.Left(pathSepIndex);
206+
filePart = levelNamePath.Right(levelNamePath.Len() - (pathSepIndex + 1));
207+
}
208+
else
209+
{
210+
filePart = levelNamePath;
211+
}
212+
213+
// Strip off PIE prefix, "UEDPIE_N_" where N is a number
214+
if (filePart.StartsWith("UEDPIE_"))
215+
{
216+
filePart = filePart.Right(filePart.Len() - 9);
217+
}
218+
219+
// Strip off _LevelInstance_N postfix
220+
const int32 levelInstStart = filePart.Find(TEXT("_LevelInstance_"), ESearchCase::CaseSensitive, ESearchDir::FromEnd);
221+
if(levelInstStart != INDEX_NONE)
222+
{
223+
filePart = filePart.Left(levelInstStart);
224+
}
225+
226+
if(!pathPart.IsEmpty())
227+
{
228+
return FString::Printf(TEXT("%s/%s"), *pathPart, *filePart);
229+
}
230+
231+
return filePart;
232+
}
233+
151234
//---------------------------------------------------------------------------------------------------------------------
152235
/**
153236
*/
154237
FString URyRuntimeLevelHelpers::GetActorLevelPackageString(const AActor* actorIn, bool longName)
155238
{
156-
ULevel* level = GetActorLevel(actorIn);
239+
const ULevel* level = GetActorLevel(actorIn);
157240
if(level)
158241
{
159242
return GetLevelPackageString(level, longName);
@@ -180,7 +263,7 @@ FString URyRuntimeLevelHelpers::GetLevelPackageString(const ULevel* levelIn, boo
180263
*/
181264
FString URyRuntimeLevelHelpers::GetLevelNameString(const ULevel* levelIn)
182265
{
183-
if(levelIn == nullptr || levelIn->GetOutermost() == nullptr)
266+
if(levelIn == nullptr || levelIn->GetOuter() == nullptr)
184267
{
185268
UE_LOG(LogRyRuntime, Warning, TEXT("URyRuntimeLevelHelpers::Get*LevelNameString called with invalid levelIn or actor!"));
186269
return TEXT("");
@@ -193,7 +276,7 @@ FString URyRuntimeLevelHelpers::GetLevelNameString(const ULevel* levelIn)
193276
*/
194277
FString URyRuntimeLevelHelpers::GetActorLevelNameString(const AActor* actorIn)
195278
{
196-
ULevel* level = GetActorLevel(actorIn);
279+
const ULevel* level = GetActorLevel(actorIn);
197280
if(level)
198281
{
199282
return GetLevelNameString(level);
@@ -235,7 +318,7 @@ void URyRuntimeLevelHelpers::GetActorsOfTypeInLevel(const ULevel* level, TSubcla
235318
return;
236319
}
237320

238-
UWorld* const World = level->GetWorld();
321+
const UWorld* const World = level->GetWorld();
239322
if(!World)
240323
{
241324
UE_LOG(LogRyRuntime, Warning, TEXT("RyRuntimeLevelHelpers::GetActorsOfTypeInLevel error. No World Context!"));
@@ -320,7 +403,7 @@ AActor* URyRuntimeLevelHelpers::SpawnActorAdvanced(UObject* WorldContextObject,
320403

321404
if(spawnedActor && useDefaultScale)
322405
{
323-
AActor* cdoActor = spawnedActor->GetClass()->GetDefaultObject<AActor>();
406+
const AActor* cdoActor = spawnedActor->GetClass()->GetDefaultObject<AActor>();
324407
if(cdoActor)
325408
{
326409
spawnedActor->SetActorScale3D(cdoActor->GetActorScale3D());
@@ -605,7 +688,7 @@ ALevelScriptActor* URyRuntimeLevelHelpers::GetStreamingLevelScriptActor(ULevelSt
605688
*/
606689
bool URyRuntimeLevelHelpers::FireLevelScriptRemoteEvent(UObject* WorldContextObject, FName EventName)
607690
{
608-
UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
691+
const UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
609692
if (!World)
610693
{
611694
return false;
@@ -616,7 +699,7 @@ bool URyRuntimeLevelHelpers::FireLevelScriptRemoteEvent(UObject* WorldContextObj
616699
// Iterate over all levels, and try to find a matching function on the level's script actor
617700
for( TArray<ULevel*>::TConstIterator it = World->GetLevels().CreateConstIterator(); it; ++it )
618701
{
619-
ULevel* CurLevel = *it;
702+
const ULevel* CurLevel = *it;
620703
if( CurLevel && CurLevel->bIsVisible )
621704
{
622705
ALevelScriptActor* LSA = CurLevel->GetLevelScriptActor();

Source/RyRuntime/Public/RyRuntimeLevelHelpers.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class RYRUNTIME_API URyRuntimeLevelHelpers : public UBlueprintFunctionLibrary
8080
// Given a full path to your world ex: "/Game/MyLevels/MyLevel" return a constructed soft object ptr.
8181
// This is used in cases such as the LevelStreamingDynamics need for a soft object pointer to a World
8282
// which cannot be constructed dynamically.
83+
// Strips any UEDPIE_N_" prefix or _LevelInstance_ postfix
8384
UFUNCTION(BlueprintPure, Category = "RyRuntime|LevelHelpers")
8485
static TSoftObjectPtr<UWorld> GetWorldSoftReferenceFromPath(const FString& PathToWorld);
8586

@@ -91,11 +92,13 @@ class RYRUNTIME_API URyRuntimeLevelHelpers : public UBlueprintFunctionLibrary
9192
UFUNCTION(BlueprintPure, Category = "RyRuntime|LevelHelpers")
9293
static TSoftObjectPtr<UWorld> GetWorldSoftReference(UWorld* worldIn);
9394

94-
/// Get the world soft reference path cleaning up the string of UEPIE prefix if needed
95+
/// Get the world soft reference path
96+
/// Strips any UEDPIE_N_" prefix or _LevelInstance_ postfix
9597
UFUNCTION(BlueprintPure, Category = "RyRuntime|LevelHelpers", meta=(AutoCreateRefTerm="worldRef"))
9698
static FString GetWorldSoftReferencePath(const TSoftObjectPtr<UWorld>& worldRef);
9799

98-
/// Get the world soft reference cleaning up the UEPIE prefix if needed
100+
/// Get the world soft reference
101+
/// Strips any UEDPIE_N_" prefix or _LevelInstance_ postfix
99102
UFUNCTION(BlueprintPure, Category = "RyRuntime|LevelHelpers", meta=(AutoCreateRefTerm="worldRef"))
100103
static TSoftObjectPtr<UWorld> GetCleanWorldSoftReference(const TSoftObjectPtr<UWorld>& worldRef);
101104

@@ -111,6 +114,11 @@ class RYRUNTIME_API URyRuntimeLevelHelpers : public UBlueprintFunctionLibrary
111114
UFUNCTION(BlueprintPure, Category = "RyRuntime|LevelHelpers")
112115
static bool IsActorInLevel(const AActor* actorToCheck, const ULevel* levelToCheck);
113116

117+
// Removes extra name decorations to paths, ex : "/Path/MyLevels/UEDPIE_N_MyLevel_LevelInstance_N" becomes "/Path/MyLevels/MyLevel"
118+
// Removes "UEDPIE_N_" and "_LevelInstance_N", whichever is found or both.
119+
UFUNCTION(BlueprintPure, Category = "RyRuntime|LevelHelpers")
120+
static FString RemoveLevelPathDecorators(const FString& levelNamePath);
121+
114122
// Return the level package string associated with this actor
115123
// @param longName - If set to true, returns the full path name of the level asset (In PIE sessions, this will be the duplicate level name)
116124
UFUNCTION(BlueprintPure, Category = "RyRuntime|LevelHelpers", meta = (AdvancedDisplay = "1"))

0 commit comments

Comments
 (0)