Subversion Repositories Tewi

Rev

Rev 159 | Rev 187 | 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 182 2024-09-27 12:55:12Z 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
 
156 nishi 12
#include <string.h>
17 nishi 13
#include <unistd.h>
14
#include <stdlib.h>
15
 
182 nishi 16
extern struct tw_config config;
17
 
18
#ifdef _PSP
19
void* tw_module_load(const char* path) { return NULL; }
20
 
21
void* tw_module_symbol(void* mod, const char* sym) { return NULL; }
22
 
23
int tw_module_init(void* mod) { return 1; }
24
 
25
#else
26
 
17 nishi 27
#ifdef __MINGW32__
28
#include <windows.h>
29
#else
30
#include <dlfcn.h>
31
#endif
32
 
33
void* tw_module_load(const char* path) {
34
	char* p = getcwd(NULL, 0);
35
	chdir(config.server_root);
36
	void* lib;
37
#ifdef __MINGW32__
38
	lib = LoadLibraryA(path);
39
#else
25 nishi 40
	lib = dlopen(path, RTLD_LAZY);
17 nishi 41
#endif
42
	if(lib == NULL) {
43
		cm_log("Module", "Could not load %s", path);
44
	}
45
	chdir(p);
46
	free(p);
47
	return lib;
48
}
49
 
50
void* tw_module_symbol(void* mod, const char* sym) {
51
#ifdef __MINGW32__
52
	return GetProcAddress(mod, sym);
53
#else
54
	return dlsym(mod, sym);
55
#endif
56
}
57
 
182 nishi 58
int tw_module_init(void* mod) {
59
	tw_mod_init_t mod_init = (tw_mod_init_t)tw_module_symbol(mod, "mod_init");
60
	if(mod_init == NULL) {
61
		cm_log("Module", "Could not init a module");
62
		return 1;
63
	} else {
64
		struct tw_tool tools;
65
		tw_init_tools(&tools);
66
		return mod_init(&config, &tools);
67
	}
68
}
69
#endif
70
 
18 nishi 71
void tw_add_version(const char* string) {
72
	if(config.extension == NULL) {
73
		config.extension = cm_strcat(" ", string);
74
	} else {
75
		char* tmp = config.extension;
76
		config.extension = cm_strcat3(tmp, " ", string);
77
		free(tmp);
78
	}
79
}
17 nishi 80
 
156 nishi 81
void tw_add_define(const char* string) {
82
	int i;
159 nishi 83
	for(i = 0; config.defined[i] != NULL; i++) {
84
		if(strcmp(config.defined[i], string) == 0) {
85
			return;
86
		}
87
	}
156 nishi 88
	for(i = 0; config.defined[i] != NULL; i++)
89
		;
90
	config.defined[i] = cm_strdup(string);
91
	config.defined[i + 1] = NULL;
92
}
93
 
94
void tw_delete_define(const char* string) {
95
	int i;
96
	for(i = 0; config.defined[i] != NULL; i++) {
97
		if(strcmp(config.defined[i], string) == 0) {
98
			free(config.defined[i]);
99
			for(; config.defined[i] != NULL; i++) {
100
				config.defined[i] = config.defined[i + 1];
101
			}
159 nishi 102
			break;
156 nishi 103
		}
104
	}
105
}
106
 
18 nishi 107
void tw_init_tools(struct tw_tool* tools) {
108
	tools->log = cm_log;
109
	tools->add_version = tw_add_version;
156 nishi 110
	tools->add_define = tw_add_define;
18 nishi 111
}