From 43a02db7f391773a11330734099af5f2cce0b38d Mon Sep 17 00:00:00 2001 From: jyelon Date: Wed, 4 Sep 2024 21:00:47 -0400 Subject: [PATCH] Working on probe: drv_call_function can now return a string --- luprex/cpp/core/drivenengine.cpp | 17 +++++++++++------ luprex/cpp/core/drivenengine.hpp | 9 ++++++--- luprex/cpp/core/enginewrapper.hpp | 3 ++- luprex/cpp/core/lpxclient.cpp | 12 ++++++------ luprex/cpp/core/lpxserver.cpp | 9 +++++---- luprex/cpp/drv/driver.cpp | 2 +- luprex/ext/base-buffer.hpp | 8 ++++---- 7 files changed, 35 insertions(+), 25 deletions(-) diff --git a/luprex/cpp/core/drivenengine.cpp b/luprex/cpp/core/drivenengine.cpp index 8de79468..6da6d905 100644 --- a/luprex/cpp/core/drivenengine.cpp +++ b/luprex/cpp/core/drivenengine.cpp @@ -475,11 +475,16 @@ void DrivenEngine::drv_update(double clock) { event_update(); } -void DrivenEngine::drv_call_function(InvocationKind kind, int64_t place, uint32_t datapklen, const char *datapk) { - Invocation *inv = new Invocation(kind, visible_actor_id_, place, std::string_view(datapk, datapklen)); +void DrivenEngine::drv_call_function(InvocationKind kind, int64_t place, uint32_t datapklen, const char *datapk, uint32_t *retpklen, const char **retpk) { // This next line is a hack, because the DrivenEngine is not supposed to care about 'kind'. if (kind == InvocationKind::LUA_SOURCE) rescan_lua_source_ = false; - event_call_function(kind, place, std::string_view(datapklen, datapk)); + call_function_retpk_.clear(); + event_call_function(kind, place, std::string_view(datapk, datapklen), &call_function_retpk_); + if (retpklen != nullptr) { + std::string_view view = call_function_retpk_.view(); + *retpklen = view.size(); + *retpk = view.data(); + } } ////////////////////////////////////////////////////////////////////////////// @@ -806,7 +811,7 @@ static void replay_update(EngineWrapper *w) { //////////////////////// -void play_call_function(EngineWrapper *w, InvocationKind kind, int64_t place, uint32_t datapklen, const char *datapk) { +void play_call_function(EngineWrapper *w, InvocationKind kind, int64_t place, uint32_t datapklen, const char *datapk, uint32_t *retpklen, const char **retpk) { assert(w->rlog == nullptr); if (w->wlog != nullptr) { w->wlog->write_cmd_hash(PLAY_CALL_FUNCTION, eng::memhash()); @@ -816,7 +821,7 @@ void play_call_function(EngineWrapper *w, InvocationKind kind, int64_t place, ui w->wlog->flush(); } - w->engine->drv_call_function(kind, place, datapklen, datapk); + w->engine->drv_call_function(kind, place, datapklen, datapk, retpklen, retpk); } void replay_call_function(EngineWrapper *w) { @@ -827,7 +832,7 @@ void replay_call_function(EngineWrapper *w) { return reset_wrapper(w, "replay log corrupt in replay_call_function_lua_call"); } - w->engine->drv_call_function(kind, place, srcpack.size(), srcpack.c_str()); + w->engine->drv_call_function(kind, place, srcpack.size(), srcpack.c_str(), nullptr, nullptr); } diff --git a/luprex/cpp/core/drivenengine.hpp b/luprex/cpp/core/drivenengine.hpp index 50f773b1..ba94bc38 100644 --- a/luprex/cpp/core/drivenengine.hpp +++ b/luprex/cpp/core/drivenengine.hpp @@ -149,9 +149,11 @@ public: // The call-function callback. This is invoked whenever drv_call_function // is called. This is the main entry point for "general" access into the - // DrivenEngine. + // DrivenEngine. The datapk parameter can contain any arbitrary data needed + // by the call. If the call wants to return anything, it can write the + // return data into the retpk datapack. // - virtual void event_call_function(InvocationKind kind, int64_t place, std::string_view datapk) {}; + virtual void event_call_function(InvocationKind kind, int64_t place, std::string_view datapk, StreamBuffer *retpk) {}; // The update callback. You may override this in a subclass. // This will be called whenever anything changes. @@ -294,7 +296,7 @@ public: void drv_notify_close(uint32_t chid, uint32_t len, const char *data); uint32_t drv_notify_accept(uint32_t port); void drv_update(double clock); - void drv_call_function(InvocationKind kind, int64_t place, uint32_t datapklen, const char *datapk); + void drv_call_function(InvocationKind kind, int64_t place, uint32_t datapklen, const char *datapk, uint32_t *retpklen, const char **retpk); private: // Find a currently-unused channel ID. Channel IDs @@ -316,6 +318,7 @@ private: int64_t visible_actor_id_; util::IdVector scan_result_; std::vector anim_queues_; + StreamBuffer call_function_retpk_; bool rescan_lua_source_; double clock_; bool stop_driver_; diff --git a/luprex/cpp/core/enginewrapper.hpp b/luprex/cpp/core/enginewrapper.hpp index a9cff38f..41ffaba3 100644 --- a/luprex/cpp/core/enginewrapper.hpp +++ b/luprex/cpp/core/enginewrapper.hpp @@ -25,6 +25,7 @@ enum class InvocationKind { LUA_CALL, LUA_EXPR, LUA_SOURCE, + LUA_PROBE, FLUSH_PRINTS, TICK, }; @@ -211,7 +212,7 @@ struct EngineWrapper { // This is the main pathway for blueprints, or the unreal C++ code, // to change the state of the world. // - void (*play_call_function)(EngineWrapper *w, InvocationKind kind, int64_t place, uint32_t datapklen, const char *datapk); + void (*play_call_function)(EngineWrapper *w, InvocationKind kind, int64_t place, uint32_t datapklen, const char *datapk, uint32_t *retpklen, const char **retpk); ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// diff --git a/luprex/cpp/core/lpxclient.cpp b/luprex/cpp/core/lpxclient.cpp index 1e3cb52a..8b2f60b9 100644 --- a/luprex/cpp/core/lpxclient.cpp +++ b/luprex/cpp/core/lpxclient.cpp @@ -19,7 +19,7 @@ public: SharedChannel channel_; LuaConsole console_; PrintChanneler print_channeler_; - eng::vector delayed_invocations_; + eng::vector delayed_invocations_; public: void set_initial_state_connect(const eng::string &hostspec) { @@ -246,14 +246,14 @@ public: return true; } - virtual void event_call_function(InvocationKind kind, int64_t place, std::string_view datapk) { - delayed_invocations_.emplace_back(kind, place, datapk); + virtual void event_call_function(InvocationKind kind, int64_t place_id, std::string_view datapk, StreamBuffer *retpk) override { + delayed_invocations_.emplace_back(kind, actor_id_, place_id, datapk); } - virtual void event_update() { + virtual void event_update() override { // Send invocations. We execute these using predictive execution. - for (const UniqueInvocation &inv : delayed_invocations_) { - send_invocation(*inv); + for (const Invocation &inv : delayed_invocations_) { + send_invocation(inv); } delayed_invocations_.clear(); diff --git a/luprex/cpp/core/lpxserver.cpp b/luprex/cpp/core/lpxserver.cpp index 5456ff4e..e09f34fa 100644 --- a/luprex/cpp/core/lpxserver.cpp +++ b/luprex/cpp/core/lpxserver.cpp @@ -32,6 +32,7 @@ public: int64_t admin_id_; int next_diff_chan_; double next_tick_; + eng::vector delayed_invocations_; public: virtual void event_init(std::string_view srcpk, int argc, char *argv[]) override { @@ -192,8 +193,8 @@ public: return true; } - virtual void event_call_function(InvocationKind kind, int64_t place, std::string_view datapk) { - delayed_invocations_.emplace_back(kind, place, datapk); + virtual void event_call_function(InvocationKind kind, int64_t place_id, std::string_view datapk, StreamBuffer *retpk) override { + delayed_invocations_.emplace_back(kind, admin_id_, place_id, datapk); } virtual void event_update() override { @@ -202,8 +203,8 @@ public: // Execute any queued invocations. // We just feed these directly into the master model. - for (const UniqueInvocation &inv : delayed_invocations_) { - master_->invoke(*inv); + for (const Invocation &inv : delayed_invocations_) { + master_->invoke(inv); } delayed_invocations_.clear(); diff --git a/luprex/cpp/drv/driver.cpp b/luprex/cpp/drv/driver.cpp index 6a196e11..f614d940 100644 --- a/luprex/cpp/drv/driver.cpp +++ b/luprex/cpp/drv/driver.cpp @@ -193,7 +193,7 @@ class Driver { std::string err = drvutil::package_lua_source(".", &oss); if_error_print_and_exit(err); std::string_view ossv = oss.view(); - engw.play_call_function(&engw, InvocationKind::LUA_SOURCE, 0, ossv.size(), ossv.data()); + engw.play_call_function(&engw, InvocationKind::LUA_SOURCE, 0, ossv.size(), ossv.data(), nullptr, nullptr); } } diff --git a/luprex/ext/base-buffer.hpp b/luprex/ext/base-buffer.hpp index 5655205d..b5ded800 100644 --- a/luprex/ext/base-buffer.hpp +++ b/luprex/ext/base-buffer.hpp @@ -424,10 +424,10 @@ public: // void clear() { assert(owned_); - if (!fixed_size_) { - if (buf_lo_ != nullptr) CoreHandler::basebuffer_free(buf_lo_); - buf_lo_ = 0; - buf_hi_ = 0; + if ((!fixed_size_) && (buf_lo_ != nullptr) && ((buf_hi_ - buf_lo_) > 100000)) { + CoreHandler::basebuffer_free(buf_lo_); + buf_lo_ = nullptr; + buf_hi_ = nullptr; } owned_ = true; read_cursor_ = buf_lo_;