diff --git a/luprex/cpp/core/drivenengine.cpp b/luprex/cpp/core/drivenengine.cpp index 42c21db9..12f1f1c2 100644 --- a/luprex/cpp/core/drivenengine.cpp +++ b/luprex/cpp/core/drivenengine.cpp @@ -21,13 +21,6 @@ DrivenEngineReg::DrivenEngineReg(const char *n, DrivenEngineMaker fn) { All = this; } -DrivenEngineInitializer DrivenEngineInitializerReg::func; - -DrivenEngineInitializerReg::DrivenEngineInitializerReg(DrivenEngineInitializer fn) { - assert(func == nullptr); - func = fn; -} - ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// // @@ -443,69 +436,6 @@ void DrivenEngine::drv_access(AccessKind kind, int64_t place, uint32_t datapklen } } -////////////////////////////////////////////////////////////////////////////// -////////////////////////////////////////////////////////////////////////////// -// -// C Wrappers: Getters -// -// These wrappers make it possible to call the drv_get routines using C -// functions instead of methods. This is important if the engine is compiled -// with one C++ compiler, but the driver is compiled with a different C++ -// compiler. -// -// Some of these take parameter 'EngineWrapper', some take 'EngineWrapper', -// and some come in two versions. This all depends on whether they are used -// during play, during replay, or both. -// -////////////////////////////////////////////////////////////////////////////// -////////////////////////////////////////////////////////////////////////////// - -static void drv_get_listen_ports(EngineWrapper *w, uint32_t *nports, const uint32_t **ports) { - return w->engine->drv_get_listen_ports(nports, ports); -} - -static void drv_get_new_outgoing(EngineWrapper *w, uint32_t *nchanids, const uint32_t **chanids) { - return w->engine->drv_get_new_outgoing(nchanids, chanids); -} - -static const char *drv_get_target(EngineWrapper *w, uint32_t chid) { - return w->engine->drv_get_target(chid); -} - -static bool drv_get_channel_released(EngineWrapper *w, uint32_t chid) { - return w->engine->drv_get_channel_released(chid); -} - -static void drv_get_outgoing(EngineWrapper *w, uint32_t chid, uint32_t *len, const char **data) { - return w->engine->drv_get_outgoing(chid, len, data); -} - -static bool drv_get_outgoing_empty(EngineWrapper *w, uint32_t chid) { - return w->engine->drv_get_outgoing_empty(chid); -} - -static double drv_get_clock(EngineWrapper *w) { - return w->engine->drv_get_clock(); -} - -static bool drv_get_have_prints(EngineWrapper *w) { - return w->engine->drv_get_have_prints(); -} - -static bool drv_get_rescan_lua_source(EngineWrapper *w) { - return w->engine->drv_get_rescan_lua_source(); -} - -static bool drv_get_stop_driver(EngineWrapper *w) { - return w->engine->drv_get_stop_driver(); -} - -static uint64_t drv_get_actor_id(EngineWrapper *w) { - return w->engine->drv_get_actor_id(); -} - - - ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// // @@ -846,47 +776,58 @@ static void replaycore_step(EngineWrapper *w) { static int64_t empty_id_list_ = 0; -static void default_get_tangibles_near(EngineWrapper *, uint64_t, double, double, double, uint32_t *count, int64_t **ids) { - *count = 0; - *ids = &empty_id_list_; -} - -static void default_get_animation_queues(EngineWrapper *, uint32_t count, const int64_t *, uint32_t *lengths, const char **strings) { - for (int i = 0; i < int(count); i++) { - lengths[i] = 0; - strings[i] = ""; - } -} - void DrivenEngine::unexpose_world_to_driver(EngineWrapper *w) { w->world = nullptr; - w->get_tangibles_near = default_get_tangibles_near; - w->get_animation_queues = default_get_animation_queues; + w->get_tangibles_near = [](EngineWrapper *, uint64_t, double, double, double, uint32_t *count, int64_t **ids) { + *count = 0; + *ids = &empty_id_list_; + }; + w->get_animation_queues = [](EngineWrapper *, uint32_t count, const int64_t *, uint32_t *lengths, const char **strings) { + for (int i = 0; i < int(count); i++) { + lengths[i] = 0; + strings[i] = ""; + } + }; } -static void init_engine_wrapper_helper(EngineWrapper *w) { - static bool called_initializer; - assert(DrivenEngineInitializerReg::func != nullptr); - if (!called_initializer) { - DrivenEngineInitializerReg::func(); - called_initializer = true; - } - +void DrivenEngine::init_engine_wrapper(EngineWrapper *w) { memset(w, 0, sizeof(EngineWrapper)); - w->get_listen_ports = drv_get_listen_ports; - w->get_new_outgoing = drv_get_new_outgoing; - w->get_target = drv_get_target; - w->get_channel_released = drv_get_channel_released; - w->get_outgoing = drv_get_outgoing; - w->get_outgoing_empty = drv_get_outgoing_empty; - w->get_clock = drv_get_clock; - w->get_have_prints = drv_get_have_prints; - w->get_rescan_lua_source = drv_get_rescan_lua_source; - w->get_stop_driver = drv_get_stop_driver; - w->get_actor_id = drv_get_actor_id; - w->get_tangibles_near = default_get_tangibles_near; - w->get_animation_queues = default_get_animation_queues; + w->get_listen_ports = [](EngineWrapper *w, uint32_t *nports, const uint32_t **ports) { + w->engine->drv_get_listen_ports(nports, ports); + }; + w->get_new_outgoing = [](EngineWrapper *w, uint32_t *nchanids, const uint32_t **chanids) { + w->engine->drv_get_new_outgoing(nchanids, chanids); + }; + w->get_target = [](EngineWrapper *w, uint32_t chid) { + return w->engine->drv_get_target(chid); + }; + w->get_channel_released = [](EngineWrapper *w, uint32_t chid) { + return w->engine->drv_get_channel_released(chid); + }; + w->get_outgoing = [](EngineWrapper *w, uint32_t chid, uint32_t *len, const char **data) { + w->engine->drv_get_outgoing(chid, len, data); + }; + w->get_outgoing_empty = [](EngineWrapper *w, uint32_t chid) { + return w->engine->drv_get_outgoing_empty(chid); + }; + w->get_clock = [](EngineWrapper *w) { + return w->engine->drv_get_clock(); + }; + w->get_have_prints = [](EngineWrapper *w) { + return w->engine->drv_get_have_prints(); + }; + w->get_rescan_lua_source = [](EngineWrapper *w) { + return w->engine->drv_get_rescan_lua_source(); + }; + w->get_stop_driver = [](EngineWrapper *w) { + return w->engine->drv_get_stop_driver(); + }; + w->get_actor_id = [](EngineWrapper *w) { + return w->engine->drv_get_actor_id(); + }; + + unexpose_world_to_driver(w); w->play_initialize = play_initialize; w->play_clear_new_outgoing = play_clear_new_outgoing; @@ -902,7 +843,7 @@ static void init_engine_wrapper_helper(EngineWrapper *w) { w->hook_dprint = util::hook_dprint; w->release = release; -}; +} #if defined(__linux__) #define DLLEXPORT __attribute__((visibility("default"))) @@ -912,6 +853,7 @@ static void init_engine_wrapper_helper(EngineWrapper *w) { extern "C" { DLLEXPORT void init_engine_wrapper(EngineWrapper *w) { - init_engine_wrapper_helper(w); + DrivenEngine::init_engine_wrapper(w); } } + diff --git a/luprex/cpp/core/drivenengine.hpp b/luprex/cpp/core/drivenengine.hpp index 99513424..5a52aba8 100644 --- a/luprex/cpp/core/drivenengine.hpp +++ b/luprex/cpp/core/drivenengine.hpp @@ -56,7 +56,6 @@ class DrivenEngine; using UniqueDrivenEngine = std::unique_ptr; using DrivenEngineMaker = UniqueDrivenEngine (*)(EngineWrapper *); -using DrivenEngineInitializer = void (*)(); class Channel : public eng::opnew { @@ -134,6 +133,8 @@ public: static void print_usage(std::ostream &strm, std::string_view progname); + static void init_engine_wrapper(EngineWrapper *w); + ////////////////////////////////////////////////////////////// // // The following methods are the 'engine' side of the pipe. @@ -320,7 +321,3 @@ struct DrivenEngineReg { } \ DrivenEngineReg dengreg_##cname(name, dengmake_##cname); -struct DrivenEngineInitializerReg { - static DrivenEngineInitializer func; - DrivenEngineInitializerReg(DrivenEngineInitializer f); -}; diff --git a/luprex/cpp/core/enginewrapper.hpp b/luprex/cpp/core/enginewrapper.hpp index 0c2391cc..bcafc9fe 100644 --- a/luprex/cpp/core/enginewrapper.hpp +++ b/luprex/cpp/core/enginewrapper.hpp @@ -60,7 +60,8 @@ struct EngineWrapper { // Of course, there's no constructor, since this is a C struct. // To initialize it, you use 'dlsym' or 'GetProcAddress' to get the // address of the function 'init_engine_wrapper'. Then, you call - // the function init_engine_wrapper(&wrapper). + // the function init_engine_wrapper(&wrapper). It is OK to call + // init_engine_wrapper more than once. ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// @@ -131,7 +132,7 @@ struct EngineWrapper { // Get the actor ID. May return zero if the server is down. // - uint64_t (*get_actor_id)(EngineWrapper *w); + int64_t (*get_actor_id)(EngineWrapper *w); // Do a scan to find tangibles near the specified player. // diff --git a/luprex/cpp/core/source.cpp b/luprex/cpp/core/source.cpp index 5d6b35f7..ec1704e8 100644 --- a/luprex/cpp/core/source.cpp +++ b/luprex/cpp/core/source.cpp @@ -434,6 +434,7 @@ eng::string SourceDB::rebuild_module(const eng::string &mod) { } void SourceDB::init(lua_State *L) { + register_lua_builtins(); lua_state_ = L; LuaVar globtab, persist, unpersist, classname, classtab, funcname, funcp, rawfunc, nullstring; LuaExtStack LS(L, globtab, persist, unpersist, classname, classtab, funcname, funcp, rawfunc, nullstring); @@ -508,6 +509,10 @@ void SourceDB::deserialize_source(util::LuaSourceVec *sv, StreamBuffer *sb) { // print out an error. // void SourceDB::register_lua_builtins() { + static bool initialized = false; + if (initialized) return; + initialized = true; + lua_State *L = LuaCoreStack::newstate(nullptr); luaL_openlibs(L); { diff --git a/luprex/cpp/core/source.hpp b/luprex/cpp/core/source.hpp index 05a2975b..73011feb 100644 --- a/luprex/cpp/core/source.hpp +++ b/luprex/cpp/core/source.hpp @@ -127,6 +127,7 @@ class SourceDB : public eng::nevernew { private: lua_State *lua_state_; + static void register_lua_builtins(); public: void init(lua_State *L); @@ -188,10 +189,6 @@ public: void set(const eng::string &fn, const eng::string &code, int sequence); eng::string get(const eng::string &fn); - // Add builtins to the global function registry. - // - static void register_lua_builtins(); - // Get function documentation. // // Returns false if it has no documentation for the specified function. diff --git a/luprex/cpp/core/world-core.cpp b/luprex/cpp/core/world-core.cpp index ac24cbd0..9d85e7da 100644 --- a/luprex/cpp/core/world-core.cpp +++ b/luprex/cpp/core/world-core.cpp @@ -1258,16 +1258,6 @@ void World::rollback() { assert(snapshot_.empty()); } -// This is the main routine for the DLL. We have to use a registration device -// to register this main routine with DrivenEngine. DrivenEngine will then call -// it exactly once the first time that the driver initializes an EngineWrapper. -// -void engine_initialization() { - SourceDB::register_lua_builtins(); -} - -static DrivenEngineInitializerReg eireg(engine_initialization); - ////////////////////////////////////////////////////////////////////////////// // // Functions that allow the Driver to Peer Directly into the World. diff --git a/luprex/cpp/core/world-difftab.cpp b/luprex/cpp/core/world-difftab.cpp index d4982b9d..439c03f2 100644 --- a/luprex/cpp/core/world-difftab.cpp +++ b/luprex/cpp/core/world-difftab.cpp @@ -460,7 +460,6 @@ class DiffTester { public: DiffTester(lua_State *caller) : caller_(caller) { - SourceDB::register_lua_builtins(); master_L_ = LuaCoreStack::newstate(eng::l_alloc); synch_L_ = LuaCoreStack::newstate(eng::l_alloc); master_sdb_.init(master_L_);