Subversion Repositories Tewi

Rev

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