diff --git a/clientmods/preview/example.lua b/clientmods/preview/example.lua index 2f661c073..41dc3b284 100644 --- a/clientmods/preview/example.lua +++ b/clientmods/preview/example.lua @@ -1,2 +1,2 @@ print("Loaded example file!, loading more examples") -dofile("preview:examples/first.lua") +dofile(core.get_modpath(core.get_current_modname()) .. "examples/first.lua") diff --git a/clientmods/preview/init.lua b/clientmods/preview/init.lua index bb8d1d600..95cf9ce64 100644 --- a/clientmods/preview/init.lua +++ b/clientmods/preview/init.lua @@ -1,9 +1,9 @@ -local modname = core.get_current_modname() or "??" +local modname = assert(core.get_current_modname()) local modstorage = core.get_mod_storage() local mod_channel -dofile("preview:example.lua") --- This is an example function to ensure it's working properly, should be removed before merge +dofile(core.get_modpath(modname) .. "example.lua") + core.register_on_shutdown(function() print("[PREVIEW] shutdown client") end) diff --git a/doc/client_lua_api.txt b/doc/client_lua_api.txt index d355376f5..ab7963029 100644 --- a/doc/client_lua_api.txt +++ b/doc/client_lua_api.txt @@ -631,6 +631,10 @@ Minetest namespace reference ### Utilities * `minetest.get_current_modname()`: returns the currently loading mod's name, when we are loading a mod +* `minetest.get_modpath(modname)`: returns virtual path of given mod including + the trailing separator. This is useful to load additional Lua files + contained in your mod: + e.g. `dofile(minetest.get_modpath(minetest.get_current_modname()) .. "stuff.lua")` * `minetest.get_language()`: returns the currently set gettext language. * `minetest.get_version()`: returns a table containing components of the engine version. Components: diff --git a/src/script/lua_api/l_client.cpp b/src/script/lua_api/l_client.cpp index 6345fc75f..febf528de 100644 --- a/src/script/lua_api/l_client.cpp +++ b/src/script/lua_api/l_client.cpp @@ -36,12 +36,23 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/string.h" #include "nodedef.h" +// get_current_modname() int ModApiClient::l_get_current_modname(lua_State *L) { lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME); return 1; } +// get_modpath(modname) +int ModApiClient::l_get_modpath(lua_State *L) +{ + std::string modname = readParam(L, 1); + // Client mods use a virtual filesystem, see Client::scanModSubfolder() + std::string path = modname + ":"; + lua_pushstring(L, path.c_str()); + return 1; +} + // get_last_run_mod() int ModApiClient::l_get_last_run_mod(lua_State *L) { @@ -365,6 +376,7 @@ int ModApiClient::l_get_builtin_path(lua_State *L) void ModApiClient::Initialize(lua_State *L, int top) { API_FCT(get_current_modname); + API_FCT(get_modpath); API_FCT(print); API_FCT(display_chat_message); API_FCT(send_chat_message); diff --git a/src/script/lua_api/l_client.h b/src/script/lua_api/l_client.h index 0d3e6b106..0a68eeff0 100644 --- a/src/script/lua_api/l_client.h +++ b/src/script/lua_api/l_client.h @@ -30,6 +30,9 @@ private: // get_current_modname() static int l_get_current_modname(lua_State *L); + // get_modpath(modname) + static int l_get_modpath(lua_State *L); + // print(text) static int l_print(lua_State *L);