Working on probe: drv_call_function can now return a string

This commit is contained in:
2024-09-04 21:00:47 -04:00
parent a6e68cbb35
commit 43a02db7f3
7 changed files with 35 additions and 25 deletions

View File

@@ -475,11 +475,16 @@ void DrivenEngine::drv_update(double clock) {
event_update(); event_update();
} }
void DrivenEngine::drv_call_function(InvocationKind kind, int64_t place, uint32_t datapklen, const char *datapk) { void DrivenEngine::drv_call_function(InvocationKind kind, int64_t place, uint32_t datapklen, const char *datapk, uint32_t *retpklen, const char **retpk) {
Invocation *inv = new Invocation(kind, visible_actor_id_, place, std::string_view(datapk, datapklen));
// This next line is a hack, because the DrivenEngine is not supposed to care about 'kind'. // 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; 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); assert(w->rlog == nullptr);
if (w->wlog != nullptr) { if (w->wlog != nullptr) {
w->wlog->write_cmd_hash(PLAY_CALL_FUNCTION, eng::memhash()); 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->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) { 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"); 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);
} }

View File

@@ -149,9 +149,11 @@ public:
// The call-function callback. This is invoked whenever drv_call_function // The call-function callback. This is invoked whenever drv_call_function
// is called. This is the main entry point for "general" access into the // 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. // The update callback. You may override this in a subclass.
// This will be called whenever anything changes. // 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); void drv_notify_close(uint32_t chid, uint32_t len, const char *data);
uint32_t drv_notify_accept(uint32_t port); uint32_t drv_notify_accept(uint32_t port);
void drv_update(double clock); 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: private:
// Find a currently-unused channel ID. Channel IDs // Find a currently-unused channel ID. Channel IDs
@@ -316,6 +318,7 @@ private:
int64_t visible_actor_id_; int64_t visible_actor_id_;
util::IdVector scan_result_; util::IdVector scan_result_;
std::vector<util::SharedStdString> anim_queues_; std::vector<util::SharedStdString> anim_queues_;
StreamBuffer call_function_retpk_;
bool rescan_lua_source_; bool rescan_lua_source_;
double clock_; double clock_;
bool stop_driver_; bool stop_driver_;

View File

@@ -25,6 +25,7 @@ enum class InvocationKind {
LUA_CALL, LUA_CALL,
LUA_EXPR, LUA_EXPR,
LUA_SOURCE, LUA_SOURCE,
LUA_PROBE,
FLUSH_PRINTS, FLUSH_PRINTS,
TICK, TICK,
}; };
@@ -211,7 +212,7 @@ struct EngineWrapper {
// This is the main pathway for blueprints, or the unreal C++ code, // This is the main pathway for blueprints, or the unreal C++ code,
// to change the state of the world. // 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);
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////

View File

@@ -19,7 +19,7 @@ public:
SharedChannel channel_; SharedChannel channel_;
LuaConsole console_; LuaConsole console_;
PrintChanneler print_channeler_; PrintChanneler print_channeler_;
eng::vector<UniqueInvocation> delayed_invocations_; eng::vector<Invocation> delayed_invocations_;
public: public:
void set_initial_state_connect(const eng::string &hostspec) { void set_initial_state_connect(const eng::string &hostspec) {
@@ -246,14 +246,14 @@ public:
return true; return true;
} }
virtual void event_call_function(InvocationKind kind, int64_t place, std::string_view datapk) { virtual void event_call_function(InvocationKind kind, int64_t place_id, std::string_view datapk, StreamBuffer *retpk) override {
delayed_invocations_.emplace_back(kind, place, datapk); 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. // Send invocations. We execute these using predictive execution.
for (const UniqueInvocation &inv : delayed_invocations_) { for (const Invocation &inv : delayed_invocations_) {
send_invocation(*inv); send_invocation(inv);
} }
delayed_invocations_.clear(); delayed_invocations_.clear();

View File

@@ -32,6 +32,7 @@ public:
int64_t admin_id_; int64_t admin_id_;
int next_diff_chan_; int next_diff_chan_;
double next_tick_; double next_tick_;
eng::vector<Invocation> delayed_invocations_;
public: public:
virtual void event_init(std::string_view srcpk, int argc, char *argv[]) override { virtual void event_init(std::string_view srcpk, int argc, char *argv[]) override {
@@ -192,8 +193,8 @@ public:
return true; return true;
} }
virtual void event_call_function(InvocationKind kind, int64_t place, std::string_view datapk) { virtual void event_call_function(InvocationKind kind, int64_t place_id, std::string_view datapk, StreamBuffer *retpk) override {
delayed_invocations_.emplace_back(kind, place, datapk); delayed_invocations_.emplace_back(kind, admin_id_, place_id, datapk);
} }
virtual void event_update() override { virtual void event_update() override {
@@ -202,8 +203,8 @@ public:
// Execute any queued invocations. // Execute any queued invocations.
// We just feed these directly into the master model. // We just feed these directly into the master model.
for (const UniqueInvocation &inv : delayed_invocations_) { for (const Invocation &inv : delayed_invocations_) {
master_->invoke(*inv); master_->invoke(inv);
} }
delayed_invocations_.clear(); delayed_invocations_.clear();

View File

@@ -193,7 +193,7 @@ class Driver {
std::string err = drvutil::package_lua_source(".", &oss); std::string err = drvutil::package_lua_source(".", &oss);
if_error_print_and_exit(err); if_error_print_and_exit(err);
std::string_view ossv = oss.view(); 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);
} }
} }

View File

@@ -424,10 +424,10 @@ public:
// //
void clear() { void clear() {
assert(owned_); assert(owned_);
if (!fixed_size_) { if ((!fixed_size_) && (buf_lo_ != nullptr) && ((buf_hi_ - buf_lo_) > 100000)) {
if (buf_lo_ != nullptr) CoreHandler::basebuffer_free(buf_lo_); CoreHandler::basebuffer_free(buf_lo_);
buf_lo_ = 0; buf_lo_ = nullptr;
buf_hi_ = 0; buf_hi_ = nullptr;
} }
owned_ = true; owned_ = true;
read_cursor_ = buf_lo_; read_cursor_ = buf_lo_;