diff options
author | Elias Fleckenstein <eliasfleckenstein@web.de> | 2021-06-09 16:58:26 +0200 |
---|---|---|
committer | Elias Fleckenstein <eliasfleckenstein@web.de> | 2021-06-09 16:58:26 +0200 |
commit | 33e798108d62684d1681f9de22963bebc4182247 (patch) | |
tree | ecd7c2c80bcbbfaa22f5e39648d414ac94ebfeb6 /dungeon.c | |
download | dungeon_game-33e798108d62684d1681f9de22963bebc4182247.tar.xz |
Initial commit
Diffstat (limited to 'dungeon.c')
-rw-r--r-- | dungeon.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/dungeon.c b/dungeon.c new file mode 100644 index 0000000..a1b0064 --- /dev/null +++ b/dungeon.c @@ -0,0 +1,46 @@ +#include <stdio.h> +#include <dlfcn.h> +#include <stdlib.h> +#include <assert.h> +#include <dirent.h> +#include <string.h> + +static void *load_plugin(const char *name) +{ + size_t len = strlen(name); + char filename[1 + 1 + 7 + 1 + len + 1 + len + 1 + 2 + 1]; + sprintf(filename, "./plugins/%s/%s.so", name, name); + + void *plugin_handle = dlmopen(LM_ID_BASE, filename, RTLD_NOW | RTLD_GLOBAL); + + if (! plugin_handle) { + printf("%s\n", dlerror()); + exit(EXIT_FAILURE); + } + + return plugin_handle; +} + +int main() +{ + void *main_plugin = load_plugin("game"); + + DIR *dir = opendir("plugins"); + assert(dir); + + struct dirent *dp; + + while (dp = readdir(dir)) { + if (dp->d_name[0] != '.' && strcmp(dp->d_name, "game") != 0) { + load_plugin(dp->d_name); + } + } + + closedir(dir); + + void (*game_func)() = dlsym(main_plugin, "game"); + assert(game_func); + + game_func(); +} + |