Skip to content

Commit

Permalink
Add option to avoid ensure if CVar is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
slonopotamus committed Aug 6, 2024
1 parent 5334e76 commit 0291650
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
16 changes: 8 additions & 8 deletions Source/UEST/Private/ScopedGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ struct TGWorldGuard final : FNoncopyable

struct FCVarGuard final : FNoncopyable
{
explicit FCVarGuard(IConsoleVariable* Variable, const TCHAR* NewValue)
: Variable(Variable)
, OldValue(Variable ? Variable->GetString() : TEXT(""))
explicit FCVarGuard(IConsoleVariable* Variable, const FScopedGameInstance::FCVarConfig& CVarConfig)
: Variable{Variable}
, OldValue{Variable ? Variable->GetString() : TEXT("")}
{
if (ensureAlways(Variable))
if (Variable || CVarConfig.bEnsureIfVariableNotFound && ensureAlways(Variable))
{
Variable->Set(NewValue);
Variable->Set(*CVarConfig.Value);
}
}

Expand All @@ -56,7 +56,7 @@ static int32 NumScopedGames = 0;

static TUniquePtr<FCVarsGuard> CVarsGuard;

FScopedGameInstance::FScopedGameInstance(TSubclassOf<UGameInstance> GameInstanceClass, const bool bGarbageCollectOnDestroy, const TMap<FString, FString>& CVars)
FScopedGameInstance::FScopedGameInstance(TSubclassOf<UGameInstance> GameInstanceClass, const bool bGarbageCollectOnDestroy, const TMap<FString, FCVarConfig>& CVars)
: GameInstanceClass{MoveTemp(GameInstanceClass)}
, LastInstanceId{0}
, bGarbageCollectOnDestroy{bGarbageCollectOnDestroy}
Expand All @@ -67,7 +67,7 @@ FScopedGameInstance::FScopedGameInstance(TSubclassOf<UGameInstance> GameInstance

for (const auto& CVar : CVars)
{
CVarsGuard->ConsoleVariableGuards.Emplace(IConsoleManager::Get().FindConsoleVariable(*CVar.Key), *CVar.Value);
CVarsGuard->ConsoleVariableGuards.Emplace(IConsoleManager::Get().FindConsoleVariable(*CVar.Key), CVar.Value);
}
}

Expand Down Expand Up @@ -409,7 +409,7 @@ FScopedGame::FScopedGame()

// Client runs DNS lookup in separate thread without any way to wait for it (except sleeping real time)
// So just disable it for now because we know that we connect via IP address
CVars.Add(TEXT("net.IpConnectionDisableResolution"), TEXT("1"));
WithConsoleVariable(TEXT("net.IpConnectionDisableResolution"), TEXT("1"));
}

FScopedGameInstance FScopedGame::Create() const
Expand Down
16 changes: 11 additions & 5 deletions Source/UEST/Public/ScopedGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ class FScopedGameInstance : FNoncopyable
public:
static constexpr auto DefaultStepSeconds = 0.1f;

explicit FScopedGameInstance(TSubclassOf<UGameInstance> GameInstanceClass, const bool bGarbageCollectOnDestroy, const TMap<FString, FString>& CVars);
struct FCVarConfig
{
FString Value;
bool bEnsureIfVariableNotFound;
};

explicit FScopedGameInstance(TSubclassOf<UGameInstance> GameInstanceClass, const bool bGarbageCollectOnDestroy, const TMap<FString, FCVarConfig>& CVars);

FScopedGameInstance(FScopedGameInstance&& Other);

Expand All @@ -47,7 +53,7 @@ class FScopedGameInstance : FNoncopyable
bool TickUntil(const TFunction<bool()>& Condition, float StepSeconds = DefaultStepSeconds, float MaxWaitTime = 10.f, ELevelTick TickType = LEVELTICK_All);

template<class T = UObject>
requires std::is_convertible_v<T*, const UObject*>
requires std::is_convertible_v<T*, const UObject*>
T* FindReplicatedObjectIn(T* Object, const UWorld* World)
{
auto* Result = StaticFindReplicatedObjectIn(Object, World);
Expand All @@ -59,7 +65,7 @@ class FScopedGame
{
TSubclassOf<UGameInstance> GameInstanceClass;
bool bGarbageCollectOnDestroy = true;
TMap<FString, FString> CVars;
TMap<FString, FScopedGameInstance::FCVarConfig> CVars;

public:
FScopedGame();
Expand All @@ -70,9 +76,9 @@ class FScopedGame
return *this;
}

FScopedGame& WithConsoleVariable(FString Name, FString Value)
FScopedGame& WithConsoleVariable(FString Name, FString Value, const bool bEnsureIfVariableNotFound = true)
{
CVars.Add(MoveTemp(Name), MoveTemp(Value));
CVars.Add(MoveTemp(Name), {MoveTemp(Value), bEnsureIfVariableNotFound});
return *this;
}

Expand Down

0 comments on commit 0291650

Please sign in to comment.