Init/shutdown working

- stk_init/shutdown do what they are supposed to.
- buffer sizes moved to headers
This commit is contained in:
2025-11-02 18:40:33 +01:00
parent a0c9cdb577
commit 5c621c8367
5 changed files with 213 additions and 11 deletions
+13 -8
View File
@@ -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;
}
+1 -1
View File
@@ -1,3 +1,4 @@
#include "stk.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -13,7 +14,6 @@
#endif
#define EVENT_BUFFER_SIZE 4096
#define PATH_BUFFER_SIZE 1024
void *platform_load_library(const char *path)
{
+47 -2
View File
@@ -1,16 +1,61 @@
#include "stk.h"
#include "stk_log.h"
#include <string.h>
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;
}