require now looks for both .dylib and .so extensions for C dynamic libraries in macOS, instead of just .so.

It also now replaces all occurrences of '??' and '?' in the path with the module filename and module name respectively, rather than just the first occurrence.

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2017-07-27 19:54:56 -03:00
parent 6f7fe0611b
commit 94ce558126
2 changed files with 48 additions and 27 deletions
+44 -22
View File
@@ -634,6 +634,22 @@ int w_setCRequirePath(lua_State *L)
return 0;
}
static void replaceAll(std::string &str, const std::string &substr, const std::string &replacement)
{
std::vector<size_t> locations;
size_t pos = 0;
size_t sublen = substr.length();
while ((pos = str.find(substr, pos)) != std::string::npos)
{
locations.push_back(pos);
pos += sublen;
}
for (int i = (int) locations.size() - 1; i >= 0; i--)
str.replace(locations[i], sublen, replacement);
}
int loader(lua_State *L)
{
std::string modulename = luax_tostring(L, 1);
@@ -647,9 +663,7 @@ int loader(lua_State *L)
auto *inst = instance();
for (std::string element : inst->getRequirePath())
{
size_t pos = 0;
while ((pos = element.find('?', pos)) != std::string::npos)
element.replace(pos, 1, modulename);
replaceAll(element, "?", modulename);
if (inst->isFile(element.c_str()))
{
@@ -665,14 +679,16 @@ int loader(lua_State *L)
return 1;
}
inline const char *library_extension()
static const char *library_extensions[] =
{
#ifdef LOVE_WINDOWS
return ".dll";
".dll"
#elif defined(LOVE_MACOSX) || defined(LOVE_IOS)
".dylib", ".so"
#else
return ".so";
".so"
#endif
}
};
int extloader(lua_State *L)
{
@@ -694,25 +710,31 @@ int extloader(lua_State *L)
void *handle = nullptr;
auto *inst = instance();
for (std::string element : inst->getCRequirePath())
for (const std::string &el : inst->getCRequirePath())
{
// Replace ?? with the filename and extension
size_t pos = element.find("??");
if (pos != std::string::npos)
element.replace(pos, 2, tokenized_name + library_extension());
// Or ? with just the filename
pos = element.find('?');
if (pos != std::string::npos)
element.replace(pos, 1, tokenized_name);
for (const char *ext : library_extensions)
{
std::string element = el;
if (!inst->isFile(element.c_str()))
continue;
// Replace ?? with the filename and extension
replaceAll(element, "??", tokenized_name + ext);
// Now resolve the full path, as we're bypassing physfs for the next part.
element = inst->getRealDirectory(element.c_str()) + LOVE_PATH_SEPARATOR + element;
// And ? with just the filename
replaceAll(element, "?", tokenized_name);
if (!inst->isFile(element.c_str()))
continue;
// Now resolve the full path, as we're bypassing physfs for the next part.
std::string filepath = inst->getRealDirectory(element.c_str()) + LOVE_PATH_SEPARATOR + element;
handle = SDL_LoadObject(filepath.c_str());
// Can fail, for instance if it turned out the source was a zip
if (handle)
break;
}
handle = SDL_LoadObject(element.c_str());
// Can fail, for instance if it turned out the source was a zip
if (handle)
break;
}