diff --git a/diff.txt b/diff.txt new file mode 100644 index 0000000..e2200f7 --- /dev/null +++ b/diff.txt @@ -0,0 +1,151 @@ +diff --git a/include/stk.h b/include/stk.h +index 1097a45..f93177a 100644 +--- a/include/stk.h ++++ b/include/stk.h +@@ -5,6 +5,7 @@ + #include + + #define MOD_DIR_BUFFER_SIZE 32 ++#define PATH_BUFFER_SIZE 1024 + + #ifdef __cplusplus + extern "C" { +diff --git a/src/module.c b/src/module.c +index 13d5971..2a3e067 100644 +--- a/src/module.c ++++ b/src/module.c +@@ -7,16 +7,15 @@ void *platform_get_symbol(void *handle, const char *symbol); + + typedef void (*stk_module_func)(void); + +-char stk_mod_dir[MOD_DIR_BUFFER_SIZE]; + static void **stk_handles = NULL; + static stk_module_func *stk_inits = NULL; + static stk_module_func *stk_shutdowns = NULL; + +-static size_t module_count = 0; ++size_t module_count = 0; + + size_t stk_module_count(void) { return module_count; } + +-static int stk_module_load(const char *path) ++int stk_module_load(const char *path) + { + void *handle; + stk_module_func init_func; +@@ -46,7 +45,7 @@ static int stk_module_load(const char *path) + return 0; + } + +-static void stk_module_unload(size_t index) ++void stk_module_unload(size_t index) + { + size_t i; + +@@ -62,11 +61,17 @@ static void stk_module_unload(size_t index) + --module_count; + } + +-static void stk_free_file_list(char **list, size_t count) ++void stk_module_unload_all(void) + { + size_t i; +- for (i = 0; i < count; ++i) +- free(list[i]); ++ for (i = module_count; i > 0; --i) ++ stk_module_unload(i - 1); + +- free(list); ++ free(stk_handles); ++ free(stk_inits); ++ free(stk_shutdowns); ++ ++ stk_handles = NULL; ++ stk_inits = NULL; ++ stk_shutdowns = NULL; + } +diff --git a/src/platform.c b/src/platform.c +index b1d2e29..50379c7 100644 +--- a/src/platform.c ++++ b/src/platform.c +@@ -1,3 +1,4 @@ ++#include "stk.h" + #include + #include + #include +@@ -13,7 +14,6 @@ + #endif + + #define EVENT_BUFFER_SIZE 4096 +-#define PATH_BUFFER_SIZE 1024 + + void *platform_load_library(const char *path) + { +diff --git a/src/stk.c b/src/stk.c +index f472b55..3375d62 100644 +--- a/src/stk.c ++++ b/src/stk.c +@@ -1,16 +1,61 @@ + #include "stk.h" + #include "stk_log.h" ++#include + +-extern char stk_mod_dir[MOD_DIR_BUFFER_SIZE]; ++extern size_t module_count; ++ ++static char stk_mod_dir[MOD_DIR_BUFFER_SIZE]; ++static void *watch_handle = NULL; ++ ++char **platform_directory_init_scan(const char *path, size_t *out_count); ++void *platform_directory_watch_start(const char *path); ++void platform_directory_watch_stop(void *handle); ++size_t stk_module_count(void); ++int stk_module_load(const char *path); ++void stk_module_unload(size_t index); ++void stk_module_unload_all(void); + + int stk_init(const char *mod_dir) + { +- stk_log(stdout, "[stk] stk initialized v%s!", STK_VERSION_STRING); ++ char **files; ++ size_t file_count, i; ++ char full_path[PATH_BUFFER_SIZE]; ++ ++ if (mod_dir) { ++ strncpy(stk_mod_dir, mod_dir, MOD_DIR_BUFFER_SIZE - 1); ++ stk_mod_dir[MOD_DIR_BUFFER_SIZE - 1] = '\0'; ++ } else { ++ strcpy(stk_mod_dir, "mods"); ++ } ++ ++ files = platform_directory_init_scan(stk_mod_dir, &file_count); ++ if (!files) ++ goto scanned; ++ ++ for (i = 0; i < file_count; ++i) { ++ sprintf(full_path, "%s/%s", stk_mod_dir, files[i]); ++ stk_module_load(full_path); ++ free(files[i]); ++ } ++ ++ free(files); ++ ++scanned: ++ watch_handle = platform_directory_watch_start(stk_mod_dir); ++ stk_log(stdout, "[stk] stk initialized v%s! Loaded %zu from %s", ++ STK_VERSION_STRING, module_count, stk_mod_dir); + return 0; + } + + int stk_shutdown(void) + { ++ if (watch_handle) { ++ platform_directory_watch_stop(watch_handle); ++ watch_handle = NULL; ++ } ++ ++ stk_module_unload_all(); ++ + stk_log(stdout, "[stk] stk shutdown"); + return 0; + } diff --git a/include/stk.h b/include/stk.h index 1097a45..f93177a 100644 --- a/include/stk.h +++ b/include/stk.h @@ -5,6 +5,7 @@ #include #define MOD_DIR_BUFFER_SIZE 32 +#define PATH_BUFFER_SIZE 1024 #ifdef __cplusplus extern "C" { diff --git a/src/module.c b/src/module.c index 13d5971..2a3e067 100644 --- a/src/module.c +++ b/src/module.c @@ -7,16 +7,15 @@ void *platform_get_symbol(void *handle, const char *symbol); typedef void (*stk_module_func)(void); -char stk_mod_dir[MOD_DIR_BUFFER_SIZE]; static void **stk_handles = NULL; static stk_module_func *stk_inits = NULL; static stk_module_func *stk_shutdowns = NULL; -static size_t module_count = 0; +size_t module_count = 0; size_t stk_module_count(void) { return module_count; } -static int stk_module_load(const char *path) +int stk_module_load(const char *path) { void *handle; stk_module_func init_func; @@ -46,7 +45,7 @@ static int stk_module_load(const char *path) return 0; } -static void stk_module_unload(size_t index) +void stk_module_unload(size_t index) { size_t i; @@ -62,11 +61,17 @@ static void stk_module_unload(size_t index) --module_count; } -static void stk_free_file_list(char **list, size_t count) +void stk_module_unload_all(void) { size_t i; - for (i = 0; i < count; ++i) - free(list[i]); + for (i = module_count; i > 0; --i) + stk_module_unload(i - 1); - free(list); + free(stk_handles); + free(stk_inits); + free(stk_shutdowns); + + stk_handles = NULL; + stk_inits = NULL; + stk_shutdowns = NULL; } diff --git a/src/platform.c b/src/platform.c index b1d2e29..50379c7 100644 --- a/src/platform.c +++ b/src/platform.c @@ -1,3 +1,4 @@ +#include "stk.h" #include #include #include @@ -13,7 +14,6 @@ #endif #define EVENT_BUFFER_SIZE 4096 -#define PATH_BUFFER_SIZE 1024 void *platform_load_library(const char *path) { diff --git a/src/stk.c b/src/stk.c index f472b55..3375d62 100644 --- a/src/stk.c +++ b/src/stk.c @@ -1,16 +1,61 @@ #include "stk.h" #include "stk_log.h" +#include -extern char stk_mod_dir[MOD_DIR_BUFFER_SIZE]; +extern size_t module_count; + +static char stk_mod_dir[MOD_DIR_BUFFER_SIZE]; +static void *watch_handle = NULL; + +char **platform_directory_init_scan(const char *path, size_t *out_count); +void *platform_directory_watch_start(const char *path); +void platform_directory_watch_stop(void *handle); +size_t stk_module_count(void); +int stk_module_load(const char *path); +void stk_module_unload(size_t index); +void stk_module_unload_all(void); int stk_init(const char *mod_dir) { - stk_log(stdout, "[stk] stk initialized v%s!", STK_VERSION_STRING); + char **files; + size_t file_count, i; + char full_path[PATH_BUFFER_SIZE]; + + if (mod_dir) { + strncpy(stk_mod_dir, mod_dir, MOD_DIR_BUFFER_SIZE - 1); + stk_mod_dir[MOD_DIR_BUFFER_SIZE - 1] = '\0'; + } else { + strcpy(stk_mod_dir, "mods"); + } + + files = platform_directory_init_scan(stk_mod_dir, &file_count); + if (!files) + goto scanned; + + for (i = 0; i < file_count; ++i) { + sprintf(full_path, "%s/%s", stk_mod_dir, files[i]); + stk_module_load(full_path); + free(files[i]); + } + + free(files); + +scanned: + watch_handle = platform_directory_watch_start(stk_mod_dir); + stk_log(stdout, "[stk] stk initialized v%s! Loaded %zu from %s", + STK_VERSION_STRING, module_count, stk_mod_dir); return 0; } int stk_shutdown(void) { + if (watch_handle) { + platform_directory_watch_stop(watch_handle); + watch_handle = NULL; + } + + stk_module_unload_all(); + stk_log(stdout, "[stk] stk shutdown"); return 0; }