Skip to content

Commit

Permalink
Introduce get_modpath() for CSM
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Nov 9, 2019
1 parent 82a2e02 commit b1f2a69
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion 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")
6 changes: 3 additions & 3 deletions 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)
Expand Down
4 changes: 4 additions & 0 deletions doc/client_lua_api.txt
Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions src/script/lua_api/l_client.cpp
Expand Up @@ -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<std::string>(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)
{
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/script/lua_api/l_client.h
Expand Up @@ -30,6 +30,9 @@ class ModApiClient : public ModApiBase
// 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);

Expand Down

0 comments on commit b1f2a69

Please sign in to comment.