Add raylua.listfiles()

This commit is contained in:
TSnake41 2020-04-11 15:27:37 +02:00
parent 3d7a4ceaf0
commit e9586bed80
4 changed files with 30 additions and 5 deletions

View File

@ -39,7 +39,7 @@
extern const char *raylua_boot_str;
void raylua_boot(lua_State *L, lua_CFunction loadfile, bool repl)
void raylua_boot(lua_State *L, lua_CFunction loadfile, lua_CFunction listfiles, bool repl)
{
lua_newtable(L);
@ -49,6 +49,12 @@ void raylua_boot(lua_State *L, lua_CFunction loadfile, bool repl)
lua_settable(L, -3);
}
if (listfiles) {
lua_pushstring(L, "listfiles");
lua_pushcfunction(L, listfiles);
lua_settable(L, -3);
}
lua_pushstring(L, "bind_entries");
lua_pushlightuserdata(L, raylua_entries);
lua_settable(L, -3);
@ -65,6 +71,6 @@ void raylua_boot(lua_State *L, lua_CFunction loadfile, bool repl)
int luaopen_raylua(lua_State *L)
{
raylua_boot(L, NULL, false);
raylua_boot(L, NULL, NULL, false);
return 0;
}

View File

@ -20,7 +20,7 @@
#include <stdbool.h>
#include <lua.h>
void raylua_boot(lua_State *L, lua_CFunction loadfile, bool repl);
void raylua_boot(lua_State *L, lua_CFunction loadfile, lua_CFunction listfiles, bool repl);
/* raylua_boot alias to allow direct loading */
int luaopen_raylua(lua_State *L);

View File

@ -67,6 +67,25 @@ int raylua_loadfile(lua_State *L)
return 1;
}
int raylua_listfiles(lua_State *L)
{
size_t count = mz_zip_reader_get_num_files(&zip_file);
char filename[1024];
lua_createtable(L, count, 0);
size_t i = 0;
while (i < count) {
mz_zip_reader_get_filename(&zip_file, i, filename, sizeof(filename));
lua_pushstring(L, filename);
lua_rawseti(L, -2, i + 1);
i++;
}
return 1;
}
unsigned char *raylua_loadFileData(const char *path, unsigned int *out_size)
{
int index = mz_zip_reader_locate_file(&zip_file, path, NULL, 0);
@ -178,7 +197,7 @@ int main(int argc, const char **argv)
#endif
} else {
/* Boot on payload. */
raylua_boot(L, raylua_loadfile, false);
raylua_boot(L, raylua_loadfile, raylua_listfiles, false);
}
lua_close(L);

View File

@ -44,7 +44,7 @@ int main(int argc, const char **argv)
lua_setglobal(L, "arg");
raylua_boot(L, NULL, true);
raylua_boot(L, NULL, NULL, true);
lua_close(L);
return 0;
}