161 lines
5.4 KiB
C++
161 lines
5.4 KiB
C++
|
|
// Fill out your copyright notice in the Description page of Project Settings.
|
|||
|
|
|
|||
|
|
|
|||
|
|
#include "GameLift/GameLiftValidators.h"
|
|||
|
|
|
|||
|
|
#include "Game/ShooterGameMode.h"
|
|||
|
|
|
|||
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////
|
|||
|
|
///Lazy Singleton
|
|||
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////
|
|||
|
|
|
|||
|
|
TArray<FString>& UGameLiftValidators::GetPatterns()
|
|||
|
|
{
|
|||
|
|
static TArray<FString> Patterns;
|
|||
|
|
if (Patterns.Num() == 0)
|
|||
|
|
{
|
|||
|
|
Patterns.AddDefaulted(static_cast<int32>(EGameLiftParams::Max));
|
|||
|
|
Patterns[static_cast<int32>(EGameLiftParams::AuthToken)] =
|
|||
|
|
TEXT("^[a-zA-Z0-9\\-]+$"); // AuthToken
|
|||
|
|
Patterns[static_cast<int32>(EGameLiftParams::FleetId)] =
|
|||
|
|
TEXT("^[a-z]*fleet-[a-zA-Z0-9\\-]+$|^arn:.*:[a-z]*fleet\\/[a-z]*fleet-[a-zA-Z0-9\\-]+$"); // FleetId
|
|||
|
|
Patterns[static_cast<int32>(EGameLiftParams::HostId)] =
|
|||
|
|
TEXT("^[0-9A-Z]\\d+$"); // HostId
|
|||
|
|
Patterns[static_cast<int32>(EGameLiftParams::WebSocketUrl)] =
|
|||
|
|
TEXT("^wss:\\/\\/[a-zA-Z0-9.-]+(\\.amazonaws\\.com)?(\\/[^\\s]*)?$"); // WebSocketUrl
|
|||
|
|
Patterns[static_cast<int32>(EGameLiftParams::ServerRegion)] =
|
|||
|
|
TEXT("^[a-z]{2}-[a-z]+-[0-9]$"); // Region (ca-central-1)
|
|||
|
|
}
|
|||
|
|
return Patterns;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////
|
|||
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|||
|
|
|
|||
|
|
bool UGameLiftValidators::IsMatchesRegex(const FString& Value, EGameLiftParams ParamType)
|
|||
|
|
{
|
|||
|
|
ensure(ParamType < EGameLiftParams::Max);
|
|||
|
|
ensure(GetPatterns().Num() > static_cast<int32>(ParamType));
|
|||
|
|
const FRegexPattern Pattern = FRegexPattern(GetPatterns()[static_cast<int32>(ParamType)]);
|
|||
|
|
FRegexMatcher TestMatcher(Pattern, Value);
|
|||
|
|
return TestMatcher.FindNext();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
bool UGameLiftValidators::IsStringValid(const FString& Value, EGameLiftParams ParamType)
|
|||
|
|
{
|
|||
|
|
// Checks to make sure the ParamType is not the sentinel
|
|||
|
|
ensure(ParamType < EGameLiftParams::Max);
|
|||
|
|
|
|||
|
|
if (Value.IsEmpty())
|
|||
|
|
return false;
|
|||
|
|
|
|||
|
|
const int32 Len = Value.Len();
|
|||
|
|
const int32 Index = static_cast<int32>(ParamType);
|
|||
|
|
|
|||
|
|
if (Len < MinLengths[Index] || Len > MaxLengths[Index]) return false;
|
|||
|
|
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool UGameLiftValidators::IsStringShort(const FString& Value, EGameLiftParams ParamType)
|
|||
|
|
{
|
|||
|
|
ensure(ParamType < EGameLiftParams::Max);
|
|||
|
|
if (Value.IsEmpty()) return false;
|
|||
|
|
const int32 Len = Value.Len();
|
|||
|
|
const int32 Index = static_cast<int32>(ParamType);
|
|||
|
|
if (Len < MinLengths[Index]) return true;
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool UGameLiftValidators::IsStringLong(const FString& Value, EGameLiftParams ParamType)
|
|||
|
|
{
|
|||
|
|
ensure(ParamType < EGameLiftParams::Max);
|
|||
|
|
if (Value.IsEmpty()) return false;
|
|||
|
|
const int32 Len = Value.Len();
|
|||
|
|
const int32 Index = static_cast<int32>(ParamType);
|
|||
|
|
if (Len > MaxLengths[Index]) return true;
|
|||
|
|
return false;
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
FParamResult UGameLiftValidators::ValidateParam(const FString& Value, const EGameLiftParams ParamType)
|
|||
|
|
{
|
|||
|
|
ensure(ParamType < EGameLiftParams::Max);
|
|||
|
|
bool bErrorTriggered = false;
|
|||
|
|
FParamResult Result;
|
|||
|
|
Result.ParamType = ParamType;
|
|||
|
|
|
|||
|
|
// Quick empty check
|
|||
|
|
if (Value.IsEmpty() && !bErrorTriggered)
|
|||
|
|
{
|
|||
|
|
Result.bValid = false;
|
|||
|
|
Result.ErrorMessage = FString::Format(TEXT("Missing {0} in command line arguments"), {GetParameterFromEnum(ParamType)});
|
|||
|
|
Result.ErrorCode = EValidationError::Empty;
|
|||
|
|
bErrorTriggered = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (IsStringShort(Value, ParamType) && !bErrorTriggered)
|
|||
|
|
{
|
|||
|
|
Result.bValid = false;
|
|||
|
|
Result.Value = Value;
|
|||
|
|
Result.ErrorMessage = FString::Format(TEXT("Parameter {0} does not meet minimum length requirements."), { GetParameterFromEnum(ParamType)} );
|
|||
|
|
Result.ErrorCode = EValidationError::TooShort;
|
|||
|
|
bErrorTriggered = true;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (IsStringLong(Value, ParamType) && !bErrorTriggered)
|
|||
|
|
{
|
|||
|
|
Result.bValid = false;
|
|||
|
|
Result.Value = Value;
|
|||
|
|
Result.ErrorMessage = FString::Format(TEXT("Parameter {0} does not meet maximum length requirements."), { GetParameterFromEnum(ParamType)} );
|
|||
|
|
Result.ErrorCode = EValidationError::TooLong;
|
|||
|
|
bErrorTriggered = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (!IsMatchesRegex(Value, ParamType) && !bErrorTriggered)
|
|||
|
|
{
|
|||
|
|
Result.bValid = false;
|
|||
|
|
Result.Value = Value;
|
|||
|
|
Result.ErrorMessage = FString::Format(TEXT("Parameter {0} has an invalid format."), { GetParameterFromEnum(ParamType)} );
|
|||
|
|
Result.ErrorCode = EValidationError::InvalidFormat;
|
|||
|
|
bErrorTriggered = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (!bErrorTriggered)
|
|||
|
|
{
|
|||
|
|
Result.bValid = true;
|
|||
|
|
Result.Value = Value;
|
|||
|
|
Result.ErrorMessage = FString("");
|
|||
|
|
Result.ErrorCode = EValidationError::None;
|
|||
|
|
}
|
|||
|
|
return Result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
FString UGameLiftValidators::GetParameterFromEnum(const EGameLiftParams InParam)
|
|||
|
|
{
|
|||
|
|
ensure(InParam < EGameLiftParams::Max);
|
|||
|
|
const FString FullParameter = UEnum::GetValueAsString(InParam);
|
|||
|
|
FString Left, Right;
|
|||
|
|
FullParameter.Split(TEXT("::"), &Left, &Right);
|
|||
|
|
const FString NameOnly = Right;
|
|||
|
|
return NameOnly;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
FString UGameLiftValidators::GetErrorFromEnum(EValidationError InParam)
|
|||
|
|
{
|
|||
|
|
ensure(InParam <= EValidationError::NotFound);
|
|||
|
|
const FString FullParameter = UEnum::GetValueAsString(InParam);
|
|||
|
|
FString Left, Right;
|
|||
|
|
FullParameter.Split(TEXT("::"), &Left, &Right);
|
|||
|
|
const FString NameOnly = Right;
|
|||
|
|
return NameOnly;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void UGameLiftValidators::LogValidationErrorMessage(FParamResult ValidationResult)
|
|||
|
|
{
|
|||
|
|
if (ValidationResult.ErrorCode == EValidationError::None) return; // no error logging required
|
|||
|
|
UE_LOGFMT(LogShooterGameMode, Error, "{0}", ValidationResult.ErrorMessage);
|
|||
|
|
}
|