Subversion Repositories Tewi

Rev

Rev 18 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 nishi 1
/* $Id: module.c 17 2024-09-13 17:41:07Z nishi $ */
2
 
3
#include "tw_module.h"
4
 
5
#include "tw_config.h"
6
 
7
#include <cm_string.h>
8
#include <cm_log.h>
9
 
10
#include <unistd.h>
11
#include <stdlib.h>
12
 
13
#ifdef __MINGW32__
14
#include <windows.h>
15
#else
16
#include <dlfcn.h>
17
#endif
18
 
19
extern struct tw_config config;
20
 
21
void* tw_module_load(const char* path) {
22
	char* p = getcwd(NULL, 0);
23
	chdir(config.server_root);
24
	void* lib;
25
#ifdef __MINGW32__
26
	lib = LoadLibraryA(path);
27
#else
28
	lib = dlopen(path, DL_LAZY);
29
#endif
30
	if(lib == NULL) {
31
		cm_log("Module", "Could not load %s", path);
32
	}
33
	chdir(p);
34
	free(p);
35
	return lib;
36
}
37
 
38
void* tw_module_symbol(void* mod, const char* sym) {
39
#ifdef __MINGW32__
40
	return GetProcAddress(mod, sym);
41
#else
42
	return dlsym(mod, sym);
43
#endif
44
}
45
 
46
void tw_init_tools(struct tw_tool* tools) { tools->log = cm_log; }
47
 
48
int tw_module_init(void* mod) {
49
	tw_mod_init_t mod_init = (tw_mod_init_t)tw_module_symbol(mod, "mod_init");
50
	if(mod_init == NULL) {
51
		cm_log("Module", "Could not init a module");
52
		return 1;
53
	} else {
54
		struct tw_tool tools;
55
		tw_init_tools(&tools);
56
		return mod_init(&config, &tools);
57
	}
58
}