Skip to content

Commit 0df7361

Browse files
committedApr 8, 2015
Schematics: Prepend mod path to relative schematic filepaths
1 parent 5132908 commit 0df7361

File tree

6 files changed

+92
-48
lines changed

6 files changed

+92
-48
lines changed
 

Diff for: ‎doc/lua_api.txt

+1
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ the global `minetest.registered_*` tables.
439439
* added to `minetest.registered_schematic` with the key of `schematic.name`
440440
* if `schematic.name` is nil, the key is the returned ID
441441
* if the schematic is loaded from a file, schematic.name is set to the filename
442+
* the current mod path will be prepended to the schematic filename if it is a relative path
442443

443444
* `minetest.clear_registered_biomes()`
444445
* clears all biomes currently registered

Diff for: ‎src/filesys.cpp

+35-24
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ namespace fs
3434

3535
#define _WIN32_WINNT 0x0501
3636
#include <windows.h>
37+
#include <shlwapi.h>
3738

38-
std::vector<DirListNode> GetDirListing(std::string pathstring)
39+
std::vector<DirListNode> GetDirListing(const std::string &pathstring)
3940
{
4041
std::vector<DirListNode> listing;
4142

@@ -44,7 +45,7 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
4445
DWORD dwError;
4546

4647
std::string dirSpec = pathstring + "\\*";
47-
48+
4849
// Find the first file in the directory.
4950
hFind = FindFirstFile(dirSpec.c_str(), &FindFileData);
5051

@@ -86,7 +87,7 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
8687
return listing;
8788
}
8889

89-
bool CreateDir(std::string path)
90+
bool CreateDir(const std::string &path)
9091
{
9192
bool r = CreateDirectory(path.c_str(), NULL);
9293
if(r == true)
@@ -96,12 +97,17 @@ bool CreateDir(std::string path)
9697
return false;
9798
}
9899

99-
bool PathExists(std::string path)
100+
bool PathExists(const std::string &path)
100101
{
101102
return (GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES);
102103
}
103104

104-
bool IsDir(std::string path)
105+
bool IsPathAbsolute(const std::string &path)
106+
{
107+
return !PathIsRelative(path.c_str());
108+
}
109+
110+
bool IsDir(const std::string &path)
105111
{
106112
DWORD attr = GetFileAttributes(path.c_str());
107113
return (attr != INVALID_FILE_ATTRIBUTES &&
@@ -113,7 +119,7 @@ bool IsDirDelimiter(char c)
113119
return c == '/' || c == '\\';
114120
}
115121

116-
bool RecursiveDelete(std::string path)
122+
bool RecursiveDelete(const std::string &path)
117123
{
118124
infostream<<"Recursively deleting \""<<path<<"\""<<std::endl;
119125

@@ -158,7 +164,7 @@ bool RecursiveDelete(std::string path)
158164
return true;
159165
}
160166

161-
bool DeleteSingleFileOrEmptyDirectory(std::string path)
167+
bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
162168
{
163169
DWORD attr = GetFileAttributes(path.c_str());
164170
bool is_directory = (attr != INVALID_FILE_ATTRIBUTES &&
@@ -199,7 +205,7 @@ std::string TempPath()
199205
#include <sys/wait.h>
200206
#include <unistd.h>
201207

202-
std::vector<DirListNode> GetDirListing(std::string pathstring)
208+
std::vector<DirListNode> GetDirListing(const std::string &pathstring)
203209
{
204210
std::vector<DirListNode> listing;
205211

@@ -252,7 +258,7 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
252258
return listing;
253259
}
254260

255-
bool CreateDir(std::string path)
261+
bool CreateDir(const std::string &path)
256262
{
257263
int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
258264
if(r == 0)
@@ -268,13 +274,18 @@ bool CreateDir(std::string path)
268274
}
269275
}
270276

271-
bool PathExists(std::string path)
277+
bool PathExists(const std::string &path)
272278
{
273279
struct stat st;
274280
return (stat(path.c_str(),&st) == 0);
275281
}
276282

277-
bool IsDir(std::string path)
283+
bool IsPathAbsolute(const std::string &path)
284+
{
285+
return path[0] == '/';
286+
}
287+
288+
bool IsDir(const std::string &path)
278289
{
279290
struct stat statbuf;
280291
if(stat(path.c_str(), &statbuf))
@@ -287,16 +298,16 @@ bool IsDirDelimiter(char c)
287298
return c == '/';
288299
}
289300

290-
bool RecursiveDelete(std::string path)
301+
bool RecursiveDelete(const std::string &path)
291302
{
292303
/*
293304
Execute the 'rm' command directly, by fork() and execve()
294305
*/
295-
306+
296307
infostream<<"Removing \""<<path<<"\""<<std::endl;
297308

298309
//return false;
299-
310+
300311
pid_t child_pid = fork();
301312

302313
if(child_pid == 0)
@@ -314,9 +325,9 @@ bool RecursiveDelete(std::string path)
314325

315326
verbosestream<<"Executing '"<<argv[0]<<"' '"<<argv[1]<<"' '"
316327
<<argv[2]<<"'"<<std::endl;
317-
328+
318329
execv(argv[0], argv);
319-
330+
320331
// Execv shouldn't return. Failed.
321332
_exit(1);
322333
}
@@ -333,7 +344,7 @@ bool RecursiveDelete(std::string path)
333344
}
334345
}
335346

