diff --git a/libraries/AP_Scripting/lua/src/loadlib.c b/libraries/AP_Scripting/lua/src/loadlib.c index 9ef7edb215d5c..20af8e47660c0 100644 --- a/libraries/AP_Scripting/lua/src/loadlib.c +++ b/libraries/AP_Scripting/lua/src/loadlib.c @@ -798,7 +798,11 @@ LUAMOD_API int luaopen_package (lua_State *L) { luaL_newlib(L, pk_funcs); /* create 'package' table */ // createsearcherstable(L); /* set paths */ +#if defined(ARDUPILOT_BUILD) + setpath(L, "path", LUA_PATH_VAR, lua_get_modules_path()); +#else setpath(L, "path", LUA_PATH_VAR, LUA_PATH_DEFAULT); +#endif // setpath(L, "cpath", LUA_CPATH_VAR, LUA_CPATH_DEFAULT); /* store config information */ lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n" diff --git a/libraries/AP_Scripting/lua/src/luaconf.h b/libraries/AP_Scripting/lua/src/luaconf.h index f148e1d48ff0a..885a40f14b0be 100644 --- a/libraries/AP_Scripting/lua/src/luaconf.h +++ b/libraries/AP_Scripting/lua/src/luaconf.h @@ -195,11 +195,13 @@ #define LUA_LDIR "!\\lua\\" #define LUA_CDIR "!\\" #define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\" +#if !defined(ARDUPILOT_BUILD) #define LUA_PATH_DEFAULT \ LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \ LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \ ".\\?.lua;" ".\\?\\init.lua" +#endif #define LUA_CPATH_DEFAULT \ LUA_CDIR"?.dll;" \ LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \ @@ -210,9 +212,11 @@ #define LUA_ROOT "/usr/local/" #define LUA_LDIR SCRIPTING_DIRECTORY "/modules/" #define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/" +#if !defined(ARDUPILOT_BUILD) #define LUA_PATH_DEFAULT \ LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ "./?.lua;" "./?/init.lua" +#endif #define LUA_CPATH_DEFAULT \ LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so" #endif /* } */ diff --git a/libraries/AP_Scripting/lua_bindings.cpp b/libraries/AP_Scripting/lua_bindings.cpp index 6e234130eb0e1..b1f7baea56152 100644 --- a/libraries/AP_Scripting/lua_bindings.cpp +++ b/libraries/AP_Scripting/lua_bindings.cpp @@ -920,6 +920,33 @@ int lua_get_current_ref() return scripting->get_current_ref(); } +// This is used when loading modules with require, lua must only look in enabled directory's +const char* lua_get_modules_path() +{ +#define LUA_PATH_ROMFS "@ROMFS/scripts/modules/?.lua;" "@ROMFS/scripts/modules/?/init.lua" +#define LUA_PATH_SCRIPTS LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua" + + uint16_t dir_disable = AP_Scripting::get_singleton()->get_disabled_dir(); + dir_disable &= uint16_t(AP_Scripting::SCR_DIR::SCRIPTS) | uint16_t(AP_Scripting::SCR_DIR::ROMFS); + if (dir_disable == 0) { + // Both directory's are enabled, return both, ROMFS takes priority + return LUA_PATH_ROMFS ";" LUA_PATH_SCRIPTS; + } + + if ((dir_disable & uint16_t(AP_Scripting::SCR_DIR::SCRIPTS)) == 0) { + // Only scripts enabled + return LUA_PATH_SCRIPTS; + } + + if ((dir_disable & uint16_t(AP_Scripting::SCR_DIR::ROMFS)) == 0) { + // Only ROMFS enabled + return LUA_PATH_ROMFS; + } + + // Nothing enabled? + return ""; +} + // Simple print to GCS or over CAN int lua_print(lua_State *L) { // Only support a single argument diff --git a/libraries/AP_Scripting/lua_common_defs.h b/libraries/AP_Scripting/lua_common_defs.h index 664541ddfa003..9776e687a8900 100644 --- a/libraries/AP_Scripting/lua_common_defs.h +++ b/libraries/AP_Scripting/lua_common_defs.h @@ -27,5 +27,6 @@ #endif // REPL_OUT int lua_get_current_ref(); +const char* lua_get_modules_path(); void lua_abort(void) __attribute__((noreturn));