1
0
Fork 0
mirror of https://github.com/ctruLua/ctruLua.git synced 2025-10-27 16:39:29 +00:00

Added fs lib; fs.list working. Added a simple shell.

This commit is contained in:
Reuh 2015-08-20 21:12:15 +02:00
parent 551e8e121e
commit 85d8f60f94
5 changed files with 211 additions and 65 deletions

View file

@ -10,6 +10,7 @@ int load_news_lib(lua_State *L);
int load_ptm_lib(lua_State *L);
int load_hid_lib(lua_State *L);
int load_ir_lib(lua_State *L);
int load_fs_lib(lua_State *L);
static int ctr_run(lua_State *L) {
lua_pushboolean(L, aptMainLoop());
@ -37,6 +38,7 @@ struct { char *name; int (*load)(lua_State *L); } ctr_libs[] = {
{ "ptm", load_ptm_lib },
{ "hid", load_hid_lib },
{ "ir", load_ir_lib },
{ "fs", load_fs_lib },
{ NULL, NULL }
};

92
source/fs.c Normal file
View file

@ -0,0 +1,92 @@
#include <3ds/types.h>
#include <3ds/util/utf.h>
#include <3ds/services/fs.h>
#include <lua.h>
#include <lauxlib.h>
Handle *fsuHandle;
FS_archive sdmcArchive;
int fs_list(lua_State *L) {
const char *path = luaL_checkstring(L, 1);
lua_newtable(L);
int i = 1; // table index
FS_path dirPath = FS_makePath(PATH_CHAR, path);
Handle dirHandle;
FSUSER_OpenDirectory(fsuHandle, &dirHandle, sdmcArchive, dirPath);
u32 entriesRead = 0;
do {
FS_dirent buffer;
FSDIR_Read(dirHandle, &entriesRead, 1, &buffer);
if (!entriesRead) break;
uint8_t name[256]; // utf8 file name
size_t size = utf16_to_utf8(name, buffer.name, 0x106);
*(name+size) = 0x0; // mark text end
lua_createtable(L, 0, 8);
lua_pushstring(L, (const char *)name);
lua_setfield(L, -2, "name");
lua_pushstring(L, (const char *)buffer.shortName);
lua_setfield(L, -2, "shortName");
lua_pushstring(L, (const char *)buffer.shortExt);
lua_setfield(L, -2, "shortExt");
lua_pushboolean(L, buffer.isDirectory);
lua_setfield(L, -2, "isDirectory");
lua_pushboolean(L, buffer.isHidden);
lua_setfield(L, -2, "isHidden");
lua_pushboolean(L, buffer.isArchive);
lua_setfield(L, -2, "isArchive");
lua_pushboolean(L, buffer.isReadOnly);
lua_setfield(L, -2, "isReadOnly");
lua_pushinteger(L, buffer.fileSize);
lua_setfield(L, -2, "fileSize");
lua_seti(L, -2, i);
i++;
} while (entriesRead > 0);
FSDIR_Close(dirHandle);
return 1;
}
static const struct luaL_Reg fs_lib[] = {
{ "list", fs_list },
{ NULL, NULL }
};
int luaopen_fs_lib(lua_State *L) {
luaL_newlib(L, fs_lib);
return 1;
}
int load_fs_lib(lua_State *L) {
fsInit();
fsuHandle = fsGetSessionHandle();
FSUSER_Initialize(fsuHandle);
sdmcArchive = (FS_archive){ARCH_SDMC, FS_makePath(PATH_EMPTY, "")};
FSUSER_OpenArchive(fsuHandle, &sdmcArchive);
luaL_requiref(L, "ctr.fs", luaopen_fs_lib, false);
return 0;
}
void unload_fs_lib(lua_State *L) {
FSUSER_CloseArchive(fsuHandle, &sdmcArchive);
fsExit();
}

View file

@ -10,6 +10,7 @@
int load_ctr_lib(lua_State *L);
void unload_font_lib();
void unload_fs_lib();
bool gfxinit = false;
@ -64,8 +65,9 @@ int main() {
// Unload Lua
lua_close(L);
// Unload current font
// Unload libs
unload_font_lib();
unload_fs_lib();
// Disable accel/gyro
HIDUSER_DisableAccelerometer();