From b2ad52fa27c3ed28f487bcdd9d9855fa9e536fb3 Mon Sep 17 00:00:00 2001 From: Iampete1 Date: Tue, 27 Oct 2020 01:08:14 +0000 Subject: [PATCH] AP_Scripting: add DIR_DISABLE param --- libraries/AP_Scripting/AP_Scripting.cpp | 8 ++++++++ libraries/AP_Scripting/AP_Scripting.h | 7 +++++++ libraries/AP_Scripting/lua_scripts.cpp | 16 ++++++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/libraries/AP_Scripting/AP_Scripting.cpp b/libraries/AP_Scripting/AP_Scripting.cpp index 31c92d16e07c3..807d90d24b22d 100644 --- a/libraries/AP_Scripting/AP_Scripting.cpp +++ b/libraries/AP_Scripting/AP_Scripting.cpp @@ -101,6 +101,14 @@ const AP_Param::GroupInfo AP_Scripting::var_info[] = { // @User: Standard AP_GROUPINFO("USER4", 8, AP_Scripting, _user[3], 0.0), + // @Param: DIR_DISABLE + // @DisplayName: Directory disable + // @Description: This will stop scripts being loaded from the given locations + // @Bitmask: 0:ROMFS, 1:APM/scripts + // @RebootRequired: True + // @User: Advanced + AP_GROUPINFO("DIR_DISABLE", 9, AP_Scripting, _dir_disable, 0), + AP_GROUPEND }; diff --git a/libraries/AP_Scripting/AP_Scripting.h b/libraries/AP_Scripting/AP_Scripting.h index 01ccbc6dbeb7d..71891fef4fedb 100644 --- a/libraries/AP_Scripting/AP_Scripting.h +++ b/libraries/AP_Scripting/AP_Scripting.h @@ -50,6 +50,12 @@ class AP_Scripting bool session; } terminal; + enum class SCR_DIR { + ROMFS = 1 << 0, + SCRIPTS = 1 << 1, + }; + uint16_t get_disabled_dir() { return uint16_t(_dir_disable.get());} + private: bool repl_start(void); @@ -63,6 +69,7 @@ class AP_Scripting AP_Int32 _script_vm_exec_count; AP_Int32 _script_heap_size; AP_Int8 _debug_level; + AP_Int16 _dir_disable; bool _init_failed; // true if memory allocation failed diff --git a/libraries/AP_Scripting/lua_scripts.cpp b/libraries/AP_Scripting/lua_scripts.cpp index 3b6f85392d056..e497496094540 100644 --- a/libraries/AP_Scripting/lua_scripts.cpp +++ b/libraries/AP_Scripting/lua_scripts.cpp @@ -363,8 +363,20 @@ void lua_scripts::run(void) { load_generated_bindings(L); // Scan the filesystem in an appropriate manner and autostart scripts - load_all_scripts_in_dir(L, SCRIPTING_DIRECTORY); - load_all_scripts_in_dir(L, "@ROMFS/scripts"); + // Skip those directores disabled with SCR_DIR_DISABLE param + uint16_t dir_disable = AP_Scripting::get_singleton()->get_disabled_dir(); + bool loaded = false; + if ((dir_disable & uint16_t(AP_Scripting::SCR_DIR::SCRIPTS)) == 0) { + load_all_scripts_in_dir(L, SCRIPTING_DIRECTORY); + loaded = true; + } + if ((dir_disable & uint16_t(AP_Scripting::SCR_DIR::ROMFS)) == 0) { + load_all_scripts_in_dir(L, "@ROMFS/scripts"); + loaded = true; + } + if (!loaded) { + gcs().send_text(MAV_SEVERITY_CRITICAL, "Lua: All directory's disabled see SCR_DIR_DISABLE"); + } #ifndef __clang_analyzer__ succeeded_initial_load = true;