From: Matthew I Date: Sat, 8 Sep 2012 18:44:26 +0000 (-0400) Subject: Add shutdown hook interface to Lua API X-Git-Url: http://81.2.79.47:8989/gitweb/?a=commitdiff_plain;h=6c8fa83ecd3f40a8e2fec890caaaa955f9d4255c;p=zefram%2Fminetest%2Fminetest_engine.git Add shutdown hook interface to Lua API Scripts can call minetest.register_on_shutdown() to register a shutdown hook. Document that minetest.register_on_shutdown() callbacks may not be run If the server crashes, it is unlikely that callbacks registered using minetest.register_on_shutdown() will be called. --- diff --git a/builtin/misc_register.lua b/builtin/misc_register.lua index 77c594de..f9c06a02 100644 --- a/builtin/misc_register.lua +++ b/builtin/misc_register.lua @@ -303,6 +303,7 @@ end minetest.registered_on_chat_messages, minetest.register_on_chat_message = make_registration() minetest.registered_globalsteps, minetest.register_globalstep = make_registration() +minetest.registered_on_shutdown, minetest.register_on_shutdown = make_registration() minetest.registered_on_punchnodes, minetest.register_on_punchnode = make_registration() minetest.registered_on_placenodes, minetest.register_on_placenode = make_registration() minetest.registered_on_dignodes, minetest.register_on_dignode = make_registration() diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 550716ce..e32efc6d 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -792,6 +792,11 @@ minetest.register_craft(recipe) Global callback registration functions: (Call these only at load time) minetest.register_globalstep(func(dtime)) ^ Called every server step, usually interval of 0.05s +minetest.register_on_shutdown(func()) +^ Called before server shutdown +^ WARNING: If the server terminates abnormally (i.e. crashes), the registered + callbacks WILL LIKELY NOT BE RUN. Data should be saved at + semi-frequent intervals as well as on server shutdown. minetest.register_on_placenode(func(pos, newnode, placer, oldnode)) ^ Called when a node has been placed ^ Deprecated: Use on_construct or after_place_node in node definition instead diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index 91100d31..e5815c46 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -5576,6 +5576,19 @@ bool scriptapi_on_chat_message(lua_State *L, const std::string &name, return ate; } +void scriptapi_on_shutdown(lua_State *L) +{ + realitycheck(L); + assert(lua_checkstack(L, 20)); + StackUnroller stack_unroller(L); + + // Get registered shutdown hooks + lua_getglobal(L, "minetest"); + lua_getfield(L, -1, "registered_on_shutdown"); + // Call callbacks + scriptapi_run_callbacks(L, 0, RUN_CALLBACKS_MODE_FIRST); +} + void scriptapi_on_newplayer(lua_State *L, ServerActiveObject *player) { realitycheck(L); diff --git a/src/scriptapi.h b/src/scriptapi.h index 144cb3bc..d71b8fe4 100644 --- a/src/scriptapi.h +++ b/src/scriptapi.h @@ -55,6 +55,9 @@ void scriptapi_environment_step(lua_State *L, float dtime); void scriptapi_environment_on_generated(lua_State *L, v3s16 minp, v3s16 maxp, u32 blockseed); +/* server */ +void scriptapi_on_shutdown(lua_State *L); + /* misc */ void scriptapi_on_newplayer(lua_State *L, ServerActiveObject *player); void scriptapi_on_dieplayer(lua_State *L, ServerActiveObject *player); diff --git a/src/server.cpp b/src/server.cpp index 961bdeaf..67c1ce92 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1112,6 +1112,11 @@ Server::~Server() {} } } + + /* + Execute script shutdown hooks + */ + scriptapi_on_shutdown(m_lua); { JMutexAutoLock envlock(m_env_mutex);