diff --git a/src/modules/filesystem/wrap_Filesystem.cpp b/src/modules/filesystem/wrap_Filesystem.cpp index c1a40c70f..22fd3fb14 100644 --- a/src/modules/filesystem/wrap_Filesystem.cpp +++ b/src/modules/filesystem/wrap_Filesystem.cpp @@ -28,6 +28,11 @@ // SDL #include +// STL +#include +#include +#include + namespace love { namespace filesystem @@ -505,58 +510,69 @@ int w_isSymlink(lua_State *L) return 1; } +static std::vector requirePath = {"?.lua", "?/init.lua"}; + +int w_getRequirePath(lua_State *L) +{ + std::stringstream path; + bool seperator = false; + for (auto &element : requirePath) + { + if (seperator) + path << ";"; + else + seperator = true; + + path << element; + } + + luax_pushstring(L, path.str()); + return 1; +} + +int w_setRequirePath(lua_State *L) +{ + std::string element = luax_checkstring(L, 1); + + requirePath.clear(); + std::stringstream path; + path << element; + + while(std::getline(path, element, ';')) + requirePath.push_back(element); + + return 0; +} + int loader(lua_State *L) { - const char *filename = lua_tostring(L, -1); + std::string modulename = luax_tostring(L, 1); - std::string tmp(filename); - tmp += ".lua"; - - int size = tmp.size(); - - for (int i=0; iisFile(tmp.c_str())) + auto *inst = instance(); + for (std::string element : requirePath) { - lua_pop(L, 1); - lua_pushstring(L, tmp.c_str()); - // Ok, load it. - return w_load(L); - } + size_t pos = element.find('?'); + if (pos == std::string::npos) + continue; - tmp = filename; - size = tmp.size(); - for (int i=0; iisDirectory(tmp.c_str())) - { - tmp += "/init.lua"; - if (instance()->isFile(tmp.c_str())) + element.replace(pos, 1, modulename); + if (inst->isFile(element.c_str())) { lua_pop(L, 1); - lua_pushstring(L, tmp.c_str()); - // Ok, load it. + lua_pushstring(L, element.c_str()); return w_load(L); } } - std::string errstr = "\n\tno file '%s' in LOVE game directories."; - errstr += errstr; + std::string errstr = "\n\tno '%s' in LOVE game directories."; - lua_pushfstring(L, errstr.c_str(), (tmp + ".lua").c_str(), (tmp + "/init.lua").c_str()); + lua_pushfstring(L, errstr.c_str(), modulename.c_str()); return 1; } @@ -667,6 +683,8 @@ static const luaL_Reg functions[] = { "areSymlinksEnabled", w_areSymlinksEnabled }, { "isSymlink", w_isSymlink }, { "newFileData", w_newFileData }, + { "getRequirePath", w_getRequirePath }, + { "setRequirePath", w_setRequirePath }, { 0, 0 } };