]> git.lizzy.rs Git - dungeon_game.git/blob - dungeon.c
Include loader binary in .gitignore
[dungeon_game.git] / dungeon.c
1 #include <stdio.h>
2 #include <dlfcn.h>
3 #include <stdlib.h>
4 #include <assert.h>
5 #include <dirent.h>
6 #include <string.h>
7
8 static void *load_plugin(const char *name)
9 {
10         size_t len = strlen(name);
11         char filename[1 + 1 + 7 + 1 + len + 1 + len + 1 + 2 + 1];
12         sprintf(filename, "./plugins/%s/%s.so", name, name);
13
14         void *plugin_handle = dlmopen(LM_ID_BASE, filename, RTLD_NOW | RTLD_GLOBAL);
15
16         if (! plugin_handle) {
17                 printf("%s\n", dlerror());
18                 exit(EXIT_FAILURE);
19         }
20
21         return plugin_handle;
22 }
23
24 int main()
25 {
26         void *main_plugin = load_plugin("game");
27
28         DIR *dir = opendir("plugins");
29         assert(dir);
30
31         struct dirent *dp;
32
33         while (dp = readdir(dir)) {
34                 if (dp->d_name[0] != '.' && strcmp(dp->d_name, "game") != 0) {
35                         load_plugin(dp->d_name);
36                 }
37         }
38
39         closedir(dir);
40
41         void (*game_func)() = dlsym(main_plugin, "game");
42         assert(game_func);
43
44         game_func();
45 }
46