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:
+4
-25
@@ -66,34 +66,13 @@ uint8_t is_valid_module_file(const char *filename)
|
||||
return strcmp(ext, STK_MODULE_EXT) == 0;
|
||||
}
|
||||
|
||||
int is_module_loaded(const char *filename,
|
||||
char (*loaded_module_ids)[STK_MOD_ID_BUFFER],
|
||||
size_t loaded_count)
|
||||
int is_mod_loaded(const char *module_name)
|
||||
{
|
||||
char module_id[STK_MOD_ID_BUFFER];
|
||||
const char *basename;
|
||||
char *dot;
|
||||
size_t i;
|
||||
|
||||
basename = strrchr(filename, '/');
|
||||
#ifdef _WIN32
|
||||
if (!basename)
|
||||
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)
|
||||
for (i = 0; i < module_count; i++)
|
||||
if (strncmp(stk_module_ids[i], module_name,
|
||||
STK_MOD_ID_BUFFER) == 0)
|
||||
return i;
|
||||
|
||||
return -1;
|
||||
|
||||
+5
-4
@@ -24,9 +24,9 @@
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
int is_module_loaded(const char *filename,
|
||||
char (*loaded_ids)[STK_MOD_ID_BUFFER], size_t count);
|
||||
int is_mod_loaded(const char *module_name);
|
||||
uint8_t is_valid_module_file(const char *filename);
|
||||
void extract_module_id(const char *path, char *out_id);
|
||||
|
||||
#ifdef _WIN32
|
||||
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;
|
||||
if (e->mask & (IN_CLOSE_WRITE | IN_MOVED_TO)) {
|
||||
if (is_module_loaded(e->name, loaded_ids,
|
||||
loaded_count) >= 0)
|
||||
char event_module_name[STK_MOD_ID_BUFFER];
|
||||
extract_module_id(e->name, event_module_name);
|
||||
if (is_mod_loaded(event_module_name) >= 0)
|
||||
event_type = STK_MOD_RELOAD;
|
||||
else
|
||||
event_type = STK_MOD_LOAD;
|
||||
|
||||
@@ -32,7 +32,9 @@ int platform_mkdir(const char *path);
|
||||
int platform_copy_file(const char *from, const char *to);
|
||||
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);
|
||||
int stk_module_load(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;
|
||||
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,
|
||||
&file_count, stk_module_ids,
|
||||
module_count);
|
||||
if (!events)
|
||||
return 0;
|
||||
goto finish_stk_poll;
|
||||
|
||||
for (i = 0; i < file_count; ++i) {
|
||||
switch (events[i]) {
|
||||
case STK_MOD_RELOAD:
|
||||
/* TODO: Implement reload */
|
||||
stk_log(stdout, "STK_MOD_RELOAD");
|
||||
++reload_count;
|
||||
break;
|
||||
case STK_MOD_LOAD:
|
||||
/* TODO: Implement load */
|
||||
stk_log(stdout, "STK_MOD_LOAD");
|
||||
++load_count;
|
||||
break;
|
||||
case STK_MOD_UNLOAD:
|
||||
/* TODO: Implement unload */
|
||||
stk_log(stdout, "STK_MOD_UNLOAD");
|
||||
++unload_count;
|
||||
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(file_list);
|
||||
|
||||
finish_stk_poll:
|
||||
return file_count;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user