Skip to content

Commit

Permalink
add fs.ask_select_file and fs.ask_select_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
ihaveamac committed Dec 3, 2024
1 parent 9d9b327 commit f8738ef
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 5 deletions.
55 changes: 55 additions & 0 deletions arm9/source/lua/gm9internalfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,59 @@ static int internalfs_dir_info(lua_State* L) {
return 1;
}

static int FileDirSelector(lua_State* L, const char* path_orig, const char* prompt, bool is_dir, bool include_dirs, bool explorer) {
bool ret;
char path[_VAR_CNT_LEN] = { 0 };
char choice[_VAR_CNT_LEN] = { 0 };
strncpy(path, path_orig, _VAR_CNT_LEN);
if (strncmp(path, "Z:", 2) == 0) {
return luaL_error(L, "forbidden drive");
} else if (!is_dir) {
u32 flags_ext = include_dirs ? 0 : NO_DIRS;
char *npattern = strrchr(path, '/');
if (!npattern) {
return luaL_error(L, "invalid path");
}
*(npattern++) = '\0';
ret = FileSelector(choice, prompt, path, npattern, flags_ext, explorer);
} else {
ret = FileSelector(choice, prompt, path, NULL, NO_FILES | SELECT_DIRS, explorer);
}

if (ret) {
lua_pushstring(L, choice);
} else {
lua_pushnil(L);
}
return 1;
}

static int internalfs_ask_select_file(lua_State* L) {
bool extra = CheckLuaArgCountPlusExtra(L, 2, "_fs.ask_select_file");
const char* prompt = luaL_checkstring(L, 1);
const char* path = luaL_checkstring(L, 2);

u32 flags = 0;
if (extra) {
flags = GetFlagsFromTable(L, 3, flags, INCLUDE_DIRS | EXPLORER);
};

return FileDirSelector(L, path, prompt, false, (flags & INCLUDE_DIRS), (flags & EXPLORER));
}

static int internalfs_ask_select_dir(lua_State* L) {
bool extra = CheckLuaArgCountPlusExtra(L, 2, "_fs.ask_select_dir");
const char* prompt = luaL_checkstring(L, 1);
const char* path = luaL_checkstring(L, 2);

u32 flags = 0;
if (extra) {
flags = GetFlagsFromTable(L, 3, flags, EXPLORER);
};

return FileDirSelector(L, path, prompt, true, true, (flags & EXPLORER));
}

static int internalfs_exists(lua_State* L) {
CheckLuaArgCount(L, 1, "_fs.exists");
const char* path = luaL_checkstring(L, 1);
Expand Down Expand Up @@ -537,6 +590,8 @@ static const luaL_Reg internalfs_lib[] = {
{"stat", internalfs_stat},
{"stat_fs", internalfs_stat_fs},
{"dir_info", internalfs_dir_info},
{"ask_select_file", internalfs_ask_select_file},
{"ask_select_dir", internalfs_ask_select_dir},
{"exists", internalfs_exists},
{"is_dir", internalfs_is_dir},
{"is_file", internalfs_is_file},
Expand Down
2 changes: 0 additions & 2 deletions arm9/source/lua/gm9lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
#include "gm9internalsys.h"

#define DEBUGSP(x) ShowPrompt(false, (x))
// this is taken from scripting.c
#define _VAR_CNT_LEN 256

typedef struct GM9LuaLoadF {
int n; // pre-read characters
Expand Down
11 changes: 8 additions & 3 deletions arm9/source/lua/gm9lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
#define TO_EMUNAND (1UL<<12)
#define LEGIT (1UL<<13)
#define FIND_FIRST (1UL<<14)
#define INCLUDE_DIRS (1UL<<15)
#define EXPLORER (1UL<<16)

#define FLAGS_STR "no_cancel", "silent", "calc_sha", "sha1", "skip", "overwrite_all", "append_all", "all", "recursive", "to_emunand", "legit", "first"
#define FLAGS_CONSTS NO_CANCEL, SILENT, CALC_SHA, USE_SHA1, SKIP_ALL, OVERWRITE_ALL, APPEND_ALL, ASK_ALL, RECURSIVE, TO_EMUNAND, LEGIT, FIND_FIRST
#define FLAGS_COUNT 12
#define FLAGS_STR "no_cancel", "silent", "calc_sha", "sha1", "skip", "overwrite_all", "append_all", "all", "recursive", "to_emunand", "legit", "first", "include_dirs", "explorer"
#define FLAGS_CONSTS NO_CANCEL, SILENT, CALC_SHA, USE_SHA1, SKIP_ALL, OVERWRITE_ALL, APPEND_ALL, ASK_ALL, RECURSIVE, TO_EMUNAND, LEGIT, FIND_FIRST, INCLUDE_DIRS, EXPLORER
#define FLAGS_COUNT 14

#define LUASCRIPT_EXT "lua"
#define LUASCRIPT_MAX_SIZE STD_BUFFER_SIZE

// taken from arm9/source/utils/scripting.c
#define _VAR_CNT_LEN 256

#ifndef NO_LUA
static inline void CheckLuaArgCount(lua_State* L, int argcount, const char* cmd) {
int args = lua_gettop(L);
Expand Down
Binary file modified command comparison table.ods
Binary file not shown.
2 changes: 2 additions & 0 deletions data/luapackages/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ fs.list_dir = _fs.list_dir
fs.stat = _fs.stat
fs.stat_fs = _fs.stat_fs
fs.dir_info = _fs.dir_info
fs.ask_select_file = _fs.ask_select_file
fs.ask_select_dir = _fs.ask_select_dir
fs.exists = _fs.exists
fs.is_dir = _fs.is_dir
fs.is_file = _fs.is_file
Expand Down
21 changes: 21 additions & 0 deletions data/luascripts/selector-test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
for i, v in pairs({"9:/test_1", "9:/test_2", "9:/extradir_3"}) do
print("Making dir", v)
fs.mkdir(v)
end

for i, v in pairs({"9:/test_1.bin", "9:/test_2.bin", "9:/extratest_3.bin", "9:/test_2/thingy.txt"}) do
print("Touching file", v)
fs.write_file(v, 0, "a")
end

print()
print("Selected:", fs.ask_select_file("Select a file!", "9:/test_*"))
print("Selected:", fs.ask_select_file("Select a file! (explorer)", "9:/test_*", {explorer=true}))

print("Selected:", fs.ask_select_dir("Select a directory!", "9:/"))
print("Selected:", fs.ask_select_dir("Select a directory! (explorer)", "9:/", {explorer=true}))

print("Selected:", fs.ask_select_file("Select a file OR directory!", "9:/test_*", {include_dirs=true}))
print("Selected:", fs.ask_select_file("Select a file OR directory! (explorer)", "9:/test_*", {include_dirs=true, explorer=true}))
print("Done")
ui.echo("Done")
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
pkgs.devkitNix.devkitARM
python3Packages.python
( python3Packages.callPackage ./firmtool.nix { } )
lua54Packages.lua
];

inherit (pkgs.devkitNix.devkitARM) shellHook;
Expand Down

0 comments on commit f8738ef

Please sign in to comment.