aboutsummaryrefslogtreecommitdiff
path: root/dungeon.c
blob: a1b00645a8b580f3d6f8e714f12bad09bbebf12c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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();
}