A lot of work on the Lua Call interface and some work on animation queues

This commit is contained in:
2025-02-26 14:47:53 -05:00
parent bed4f3e805
commit 72eda3026f
11 changed files with 530 additions and 409 deletions

View File

@@ -1,6 +1,7 @@
#include "LuaCall.h"
#include "IntegrationGameModeBase.h"
#include "EdGraphSchema_K2.h"
static void FatalBlueprintError(const TCHAR *message) {
FBlueprintExceptionInfo ExceptionInfo(EBlueprintExceptionType::FatalError, FText::FromString(FString(message)));
@@ -43,6 +44,164 @@ static constexpr uint64_t ParseNameAsToken(std::string_view str) {
}
/////////////////////////////////////////////////////////////////
//
// 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)
{
FString LType = Type.ToLower();
FName PackerName(FString::Printf(TEXT("LuaCallArgument_%s"), *LType));
return UlxLuaCallLibrary::StaticClass()->FindFunctionByName(PackerName);
}
UFunction *UlxLuaCallLibrary::GetReturnValueUnpacker(const FString &Type)
{
FString LType = Type.ToLower();
FName PackerName(FString::Printf(TEXT("LuaCallReturnValue_%s"), *LType));
return UlxLuaCallLibrary::StaticClass()->FindFunctionByName(PackerName);
}
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.
//
/////////////////////////////////////////////////////////////////
void UlxLuaCallLibrary::LuaCallBegin(UObject *context, const FString &cname, const FString &fname) {
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
@@ -53,71 +212,6 @@ void UlxLuaCallLibrary::LuaCallBegin(UObject *context, const FString &cname, con
}
void UlxLuaCallLibrary::LuaCallAddStringParameter(UObject *context, const FString &pstring) {
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
CheckNotEmpty(sb);
sb.write_simple_dynamic_tag(SimpleDynamicTag::STRING);
sb.write_string(pstring);
}
void UlxLuaCallLibrary::LuaCallAddNameParameter(UObject *context, const FName &pname) {
FTCHARToUTF8 utf8str(pname.ToString());
std::string_view namestr(utf8str.Get(), utf8str.Length());
uint64_t tokvalue = ParseNameAsToken(namestr);
if ((tokvalue == 0) && !namestr.empty()) {
FatalBlueprintError(TEXT("Names passed to lua must be short, and must contain only lowercase and digits"));
}
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
CheckNotEmpty(sb);
sb.write_simple_dynamic_tag(SimpleDynamicTag::TOKEN);
sb.write_string(namestr);
}
void UlxLuaCallLibrary::LuaCallAddFloatParameter(UObject *context, double pfloat) {
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
CheckNotEmpty(sb);
sb.write_simple_dynamic_tag(SimpleDynamicTag::NUMBER);
sb.write_double(pfloat);
}
void UlxLuaCallLibrary::LuaCallAddIntParameter(UObject *context, int value) {
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
CheckNotEmpty(sb);
sb.write_simple_dynamic_tag(SimpleDynamicTag::NUMBER);
sb.write_double(value);
}
void UlxLuaCallLibrary::LuaCallAddVectorParameter(UObject *context, const FVector &pvector) {
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
CheckNotEmpty(sb);
sb.write_simple_dynamic_tag(SimpleDynamicTag::VECTOR);
sb.write_fvector(pvector);
}
void UlxLuaCallLibrary::LuaCallAddVector2DParameter(UObject *context, const FVector2D &pvector) {
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
CheckNotEmpty(sb);
sb.write_simple_dynamic_tag(SimpleDynamicTag::VECTOR);
sb.write_double(pvector.X);
sb.write_double(pvector.Y);
sb.write_double(0.0);
}
void UlxLuaCallLibrary::LuaCallAddBooleanParameter(UObject *context, bool pbool) {
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
CheckNotEmpty(sb);
sb.write_simple_dynamic_tag(SimpleDynamicTag::BOOLEAN);
sb.write_bool(pbool);
}
void UlxLuaCallLibrary::LuaCallInvoke(UObject *context, AActor *place) {
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
@@ -166,7 +260,85 @@ ELpxSimpleDynamicTag UlxLuaCallLibrary::LuaCallNextResultType(UObject *context)
}
}
FString UlxLuaCallLibrary::LuaCallGetStringResult(UObject *context) {
/////////////////////////////////////////////////////////////////
//
// Argument Packing functions
//
/////////////////////////////////////////////////////////////////
void UlxLuaCallLibrary::LuaCallArgument_string(UObject *context, const FString &pstring) {
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
CheckNotEmpty(sb);
sb.write_simple_dynamic_tag(SimpleDynamicTag::STRING);
sb.write_string(pstring);
}
void UlxLuaCallLibrary::LuaCallArgument_name(UObject *context, const FName &pname) {
FTCHARToUTF8 utf8str(pname.ToString());
std::string_view namestr(utf8str.Get(), utf8str.Length());
uint64_t tokvalue = ParseNameAsToken(namestr);
if ((tokvalue == 0) && !namestr.empty()) {
FatalBlueprintError(TEXT("Names passed to lua must be short, and must contain only lowercase and digits"));
}
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
CheckNotEmpty(sb);
sb.write_simple_dynamic_tag(SimpleDynamicTag::TOKEN);
sb.write_string(namestr);
}
void UlxLuaCallLibrary::LuaCallArgument_float(UObject *context, double pfloat) {
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
CheckNotEmpty(sb);
sb.write_simple_dynamic_tag(SimpleDynamicTag::NUMBER);
sb.write_double(pfloat);
}
void UlxLuaCallLibrary::LuaCallArgument_int(UObject *context, int value) {
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
CheckNotEmpty(sb);
sb.write_simple_dynamic_tag(SimpleDynamicTag::NUMBER);
sb.write_double(value);
}
void UlxLuaCallLibrary::LuaCallArgument_vector(UObject *context, const FVector &pvector) {
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
CheckNotEmpty(sb);
sb.write_simple_dynamic_tag(SimpleDynamicTag::VECTOR);
sb.write_fvector(pvector);
}
void UlxLuaCallLibrary::LuaCallArgument_vector2d(UObject *context, const FVector2D &pvector) {
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
CheckNotEmpty(sb);
sb.write_simple_dynamic_tag(SimpleDynamicTag::VECTOR);
sb.write_double(pvector.X);
sb.write_double(pvector.Y);
sb.write_double(0.0);
}
void UlxLuaCallLibrary::LuaCallArgument_boolean(UObject *context, bool pbool) {
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetBuffer();
CheckNotEmpty(sb);
sb.write_simple_dynamic_tag(SimpleDynamicTag::BOOLEAN);
sb.write_bool(pbool);
}
/////////////////////////////////////////////////////////////////
//
// Return Value Unpacking functions
//
/////////////////////////////////////////////////////////////////
FString UlxLuaCallLibrary::LuaCallReturnValue_string(UObject *context) {
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetResult();
SimpleDynamicTag tag = sb.read_simple_dynamic_tag();
@@ -174,7 +346,7 @@ FString UlxLuaCallLibrary::LuaCallGetStringResult(UObject *context) {
return sb.read_fstring();
}
FName UlxLuaCallLibrary::LuaCallGetNameResult(UObject *context) {
FName UlxLuaCallLibrary::LuaCallReturnValue_name(UObject *context) {
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetResult();
SimpleDynamicTag tag = sb.read_simple_dynamic_tag();
@@ -182,7 +354,7 @@ FName UlxLuaCallLibrary::LuaCallGetNameResult(UObject *context) {
return sb.read_fname();
}
double UlxLuaCallLibrary::LuaCallGetFloatResult(UObject *context) {
double UlxLuaCallLibrary::LuaCallReturnValue_float(UObject *context) {
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetResult();
SimpleDynamicTag tag = sb.read_simple_dynamic_tag();
@@ -190,7 +362,15 @@ double UlxLuaCallLibrary::LuaCallGetFloatResult(UObject *context) {
return sb.read_double();
}
FVector UlxLuaCallLibrary::LuaCallGetVectorResult(UObject *context) {
int UlxLuaCallLibrary::LuaCallReturnValue_int(UObject *context) {
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetResult();
SimpleDynamicTag tag = sb.read_simple_dynamic_tag();
if (tag != SimpleDynamicTag::NUMBER) FatalBlueprintError(TEXT("expected lua to return a number"));
return int(sb.read_double());
}
FVector UlxLuaCallLibrary::LuaCallReturnValue_vector(UObject *context) {
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetResult();
SimpleDynamicTag tag = sb.read_simple_dynamic_tag();
@@ -198,10 +378,20 @@ FVector UlxLuaCallLibrary::LuaCallGetVectorResult(UObject *context) {
return sb.read_fvector();
}
bool UlxLuaCallLibrary::LuaCallGetBooleanResult(UObject *context) {
FVector2D UlxLuaCallLibrary::LuaCallReturnValue_vector2d(UObject *context) {
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetResult();
SimpleDynamicTag tag = sb.read_simple_dynamic_tag();
if (tag != SimpleDynamicTag::VECTOR) FatalBlueprintError(TEXT("expected lua to return a vector"));
FVector v = sb.read_fvector();
return FVector2D(v.X, v.Y);
}
bool UlxLuaCallLibrary::LuaCallReturnValue_boolean(UObject *context) {
AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context);
FlxStreamBuffer &sb = mode->LuaCallGetResult();
SimpleDynamicTag tag = sb.read_simple_dynamic_tag();
if (tag != SimpleDynamicTag::BOOLEAN) FatalBlueprintError(TEXT("expected lua to return a boolean"));
return sb.read_bool();
}