Skip to content

Commit b1f2a69

Browse files
committedNov 9, 2019
Introduce get_modpath() for CSM
1 parent 82a2e02 commit b1f2a69

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed
 

‎clientmods/preview/example.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
print("Loaded example file!, loading more examples")
2-
dofile("preview:examples/first.lua")
2+
dofile(core.get_modpath(core.get_current_modname()) .. "examples/first.lua")

‎clientmods/preview/init.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
local modname = core.get_current_modname() or "??"
1+
local modname = assert(core.get_current_modname())
22
local modstorage = core.get_mod_storage()
33
local mod_channel
44

5-
dofile("preview:example.lua")
6-
-- This is an example function to ensure it's working properly, should be removed before merge
5+
dofile(core.get_modpath(modname) .. "example.lua")
6+
77
core.register_on_shutdown(function()
88
print("[PREVIEW] shutdown client")
99
end)

‎doc/client_lua_api.txt

+4
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,10 @@ Minetest namespace reference
631631
### Utilities
632632

633633
* `minetest.get_current_modname()`: returns the currently loading mod's name, when we are loading a mod
634+
* `minetest.get_modpath(modname)`: returns virtual path of given mod including
635+
the trailing separator. This is useful to load additional Lua files
636+
contained in your mod:
637+
e.g. `dofile(minetest.get_modpath(minetest.get_current_modname()) .. "stuff.lua")`
634638
* `minetest.get_language()`: returns the currently set gettext language.
635639
* `minetest.get_version()`: returns a table containing components of the
636640
engine version. Components:

‎src/script/lua_api/l_client.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,23 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3636
#include "util/string.h"
3737
#include "nodedef.h"
3838

39+
// get_current_modname()
3940
int ModApiClient::l_get_current_modname(lua_State *L)
4041
{
4142
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
4243
return 1;
4344
}
4445

46+
// get_modpath(modname)
47+
int ModApiClient::l_get_modpath(lua_State *L)
48+
{
49+
std::string modname = readParam<std::string>(L, 1);
50+
// Client mods use a virtual filesystem, see Client::scanModSubfolder()
51+
std::string path = modname + ":";
52+
lua_pushstring(L, path.c_str());
53+
return 1;
54+
}
55+
4556
// get_last_run_mod()
4657
int ModApiClient::l_get_last_run_mod(lua_State *L)
4758
{
@@ -365,6 +376,7 @@ int ModApiClient::l_get_builtin_path(lua_State *L)
365376
void ModApiClient::Initialize(lua_State *L, int top)
366377
{
367378
API_FCT(get_current_modname);
379+
API_FCT(get_modpath);
368380
API_FCT(print);
369381
API_FCT(display_chat_message);
370382
API_FCT(send_chat_message);

‎src/script/lua_api/l_client.h

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class ModApiClient : public ModApiBase
3030
// get_current_modname()
3131
static int l_get_current_modname(lua_State *L);
3232

33+
// get_modpath(modname)
34+
static int l_get_modpath(lua_State *L);
35+
3336
// print(text)
3437
static int l_print(lua_State *L);
3538

0 commit comments

Comments
 (0)
Please sign in to comment.