diff --git a/src/lovr/filesystem.c b/src/lovr/filesystem.c index 1637690c..e779de14 100644 --- a/src/lovr/filesystem.c +++ b/src/lovr/filesystem.c @@ -58,6 +58,7 @@ const luaL_Reg lovrFilesystem[] = { { "getUserDirectory", l_lovrFilesystemGetUserDirectory }, { "isDirectory", l_lovrFilesystemIsDirectory }, { "isFile", l_lovrFilesystemIsFile }, + { "load", l_lovrFilesystemLoad }, { "read", l_lovrFilesystemRead }, { "setIdentity", l_lovrFilesystemSetIdentity }, { "setSource", l_lovrFilesystemSetSource }, @@ -151,6 +152,19 @@ int l_lovrFilesystemIsFile(lua_State* L) { return 1; } +int l_lovrFilesystemLoad(lua_State* L) { + const char* path = luaL_checkstring(L, 1); + int size; + char* content = lovrFilesystemRead(path, &size); + + int status = luaL_loadbuffer(L, content, size, path); + switch (status) { + 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)); + default: return 1; + } +} + int l_lovrFilesystemRead(lua_State* L) { const char* path = luaL_checkstring(L, 1); int size; diff --git a/src/lovr/filesystem.h b/src/lovr/filesystem.h index b38d7a5e..b0f418d9 100644 --- a/src/lovr/filesystem.h +++ b/src/lovr/filesystem.h @@ -12,6 +12,7 @@ int l_lovrFilesystemGetSource(lua_State* L); int l_lovrFilesystemGetUserDirectory(lua_State* L); int l_lovrFilesystemIsDirectory(lua_State* L); int l_lovrFilesystemIsFile(lua_State* L); +int l_lovrFilesystemLoad(lua_State* L); int l_lovrFilesystemRead(lua_State* L); int l_lovrFilesystemSetIdentity(lua_State* L); int l_lovrFilesystemSetSource(lua_State* L);