feat: make module paths and entry points configurable
* Added functions to set mod dir, temp dir name, and module init/shutdown function names. * Replaced hardcoded string literals with configurable static buffers in module.c. * Improved string safety by replacing sprintf with strncpy/strncat. * Updated stk_init local path buffers to handle maximum combined path lengths.
This commit is contained in:
@@ -37,6 +37,10 @@ int stk_init(void);
|
||||
void stk_shutdown(void);
|
||||
size_t stk_module_count(void);
|
||||
size_t stk_poll(void);
|
||||
void stk_set_mod_dir(const char *path);
|
||||
void stk_set_tmp_dir_name(const char *name);
|
||||
void stk_set_module_init_fn(const char *name);
|
||||
void stk_set_module_shutdown_fn(const char *name);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
+26
-2
@@ -3,6 +3,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define STK_MOD_FUNC_NAME_BUFFER 64
|
||||
|
||||
void *platform_load_library(const char *path);
|
||||
void platform_unload_library(void *handle);
|
||||
void *platform_get_symbol(void *handle, const char *symbol);
|
||||
@@ -14,6 +16,10 @@ void **stk_handles = NULL;
|
||||
stk_module_func *stk_inits = NULL;
|
||||
stk_module_func *stk_shutdowns = NULL;
|
||||
|
||||
static char stk_mod_init_name[STK_MOD_FUNC_NAME_BUFFER] = "stk_mod_init";
|
||||
static char stk_mod_shutdown_name[STK_MOD_FUNC_NAME_BUFFER] =
|
||||
"stk_mod_shutdown";
|
||||
|
||||
size_t module_count = 0;
|
||||
|
||||
size_t stk_module_count(void) { return module_count; }
|
||||
@@ -108,10 +114,10 @@ int stk_module_load(const char *path, int index)
|
||||
if (!handle)
|
||||
return -1;
|
||||
|
||||
u.obj = platform_get_symbol(handle, "stk_module_init");
|
||||
u.obj = platform_get_symbol(handle, stk_mod_init_name);
|
||||
init_func = u.func;
|
||||
|
||||
u.obj = platform_get_symbol(handle, "stk_module_shutdown");
|
||||
u.obj = platform_get_symbol(handle, stk_mod_shutdown_name);
|
||||
shutdown_func = u.func;
|
||||
|
||||
if (!init_func || !shutdown_func) {
|
||||
@@ -200,3 +206,21 @@ void stk_module_unload_all(void)
|
||||
|
||||
stk_module_free_memory();
|
||||
}
|
||||
|
||||
void stk_set_module_init_fn(const char *name)
|
||||
{
|
||||
if (!name)
|
||||
return;
|
||||
|
||||
strncpy(stk_mod_init_name, name, STK_MOD_FUNC_NAME_BUFFER - 1);
|
||||
stk_mod_init_name[STK_MOD_FUNC_NAME_BUFFER - 1] = '\0';
|
||||
}
|
||||
|
||||
void stk_set_module_shutdown_fn(const char *name)
|
||||
{
|
||||
if (!name)
|
||||
return;
|
||||
|
||||
strncpy(stk_mod_shutdown_name, name, STK_MOD_FUNC_NAME_BUFFER - 1);
|
||||
stk_mod_shutdown_name[STK_MOD_FUNC_NAME_BUFFER - 1] = '\0';
|
||||
}
|
||||
|
||||
@@ -13,21 +13,15 @@ extern char (*stk_module_ids)[STK_MOD_ID_BUFFER];
|
||||
|
||||
extern size_t module_count;
|
||||
|
||||
static char stk_mod_dir[STK_MOD_DIR_BUFFER] = "mods";
|
||||
static char stk_tmp_dir[STK_MOD_DIR_BUFFER] = "mods/.tmp";
|
||||
static char stk_mod_dir[STK_PATH_MAX_OS] = "mods";
|
||||
static char stk_tmp_name[STK_MOD_ID_BUFFER] = ".tmp";
|
||||
static char stk_tmp_dir[STK_PATH_MAX_OS] = "mods/.tmp";
|
||||
static void *watch_handle = NULL;
|
||||
|
||||
char *extract_module_id(const char *path);
|
||||
char (*platform_directory_init_scan(const char *path,
|
||||
size_t *out_count))[STK_PATH_MAX];
|
||||
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, int index);
|
||||
int stk_module_load_init(const char *path, int index);
|
||||
void stk_module_unload(size_t index);
|
||||
void stk_module_unload_all(void);
|
||||
int stk_module_init_memory(size_t capacity);
|
||||
stk_module_event_t *platform_directory_watch_check(
|
||||
void *handle, char (**file_list)[STK_PATH_MAX], size_t *out_count,
|
||||
char (*loaded_module_ids)[STK_MOD_ID_BUFFER], const size_t loaded_count);
|
||||
@@ -35,12 +29,20 @@ 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);
|
||||
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);
|
||||
void stk_module_unload(size_t index);
|
||||
void stk_module_unload_all(void);
|
||||
int stk_module_init_memory(size_t capacity);
|
||||
|
||||
int stk_init(void)
|
||||
{
|
||||
char (*files)[STK_PATH_MAX] = NULL;
|
||||
size_t file_count, i;
|
||||
char full_path[STK_PATH_MAX_OS];
|
||||
char tmp_path[STK_PATH_MAX_OS];
|
||||
char full_path[STK_PATH_MAX_OS + STK_PATH_MAX];
|
||||
char tmp_path[STK_PATH_MAX_OS + STK_PATH_MAX];
|
||||
|
||||
platform_mkdir(stk_tmp_dir);
|
||||
files = platform_directory_init_scan(stk_mod_dir, &file_count);
|
||||
@@ -114,3 +116,36 @@ size_t stk_poll(void)
|
||||
|
||||
return file_count;
|
||||
}
|
||||
|
||||
void stk_set_mod_dir(const char *path)
|
||||
{
|
||||
if (!path)
|
||||
return;
|
||||
|
||||
strncpy(stk_mod_dir, path, STK_PATH_MAX_OS - 1);
|
||||
stk_mod_dir[STK_PATH_MAX_OS - 1] = '\0';
|
||||
|
||||
strncpy(stk_tmp_dir, stk_mod_dir, STK_PATH_MAX_OS - 1);
|
||||
stk_tmp_dir[STK_PATH_MAX_OS - 1] = '\0';
|
||||
|
||||
strncat(stk_tmp_dir, "/", STK_PATH_MAX_OS - strlen(stk_tmp_dir) - 1);
|
||||
|
||||
strncat(stk_tmp_dir, stk_tmp_name,
|
||||
STK_PATH_MAX_OS - strlen(stk_tmp_dir) - 1);
|
||||
}
|
||||
|
||||
void stk_set_tmp_dir_name(const char *name)
|
||||
{
|
||||
if (!name)
|
||||
return;
|
||||
|
||||
strncpy(stk_tmp_name, name, STK_MOD_ID_BUFFER - 1);
|
||||
stk_tmp_name[STK_MOD_ID_BUFFER - 1] = '\0';
|
||||
|
||||
strncpy(stk_tmp_dir, stk_mod_dir, STK_PATH_MAX_OS - 1);
|
||||
stk_tmp_dir[STK_PATH_MAX_OS - 1] = '\0';
|
||||
|
||||
strncat(stk_tmp_dir, "/", STK_PATH_MAX_OS - strlen(stk_tmp_dir) - 1);
|
||||
strncat(stk_tmp_dir, stk_tmp_name,
|
||||
STK_PATH_MAX_OS - strlen(stk_tmp_dir) - 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user