summaryrefslogtreecommitdiff
path: root/src/script
diff options
context:
space:
mode:
authorkwolekr <kwolekr@minetest.net>2015-04-07 23:04:48 -0400
committerkwolekr <kwolekr@minetest.net>2015-04-08 00:28:56 -0400
commit0df736173e60df06a7a7162c285b9c5731a07c20 (patch)
tree4ed5c22380dd70213b820544b1e75ae8ca1ad573 /src/script
parent5132908f4b834c344d19dcb20939cda175fc82a4 (diff)
downloadminetest-0df736173e60df06a7a7162c285b9c5731a07c20.tar.gz
minetest-0df736173e60df06a7a7162c285b9c5731a07c20.tar.bz2
minetest-0df736173e60df06a7a7162c285b9c5731a07c20.zip
Schematics: Prepend mod path to relative schematic filepaths
Diffstat (limited to 'src/script')
-rw-r--r--src/script/lua_api/l_base.cpp40
-rw-r--r--src/script/lua_api/l_base.h3
-rw-r--r--src/script/lua_api/l_mapgen.cpp8
3 files changed, 40 insertions, 11 deletions
diff --git a/src/script/lua_api/l_base.cpp b/src/script/lua_api/l_base.cpp
index b8d673ee4..136fcc507 100644
--- a/src/script/lua_api/l_base.cpp
+++ b/src/script/lua_api/l_base.cpp
@@ -20,8 +20,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "lua_api/l_base.h"
#include "lua_api/l_internal.h"
#include "cpp_api/s_base.h"
+#include <mods.h>
+#include <server.h>
-ScriptApiBase* ModApiBase::getScriptApiBase(lua_State *L) {
+ScriptApiBase *ModApiBase::getScriptApiBase(lua_State *L)
+{
// Get server from registry
lua_getfield(L, LUA_REGISTRYINDEX, "scriptapi");
ScriptApiBase *sapi_ptr = (ScriptApiBase*) lua_touserdata(L, -1);
@@ -29,23 +32,42 @@ ScriptApiBase* ModApiBase::getScriptApiBase(lua_State *L) {
return sapi_ptr;
}
-Server* ModApiBase::getServer(lua_State *L) {
+Server *ModApiBase::getServer(lua_State *L)
+{
return getScriptApiBase(L)->getServer();
}
-Environment* ModApiBase::getEnv(lua_State *L) {
+Environment *ModApiBase::getEnv(lua_State *L)
+{
return getScriptApiBase(L)->getEnv();
}
-GUIEngine* ModApiBase::getGuiEngine(lua_State *L) {
+GUIEngine *ModApiBase::getGuiEngine(lua_State *L)
+{
return getScriptApiBase(L)->getGuiEngine();
}
-bool ModApiBase::registerFunction(lua_State *L,
- const char *name,
- lua_CFunction fct,
- int top
- ) {
+std::string ModApiBase::getCurrentModPath(lua_State *L)
+{
+ lua_getfield(L, LUA_REGISTRYINDEX, "current_modname");
+ const char *current_modname = lua_tostring(L, -1);
+ if (!current_modname)
+ return ".";
+
+ const ModSpec *mod = getServer(L)->getModSpec(current_modname);
+ if (!mod)
+ return ".";
+
+ return mod->path;
+}
+
+
+bool ModApiBase::registerFunction(
+ lua_State *L,
+ const char *name,
+ lua_CFunction fct,
+ int top)
+{
//TODO check presence first!
lua_pushstring(L,name);
diff --git a/src/script/lua_api/l_base.h b/src/script/lua_api/l_base.h
index debbcd09b..c580e9597 100644
--- a/src/script/lua_api/l_base.h
+++ b/src/script/lua_api/l_base.h
@@ -35,11 +35,12 @@ class GUIEngine;
class ModApiBase {
-protected:
+public:
static ScriptApiBase* getScriptApiBase(lua_State *L);
static Server* getServer(lua_State *L);
static Environment* getEnv(lua_State *L);
static GUIEngine* getGuiEngine(lua_State *L);
+ static std::string getCurrentModPath(lua_State *L);
// Get an arbitrary subclass of ScriptApiBase
// by using dynamic_cast<> on getScriptApiBase()
diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp
index cbe3610c8..d08cfea8a 100644
--- a/src/script/lua_api/l_mapgen.cpp
+++ b/src/script/lua_api/l_mapgen.cpp
@@ -32,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mg_schematic.h"
#include "mapgen_v5.h"
#include "mapgen_v7.h"
+#include "filesys.h"
#include "settings.h"
#include "log.h"
@@ -142,7 +143,12 @@ Schematic *load_schematic(lua_State *L, int index,
return NULL;
} else if (lua_isstring(L, index)) {
schem = SchematicManager::create(SCHEMATIC_NORMAL);
- if (!schem->loadSchematicFromFile(lua_tostring(L, index),
+
+ std::string filepath = lua_tostring(L, index);
+ if (!fs::IsPathAbsolute(filepath))
+ filepath = ModApiBase::getCurrentModPath(L) + DIR_DELIM + filepath;
+
+ if (!schem->loadSchematicFromFile(filepath.c_str(),
schemmgr->getNodeDef(), replace_names)) {
delete schem;
return NULL;