1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef __WIN32__
#include <dlfcn.h>
#endif
#include <assert.h>
#include "main.h"
#include "list.h"
#include "tux.h"
#include "shot.h"
#include "modules.h"
#include "arena.h"
#include "arenaFile.h"
#include "proto.h"
#include "shareFunction.h"
#ifndef PUBLIC_SERVER
#include "interface.h"
#include "image.h"
#include "layer.h"
#include "configFile.h"
#endif
static export_fce_t export_fce =
{
#ifndef PUBLIC_SERVER
.fce_addLayer = addLayer,
.fce_getImage = getImage,
#endif
.fce_loadDepModule = loadDepModule,
.fce_addToShareFce = addToShareFce,
.fce_getShareFce = getShareFce,
.fce_getValue = getArenaValue,
.fce_getNetTypeGame = getNetTypeGame,
.fce_getTuxProportion = getTuxProportion,
.fce_setTuxProportion = setTuxProportion,
.fce_actionTux = actionTux,
.fce_getMyTime = getMyTime,
.fce_getCurrentArena = getCurrentArena,
.fce_conflictSpace = conflictSpace,
.fce_isFreeSpace = isFreeSpace,
.fce_findFreeSpace = findFreeSpace,
.fce_proto_send_module_server = proto_send_module_server,
#ifndef PUBLIC_SERVER
.fce_proto_send_module_client = proto_send_module_client,
#endif
.fce_destroyShot = destroyShot,
.fce_boundBombBall = boundBombBall,
.fce_transformOnlyLasser = transformOnlyLasser
};
static list_t *listModule;
static void* getFce(module_t *p, char *s)
{
#ifndef __WIN32__
void *ret;
char *error;
assert(p != NULL);
assert(s != NULL);
ret = dlsym(p->image, s);
if ((error = dlerror()) != NULL) {
fprintf (stderr, _("Error %s occured when using getFce function!\n"), error);
return NULL;
}
#else
FARPROC ret = GetProcAddress((HMODULE)p->image,(LPCSTR)s);
if (!ret)
fprintf(stderr, _("Error %s occured when using getFce function!\n"), s);
#endif
return (void *)ret;
}
static void* mapImage(char *name)
{
#ifndef __WIN32__
void *image;
image = dlopen (name, RTLD_LAZY);
if( image == NULL ) {
fputs (dlerror(), stderr);
return NULL;
}
#else
HINSTANCE image;
image = LoadLibrary((LPCSTR) name);
if (!image) {
LPVOID msg;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &msg,
0,
NULL
);
fputs(msg, stderr);
free(msg);
FreeLibrary(image);
return NULL;
}
//FreeLibrary(image); //never ever untag this
#endif
return image;
}
static void unmapImage(void *image)
{
#ifndef __WIN32__
dlclose(image);
#else
FreeLibrary(image);
#endif
}
static void setModulePath(char *name, char *path)
{
sprintf(path, PATH_MODULES "%s" MODULE_TYPE_UNIX, name); // linux
#ifdef __WIN32__
sprintf(path, PATH_MODULES "%s" MODULE_TYPE_WIN, name); // windows
#endif
#ifdef APPLE
sprintf(path, PATH_MODULES "%s" MODULE_TYPE_APPLE, name); // apple
#endif
}
static module_t* newModule(char *name)
{
char path[STR_PATH_SIZE];
void *image;
module_t *ret;
assert(name != NULL);
setModulePath(name, path);
image = mapImage(path);
if (image == NULL) {
fprintf(stderr, _("Unable to map file %s!\n"), name);
return NULL;
}
ret = malloc(sizeof(module_t));
ret->image = image;
ret->name = strdup(name);
ret->fce_init = getFce(ret, "init");
#ifndef PUBLIC_SERVER
ret->fce_draw = getFce(ret, "draw");
#endif
ret->fce_event = getFce(ret, "event");
ret->fce_destroy = getFce(ret, "destroy");
ret->fce_isConflict = getFce(ret, "isConflict");
ret->fce_cmd = getFce(ret, "cmdArena");
ret->fce_recvMsg = getFce(ret, "recvMsg");
#ifdef DEBUG
printf(_("Loading module: \"%s\"\n"), name);
#endif
if (ret->fce_init(&export_fce) != 0) {
fprintf(stderr, _("Failed initialization of module \"%s\""), name);
unmapImage(image);
free(ret->name);
free(ret);
return NULL;
}
return ret;
}
static int destroyModule(module_t *p)
{
assert( p != NULL );
p->fce_destroy();
unmapImage(p->image);
free(p->name);
free(p);
#ifdef DEBUG
printf(_("Destroing module...\n"));
#endif
return 0;
}
void initModule()
{
listModule = newList();
initShareFunction();
}
int isModuleLoaded(char *name)
{
int i;
for (i = 0; i < listModule->count; i++) {
module_t *this;
this = (module_t *)listModule->list[i];
if (strcmp(this->name, name) == 0)
return 1;
}
return 0;
}
int loadModule(char *name)
{
module_t *module;
if (isModuleLoaded(name)) {
fprintf(stderr, _("I've done this once, dont want to mess memory with module %s again!\n"), name);
return -1;
}
module = newModule(name);
if (module == NULL) {
fprintf(stderr, _("Loading module %s failed!\n"), name);
return -1;
}
addList(listModule, module);
return 0;
}
int loadDepModule(char *name)
{
if (isModuleLoaded(name))
return 0;
return loadModule(name);
}
void cmdModule(char *s)
{
int i;
for (i = 0; i < listModule->count; i++) {
module_t *this;
this = (module_t *)listModule->list[i];
this->fce_cmd(s);
}
}
#ifndef PUBLIC_SERVER
void drawModule(int x, int y, int w, int h)
{
int i;
for (i = 0; i < listModule->count; i++ ) {
module_t *this;
this = (module_t *)listModule->list[i];
this->fce_draw(x, y, w, h);
}
}
#endif
void eventModule()
{
int i;
for (i = 0; i < listModule->count; i++ ) {
module_t *this;
this = (module_t *)listModule->list[i];
this->fce_event();
}
}
int isConflictModule(int x, int y, int w, int h)
{
int i;
for (i = 0; i < listModule->count; i++) {
module_t *this;
this = (module_t *)listModule->list[i];
if (this->fce_isConflict(x, y, w, h) == 1)
return 1;
}
return 0;
}
int recvMsgModule(char *msg)
{
int i;
for (i = 0; i < listModule->count; i++) {
module_t *this;
this = (module_t *)listModule->list[i];
this->fce_recvMsg(msg);
}
return 0;
}
void quitModule()
{
destroyListItem(listModule, destroyModule);
quitShareFunction();
}
|