Improve error handling in LuaModuleLoader and don't keep trying to load modules after one has been found

Originally committed to SVN as r5662.
This commit is contained in:
Thomas Goyne 2011-09-28 19:52:02 +00:00
parent d3ea9236cf
commit 1e13670c70

View file

@ -53,6 +53,7 @@
#include <wx/window.h> #include <wx/window.h>
#endif #endif
#include <libaegisub/access.h>
#include <libaegisub/log.h> #include <libaegisub/log.h>
#include <libaegisub/scoped_ptr.h> #include <libaegisub/scoped_ptr.h>
@ -379,6 +380,7 @@ namespace Automation4 {
wxString module(get_wxstring(L, -1)); wxString module(get_wxstring(L, -1));
module.Replace(".", LUA_DIRSEP); module.Replace(".", LUA_DIRSEP);
// Get the lua package include path (which the user may have modified)
lua_getglobal(L, "package"); lua_getglobal(L, "package");
lua_pushstring(L, "path"); lua_pushstring(L, "path");
lua_gettable(L, -2); lua_gettable(L, -2);
@ -389,13 +391,24 @@ namespace Automation4 {
while (toker.HasMoreTokens()) { while (toker.HasMoreTokens()) {
wxString filename = toker.GetNextToken(); wxString filename = toker.GetNextToken();
filename.Replace("?", module); filename.Replace("?", module);
if (wxFileName::FileExists(filename)) { try {
LuaScriptReader script_reader(filename); LuaScriptReader script_reader(filename);
if (lua_load(L, script_reader.reader_func, &script_reader, filename.utf8_str())) { if (lua_load(L, script_reader.reader_func, &script_reader, filename.utf8_str())) {
return luaL_error(L, "Error loading Lua module \"%s\":\n\n%s", filename.utf8_str().data(), lua_tostring(L, -1)); return luaL_error(L, "Error loading Lua module \"%s\":\n\n%s", filename.utf8_str().data(), lua_tostring(L, -1));
} }
break;
}
catch (agi::acs::AcsNotFound const&) {
// Not an error so swallow and continue on
}
catch (agi::acs::AcsNotAFile const&) {
// Not an error so swallow and continue on
}
catch (agi::Exception const& e) {
return luaL_error(L, "Error loading Lua module \"%s\":\n\n%s", filename.utf8_str().data(), e.GetChainedMessage().c_str());
} }
} }
return lua_gettop(L) - pretop; return lua_gettop(L) - pretop;
} }