Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make error message in require more descriptive for custom LOVE library loader. #1865

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/modules/filesystem/wrap_Filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,13 +738,16 @@ static void replaceAll(std::string &str, const std::string &substr, const std::s
int loader(lua_State *L)
{
std::string modulename = luax_checkstring(L, 1);
std::stringstream errstr;

for (char &c : modulename)
{
if (c == '.')
c = '/';
}

errstr << "\n\tno '" << modulename << "' in LOVE game directories:";

auto *inst = instance();
for (std::string element : inst->getRequirePath())
{
Expand All @@ -757,11 +760,11 @@ int loader(lua_State *L)
lua_pushstring(L, element.c_str());
return w_load(L);
}
}

std::string errstr = "\n\tno '%s' in LOVE game directories.";
errstr << "\n\t tried '" << element << "'";
}

lua_pushfstring(L, errstr.c_str(), modulename.c_str());
luax_pushstring(L, errstr.str());
return 1;
}

Expand All @@ -781,6 +784,7 @@ int extloader(lua_State *L)
std::string filename = luax_checkstring(L, 1);
std::string tokenized_name(filename);
std::string tokenized_function(filename);
std::stringstream errstr;

// We need both the tokenized filename (dots replaced with slashes)
// and the tokenized function name (dots replaced with underscores)
Expand All @@ -794,6 +798,8 @@ int extloader(lua_State *L)
}
}

errstr << "\n\tno file '" << tokenized_name << "' in LOVE paths:";

void *handle = nullptr;
auto *inst = instance();

Expand Down Expand Up @@ -828,7 +834,10 @@ int extloader(lua_State *L)

Filesystem::Info info = {};
if (!inst->getInfo(element.c_str(), info) || info.type == Filesystem::FILETYPE_DIRECTORY)
{
errstr << "\n\t tried '" << element << "' in save directory";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this isn't guaranteed to only look (or work) in the save directory right now, especially if love 12's new mount APIs have been used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be a good replacement text for those?

Copy link
Member

@slime73 slime73 Nov 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it could be something like this for the Lua loader:

No Lua code 'path/to/module' in LOVE filesystem paths:
  tried 'path/to/module.lua'

And for the C module loader:

No C library 'path/to/module' in LOVE filesystem paths:
  tried 'path/to/module.dll'

Does that make sense? I haven't thought about this too hard. Maybe the language could still be tweaked a bit...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the C module paths, I was thinking of resolving the fullpath then printing the absolute path, but then I realize PHYSFS_getRealDir only works when the file is there (which most likely succeeded when this codepath is reached, defeating the purpose).

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;
Expand All @@ -837,6 +846,8 @@ int extloader(lua_State *L)
// Can fail, for instance if it turned out the source was a zip
if (handle)
break;

errstr << "\n\t tried '" << filepath << "'";
}

if (handle)
Expand All @@ -849,10 +860,12 @@ int extloader(lua_State *L)

if (!handle)
{
lua_pushfstring(L, "\n\tno file '%s' in LOVE paths.", tokenized_name.c_str());
luax_pushstring(L, errstr.str());
return 1;
}

errstr.clear();

// We look for both loveopen_ and luaopen_, so libraries with specific love support
// can tell when they've been loaded by love.
void *func = SDL_LoadFunction(handle, ("loveopen_" + tokenized_function).c_str());
Expand Down