refactor(WIP): replace event stubs with module identification logic

Replace the placeholder TODO logs in the polling loop with logic to
resolve filesystem events into specific module indices.

- Centralizing State: Refactored is_module_loaded to is_mod_loaded to
  check against the global stk_module_ids array, removing the need to
  pass local buffers and preparing for unified lifetime management.
- Standardizing Identity: Updated extract_module_id to use a
  consistent output buffer.
- Categorized Event Processing: Implemented a two-pass approach in
  stk_poll to count event types (LOAD, UNLOAD, RELOAD) and allocate
  tracking arrays, replacing the previous stubbed switch statement.
- Mapping Events to Indices: The poll loop now resolves filenames
  back to their specific loaded indices via is_mod_loaded to
  identify exactly which mod_id and index require action.
- Improved Flow Control: Introduced a finish_stk_poll label to
  ensure consistent cleanup and return values when no events are
  detected or processing is complete.
This commit is contained in:
2026-01-27 07:54:51 +01:00
parent c558032ea0
commit 9491b070d2
3 changed files with 52 additions and 39 deletions
+4 -25
View File
@@ -66,34 +66,13 @@ uint8_t is_valid_module_file(const char *filename)
return strcmp(ext, STK_MODULE_EXT) == 0; return strcmp(ext, STK_MODULE_EXT) == 0;
} }
int is_module_loaded(const char *filename, int is_mod_loaded(const char *module_name)
char (*loaded_module_ids)[STK_MOD_ID_BUFFER],
size_t loaded_count)
{ {
char module_id[STK_MOD_ID_BUFFER];
const char *basename;
char *dot;
size_t i; size_t i;
basename = strrchr(filename, '/'); for (i = 0; i < module_count; i++)
#ifdef _WIN32 if (strncmp(stk_module_ids[i], module_name,
if (!basename) STK_MOD_ID_BUFFER) == 0)
basename = strrchr(filename, '\\');
#endif
if (!basename)
basename = filename;
else
basename++;
strncpy(module_id, basename, STK_MOD_ID_BUFFER - 1);
module_id[STK_MOD_ID_BUFFER - 1] = '\0';
dot = strrchr(module_id, '.');
if (dot)
*dot = '\0';
for (i = 0; i < loaded_count; i++)
if (strcmp(loaded_module_ids[i], module_id) == 0)
return i; return i;
return -1; return -1;
+5 -4
View File
@@ -24,9 +24,9 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
int is_module_loaded(const char *filename, int is_mod_loaded(const char *module_name);
char (*loaded_ids)[STK_MOD_ID_BUFFER], size_t count);
uint8_t is_valid_module_file(const char *filename); uint8_t is_valid_module_file(const char *filename);
void extract_module_id(const char *path, char *out_id);
#ifdef _WIN32 #ifdef _WIN32
static uint8_t is_file_ready(const char *dir_path, const char *filename) static uint8_t is_file_ready(const char *dir_path, const char *filename)
@@ -629,8 +629,9 @@ stk_module_event_t *platform_directory_watch_check(
event_type = STK_MOD_UNLOAD; event_type = STK_MOD_UNLOAD;
if (e->mask & (IN_CLOSE_WRITE | IN_MOVED_TO)) { if (e->mask & (IN_CLOSE_WRITE | IN_MOVED_TO)) {
if (is_module_loaded(e->name, loaded_ids, char event_module_name[STK_MOD_ID_BUFFER];
loaded_count) >= 0) extract_module_id(e->name, event_module_name);
if (is_mod_loaded(event_module_name) >= 0)
event_type = STK_MOD_RELOAD; event_type = STK_MOD_RELOAD;
else else
event_type = STK_MOD_LOAD; event_type = STK_MOD_LOAD;
+43 -10
View File
@@ -32,7 +32,9 @@ int platform_mkdir(const char *path);
int platform_copy_file(const char *from, const char *to); int platform_copy_file(const char *from, const char *to);
int platform_remove_dir(const char *path); int platform_remove_dir(const char *path);
char *extract_module_id(const char *path); char *extract_module_id(const char *path, char *out_id);
int is_mod_loaded(const char *module_id);
size_t stk_module_count(void); size_t stk_module_count(void);
int stk_module_load(const char *path, int index); int stk_module_load(const char *path, int index);
int stk_module_load_init(const char *path, int index); int stk_module_load_init(const char *path, int index);
@@ -92,34 +94,65 @@ size_t stk_poll(void)
{ {
char (*file_list)[STK_PATH_MAX] = NULL; char (*file_list)[STK_PATH_MAX] = NULL;
stk_module_event_t *events = NULL; stk_module_event_t *events = NULL;
size_t file_count = 0, i; size_t i, file_count = 0, reload_count = 0, load_count = 0,
unload_count = 0, reload_index = 0, load_index = 0,
unload_index = 0;
int *reloaded_mods, *unloaded_mods, *loaded_mods;
events = platform_directory_watch_check(watch_handle, &file_list, events = platform_directory_watch_check(watch_handle, &file_list,
&file_count, stk_module_ids, &file_count, stk_module_ids,
module_count); module_count);
if (!events) if (!events)
return 0; goto finish_stk_poll;
for (i = 0; i < file_count; ++i) { for (i = 0; i < file_count; ++i) {
switch (events[i]) { switch (events[i]) {
case STK_MOD_RELOAD: case STK_MOD_RELOAD:
/* TODO: Implement reload */ ++reload_count;
stk_log(stdout, "STK_MOD_RELOAD");
break; break;
case STK_MOD_LOAD: case STK_MOD_LOAD:
/* TODO: Implement load */ ++load_count;
stk_log(stdout, "STK_MOD_LOAD");
break; break;
case STK_MOD_UNLOAD: case STK_MOD_UNLOAD:
/* TODO: Implement unload */ ++unload_count;
stk_log(stdout, "STK_MOD_UNLOAD");
break; break;
} }
} }
reloaded_mods = malloc(reload_count * sizeof(int));
unloaded_mods = malloc(unload_count * sizeof(int));
loaded_mods = malloc(load_count * sizeof(int));
for (i = 0; i < file_count; ++i) {
char mod_id[STK_MOD_ID_BUFFER];
extract_module_id(file_list[i], mod_id);
switch (events[i]) {
case STK_MOD_RELOAD:
reloaded_mods[reload_index++] = is_mod_loaded(mod_id);
stk_log(stdout, "STK_MOD_RELOAD %s %ld", mod_id,
reload_index - 1);
break;
case STK_MOD_LOAD:
loaded_mods[load_index++] = i;
stk_log(stdout, "STK_MOD_LOAD %s %ld", mod_id, i);
break;
case STK_MOD_UNLOAD:
unloaded_mods[unload_index++] = is_mod_loaded(mod_id);
stk_log(stdout, "STK_MOD_UNLOAD %s %ld", mod_id,
unload_index - 1);
break;
}
}
free(reloaded_mods);
free(unloaded_mods);
free(loaded_mods);
free(events); free(events);
free(file_list); free(file_list);
finish_stk_poll:
return file_count; return file_count;
} }