Subversion Repositories Tewi

Rev

Rev 219 | Rev 313 | 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 312 2024-10-13 18:17: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__)
312 nishi 30
#ifdef __OS2__
31
#define INCL_DOSMODULEMGR
32
#include <os2.h>
33
#else
17 nishi 34
#include <windows.h>
217 nishi 35
#include <direct.h>
312 nishi 36
#endif
17 nishi 37
#else
38
#include <dlfcn.h>
39
#endif
40
 
41
void* tw_module_load(const char* path) {
42
	char* p = getcwd(NULL, 0);
212 nishi 43
	void* lib;
312 nishi 44
	char tmp[512];
45
	unsigned long l;
17 nishi 46
	chdir(config.server_root);
219 nishi 47
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
312 nishi 48
#ifdef __OS2__
49
	lib = NULL;
50
	l = (unsigned long)lib;
51
	DosLoadModule(tmp, 512, path, &l);
52
#else
17 nishi 53
	lib = LoadLibraryA(path);
312 nishi 54
#endif
17 nishi 55
#else
25 nishi 56
	lib = dlopen(path, RTLD_LAZY);
17 nishi 57
#endif
58
	if(lib == NULL) {
59
		cm_log("Module", "Could not load %s", path);
60
	}
61
	chdir(p);
62
	free(p);
63
	return lib;
64
}
65
 
66
void* tw_module_symbol(void* mod, const char* sym) {
219 nishi 67
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
312 nishi 68
#ifdef __OS2__
69
	void* ret;
70
	DosQueryProcAddr((unsigned long)mod, 0, sym, (PFN*)&ret);
71
	return ret;
72
#else
17 nishi 73
	return GetProcAddress(mod, sym);
312 nishi 74
#endif
17 nishi 75
#else
76
	return dlsym(mod, sym);
77
#endif
78
}
79
 
182 nishi 80
int tw_module_init(void* mod) {
81
	tw_mod_init_t mod_init = (tw_mod_init_t)tw_module_symbol(mod, "mod_init");
82
	if(mod_init == NULL) {
83
		cm_log("Module", "Could not init a module");
84
		return 1;
85
	} else {
86
		struct tw_tool tools;
87
		tw_init_tools(&tools);
88
		return mod_init(&config, &tools);
89
	}
90
}
91
#endif
92
 
18 nishi 93
void tw_add_version(const char* string) {
94
	if(config.extension == NULL) {
95
		config.extension = cm_strcat(" ", string);
96
	} else {
97
		char* tmp = config.extension;
98
		config.extension = cm_strcat3(tmp, " ", string);
99
		free(tmp);
100
	}
101
}
17 nishi 102
 
156 nishi 103
void tw_add_define(const char* string) {
104
	int i;
159 nishi 105
	for(i = 0; config.defined[i] != NULL; i++) {
106
		if(strcmp(config.defined[i], string) == 0) {
107
			return;
108
		}
109
	}
156 nishi 110
	for(i = 0; config.defined[i] != NULL; i++)
111
		;
112
	config.defined[i] = cm_strdup(string);
113
	config.defined[i + 1] = NULL;
114
}
115
 
116
void tw_delete_define(const char* string) {
117
	int i;
118
	for(i = 0; config.defined[i] != NULL; i++) {
119
		if(strcmp(config.defined[i], string) == 0) {
120
			free(config.defined[i]);
121
			for(; config.defined[i] != NULL; i++) {
122
				config.defined[i] = config.defined[i + 1];
123
			}
159 nishi 124
			break;
156 nishi 125
		}
126
	}
127
}
128
 
18 nishi 129
void tw_init_tools(struct tw_tool* tools) {
130
	tools->log = cm_log;
131
	tools->add_version = tw_add_version;
156 nishi 132
	tools->add_define = tw_add_define;
18 nishi 133
}