From ccc3af128cb4b6213b8111115c570fe988d3523b Mon Sep 17 00:00:00 2001 From: Loïc Blot Date: Wed, 6 Jun 2018 12:53:59 +0200 Subject: CSM/SSM: Add on_mods_loaded callback (#7411) * CSM/SSM: Add on_mods_loaded callback --- builtin/client/register.lua | 1 + builtin/game/register.lua | 1 + clientmods/preview/init.lua | 5 +++++ doc/client_lua_api.txt | 2 ++ doc/lua_api.txt | 3 +++ games/minimal/mods/experimental/init.lua | 5 +++++ src/client.cpp | 2 ++ src/script/cpp_api/s_client.cpp | 11 +++++++++++ src/script/cpp_api/s_client.h | 3 +++ src/script/cpp_api/s_server.cpp | 11 +++++++++++ src/script/cpp_api/s_server.h | 3 +++ src/server/mods.cpp | 3 +++ 12 files changed, 50 insertions(+) diff --git a/builtin/client/register.lua b/builtin/client/register.lua index 955655236..c1b4965c1 100644 --- a/builtin/client/register.lua +++ b/builtin/client/register.lua @@ -59,6 +59,7 @@ local function make_registration() end core.registered_globalsteps, core.register_globalstep = make_registration() +core.registered_on_mods_loaded, core.register_on_mods_loaded = make_registration() core.registered_on_shutdown, core.register_on_shutdown = make_registration() core.registered_on_receiving_chat_message, core.register_on_receiving_chat_message = make_registration() core.registered_on_sending_chat_message, core.register_on_sending_chat_message = make_registration() diff --git a/builtin/game/register.lua b/builtin/game/register.lua index 15c08b045..6a7c4747f 100644 --- a/builtin/game/register.lua +++ b/builtin/game/register.lua @@ -569,6 +569,7 @@ core.unregister_biome = make_wrap_deregistration(core.register_biome, core.clear core.registered_on_chat_messages, core.register_on_chat_message = make_registration() core.registered_globalsteps, core.register_globalstep = make_registration() core.registered_playerevents, core.register_playerevent = make_registration() +core.registered_on_mods_loaded, core.register_on_mods_loaded = make_registration() core.registered_on_shutdown, core.register_on_shutdown = make_registration() core.registered_on_punchnodes, core.register_on_punchnode = make_registration() core.registered_on_placenodes, core.register_on_placenode = make_registration() diff --git a/clientmods/preview/init.lua b/clientmods/preview/init.lua index 223577876..bb8d1d600 100644 --- a/clientmods/preview/init.lua +++ b/clientmods/preview/init.lua @@ -200,3 +200,8 @@ core.register_chatcommand("text", { return core.localplayer:hud_change(id, "text", param) end, }) + + +core.register_on_mods_loaded(function() + core.log("Yeah preview mod is loaded with other CSM mods.") +end) diff --git a/doc/client_lua_api.txt b/doc/client_lua_api.txt index bdefa3af5..46b47d892 100644 --- a/doc/client_lua_api.txt +++ b/doc/client_lua_api.txt @@ -648,6 +648,8 @@ Call these functions only at load time! * `minetest.register_globalstep(func(dtime))` * Called every client environment step, usually interval of 0.1s +* `minetest.register_on_mods_loaded(func())` + * Called just after mods have finished loading. * `minetest.register_on_shutdown(func())` * Called before client shutdown * **Warning**: If the client terminates abnormally (i.e. crashes), the registered diff --git a/doc/lua_api.txt b/doc/lua_api.txt index b89ba47c6..d9cf96800 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -2793,6 +2793,9 @@ Call these functions only at load time! * `minetest.register_globalstep(func(dtime))` * Called every server step, usually interval of 0.1s +* `minetest.register_on_mods_loaded(func())` + * Called after mods have finished loading and before the media is cached or the + aliases handled. * `minetest.register_on_shutdown(func())` * Called before server shutdown * **Warning**: If the server terminates abnormally (i.e. crashes), the diff --git a/games/minimal/mods/experimental/init.lua b/games/minimal/mods/experimental/init.lua index f4dc78b21..4d5c61488 100644 --- a/games/minimal/mods/experimental/init.lua +++ b/games/minimal/mods/experimental/init.lua @@ -765,4 +765,9 @@ minetest.log("experimental modname="..dump(minetest.get_current_modname())) minetest.log("experimental modpath="..dump(minetest.get_modpath("experimental"))) minetest.log("experimental worldpath="..dump(minetest.get_worldpath())) + +core.register_on_mods_loaded(function() + core.log("Yeah experimental loaded mods.") +end) + -- END diff --git a/src/client.cpp b/src/client.cpp index d2f585de7..18e43b1b6 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -172,6 +172,8 @@ void Client::loadMods() for (const ModSpec &mod : m_mods) m_script->loadModFromMemory(mod.name); + // Run a callback when mods are loaded + m_script->on_mods_loaded(); m_mods_loaded = true; } diff --git a/src/script/cpp_api/s_client.cpp b/src/script/cpp_api/s_client.cpp index a8d8c2de4..7be4fc6a9 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -25,6 +25,17 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "common/c_content.h" #include "s_item.h" +void ScriptApiClient::on_mods_loaded() +{ + SCRIPTAPI_PRECHECKHEADER + + // Get registered shutdown hooks + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_mods_loaded"); + // Call callbacks + runCallbacks(0, RUN_CALLBACKS_MODE_FIRST); +} + void ScriptApiClient::on_shutdown() { SCRIPTAPI_PRECHECKHEADER diff --git a/src/script/cpp_api/s_client.h b/src/script/cpp_api/s_client.h index 402b44e33..93fe96791 100644 --- a/src/script/cpp_api/s_client.h +++ b/src/script/cpp_api/s_client.h @@ -37,6 +37,9 @@ class ClientEnvironment; class ScriptApiClient : virtual public ScriptApiBase { public: + // Calls when mods are loaded + void on_mods_loaded(); + // Calls on_shutdown handlers void on_shutdown(); diff --git a/src/script/cpp_api/s_server.cpp b/src/script/cpp_api/s_server.cpp index 38bd41f87..b0459fbfa 100644 --- a/src/script/cpp_api/s_server.cpp +++ b/src/script/cpp_api/s_server.cpp @@ -147,6 +147,17 @@ bool ScriptApiServer::on_chat_message(const std::string &name, return ate; } +void ScriptApiServer::on_mods_loaded() +{ + SCRIPTAPI_PRECHECKHEADER + + // Get registered shutdown hooks + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_mods_loaded"); + // Call callbacks + runCallbacks(0, RUN_CALLBACKS_MODE_FIRST); +} + void ScriptApiServer::on_shutdown() { SCRIPTAPI_PRECHECKHEADER diff --git a/src/script/cpp_api/s_server.h b/src/script/cpp_api/s_server.h index 74de19bee..769939d3f 100644 --- a/src/script/cpp_api/s_server.h +++ b/src/script/cpp_api/s_server.h @@ -30,6 +30,9 @@ public: // Returns true if script handled message bool on_chat_message(const std::string &name, const std::string &message); + // Calls when mods are loaded + void on_mods_loaded(); + // Calls on_shutdown handlers void on_shutdown(); diff --git a/src/server/mods.cpp b/src/server/mods.cpp index c246e6446..c5616dcd6 100644 --- a/src/server/mods.cpp +++ b/src/server/mods.cpp @@ -75,6 +75,9 @@ void ServerModManager::loadMods(ServerScripting *script) std::chrono::steady_clock::now() - t).count() * 0.001f << " seconds" << std::endl; } + + // Run a callback when mods are loaded + script->on_mods_loaded(); } // clang-format on -- cgit v1.2.3