mirror of https://github.com/bjornbytes/lovr.git
Improve error messages in the filesystem loader;
Also fix a memory leak in lovr.filesystem.load.
This commit is contained in:
parent
aa4ceafbf2
commit
de320be1cd
|
@ -31,6 +31,8 @@ static int pushDirectoryItem(void* userdata, const char* path, const char* filen
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int l_lovrFilesystemLoad(lua_State* L);
|
||||||
|
|
||||||
// Loader to help Lua's require understand PhysFS.
|
// Loader to help Lua's require understand PhysFS.
|
||||||
static int filesystemLoader(lua_State* L) {
|
static int filesystemLoader(lua_State* L) {
|
||||||
const char* module = luaL_checkstring(L, -1);
|
const char* module = luaL_checkstring(L, -1);
|
||||||
|
@ -58,20 +60,9 @@ static int filesystemLoader(lua_State* L) {
|
||||||
strncat(filename, requirePath[i] + index + 1, strlen(requirePath[i]) - index);
|
strncat(filename, requirePath[i] + index + 1, strlen(requirePath[i]) - index);
|
||||||
|
|
||||||
if (lovrFilesystemIsFile(filename)) {
|
if (lovrFilesystemIsFile(filename)) {
|
||||||
size_t size;
|
lua_pop(L, 1);
|
||||||
void* data = lovrFilesystemRead(filename, &size);
|
lua_pushstring(L, filename);
|
||||||
char identifier[LOVR_PATH_MAX + 1];
|
return l_lovrFilesystemLoad(L);
|
||||||
strncpy(identifier, "@", 2);
|
|
||||||
strncat(identifier, filename, LOVR_PATH_MAX);
|
|
||||||
|
|
||||||
if (data) {
|
|
||||||
if (!luaL_loadbuffer(L, data, size, identifier)) {
|
|
||||||
free(data);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
free(data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -237,7 +228,12 @@ int l_lovrFilesystemLoad(lua_State* L) {
|
||||||
size_t size;
|
size_t size;
|
||||||
char* content = lovrFilesystemRead(path, &size);
|
char* content = lovrFilesystemRead(path, &size);
|
||||||
|
|
||||||
|
if (!content) {
|
||||||
|
return luaL_error(L, "Could not read file '%s'", path);
|
||||||
|
}
|
||||||
|
|
||||||
int status = luaL_loadbuffer(L, content, size, path);
|
int status = luaL_loadbuffer(L, content, size, path);
|
||||||
|
free(content);
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case LUA_ERRMEM: return luaL_error(L, "Memory allocation error: %s", lua_tostring(L, -1));
|
case LUA_ERRMEM: return luaL_error(L, "Memory allocation error: %s", lua_tostring(L, -1));
|
||||||
case LUA_ERRSYNTAX: return luaL_error(L, "Syntax error: %s", lua_tostring(L, -1));
|
case LUA_ERRSYNTAX: return luaL_error(L, "Syntax error: %s", lua_tostring(L, -1));
|
||||||
|
|
Loading…
Reference in New Issue