2024-08-31 16:42:07 -04:00
|
|
|
|
|
|
|
|
#include "LuaCall.h"
|
2025-03-17 18:04:55 -04:00
|
|
|
#include "LuprexGameModeBase.h"
|
2025-03-28 23:31:44 -04:00
|
|
|
#include "StringDecoder.h"
|
|
|
|
|
|
2025-02-26 14:47:53 -05:00
|
|
|
#include "EdGraphSchema_K2.h"
|
2024-08-31 16:42:07 -04:00
|
|
|
|
2024-09-05 01:33:37 -04:00
|
|
|
|
2025-04-07 16:48:27 -04:00
|
|
|
static bool NotInitialized(const FlxStreamBuffer &sb) {
|
|
|
|
|
if (sb.empty())
|
|
|
|
|
{
|
|
|
|
|
UE_LOG(LogBlueprint, Error, TEXT("Must use LuaCallBegin before other LuaCall steps"));
|
|
|
|
|
return true;
|
2024-08-31 17:48:38 -04:00
|
|
|
}
|
2025-04-07 16:48:27 -04:00
|
|
|
return false;
|
2024-08-31 16:42:07 -04:00
|
|
|
}
|
|
|
|
|
|
2025-02-24 16:46:05 -05:00
|
|
|
static constexpr uint64_t ParseNameAsToken(std::string_view str) {
|
|
|
|
|
uint64_t result = 0;
|
|
|
|
|
uint64_t maxint = uint64_t(-1);
|
|
|
|
|
|
|
|
|
|
// Leading zeros are not allowed.
|
|
|
|
|
if ((!str.empty()) && (str[0]=='0')) return 0;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < int(str.size()); i++) {
|
|
|
|
|
char c = str[i];
|
|
|
|
|
uint64_t digit = 0;
|
|
|
|
|
if ((c >= '0') && (c <= '9')) {
|
|
|
|
|
digit = uint64_t(c - '0');
|
|
|
|
|
} else if ((c >= 'a') && (c <= 'z')) {
|
|
|
|
|
digit = uint64_t(c - 'a' + 10);
|
|
|
|
|
} else if ((c >= 'A') && (c <= 'Z')) {
|
|
|
|
|
digit = uint64_t(c - 'A' + 10);
|
|
|
|
|
} else {
|
|
|
|
|
return maxint;
|
|
|
|
|
}
|
|
|
|
|
// Multiply existing number by 36, then add the digit.
|
|
|
|
|
// We have two checks to prevent integer overflow.
|
|
|
|
|
if (result > (maxint / 36)) return 0;
|
|
|
|
|
result *= 36;
|
|
|
|
|
if (digit > (maxint - result)) return 0;
|
|
|
|
|
result += digit;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-02-26 14:47:53 -05:00
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// The lua function prototype parser.
|
|
|
|
|
//
|
|
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool FlxParsedProto::IsLiteral(const TCHAR *text) {
|
|
|
|
|
return ((NextToken < Tokens.Num()) && (Tokens[NextToken] == text));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FlxParsedProto::IsIdent() {
|
|
|
|
|
return ((NextToken < Tokens.Num()) && (FChar::IsAlpha(Tokens[NextToken][0])));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlxParsedProto::Empty() {
|
|
|
|
|
ErrorMessage = TEXT("");
|
|
|
|
|
Tokens.Empty();
|
|
|
|
|
NextToken = 0;
|
|
|
|
|
ClassName = TEXT("");
|
|
|
|
|
FunctionName = TEXT("");
|
|
|
|
|
Arguments.Empty();
|
|
|
|
|
ReturnValues.Empty();
|
|
|
|
|
ExtraReturnValues = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlxParsedProto::Syntax() {
|
|
|
|
|
FString Message;
|
|
|
|
|
if (Tokens.Num() == 0) {
|
|
|
|
|
Message = TEXT("Function prototype cannot be blank");
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < Tokens.Num(); i++) {
|
|
|
|
|
if (i == NextToken) {
|
|
|
|
|
Message.Append(TEXT(" ? "));
|
|
|
|
|
} else {
|
|
|
|
|
if ((i > 0) && (FChar::IsAlpha(Tokens[i][0])) && (FChar::IsAlpha(Tokens[i-1][0]))) {
|
|
|
|
|
Message.Append(TEXT(" "));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Message.Append(Tokens[i]);
|
|
|
|
|
}
|
|
|
|
|
Empty();
|
|
|
|
|
ErrorMessage = Message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlxParsedProto::Parse(const FString &str) {
|
|
|
|
|
Empty();
|
|
|
|
|
|
|
|
|
|
// Step one: tokenize.
|
|
|
|
|
int offset = 0;
|
|
|
|
|
while (offset < str.Len()) {
|
|
|
|
|
TCHAR c = str[offset];
|
|
|
|
|
if (FChar::IsWhitespace(c)) {
|
|
|
|
|
offset++;
|
|
|
|
|
} else if (FChar::IsAlpha(c)) {
|
|
|
|
|
int lo = offset;
|
|
|
|
|
while ((offset < str.Len()) && FChar::IsAlnum(str[offset])) offset++;
|
|
|
|
|
Tokens.Add(str.Mid(lo, offset-lo));
|
|
|
|
|
} else if (str.Mid(offset, 3) == TEXT("...")) {
|
|
|
|
|
Tokens.Add(str.Mid(offset, 3));
|
|
|
|
|
offset += 3;
|
|
|
|
|
} else if (FChar::IsPunct(c)) {
|
|
|
|
|
Tokens.Add(str.Mid(offset, 1));
|
|
|
|
|
offset += 1;
|
|
|
|
|
} else {
|
|
|
|
|
Empty();
|
|
|
|
|
ErrorMessage = FString::Printf(TEXT("%s ? %s"), *str.Mid(0, offset), *str.Mid(offset));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
NextToken = 0;
|
|
|
|
|
|
|
|
|
|
// Step two: Parse.
|
|
|
|
|
if (!IsLiteral(TEXT("*")) && !IsIdent()) return Syntax();
|
|
|
|
|
ClassName = Tokens[NextToken++];
|
|
|
|
|
if (!IsLiteral(TEXT("."))) return Syntax();
|
|
|
|
|
NextToken++;
|
|
|
|
|
if (!IsIdent()) return Syntax();
|
|
|
|
|
FunctionName = Tokens[NextToken++];
|
|
|
|
|
if (!IsLiteral(TEXT("("))) return Syntax();
|
|
|
|
|
NextToken++;
|
|
|
|
|
if (IsIdent()) {
|
|
|
|
|
while (true) {
|
|
|
|
|
if (!IsIdent()) return Syntax();
|
|
|
|
|
FString Type = Tokens[NextToken++];
|
|
|
|
|
if (!IsIdent()) return Syntax();
|
|
|
|
|
FString Name = Tokens[NextToken++];
|
|
|
|
|
Arguments.Add(Pin(Type, Name));
|
|
|
|
|
if (!IsLiteral(TEXT(","))) break;
|
|
|
|
|
NextToken++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!IsLiteral(TEXT(")"))) return Syntax();
|
|
|
|
|
NextToken++;
|
|
|
|
|
if (IsLiteral(TEXT(":"))) {
|
|
|
|
|
NextToken++;
|
|
|
|
|
while (true) {
|
|
|
|
|
if (IsLiteral(TEXT("..."))) {
|
|
|
|
|
ExtraReturnValues = true;
|
|
|
|
|
NextToken++;
|
|
|
|
|
break;
|
|
|
|
|
} else if (IsIdent()) {
|
|
|
|
|
FString Type = Tokens[NextToken++];
|
|
|
|
|
if (!IsIdent()) return Syntax();
|
|
|
|
|
FString Name = Tokens[NextToken++];
|
|
|
|
|
ReturnValues.Add(Pin(Type, Name));
|
|
|
|
|
if (!IsLiteral(TEXT(","))) break;
|
|
|
|
|
NextToken++;
|
|
|
|
|
} else {
|
|
|
|
|
return Syntax();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (NextToken != Tokens.Num()) return Syntax();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// General functions of the Lua Call Library.
|
|
|
|
|
//
|
|
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
UFunction *UlxLuaCallLibrary::GetArgumentPacker(const FString &Type)
|
|
|
|
|
{
|
2025-04-08 18:53:26 -04:00
|
|
|
FName PackerName(FString::Printf(TEXT("LuaCallArgument_%s"), *Type));
|
2025-02-26 14:47:53 -05:00
|
|
|
return UlxLuaCallLibrary::StaticClass()->FindFunctionByName(PackerName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UFunction *UlxLuaCallLibrary::GetReturnValueUnpacker(const FString &Type)
|
|
|
|
|
{
|
2025-04-08 18:53:26 -04:00
|
|
|
FName PackerName(FString::Printf(TEXT("Read%s"), *Type));
|
|
|
|
|
return UlxLuaValues::StaticClass()->FindFunctionByName(PackerName);
|
2025-02-26 14:47:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FString UlxLuaCallLibrary::AllFunctionsWithPrefix(const TCHAR *Prefix)
|
|
|
|
|
{
|
|
|
|
|
FString Result;
|
|
|
|
|
|
|
|
|
|
for ( TFieldIterator<UFunction> FIT ( UlxLuaCallLibrary::StaticClass(), EFieldIteratorFlags::ExcludeSuper); FIT; ++FIT)
|
|
|
|
|
{
|
|
|
|
|
UFunction* Function = *FIT;
|
|
|
|
|
FString Name = Function->GetName();
|
|
|
|
|
if (Name.RemoveFromStart(Prefix))
|
|
|
|
|
{
|
|
|
|
|
if (!Result.IsEmpty()) Result += TEXT(", ");
|
|
|
|
|
Result += Name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// General Lua-Callable functions of the Lua Call Library.
|
|
|
|
|
//
|
|
|
|
|
/////////////////////////////////////////////////////////////////
|
2025-02-24 16:46:05 -05:00
|
|
|
|
2025-03-28 23:31:44 -04:00
|
|
|
void UlxLuaCallLibrary::LuaCallBegin(UObject *context, const FString &cname, const FString &fname)
|
|
|
|
|
{
|
2025-03-17 18:04:55 -04:00
|
|
|
ALuprexGameModeBase *mode = ALuprexGameModeBase::FromContext(context);
|
2024-08-31 17:48:38 -04:00
|
|
|
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
|
|
|
|
|
mode->LuaCallBegin();
|
|
|
|
|
sb.write_string(cname);
|
|
|
|
|
sb.write_string(fname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-03-28 23:31:44 -04:00
|
|
|
void UlxLuaCallLibrary::LuaCallInvoke(UObject *context, AActor *place)
|
|
|
|
|
{
|
2025-03-17 18:04:55 -04:00
|
|
|
ALuprexGameModeBase *mode = ALuprexGameModeBase::FromContext(context);
|
2025-02-26 14:47:53 -05:00
|
|
|
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
|
2025-04-07 16:48:27 -04:00
|
|
|
if (NotInitialized(sb)) return;
|
|
|
|
|
mode->LuaCallEnd(InvocationKind::LUA_INVOKE, place);
|
2025-02-26 14:47:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-08 18:53:26 -04:00
|
|
|
bool UlxLuaCallLibrary::LuaCallProbe(UObject *context, AActor *place, UlxLuaValues *&ReturnArray)
|
2025-03-28 23:31:44 -04:00
|
|
|
{
|
2025-03-17 18:04:55 -04:00
|
|
|
ALuprexGameModeBase *mode = ALuprexGameModeBase::FromContext(context);
|
2025-02-26 14:47:53 -05:00
|
|
|
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
|
2025-04-07 18:00:45 -04:00
|
|
|
if (NotInitialized(sb)) return false;
|
2025-04-08 18:53:26 -04:00
|
|
|
ReturnArray = mode->LuaCallEnd(InvocationKind::LUA_PROBE, place);
|
|
|
|
|
if ((ReturnArray == nullptr) || (ReturnArray->Length() < 1))
|
|
|
|
|
{
|
2025-04-07 18:00:45 -04:00
|
|
|
UE_LOG(LogLuprexIntegration, Error, TEXT("corruption in lua_probe"));
|
2025-04-08 18:53:26 -04:00
|
|
|
ReturnArray = nullptr;
|
2025-04-07 18:00:45 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
2025-04-08 18:53:26 -04:00
|
|
|
ElxSuccessOrError Status;
|
|
|
|
|
FString ErrorMessage;
|
|
|
|
|
ReturnArray->ReadString(Status, ErrorMessage);
|
|
|
|
|
if (Status != ElxSuccessOrError::Success)
|
|
|
|
|
{
|
|
|
|
|
UE_LOG(LogLuprexIntegration, Error, TEXT("corruption in lua_probe"));
|
|
|
|
|
ReturnArray = nullptr;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-04-07 18:00:45 -04:00
|
|
|
if (!ErrorMessage.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
UE_LOG(LogLuprex, Error, TEXT("%s"), *ErrorMessage);
|
2025-04-08 18:53:26 -04:00
|
|
|
ReturnArray = nullptr;
|
2025-04-07 18:00:45 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2025-02-26 14:47:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// Argument Packing functions
|
|
|
|
|
//
|
|
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
void UlxLuaCallLibrary::LuaCallArgument_string(UObject *context, const FString &pstring) {
|
2025-03-17 18:04:55 -04:00
|
|
|
ALuprexGameModeBase *mode = ALuprexGameModeBase::FromContext(context);
|
2024-08-31 17:48:38 -04:00
|
|
|
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
|
2025-04-07 16:48:27 -04:00
|
|
|
if (NotInitialized(sb)) return;
|
2024-08-31 17:48:38 -04:00
|
|
|
sb.write_simple_dynamic_tag(SimpleDynamicTag::STRING);
|
|
|
|
|
sb.write_string(pstring);
|
2024-08-31 16:42:07 -04:00
|
|
|
}
|
|
|
|
|
|
2025-02-26 14:47:53 -05:00
|
|
|
void UlxLuaCallLibrary::LuaCallArgument_name(UObject *context, const FName &pname) {
|
2025-02-24 16:46:05 -05:00
|
|
|
FTCHARToUTF8 utf8str(pname.ToString());
|
|
|
|
|
std::string_view namestr(utf8str.Get(), utf8str.Length());
|
|
|
|
|
uint64_t tokvalue = ParseNameAsToken(namestr);
|
2025-04-07 16:48:27 -04:00
|
|
|
if ((tokvalue == 0) && !namestr.empty())
|
|
|
|
|
{
|
|
|
|
|
UE_LOG(LogBlueprint, Error, TEXT("Names passed to lua must be short, and must contain only lowercase and digits"));
|
2025-02-24 16:46:05 -05:00
|
|
|
}
|
2025-03-17 18:04:55 -04:00
|
|
|
ALuprexGameModeBase *mode = ALuprexGameModeBase::FromContext(context);
|
2025-02-05 16:08:34 -05:00
|
|
|
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
|
2025-04-07 16:48:27 -04:00
|
|
|
if (NotInitialized(sb)) return;
|
2025-02-24 16:46:05 -05:00
|
|
|
sb.write_simple_dynamic_tag(SimpleDynamicTag::TOKEN);
|
|
|
|
|
sb.write_string(namestr);
|
2025-02-05 16:08:34 -05:00
|
|
|
}
|
2024-08-31 16:42:07 -04:00
|
|
|
|
2025-02-26 14:47:53 -05:00
|
|
|
void UlxLuaCallLibrary::LuaCallArgument_float(UObject *context, double pfloat) {
|
2025-03-17 18:04:55 -04:00
|
|
|
ALuprexGameModeBase *mode = ALuprexGameModeBase::FromContext(context);
|
2024-08-31 17:48:38 -04:00
|
|
|
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
|
2025-04-07 16:48:27 -04:00
|
|
|
if (NotInitialized(sb)) return;
|
2024-08-31 17:48:38 -04:00
|
|
|
sb.write_simple_dynamic_tag(SimpleDynamicTag::NUMBER);
|
|
|
|
|
sb.write_double(pfloat);
|
2024-08-31 16:42:07 -04:00
|
|
|
}
|
|
|
|
|
|
2025-02-26 14:47:53 -05:00
|
|
|
void UlxLuaCallLibrary::LuaCallArgument_int(UObject *context, int value) {
|
2025-03-17 18:04:55 -04:00
|
|
|
ALuprexGameModeBase *mode = ALuprexGameModeBase::FromContext(context);
|
2025-02-24 16:46:05 -05:00
|
|
|
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
|
2025-04-07 16:48:27 -04:00
|
|
|
if (NotInitialized(sb)) return;
|
2025-02-24 16:46:05 -05:00
|
|
|
sb.write_simple_dynamic_tag(SimpleDynamicTag::NUMBER);
|
|
|
|
|
sb.write_double(value);
|
|
|
|
|
}
|
2024-08-31 16:42:07 -04:00
|
|
|
|
2025-02-26 14:47:53 -05:00
|
|
|
void UlxLuaCallLibrary::LuaCallArgument_vector(UObject *context, const FVector &pvector) {
|
2025-03-17 18:04:55 -04:00
|
|
|
ALuprexGameModeBase *mode = ALuprexGameModeBase::FromContext(context);
|
2024-08-31 17:48:38 -04:00
|
|
|
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
|
2025-04-07 16:48:27 -04:00
|
|
|
if (NotInitialized(sb)) return;
|
2024-08-31 17:48:38 -04:00
|
|
|
sb.write_simple_dynamic_tag(SimpleDynamicTag::VECTOR);
|
|
|
|
|
sb.write_fvector(pvector);
|
2024-08-31 16:42:07 -04:00
|
|
|
}
|
2025-02-05 16:08:34 -05:00
|
|
|
|
2025-02-26 14:47:53 -05:00
|
|
|
void UlxLuaCallLibrary::LuaCallArgument_vector2d(UObject *context, const FVector2D &pvector) {
|
2025-03-17 18:04:55 -04:00
|
|
|
ALuprexGameModeBase *mode = ALuprexGameModeBase::FromContext(context);
|
2025-02-24 16:46:05 -05:00
|
|
|
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
|
2025-04-07 16:48:27 -04:00
|
|
|
if (NotInitialized(sb)) return;
|
2025-02-24 16:46:05 -05:00
|
|
|
sb.write_simple_dynamic_tag(SimpleDynamicTag::VECTOR);
|
|
|
|
|
sb.write_double(pvector.X);
|
|
|
|
|
sb.write_double(pvector.Y);
|
|
|
|
|
sb.write_double(0.0);
|
|
|
|
|
}
|
2024-08-31 16:42:07 -04:00
|
|
|
|
2025-02-26 14:47:53 -05:00
|
|
|
void UlxLuaCallLibrary::LuaCallArgument_boolean(UObject *context, bool pbool) {
|
2025-03-17 18:04:55 -04:00
|
|
|
ALuprexGameModeBase *mode = ALuprexGameModeBase::FromContext(context);
|
2024-08-31 17:48:38 -04:00
|
|
|
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
|
2025-04-07 16:48:27 -04:00
|
|
|
if (NotInitialized(sb)) return;
|
2024-08-31 17:48:38 -04:00
|
|
|
sb.write_simple_dynamic_tag(SimpleDynamicTag::BOOLEAN);
|
|
|
|
|
sb.write_bool(pbool);
|
2024-08-31 16:42:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-07 16:48:27 -04:00
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// Returning the rest of the lua return values as an array.
|
|
|
|
|
//
|
|
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FString UlxLuaValues::DebugString() const
|
|
|
|
|
{
|
|
|
|
|
TStringBuilder<2048> Output;
|
|
|
|
|
Output << TEXT("{ ");
|
|
|
|
|
for (int i = 0; i < Types.Num(); i++)
|
|
|
|
|
{
|
|
|
|
|
if (i > 0) Output << TEXT(", ");
|
|
|
|
|
|
2025-04-08 18:53:26 -04:00
|
|
|
if (i == Cursor) Output << TEXT("^ ");
|
|
|
|
|
|
2025-04-07 16:48:27 -04:00
|
|
|
ElxLuaValueType Type = Types[i];
|
|
|
|
|
FlxStreamBuffer Decoder(Data[i]);
|
|
|
|
|
switch (Type)
|
|
|
|
|
{
|
|
|
|
|
case ElxLuaValueType::Boolean:
|
|
|
|
|
Output << Decoder.read_bool();
|
|
|
|
|
break;
|
|
|
|
|
case ElxLuaValueType::Float:
|
|
|
|
|
Output << FString::Printf(TEXT("%lf"), Decoder.read_double());
|
|
|
|
|
break;
|
|
|
|
|
case ElxLuaValueType::String:
|
|
|
|
|
Output << TEXT("\"") << Decoder.read_fstring() << TEXT("\"");
|
|
|
|
|
break;
|
|
|
|
|
case ElxLuaValueType::Name:
|
|
|
|
|
Output << Decoder.read_fname();
|
|
|
|
|
break;
|
|
|
|
|
case ElxLuaValueType::Vector: {
|
|
|
|
|
FVector Vec = Decoder.read_fvector();
|
|
|
|
|
Output << FString::Printf(TEXT("(%lf, %lf, %lf)"), Vec.X, Vec.Y, Vec.Z);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Output << TEXT(" }");
|
|
|
|
|
return Output.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-07 19:29:47 -04:00
|
|
|
ElxSuccessOrError UlxLuaValues::CheckType(ElxLuaValueType Type, ElxLuaValueType Desired)
|
2025-04-07 16:48:27 -04:00
|
|
|
{
|
|
|
|
|
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);
|
2025-04-07 19:29:47 -04:00
|
|
|
return ElxSuccessOrError::Error;
|
2025-04-07 16:48:27 -04:00
|
|
|
}
|
2025-04-07 19:29:47 -04:00
|
|
|
return ElxSuccessOrError::Success;
|
2025-04-07 16:48:27 -04:00
|
|
|
}
|
|
|
|
|
|
2025-04-08 18:53:26 -04:00
|
|
|
void UlxLuaValues::DiscardBeforeCursor()
|
|
|
|
|
{
|
|
|
|
|
if (Cursor <= 0) return;
|
|
|
|
|
int Discard = Cursor;
|
|
|
|
|
if (Discard > Types.Num()) Discard = Types.Num();
|
|
|
|
|
Types.RemoveAt(0, Discard);
|
|
|
|
|
Data.RemoveAt(0, Discard);
|
|
|
|
|
Cursor -= Discard;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-07 19:29:47 -04:00
|
|
|
ElxLuaValueType UlxLuaValues::NextType() const
|
2025-04-07 16:48:27 -04:00
|
|
|
{
|
2025-04-08 16:31:16 -04:00
|
|
|
if (Cursor < 0) return ElxLuaValueType::End;
|
|
|
|
|
if (Cursor >= Types.Num()) return ElxLuaValueType::End;
|
2025-04-07 16:48:27 -04:00
|
|
|
return Types[Cursor];
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-07 19:29:47 -04:00
|
|
|
void UlxLuaValues::ReadString(ElxSuccessOrError &Status, FString &Result)
|
2025-04-07 16:48:27 -04:00
|
|
|
{
|
2025-04-07 19:29:47 -04:00
|
|
|
Status = CheckType(NextType(), ElxLuaValueType::String);
|
|
|
|
|
if (Status == ElxSuccessOrError::Error)
|
|
|
|
|
{
|
|
|
|
|
Result.Empty(); return;
|
|
|
|
|
}
|
|
|
|
|
Result = FlxStreamBuffer(Data[Cursor++]).read_fstring();
|
2025-04-07 16:48:27 -04:00
|
|
|
}
|
|
|
|
|
|
2025-04-07 19:29:47 -04:00
|
|
|
void UlxLuaValues::ReadName(ElxSuccessOrError &Status, FName &Result)
|
2025-04-07 16:48:27 -04:00
|
|
|
{
|
2025-04-07 19:29:47 -04:00
|
|
|
Status = CheckType(NextType(), ElxLuaValueType::Name);
|
|
|
|
|
if (Status == ElxSuccessOrError::Error)
|
|
|
|
|
{
|
|
|
|
|
Result = FName(); return;
|
|
|
|
|
}
|
|
|
|
|
Result = FlxStreamBuffer(Data[Cursor++]).read_fname();
|
2025-04-07 16:48:27 -04:00
|
|
|
}
|
|
|
|
|
|
2025-04-07 19:29:47 -04:00
|
|
|
void UlxLuaValues::ReadFloat(ElxSuccessOrError &Status, double &Result)
|
2025-04-07 16:48:27 -04:00
|
|
|
{
|
2025-04-07 19:29:47 -04:00
|
|
|
Status = CheckType(NextType(), ElxLuaValueType::Float);
|
|
|
|
|
if (Status == ElxSuccessOrError::Error)
|
|
|
|
|
{
|
|
|
|
|
Result = 0.0; return;
|
|
|
|
|
}
|
|
|
|
|
Result = FlxStreamBuffer(Data[Cursor++]).read_double();
|
2025-04-07 16:48:27 -04:00
|
|
|
}
|
|
|
|
|
|
2025-04-07 19:29:47 -04:00
|
|
|
void UlxLuaValues::ReadInt(ElxSuccessOrError &Status, int &Result)
|
2025-04-07 16:48:27 -04:00
|
|
|
{
|
2025-04-07 19:29:47 -04:00
|
|
|
Status = CheckType(NextType(), ElxLuaValueType::Float);
|
|
|
|
|
if (Status == ElxSuccessOrError::Error)
|
|
|
|
|
{
|
|
|
|
|
Result = 0.0; return;
|
|
|
|
|
}
|
|
|
|
|
double dvalue = FlxStreamBuffer(Data[Cursor++]).read_double();
|
|
|
|
|
Result = int(dvalue);
|
|
|
|
|
if (double(Result) != dvalue)
|
|
|
|
|
{
|
|
|
|
|
Result = 0; Status = ElxSuccessOrError::Error; return;
|
|
|
|
|
}
|
2025-04-07 16:48:27 -04:00
|
|
|
}
|
|
|
|
|
|
2025-04-07 19:29:47 -04:00
|
|
|
void UlxLuaValues::ReadVector(ElxSuccessOrError &Status, FVector &Result)
|
2025-04-07 16:48:27 -04:00
|
|
|
{
|
2025-04-07 19:29:47 -04:00
|
|
|
Status = CheckType(NextType(), ElxLuaValueType::Vector);
|
|
|
|
|
if (Status == ElxSuccessOrError::Error)
|
|
|
|
|
{
|
|
|
|
|
Result = FVector(); return;
|
|
|
|
|
}
|
|
|
|
|
Result = FlxStreamBuffer(Data[Cursor++]).read_fvector();
|
2025-04-07 16:48:27 -04:00
|
|
|
}
|
|
|
|
|
|
2025-04-07 19:29:47 -04:00
|
|
|
void UlxLuaValues::ReadVector2D(ElxSuccessOrError &Status, FVector2D &Result)
|
2025-04-07 16:48:27 -04:00
|
|
|
{
|
2025-04-07 19:29:47 -04:00
|
|
|
Status = CheckType(NextType(), ElxLuaValueType::Vector);
|
|
|
|
|
if (Status == ElxSuccessOrError::Error)
|
|
|
|
|
{
|
|
|
|
|
Result = FVector2D(); return;
|
|
|
|
|
}
|
|
|
|
|
FVector VValue = FlxStreamBuffer(Data[Cursor++]).read_fvector();
|
|
|
|
|
Result = FVector2D(VValue.X, VValue.Y);
|
2025-04-07 16:48:27 -04:00
|
|
|
}
|
|
|
|
|
|
2025-04-07 19:29:47 -04:00
|
|
|
void UlxLuaValues::ReadBoolean(ElxSuccessOrError &Status, bool &Result)
|
2025-04-07 16:48:27 -04:00
|
|
|
{
|
2025-04-07 19:29:47 -04:00
|
|
|
Status = CheckType(NextType(), ElxLuaValueType::Boolean);
|
|
|
|
|
if (Status == ElxSuccessOrError::Error)
|
|
|
|
|
{
|
|
|
|
|
Result = false; return;
|
|
|
|
|
}
|
|
|
|
|
Result = FlxStreamBuffer(Data[Cursor++]).read_bool();
|
2024-09-05 01:33:37 -04:00
|
|
|
}
|
2025-02-26 14:47:53 -05:00
|
|
|
|
2025-04-07 19:29:47 -04:00
|
|
|
|
|
|
|
|
|