| 262 | } |
| 263 | |
| 264 | void lua_scripts::load_all_scripts_in_dir(lua_State *L, const char *dirname) { |
| 265 | if (dirname == nullptr) { |
| 266 | return; |
| 267 | } |
| 268 | auto *d = AP::FS().opendir(dirname); |
| 269 | if (d == nullptr) { |
| 270 | // this disk_space check will return 0 if we don't have a real |
| 271 | // filesystem (ie. no Posix or FatFs). Do not warn in this case. |
| 272 | if (AP::FS().disk_space(dirname) != 0) { |
| 273 | GCS_SEND_TEXT(MAV_SEVERITY_WARNING, "Lua: open directory (%s) failed", dirname); |
| 274 | } |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | // load anything that ends in .lua |
| 279 | for (struct dirent *de=AP::FS().readdir(d); de; de=AP::FS().readdir(d)) { |
| 280 | uint8_t length = strlen(de->d_name); |
| 281 | if (length < 5) { |
| 282 | // not long enough |
| 283 | continue; |
| 284 | } |
| 285 | |
| 286 | if ((de->d_name[0] == '.') || strncmp(&de->d_name[length-4], ".lua", 4)) { |
| 287 | // starts with . (hidden file) or doesn't end in .lua |
| 288 | continue; |
| 289 | } |
| 290 | |
| 291 | // FIXME: because chunk name fetching is not working we are allocating and storing an extra string we shouldn't need to |
| 292 | size_t size = strlen(dirname) + strlen(de->d_name) + 2; |
| 293 | char * filename = (char *) _heap.allocate(size); |
| 294 | if (filename == nullptr) { |
| 295 | continue; |
| 296 | } |
| 297 | snprintf(filename, size, "%s/%s", dirname, de->d_name); |
| 298 | |
| 299 | // we have something that looks like a lua file, attempt to load it |
| 300 | script_info * script = load_script(L, filename); |
| 301 | if (script == nullptr) { |
| 302 | _heap.deallocate(filename); |
| 303 | continue; |
| 304 | } |
| 305 | reschedule_script(script); |
| 306 | |
| 307 | #if HAL_LOGGER_FILE_CONTENTS_ENABLED |
| 308 | if (!option_is_set(AP_Scripting::DebugOption::SUPPRESS_SCRIPT_LOG)) { |
| 309 | AP::logger().log_file_content(filename); |
| 310 | } |
| 311 | #endif |
| 312 | } |
| 313 | AP::FS().closedir(d); |
| 314 | } |
| 315 | |
| 316 | void lua_scripts::reset_loop_overtime(lua_State *L) { |
| 317 | overtime = false; |
nothing calls this directly
no test coverage detected