Subversion Repositories Tewi

Rev

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