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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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 /* PUBLIC_SERVER */
static export_fce_t export_fce = {
#ifndef PUBLIC_SERVER
.fce_addLayer = addLayer,
.fce_image_get = image_get,
#endif /* PUBLIC_SERVER */
.fce_module_load_dep = module_load_dep,
.fce_share_function_add = share_function_add,
.fce_share_function_get = share_function_get,
.fce_getValue = arena_file_get_value,
.fce_net_multiplayer_get_game_type = net_multiplayer_get_game_type,
.fce_tux_get_proportion = tux_get_proportion,
.fce_tux_set_proportion = tux_set_proportion,
.fce_tux_action = tux_action,
.fce_timer_get_current_time = timer_get_current_time,
.fce_arena_get_current = arena_get_current,
.fce_arena_conflict_space = arena_conflict_space,
.fce_arena_is_free_space = arena_is_free_space,
.fce_arena_find_free_space = arena_find_free_space,
.fce_proto_send_module_server = proto_send_module_server,
.fce_proto_send_del_server = proto_send_del_server,
#ifndef PUBLIC_SERVER
.fce_proto_send_module_client = proto_send_module_client,
#endif /* PUBLIC_SERVER */
.fce_shot_destroy = shot_destroy,
.fce_shot_bound_bombBall = shot_bound_bombBall,
.fce_shot_transform_lasser = shot_transform_lasser
};
static list_t *listModule;
static mod_reg_t mod_reglist;
/* register module as valid */
static mod_reg_t *mod_register (char *name, mod_sym_t *sym)
{
/* alloc and init context */
mod_reg_t *mod = (mod_reg_t *) malloc (sizeof (mod_reg_t));
if (!mod)
return 0;
mod->name = strdup (name);
mod->sym = sym;
/* add entry into list */
mod->next = &mod_reglist;
mod->prev = mod_reglist.prev;
mod->prev->next = mod;
mod->next->prev = mod;
return mod;
}
static mod_reg_t *mod_find (char *name)
{
mod_reg_t *mod;
for (mod = mod_reglist.next; mod != &mod_reglist; mod = mod->next) {
if (!strcmp (mod->name, name))
return mod;
}
return 0;
}
static void setModulePath(char *name, char *path)
{
sprintf(path, PATH_MODULES "%s" MODULE_TYPE, name);
}
static module_t *newModule(char *name)
{
char path[STR_PATH_SIZE];
module_t *ret;
assert(name != NULL);
mod_reg_t *mod = mod_find (name);
if (!mod) {
error ("Module [%s] is unavailable", name);
return 0;
}
setModulePath(name, path);
ret = malloc (sizeof (module_t));
if (!ret)
return 0;
ret->name = strdup (name);
ret->fce_init = mod->sym->init;
#ifndef PUBLIC_SERVER
ret->fce_draw = mod->sym->draw;
#endif /* PUBLIC_SERVER */
ret->fce_event = mod->sym->event;
ret->fce_destroy = mod->sym->destroy;
ret->fce_isConflict = mod->sym->isConflict;
ret->fce_cmd = mod->sym->cmdArena;
ret->fce_recvMsg = mod->sym->recvMsg;
debug("Loading module [%s]", name);
if (ret->fce_init(&export_fce) != 0) {
error("Unable to load module [%s]", name);
free(ret->name);
free(ret);
return NULL;
}
return ret;
}
static int destroyModule(module_t *p)
{
assert(p != NULL);
debug("Unloading module [%s]", p->name);
p->fce_destroy();
free(p->name);
free(p);
return 0;
}
void module_init()
{
listModule = list_new();
share_function_init();
mod_reglist.next = &mod_reglist;
mod_reglist.prev = &mod_reglist;
mod_register ("libmodAI", &modai_sym);
mod_register ("libmodWall", &modwall_sym);
mod_register ("libmodPipe", &modpipe_sym);
mod_register ("libmodMove", &modmove_sym);
mod_register ("libmodBasic", &modbasic_sym);
mod_register ("libmodTeleport", &modteleport_sym);
}
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 module_load(char *name)
{
module_t *module;
if (isModuleLoaded(name)) {
error("Unable to load already loaded module [%s]", name);
return -1;
}
module = newModule(name);
if (module == NULL) {
error("Unable to load module [%s]", name);
return -1;
}
list_add(listModule, module);
return 0;
}
int module_load_dep(char *name)
{
if (isModuleLoaded(name)) {
return 0;
}
return module_load(name);
}
void module_cmd(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 module_draw(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 /* PUBLIC_SERVER */
void module_event()
{
int i;
for (i = 0; i < listModule->count; i++) {
module_t *this;
this = (module_t *) listModule->list[i];
this->fce_event();
}
}
int module_is_conflict(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 module_recv_msg(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 module_quit()
{
list_destroy_item(listModule, destroyModule);
share_function_quit();
}
|