From a64831e6b496135c247a8b3910fc2659a776459a Mon Sep 17 00:00:00 2001 From: philmoz Date: Mon, 21 Jul 2025 13:40:54 +1000 Subject: [PATCH] feat(radio): allow Lua tools to be contained entirely within a folder (#6448) --- radio/src/gui/colorlcd/radio/radio_tools.cpp | 24 ++++++++++++++------ radio/src/gui/common/stdlcd/radio_tools.cpp | 24 ++++++++++++++------ radio/src/lua/interface.cpp | 2 +- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/radio/src/gui/colorlcd/radio/radio_tools.cpp b/radio/src/gui/colorlcd/radio/radio_tools.cpp index ec8e69431..18a9a7036 100644 --- a/radio/src/gui/colorlcd/radio/radio_tools.cpp +++ b/radio/src/gui/colorlcd/radio/radio_tools.cpp @@ -114,24 +114,34 @@ static void scanLuaTools(std::list& scripts) FRESULT res = f_opendir(&dir, SCRIPTS_TOOLS_PATH); if (res == FR_OK) { for (;;) { - TCHAR path[FF_MAX_LFN + 1] = SCRIPTS_TOOLS_PATH "/"; res = f_readdir(&dir, &fno); /* Read a directory item */ if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */ - if (fno.fattrib & (AM_DIR | AM_HID | AM_SYS)) - continue; // skip subfolders, hidden files and system files + if (fno.fattrib & (AM_HID | AM_SYS)) + continue; // skip hidden files and system files if (fno.fname[0] == '.') continue; /* Ignore UNIX hidden files */ + bool inFolder = (fno.fattrib & AM_DIR); + + char path[FF_MAX_LFN + 1] = SCRIPTS_TOOLS_PATH "/"; strcat(path, fno.fname); - if (isRadioScriptTool(fno.fname)) { + if (inFolder) { + strcat(path, "/main.lua"); + if (f_stat(path, nullptr) != FR_OK) + continue; + } + + if (isRadioScriptTool(path)) { char toolName[RADIO_TOOL_NAME_MAXLEN + 1] = {0}; const char* label; - char* ext = (char*)getFileExtension(path); if (readToolName(toolName, path)) { label = toolName; } else { - *ext = '\0'; - label = getBasename(path); + if (!inFolder) { + char* ext = (char*)getFileExtension(fno.fname); + if (ext) *ext = '\0'; + } + label = fno.fname; } scripts.emplace_back(ToolEntry{label, path, run_lua_tool}); diff --git a/radio/src/gui/common/stdlcd/radio_tools.cpp b/radio/src/gui/common/stdlcd/radio_tools.cpp index ce3485677..108f41ced 100644 --- a/radio/src/gui/common/stdlcd/radio_tools.cpp +++ b/radio/src/gui/common/stdlcd/radio_tools.cpp @@ -139,23 +139,33 @@ void menuRadioTools(event_t event) for (;;) { res = f_readdir(&dir, &fno); /* Read a directory item */ if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */ - if (fno.fattrib & (AM_DIR|AM_HID|AM_SYS)) continue; // skip subfolders, hidden files and system files + if (fno.fattrib & (AM_HID|AM_SYS)) continue; // skip hidden files and system files if (fno.fname[0] == '.') continue; /* Ignore UNIX hidden files */ - if (isRadioScriptTool(fno.fname)) { + bool inFolder = (fno.fattrib & AM_DIR); + + char path[FF_MAX_LFN + 1] = SCRIPTS_TOOLS_PATH "/"; + strcat(path, fno.fname); + if (inFolder) { + strcat(path, "/main.lua"); + if (f_stat(path, nullptr) != FR_OK) + continue; + } + + if (isRadioScriptTool(path)) { const char *label; char toolName[RADIO_TOOL_NAME_MAXLEN + 1] = {0}; - char path[FF_MAX_LFN + 1] = SCRIPTS_TOOLS_PATH "/"; - strcat(path, fno.fname); - char *ext = (char *)getFileExtension(fno.fname); - *ext = '\0'; if (readToolName(toolName, path)) { label = toolName; } else { + if (!inFolder) { + char *ext = (char *)getFileExtension(fno.fname); + if (ext) *ext = '\0'; + } label = fno.fname; } - luaScripts.emplace_back(LuaScript{fno.fname, label}); + luaScripts.emplace_back(LuaScript{path + DIM(SCRIPTS_TOOLS_PATH "/") - 1, label}); } } f_closedir(&dir); diff --git a/radio/src/lua/interface.cpp b/radio/src/lua/interface.cpp index eab40d0ed..bde8f3220 100644 --- a/radio/src/lua/interface.cpp +++ b/radio/src/lua/interface.cpp @@ -1416,7 +1416,7 @@ bool readToolName(char * toolName, const char * filename) UINT count; if (f_open(&file, filename, FA_READ) != FR_OK) { - return "Error opening file"; + return false; } FRESULT res = f_read(&file, &buffer, sizeof(buffer), &count);