Subversion Repositories Tewi

Rev

Rev 17 | Rev 25 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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