From 0efdc235765edc876b1ba2a657422f5cd6a2aee5 Mon Sep 17 00:00:00 2001 From: Reuh Date: Sun, 25 Oct 2015 11:08:27 +0100 Subject: [PATCH] Moved library path modifications to main.lua, made all paths relative The library path can now be easlily modified. Instead of always launching /3ds/ctruLua/main.lua, ctruLua will now launch the main.lua in the current directory (the ctruLua.3dsx directory when launched with HBL). On citra, this will be the root of the sdmc directory. --- libs/lua-5.3.1/src/luaconf.h | 35 ++++++++++++++++++++++++------ sdcard/3ds/ctruLua/editor/main.lua | 6 ++--- sdcard/3ds/ctruLua/main.lua | 7 ++++-- sdcard/3ds/ctruLua/openfile.lua | 5 +++-- source/fs.c | 13 +++++++---- source/main.c | 4 +--- 6 files changed, 49 insertions(+), 21 deletions(-) diff --git a/libs/lua-5.3.1/src/luaconf.h b/libs/lua-5.3.1/src/luaconf.h index 4cc26f7..74a9252 100644 --- a/libs/lua-5.3.1/src/luaconf.h +++ b/libs/lua-5.3.1/src/luaconf.h @@ -167,17 +167,38 @@ ** hierarchy or if you want to install your libraries in ** non-conventional directories. */ -#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR "/" - -#define LUA_ROOT "sdmc:/" -#define LUA_LDIR LUA_ROOT "3ds/ctruLua/libs/" -#define LUA_CDIR LUA_ROOT "3ds/ctruLua/libs/" +#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR +#if defined(_WIN32) /* { */ +/* +** In Windows, any exclamation mark ('!') in the path is replaced by the +** path of the directory of the executable file of the current process. +*/ +#define LUA_LDIR "!\\lua\\" +#define LUA_CDIR "!\\" +#define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\" #define LUA_PATH_DEFAULT \ - LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ - LUA_LDIR LUA_VDIR"?.lua;" LUA_LDIR LUA_VDIR"?/init.lua;" \ + LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ + LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \ + LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \ + ".\\?.lua;" ".\\?\\init.lua" +#define LUA_CPATH_DEFAULT \ + LUA_CDIR"?.dll;" \ + LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \ + LUA_CDIR"loadall.dll;" ".\\?.dll" + +#else /* }{ */ + +#define LUA_ROOT "/usr/local/" +#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR "/" +#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/" +#define LUA_PATH_DEFAULT \ + LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ + LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \ "./?.lua;" "./?/init.lua" #define LUA_CPATH_DEFAULT \ LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so" +#endif /* } */ + /* @@ LUA_DIRSEP is the directory separator (for submodules). diff --git a/sdcard/3ds/ctruLua/editor/main.lua b/sdcard/3ds/ctruLua/editor/main.lua index ec14bad..26709b4 100644 --- a/sdcard/3ds/ctruLua/editor/main.lua +++ b/sdcard/3ds/ctruLua/editor/main.lua @@ -3,8 +3,8 @@ local hid = require("ctr.hid") local gfx = require("ctr.gfx") -- Open libs -local keyboard = dofile("sdmc:/3ds/ctruLua/keyboard.lua") -local openfile = dofile("sdmc:/3ds/ctruLua/openfile.lua") +local keyboard = require("keyboard") +local openfile = require("openfile") local color = dofile("color.lua") local syntax = dofile("syntax.lua") @@ -12,7 +12,7 @@ local syntax = dofile("syntax.lua") local font = gfx.font.load("VeraMono.ttf") -- Open file -local path, status = openfile("Choose a file to edit", "/3ds/ctruLua/", nil, "any") +local path, status = openfile("Choose a file to edit", nil, nil, "any") if not path then return end local lineEnding local lines = {} diff --git a/sdcard/3ds/ctruLua/main.lua b/sdcard/3ds/ctruLua/main.lua index 560a7f6..a0870ab 100644 --- a/sdcard/3ds/ctruLua/main.lua +++ b/sdcard/3ds/ctruLua/main.lua @@ -1,8 +1,11 @@ local fs = require("ctr.fs") +-- Set up path +local ldir = fs.getDirectory().."libs/" +package.path = package.path..";".. ldir.."?.lua;".. ldir.."?/init.lua" + repeat - fs.setDirectory("sdmc:/3ds/ctruLua") - local file = dofile("openfile.lua")("Choose a Lua file to execute", "/3ds/ctruLua/", ".lua", "exist") + local file = require("openfile")("Choose a Lua file to execute", nil, ".lua", "exist") if file then fs.setDirectory(file:match("^(.-)[^/]*$")) dofile(file) diff --git a/sdcard/3ds/ctruLua/openfile.lua b/sdcard/3ds/ctruLua/openfile.lua index 32a924e..2f67bae 100644 --- a/sdcard/3ds/ctruLua/openfile.lua +++ b/sdcard/3ds/ctruLua/openfile.lua @@ -1,6 +1,6 @@ --- Open a file explorer to select a file. -- string title: title of the file explorer. --- string curdir: the directory to initially open the file explorer in. +-- string curdir: the directory to initially open the file explorer in, or nil for the current directory. -- string exts: the file extensions the user can select, separated by ";". If nil, all extensions are accepted. -- string type: "exist" to select an existing file, "new" to select an non-existing file or "any" to select a existing -- or non-existing file name. If nil, defaults to "exist". @@ -11,9 +11,10 @@ return function(title, curdir, exts, type) local ctr = require("ctr") local gfx = require("ctr.gfx") - local keyboard = dofile("sdmc:/3ds/ctruLua/keyboard.lua") + local keyboard = require("keyboard") -- Arguments + local curdir = curdir or ctr.fs.getDirectory() local type = type or "exist" -- Variables diff --git a/source/fs.c b/source/fs.c index 576ec48..ea6231e 100644 --- a/source/fs.c +++ b/source/fs.c @@ -1,4 +1,6 @@ #include +#include +#include #include <3ds/types.h> #include <3ds/util/utf.h> @@ -88,12 +90,15 @@ static int fs_setDirectory(lua_State *L) { int result = chdir(path); - if (result == 0) + if (result == 0) { lua_pushboolean(L, true); - else - lua_pushboolean(L, false); + return 1; - return 1; + } else { + lua_pushboolean(L, false); + lua_pushstring(L, strerror(errno)); + return 2; + } } static const struct luaL_Reg fs_lib[] = { diff --git a/source/main.c b/source/main.c index be7d9d7..21750ed 100644 --- a/source/main.c +++ b/source/main.c @@ -4,8 +4,6 @@ #include #include -#define BOOT_FILE "sdmc:/3ds/ctruLua/main.lua" - void load_ctr_lib(lua_State *L); void unload_ctr_lib(lua_State *L); @@ -47,7 +45,7 @@ int main() { load_ctr_lib(L); // Do the actual thing - if (luaL_dofile(L, BOOT_FILE)) error(luaL_checkstring(L, -1)); + if (luaL_dofile(L, "main.lua")) error(luaL_checkstring(L, -1)); // Unload libs unload_ctr_lib(L);