Implemented ULxLuaValues (not tested yet)
This commit is contained in:
@@ -1,2 +1,119 @@
|
||||
#include "StringDecoder.h"
|
||||
|
||||
|
||||
void UlxLuaValues::Empty()
|
||||
{
|
||||
Serialized.clear();
|
||||
Types.Empty();
|
||||
Data.Empty();
|
||||
Cursor = 0;
|
||||
}
|
||||
|
||||
bool UlxLuaValues::Initialize(std::string_view data)
|
||||
{
|
||||
Empty();
|
||||
Serialized = data;
|
||||
const char *SerializedChar = &Serialized[0];
|
||||
|
||||
FlxStreamBuffer Decoder(Serialized);
|
||||
|
||||
while (!Decoder.empty())
|
||||
{
|
||||
SimpleDynamicTag Tag = Decoder.read_simple_dynamic_tag();
|
||||
|
||||
int64 Pos = Decoder.total_reads();
|
||||
ELxLuaValueType Type;
|
||||
switch (Tag)
|
||||
{
|
||||
case SimpleDynamicTag::BOOLEAN: Type=ELxLuaValueType::Boolean; Decoder.read_bool(); break;
|
||||
case SimpleDynamicTag::NUMBER: Type=ELxLuaValueType::Float; Decoder.read_double(); break;
|
||||
case SimpleDynamicTag::STRING: Type=ELxLuaValueType::String; Decoder.read_string_view(); break;
|
||||
case SimpleDynamicTag::TOKEN: Type=ELxLuaValueType::Name; Decoder.read_string_view(); break;
|
||||
case SimpleDynamicTag::VECTOR: Type=ELxLuaValueType::Vector; Decoder.read_fvector(); break;
|
||||
default: {
|
||||
Empty();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
int64 Pos2 = Decoder.total_reads();
|
||||
Types.Add(Type);
|
||||
Data.Add(std::string_view(SerializedChar + Pos, Pos2 - Pos));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UlxLuaValues::CheckType(ELxLuaValueType Type, ELxLuaValueType Desired)
|
||||
{
|
||||
if (Type != Desired)
|
||||
{
|
||||
FString TypeName = StaticEnum<ELxLuaValueType>()->GetDisplayNameTextByValue(int64(Type)).ToString();
|
||||
FString DesiredName = StaticEnum<ELxLuaValueType>()->GetDisplayNameTextByValue(int64(Desired)).ToString();
|
||||
UE_LOG(LogBlueprint, Error, TEXT("Expected a value of type %s, but found %s instead."), *DesiredName, *TypeName);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
ELxLuaValueType UlxLuaValues::GetType(int n) const
|
||||
{
|
||||
if (n < 0) return ELxLuaValueType::None;
|
||||
if (n >= Types.Num()) return ELxLuaValueType::None;
|
||||
return Types[Cursor];
|
||||
}
|
||||
|
||||
//
|
||||
// Get the Nth value in the array. Array bounds-checking
|
||||
// is handled by 'GetType', which returns 'None' for out-of-bounds
|
||||
// accesses.
|
||||
//
|
||||
|
||||
FString UlxLuaValues::GetString(int n) const
|
||||
{
|
||||
if (!CheckType(GetType(n), ELxLuaValueType::String)) return FString();
|
||||
FlxStreamBuffer Decoder(Data[n]);
|
||||
return Decoder.read_fstring();
|
||||
}
|
||||
|
||||
FName UlxLuaValues::GetName(int n) const
|
||||
{
|
||||
if (!CheckType(GetType(n), ELxLuaValueType::Name)) return FName();
|
||||
FlxStreamBuffer Decoder(Data[n]);
|
||||
return Decoder.read_fname();
|
||||
}
|
||||
|
||||
double UlxLuaValues::GetFloat(int n) const
|
||||
{
|
||||
if (!CheckType(GetType(n), ELxLuaValueType::Float)) return 0.0;
|
||||
FlxStreamBuffer Decoder(Data[n]);
|
||||
return Decoder.read_double();
|
||||
}
|
||||
|
||||
int UlxLuaValues::GetInt(int n) const
|
||||
{
|
||||
if (!CheckType(GetType(n), ELxLuaValueType::Float)) return 0;
|
||||
FlxStreamBuffer Decoder(Data[n]);
|
||||
return int(Decoder.read_double());
|
||||
}
|
||||
|
||||
FVector UlxLuaValues::GetVector(int n) const
|
||||
{
|
||||
if (!CheckType(GetType(n), ELxLuaValueType::Vector)) return FVector();
|
||||
FlxStreamBuffer Decoder(Data[n]);
|
||||
return Decoder.read_fvector();
|
||||
}
|
||||
|
||||
FVector2D UlxLuaValues::GetVector2D(int n) const
|
||||
{
|
||||
if (!CheckType(GetType(n), ELxLuaValueType::Vector)) return FVector2D();
|
||||
FlxStreamBuffer Decoder(Data[n]);
|
||||
FVector v = Decoder.read_fvector();
|
||||
return FVector2D(v.X, v.Y);
|
||||
}
|
||||
|
||||
bool UlxLuaValues::GetBoolean(int n) const
|
||||
{
|
||||
if (!CheckType(GetType(n), ELxLuaValueType::Boolean)) return false;
|
||||
FlxStreamBuffer Decoder(Data[n]);
|
||||
return Decoder.read_bool();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user