Subversion Repositories Tewi

Rev

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