336-
bool DeleteSingleFileOrEmptyDirectory(std::string path)
347+
bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
337348
{
338349
if(IsDir(path)){
339350
bool did = (rmdir(path.c_str()) == 0);
@@ -370,7 +381,7 @@ std::string TempPath()
370381

371382
#endif
372383

373-
void GetRecursiveSubPaths(std::string path, std::vector<std::string> &dst)
384+
void GetRecursiveSubPaths(const std::string &path, std::vector<std::string> &dst)
374385
{
375386
std::vector<DirListNode> content = GetDirListing(path);
376387
for(unsigned int i=0; i<content.size(); i++){
@@ -398,7 +409,7 @@ bool DeletePaths(const std::vector<std::string> &paths)
398409
return success;
399410
}
400411

401-
bool RecursiveDeleteContent(std::string path)
412+
bool RecursiveDeleteContent(const std::string &path)
402413
{
403414
infostream<<"Removing content of \""<<path<<"\""<<std::endl;
404415
std::vector<DirListNode> list = GetDirListing(path);
@@ -417,7 +428,7 @@ bool RecursiveDeleteContent(std::string path)
417428
return true;
418429
}
419430

420-
bool CreateAllDirs(std::string path)
431+
bool CreateAllDirs(const std::string &path)
421432
{
422433

423434
std::vector<std::string> tocreate;
@@ -435,7 +446,7 @@ bool CreateAllDirs(std::string path)
435446
return true;
436447
}
437448

438-
bool CopyFileContents(std::string source, std::string target)
449+
bool CopyFileContents(const std::string &source, const std::string &target)
439450
{
440451
FILE *sourcefile = fopen(source.c_str(), "rb");
441452
if(sourcefile == NULL){
@@ -489,7 +500,7 @@ bool CopyFileContents(std::string source, std::string target)
489500
return retval;
490501
}
491502

492-
bool CopyDir(std::string source, std::string target)
503+
bool CopyDir(const std::string &source, const std::string &target)
493504
{
494505
if(PathExists(source)){
495506
if(!PathExists(target)){
@@ -519,7 +530,7 @@ bool CopyDir(std::string source, std::string target)
519530
}
520531
}
521532

522-
bool PathStartsWith(std::string path, std::string prefix)
533+
bool PathStartsWith(const std::string &path, const std::string &prefix)
523534
{
524535
size_t pathsize = path.size();
525536
size_t pathpos = 0;
@@ -569,7 +580,7 @@ bool PathStartsWith(std::string path, std::string prefix)
569580
}
570581
}
571582

572-
std::string RemoveLastPathComponent(std::string path,
583+
std::string RemoveLastPathComponent(const std::string &path,
573584
std::string *removed, int count)
574585
{
575586
if(removed)

Diff for: ‎src/filesys.h

+16-13
Original file line numberDiff line numberDiff line change
@@ -40,57 +40,60 @@ struct DirListNode
4040
std::string name;
4141
bool dir;
4242
};
43-
std::vector<DirListNode> GetDirListing(std::string path);
43+
44+
std::vector<DirListNode> GetDirListing(const std::string &path);
4445

4546
// Returns true if already exists
46-
bool CreateDir(std::string path);
47+
bool CreateDir(const std::string &path);
48+
49+
bool PathExists(const std::string &path);
4750

48-
bool PathExists(std::string path);
51+
bool IsPathAbsolute(const std::string &path);
4952

50-
bool IsDir(std::string path);
53+
bool IsDir(const std::string &path);
5154

5255
bool IsDirDelimiter(char c);
5356

5457
// Only pass full paths to this one. True on success.
5558
// NOTE: The WIN32 version returns always true.
56-
bool RecursiveDelete(std::string path);
59+
bool RecursiveDelete(const std::string &path);
5760

58-
bool DeleteSingleFileOrEmptyDirectory(std::string path);
61+
bool DeleteSingleFileOrEmptyDirectory(const std::string &path);
5962

6063
// Returns path to temp directory, can return "" on error
6164
std::string TempPath();
6265

6366
/* Multiplatform */
6467

6568
// The path itself not included
66-
void GetRecursiveSubPaths(std::string path, std::vector<std::string> &dst);
69+
void GetRecursiveSubPaths(const std::string &path, std::vector<std::string> &dst);
6770

6871
// Tries to delete all, returns false if any failed
6972
bool DeletePaths(const std::vector<std::string> &paths);
7073

7174
// Only pass full paths to this one. True on success.
72-
bool RecursiveDeleteContent(std::string path);
75+
bool RecursiveDeleteContent(const std::string &path);
7376

7477
// Create all directories on the given path that don't already exist.
75-
bool CreateAllDirs(std::string path);
78+
bool CreateAllDirs(const std::string &path);
7679

7780
// Copy a regular file
78-
bool CopyFileContents(std::string source, std::string target);
81+
bool CopyFileContents(const std::string &source, const std::string &target);
7982

8083
// Copy directory and all subdirectories
8184
// Omits files and subdirectories that start with a period
82-
bool CopyDir(std::string source, std::string target);
85+
bool CopyDir(const std::string &source, const std::string &target);
8386

8487
// Check if one path is prefix of another
8588
// For example, "/tmp" is a prefix of "/tmp" and "/tmp/file" but not "/tmp2"
8689
// Ignores case differences and '/' vs. '\\' on Windows
87-
bool PathStartsWith(std::string path, std::string prefix);
90+
bool PathStartsWith(const std::string &path, const std::string &prefix);
8891

8992
// Remove last path component and the dir delimiter before and/or after it,
9093
// returns "" if there is only one path component.
9194
// removed: If non-NULL, receives the removed component(s).
9295
// count: Number of components to remove
93-
std::string RemoveLastPathComponent(std::string path,
96+
std::string RemoveLastPathComponent(const std::string &path,
9497
std::string *removed = NULL, int count = 1);
9598

9699
// Remove "." and ".." path components and for every ".." removed, remove

Diff for: ‎src/script/lua_api/l_base.cpp

+31-9
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,54 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2020
#include "lua_api/l_base.h"
2121
#include "lua_api/l_internal.h"
2222
#include "cpp_api/s_base.h"
23+
#include <mods.h>
24+
#include <server.h>
2325

24-
ScriptApiBase* ModApiBase::getScriptApiBase(lua_State *L) {
26+
ScriptApiBase *ModApiBase::getScriptApiBase(lua_State *L)
27+
{
2528
// Get server from registry
2629
lua_getfield(L, LUA_REGISTRYINDEX, "scriptapi");
2730
ScriptApiBase *sapi_ptr = (ScriptApiBase*) lua_touserdata(L, -1);
2831
lua_pop(L, 1);
2932
return sapi_ptr;
3033
}
3134

32-
Server* ModApiBase::getServer(lua_State *L) {
35+
Server *ModApiBase::getServer(lua_State *L)
36+
{
3337
return getScriptApiBase(L)->getServer();
3438
}
3539

36-
Environment* ModApiBase::getEnv(lua_State *L) {
40+
Environment *ModApiBase::getEnv(lua_State *L)
41+
{
3742
return getScriptApiBase(L)->getEnv();
3843
}
3944

40-
GUIEngine* ModApiBase::getGuiEngine(lua_State *L) {
45+
GUIEngine *ModApiBase::getGuiEngine(lua_State *L)
46+
{
4147
return getScriptApiBase(L)->getGuiEngine();
4248
}
4349

44-
bool ModApiBase::registerFunction(lua_State *L,
45-
const char *name,
46-
lua_CFunction fct,
47-
int top
48-
) {
50+
std::string ModApiBase::getCurrentModPath(lua_State *L)
51+
{
52+
lua_getfield(L, LUA_REGISTRYINDEX, "current_modname");
53+
const char *current_modname = lua_tostring(L, -1);
54+
if (!current_modname)
55+
return ".";
56+
57+
const ModSpec *mod = getServer(L)->getModSpec(current_modname);
58+
if (!mod)
59+
return ".";
60+
61+
return mod->path;
62+
}
63+
64+
65+
bool ModApiBase::registerFunction(
66+
lua_State *L,
67+
const char *name,
68+
lua_CFunction fct,
69+
int top)
70+
{
4971
//TODO check presence first!
5072

5173
lua_pushstring(L,name);

Diff for: ‎src/script/lua_api/l_base.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ class GUIEngine;
3535

3636
class ModApiBase {
3737

38-
protected:
38+
public:
3939
static ScriptApiBase* getScriptApiBase(lua_State *L);
4040
static Server* getServer(lua_State *L);
4141
static Environment* getEnv(lua_State *L);
4242
static GUIEngine* getGuiEngine(lua_State *L);
43+
static std::string getCurrentModPath(lua_State *L);
4344

4445
// Get an arbitrary subclass of ScriptApiBase
4546
// by using dynamic_cast<> on getScriptApiBase()

Diff for: ‎src/script/lua_api/l_mapgen.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3232
#include "mg_schematic.h"
3333
#include "mapgen_v5.h"
3434
#include "mapgen_v7.h"
35+
#include "filesys.h"
3536
#include "settings.h"
3637
#include "log.h"
3738

@@ -142,7 +143,12 @@ Schematic *load_schematic(lua_State *L, int index,
142143
return NULL;
143144
} else if (lua_isstring(L, index)) {
144145
schem = SchematicManager::create(SCHEMATIC_NORMAL);
145-
if (!schem->loadSchematicFromFile(lua_tostring(L, index),
146+
147+
std::string filepath = lua_tostring(L, index);
148+
if (!fs::IsPathAbsolute(filepath))
149+
filepath = ModApiBase::getCurrentModPath(L) + DIR_DELIM + filepath;
150+
151+
if (!schem->loadSchematicFromFile(filepath.c_str(),
146152
schemmgr->getNodeDef(), replace_names)) {
147153
delete schem;
148154
return NULL;

0 commit comments

Comments
 (0)
Please sign in to comment.