From ede234fc8a57f4386032084fe2aa6fd60ada79f9 Mon Sep 17 00:00:00 2001 From: jyelon Date: Wed, 4 Sep 2024 23:15:13 -0400 Subject: [PATCH] More work on the LuaCall interface --- .../Integration/IntegrationGameModeBase.cpp | 46 ++++++++----------- Source/Integration/IntegrationGameModeBase.h | 22 ++++++--- Source/Integration/LockedWrapper.cpp | 7 --- Source/Integration/LockedWrapper.h | 4 -- Source/Integration/LuaCall.cpp | 7 ++- 5 files changed, 37 insertions(+), 49 deletions(-) diff --git a/Source/Integration/IntegrationGameModeBase.cpp b/Source/Integration/IntegrationGameModeBase.cpp index fafc208b..421b35c4 100644 --- a/Source/Integration/IntegrationGameModeBase.cpp +++ b/Source/Integration/IntegrationGameModeBase.cpp @@ -141,38 +141,32 @@ void AIntegrationGameModeBase::UpdateTangibles() { TangibleManager->DeleteFarawayTangibles(); } -// void AIntegrationGameModeBase::InvokeEngioMove(const FString &action, const FVector &xyz, double facing) { -// FTCHARToUTF8 utf8action(action); -// std::string uaction(utf8action.Get(), utf8action.Length()); -// FlxStreamBuffer sb; -// sb.write_string("engio"); -// sb.write_string("move"); -// sb.write_simple_dynamic_tag(SimpleDynamicTag::STRING); -// sb.write_string(uaction); -// sb.write_simple_dynamic_tag(SimpleDynamicTag::VECTOR); -// sb.write_fvector(xyz); -// sb.write_simple_dynamic_tag(SimpleDynamicTag::NUMBER); -// sb.write_double(facing); -// std::string_view datapk = sb.view(); -// FlxLockedWrapper w(LockableWrapper); -// int64 player = w.GetActor(); -// w->play_call_function_lua_call(w.Get(), player, datapk.size(), datapk.data()); -// } - -void AIntegrationGameModeBase::LuaCallInvoke(AActor *place) { +void AIntegrationGameModeBase::LuaCallEnd(InvocationKind kind, int64 place_id) { std::string_view datapk = LuaCallBuffer.view(); FlxLockedWrapper w(LockableWrapper); - int64_t place_id = w.GetActor(); - if (place != nullptr) { - place_id = UlxTangible::GetActorTangible(place)->TangibleId; + if (place_id == 0) place_id = w.GetActor(); + uint32_t retpklen; + const char *retpk; + w->play_call_function(w.Get(), kind, place_id, datapk.size(), datapk.data(), &retpklen, &retpk); + LuaCallResult.open(std::string_view(retpk, retpklen)); +} + +void AIntegrationGameModeBase::LuaCallEnd(InvocationKind kind) { + LuaCallEnd(kind, int64(0)); +} + +void AIntegrationGameModeBase::LuaCallEnd(InvocationKind kind, AActor *place) { + if (place == nullptr) { + LuaCallEnd(kind, int64(0)); + } else { + LuaCallEnd(kind, UlxTangible::GetActorTangible(place)->TangibleId); } - w.PlayCallFunction(InvocationKind::LUA_CALL, place_id, datapk); } void AIntegrationGameModeBase::ExecuteDebuggingCommand(FlxLockedWrapper &w, const FString &fs) { if (fs == "\\invokeplayer") { DPrint(TEXT("Trying to invoke 'myfunction' in lua")); - FlxStreamBuffer sb; + FlxStreamBuffer &sb = LuaCallBegin(); sb.write_string("engio"); sb.write_string("myfunction"); sb.write_simple_dynamic_tag(SimpleDynamicTag::NUMBER); @@ -181,9 +175,7 @@ void AIntegrationGameModeBase::ExecuteDebuggingCommand(FlxLockedWrapper &w, cons sb.write_string("Howdy"); sb.write_simple_dynamic_tag(SimpleDynamicTag::VECTOR); sb.write_fvector(FVector(2,3,4)); - std::string_view datapk = sb.view(); - int64 player = w.GetActor(); - w.PlayCallFunction(InvocationKind::LUA_CALL, player, datapk); + LuaCallEnd(InvocationKind::LUA_INVOKE); } else { ConsoleOutput.AppendLine(TEXT("Unknown Command")); } diff --git a/Source/Integration/IntegrationGameModeBase.h b/Source/Integration/IntegrationGameModeBase.h index 7c16ebbd..a1e5c96e 100644 --- a/Source/Integration/IntegrationGameModeBase.h +++ b/Source/Integration/IntegrationGameModeBase.h @@ -46,15 +46,22 @@ public: // Assemble a lua call. To call into lua: // // * Use LuaCallBegin - // * Get the lua call buffer. - // * Add a class name and a function name to the buffer. - // * Add parameters to the buffer. - // * Use LuaCallInvoke. + // * Get the lua call buffer: + // - add a class name + // - add a function name + // - add function parameters + // * Use LuaCallEnd. + // * Get the lua call result. + // - parse out any return values // - void LuaCallBegin() { LuaCallBuffer.clear(); } + FlxStreamBuffer &LuaCallBegin() { LuaCallBuffer.clear(); return LuaCallBuffer; } FlxStreamBuffer &LuaCallGetBuffer() { return LuaCallBuffer; } - void LuaCallInvoke(AActor *place); - + void LuaCallEnd(InvocationKind kind); + void LuaCallEnd(InvocationKind kind, int64 place_id); + void LuaCallEnd(InvocationKind kind, AActor *place); + FlxStreamBuffer &LuaCallGetResult() { return LuaCallResult; } + void LuaCallClear() { LuaCallBuffer.clear(); LuaCallResult.clear(); } + // Execute a debugging command, typed on the GUI. void ExecuteDebuggingCommand(FlxLockedWrapper &w, const FString &fs); @@ -89,6 +96,7 @@ public: // The Lua Call assembly buffer. FlxStreamBuffer LuaCallBuffer; + FlxStreamBuffer LuaCallResult; // This utility runs the luprex update and socket update in a thread. FTriggeredTask LuprexUpdateTask; diff --git a/Source/Integration/LockedWrapper.cpp b/Source/Integration/LockedWrapper.cpp index 30bea28b..0b1a4432 100644 --- a/Source/Integration/LockedWrapper.cpp +++ b/Source/Integration/LockedWrapper.cpp @@ -50,13 +50,6 @@ IdView FlxLockedWrapper::GetNear(int64 id, double rx, double ry, double rz) { return IdView(data, size); } -std::string_view FlxLockedWrapper::PlayCallFunction(InvocationKind kind, int64 place, std::string_view datapk) { - uint32_t retpklen; - const char *retpk; - Lockable.Wrapper.play_call_function(Get(), kind, place, datapk.size(), datapk.data(), &retpklen, &retpk); - return std::string_view(retpk, retpklen); -} - StringViewVec FlxLockedWrapper::GetAnimationQueues(IdView ids) { // How many animation queues are we fetching? int num = ids.Num(); diff --git a/Source/Integration/LockedWrapper.h b/Source/Integration/LockedWrapper.h index 431e885d..746d0962 100644 --- a/Source/Integration/LockedWrapper.h +++ b/Source/Integration/LockedWrapper.h @@ -84,10 +84,6 @@ public: // IdView GetNear(int64 id, double rx, double ry, double rz); - // PlayCallFunction - a thin wrapper around play_call_function. - // - std::string_view PlayCallFunction(InvocationKind kind, int64 place, std::string_view datapk); - // Get animation queues. // // The array returned by this is valid until the diff --git a/Source/Integration/LuaCall.cpp b/Source/Integration/LuaCall.cpp index 05ef2987..ea1e4f62 100644 --- a/Source/Integration/LuaCall.cpp +++ b/Source/Integration/LuaCall.cpp @@ -57,14 +57,13 @@ void UlxLuaCallLibrary::LuaCallInvoke(UObject *context, AActor *place) { AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context); FlxStreamBuffer &sb = mode->LuaCallGetBuffer(); CheckNotEmpty(sb); - mode->LuaCallInvoke(place); + mode->LuaCallEnd(InvocationKind::LUA_INVOKE, place); } void UlxLuaCallLibrary::InvokeEngioMove(UObject *context, const FString &action, const FVector &xyz, double facing) { AIntegrationGameModeBase *mode = AIntegrationGameModeBase::GetFromContext(context); - FlxStreamBuffer &sb = mode->LuaCallGetBuffer(); - mode->LuaCallBegin(); + FlxStreamBuffer &sb = mode->LuaCallBegin(); sb.write_string("engio"); sb.write_string("move"); sb.write_simple_dynamic_tag(SimpleDynamicTag::STRING); @@ -73,5 +72,5 @@ void UlxLuaCallLibrary::InvokeEngioMove(UObject *context, const FString &action, sb.write_fvector(xyz); sb.write_simple_dynamic_tag(SimpleDynamicTag::NUMBER); sb.write_double(facing); - mode->LuaCallInvoke(nullptr); + mode->LuaCallEnd(InvocationKind::LUA_INVOKE); }