Subversion Repositories Tewi

Rev

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