Subversion Repositories Tewi

Rev

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