summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/base/arenaFile.c7
-rw-r--r--src/base/modules.c133
-rw-r--r--src/base/modules.h27
-rw-r--r--src/modules/modAI.c34
-rw-r--r--src/modules/modBasic.c23
-rw-r--r--src/modules/modMove.c22
-rw-r--r--src/modules/modPipe.c22
-rw-r--r--src/modules/modTeleport.c26
-rw-r--r--src/modules/modWall.c32
9 files changed, 191 insertions, 135 deletions
diff --git a/src/base/arenaFile.c b/src/base/arenaFile.c
index c494e1f..b5ef6a2 100644
--- a/src/base/arenaFile.c
+++ b/src/base/arenaFile.c
@@ -362,6 +362,8 @@ static void load_arenaFromDirector(char *director)
exit(-1);
}
+ unsigned l = strlen(director) - 1;
+
for (i = 0; i < p->list->count; i++) {
char *line;
@@ -369,8 +371,9 @@ static void load_arenaFromDirector(char *director)
if (strstr(line, ".zip") != NULL && strstr(line, "~") == NULL) {
char path[STR_PATH_SIZE];
-
- if (director[strlen(director)-1] == PATH_SEPARATOR) {
+
+
+ if (director[l] == (char) PATH_SEPARATOR[0]) {
sprintf(path, "%s%s", director, line);
} else {
sprintf(path, "%s%s%s", director, PATH_SEPARATOR, line);
diff --git a/src/base/modules.c b/src/base/modules.c
index 4290d79..84501dc 100644
--- a/src/base/modules.c
+++ b/src/base/modules.c
@@ -1,9 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#ifndef __WIN32__
-#include <dlfcn.h>
-#endif /* __WIN32__ */
#include <assert.h>
#include "main.h"
@@ -54,72 +51,40 @@ static export_fce_t export_fce = {
.fce_shot_bound_bombBall = shot_bound_bombBall,
.fce_shot_transform_lasser = shot_transform_lasser
};
+
static list_t *listModule;
+static mod_reg_t mod_reglist;
-static void *getFce(module_t *p, char *s)
+/* register module as valid */
+static mod_reg_t *mod_register (char *name, mod_sym_t *sym)
{
-#ifndef __WIN32__
- void *ret;
- char *error;
+ /* alloc and init context */
+ mod_reg_t *mod = (mod_reg_t *) malloc (sizeof (mod_reg_t));
+
+ if (!mod)
+ return 0;
- assert(p != NULL);
- assert(s != NULL);
- ret = dlsym(p->image, s);
+ 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;
- if ((error = dlerror()) != NULL) {
- error("Use of getFce function failed: %s", error);
- return NULL;
- }
-#else /* __WIN32__ */
- FARPROC ret = GetProcAddress((HMODULE) p->image, (LPCSTR) s);
- if (!ret) {
- error("Use of getFce function failed: %s", s);
- }
-#endif /* __WIN32__ */
-
- return (void *) ret;
+ return mod;
}
-static void *mapImage(char *name)
+static mod_reg_t *mod_find (char *name)
{
-#ifndef __WIN32__
- void *image;
- image = dlopen(name, RTLD_LAZY);
-
- if (image == NULL) {
- fputs(dlerror(), stderr);
- return NULL;
- }
-#else /* __WIN32__ */
- 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;
+ mod_reg_t *mod;
+ for (mod = mod_reglist.next; mod != &mod_reglist; mod = mod->next) {
+ if (!strcmp (mod->name, name))
+ return mod;
}
- /*FreeLibrary(image);*/ /* never ever uncomment this */
-#endif /* __WIN32__ */
- return image;
-}
-static void unmapImage(void *image)
-{
-#ifndef __WIN32__
- dlclose(image);
-#else /* __WIN32__ */
- FreeLibrary(image);
-#endif /* __WIN32__ */
+ return 0;
}
static void setModulePath(char *name, char *path)
@@ -130,36 +95,41 @@ static void setModulePath(char *name, char *path)
static module_t *newModule(char *name)
{
char path[STR_PATH_SIZE];
- void *image;
+
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);
- image = mapImage(path);
- if (image == NULL) {
- error("Unable to map image of new module [%s]", name);
- return NULL;
- }
+ ret = malloc (sizeof (module_t));
+
+ if (!ret)
+ return 0;
+
+ ret->name = strdup (name);
- ret = malloc(sizeof(module_t));
- ret->image = image;
- ret->name = strdup(name);
- ret->fce_init = getFce(ret, "init");
+ ret->fce_init = mod->sym->init;
#ifndef PUBLIC_SERVER
- ret->fce_draw = getFce(ret, "draw");
+ ret->fce_draw = mod->sym->draw;
#endif /* PUBLIC_SERVER */
- 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");
+ 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);
- unmapImage(image);
free(ret->name);
free(ret);
return NULL;
@@ -175,7 +145,6 @@ static int destroyModule(module_t *p)
debug("Unloading module [%s]", p->name);
p->fce_destroy();
- unmapImage(p->image);
free(p->name);
free(p);
@@ -186,6 +155,16 @@ 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)
diff --git a/src/base/modules.h b/src/base/modules.h
index f6cca42..1bda810 100644
--- a/src/base/modules.h
+++ b/src/base/modules.h
@@ -78,6 +78,33 @@ typedef struct module_s {
} module_t;
+/* module's call list */
+typedef struct {
+ void *init;
+ void *draw;
+ void *event;
+ void *isConflict;
+ void *cmdArena;
+ void *recvMsg;
+ void *destroy;
+} mod_sym_t;
+
+/* structure of the registered modules */
+typedef struct mod_context {
+ struct mod_context *next, *prev;
+
+ char *name;
+ mod_sym_t *sym;
+} mod_reg_t;
+
+/* available modules */
+extern mod_sym_t modai_sym;
+extern mod_sym_t modwall_sym;
+extern mod_sym_t modpipe_sym;
+extern mod_sym_t modmove_sym;
+extern mod_sym_t modbasic_sym;
+extern mod_sym_t modteleport_sym;
+
extern void module_init();
extern int module_load(char *name);
extern int module_load_dep(char *name);
diff --git a/src/modules/modAI.c b/src/modules/modAI.c
index 57fe96d..13d1419 100644
--- a/src/modules/modAI.c
+++ b/src/modules/modAI.c
@@ -27,7 +27,7 @@ typedef struct alternative_struct {
int x, y;
} alternative_t;
-alternative_t *newAlternative(int route, int x, int y)
+static alternative_t *newAlternative(int route, int x, int y)
{
alternative_t *new;
@@ -41,7 +41,7 @@ alternative_t *newAlternative(int route, int x, int y)
return new;
}
-alternative_t *cloneAlternative(alternative_t *p, int route, int x, int y)
+static alternative_t *cloneAlternative(alternative_t *p, int route, int x, int y)
{
alternative_t *new;
@@ -57,7 +57,7 @@ alternative_t *cloneAlternative(alternative_t *p, int route, int x, int y)
return new;
}
-void forkAlternative(list_t *list, alternative_t *p, int w, int h)
+static void forkAlternative(list_t *list, alternative_t *p, int w, int h)
{
int x, y;
@@ -88,7 +88,7 @@ void forkAlternative(list_t *list, alternative_t *p, int w, int h)
}
-void moveAlternative(alternative_t *p, int offset)
+static void moveAlternative(alternative_t *p, int offset)
{
assert(p != NULL);
@@ -112,7 +112,7 @@ void moveAlternative(alternative_t *p, int offset)
}
}
-void destroyAlternative(alternative_t *p)
+static void destroyAlternative(alternative_t *p)
{
assert(p != NULL);
free(p);
@@ -123,7 +123,7 @@ static void cmd_ai(char *line)
UNUSED(line);
}
-int init(export_fce_t *p)
+static int init(export_fce_t *p)
{
export_fce = p;
@@ -131,7 +131,7 @@ int init(export_fce_t *p)
}
#ifndef PUBLIC_SERVER
-int draw(int x, int y, int w, int h)
+static int draw(int x, int y, int w, int h)
{
UNUSED(x);
UNUSED(y);
@@ -142,7 +142,7 @@ int draw(int x, int y, int w, int h)
}
#endif
-tux_t *findOtherTux(space_t *space)
+static tux_t *findOtherTux(space_t *space)
{
int i;
@@ -343,7 +343,7 @@ static void action_tuxAI(space_t *space, tux_t *tux, void *p)
}
}
-int event()
+static int event()
{
static my_time_t lastEvent = 0;
my_time_t curentTime;
@@ -391,7 +391,7 @@ int event()
return 0;
}
-int isConflict(int x, int y, int w, int h)
+static int isConflict(int x, int y, int w, int h)
{
UNUSED(x);
UNUSED(y);
@@ -401,18 +401,26 @@ int isConflict(int x, int y, int w, int h)
return 0;
}
-void cmdArena(char *line)
+static void cmdArena(char *line)
{
if (strncmp(line, "ai", 2) == 0)
cmd_ai(line);
}
-void recvMsg(char *msg)
+static void recvMsg(char *msg)
{
UNUSED(msg);
}
-int destroy()
+static int destroy()
{
return 0;
}
+
+mod_sym_t modai_sym = { &init,
+ &draw,
+ &event,
+ &isConflict,
+ &cmdArena,
+ &recvMsg,
+ &destroy }; \ No newline at end of file
diff --git a/src/modules/modBasic.c b/src/modules/modBasic.c
index a91b7f6..1b5aa41 100644
--- a/src/modules/modBasic.c
+++ b/src/modules/modBasic.c
@@ -20,7 +20,7 @@
static export_fce_t *export_fce;
-int init(export_fce_t *p)
+static int init(export_fce_t *p)
{
export_fce = p;
@@ -29,8 +29,7 @@ int init(export_fce_t *p)
}
#ifndef PUBLIC_SERVER
-
-int draw(int x, int y, int w, int h)
+static int draw(int x, int y, int w, int h)
{
UNUSED(x);
UNUSED(y);
@@ -42,13 +41,13 @@ int draw(int x, int y, int w, int h)
}
#endif
-int event()
+static int event()
{
debug("Basic module catch event");
return 0;
}
-int isConflict(int x, int y, int w, int h)
+static int isConflict(int x, int y, int w, int h)
{
debug("isConflict(%d, %d, %d, %d) in Basic module", x, y, w, h);
return 0;
@@ -59,19 +58,27 @@ static void cmd_basic(char *line)
debug("cmd_basic(%s) in Basic module", line);
}
-void cmdArena(char *line)
+static void cmdArena(char *line)
{
if (strncmp(line, "basic", 5) == 0)
cmd_basic(line);
}
-void recvMsg(char *msg)
+static void recvMsg(char *msg)
{
debug("recvMsg(%s) in Basic module", msg);
}
-int destroy()
+static int destroy()
{
debug("Destroying Basic module");
return 0;
}
+
+mod_sym_t modbasic_sym = { &init,
+ &draw,
+ &event,
+ &isConflict,
+ &cmdArena,
+ &recvMsg,
+ &destroy };
diff --git a/src/modules/modMove.c b/src/modules/modMove.c
index 7c7a862..95157f9 100644
--- a/src/modules/modMove.c
+++ b/src/modules/modMove.c
@@ -160,7 +160,7 @@ static void move_shot(shot_t *shot, int position, int src_x, int src_y, int dist
}
}
-int init(export_fce_t *p)
+static int init(export_fce_t *p)
{
export_fce = p;
export_fce->fce_share_function_add("move_tux", (void *) move_tux);
@@ -216,7 +216,7 @@ static void proto_moveshot(char *msg)
}
#ifndef PUBLIC_SERVER
-int draw(int x, int y, int w, int h)
+static int draw(int x, int y, int w, int h)
{
UNUSED(x);
UNUSED(y);
@@ -227,12 +227,12 @@ int draw(int x, int y, int w, int h)
}
#endif
-int event()
+static int event()
{
return 0;
}
-int isConflict(int x, int y, int w, int h)
+static int isConflict(int x, int y, int w, int h)
{
UNUSED(x);
UNUSED(y);
@@ -242,12 +242,12 @@ int isConflict(int x, int y, int w, int h)
return 0;
}
-void cmdArena(char *line)
+static void cmdArena(char *line)
{
UNUSED(line);
}
-void recvMsg(char *msg)
+static void recvMsg(char *msg)
{
if (export_fce->fce_net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) {
return;
@@ -259,7 +259,15 @@ void recvMsg(char *msg)
proto_moveshot(msg);
}
-int destroy()
+static int destroy()
{
return 0;
}
+
+mod_sym_t modmove_sym = { &init,
+ &draw,
+ &event,
+ &isConflict,
+ &cmdArena,
+ &recvMsg,
+ &destroy };
diff --git a/src/modules/modPipe.c b/src/modules/modPipe.c
index dea5ea1..d2e9fec 100644
--- a/src/modules/modPipe.c
+++ b/src/modules/modPipe.c
@@ -188,7 +188,7 @@ static void moveShotFromPipe(shot_t *shot, pipe_t *pipe)
fce_move_shot(shot, distPipe->position, pipe->x, pipe->y, distPipe->x, distPipe->y, distPipe->w, distPipe->h);
}
-int init(export_fce_t *p)
+static int init(export_fce_t *p)
{
export_fce = p;
@@ -214,7 +214,7 @@ static void action_drawpipe(space_t *space, pipe_t *pipe, void *p)
drawPipe(pipe);
}
-int draw(int x, int y, int w, int h)
+static int draw(int x, int y, int w, int h)
{
if (spacePipe == NULL) {
return 0;
@@ -289,7 +289,7 @@ static void action_eventshot(space_t *space, shot_t *shot, space_t *p_spacePipe)
}
}
-int event()
+static int event()
{
if (spacePipe == NULL) {
return 0;
@@ -305,7 +305,7 @@ int event()
return 0;
}
-int isConflict(int x, int y, int w, int h)
+static int isConflict(int x, int y, int w, int h)
{
if (spacePipe == NULL) {
return 0;
@@ -314,22 +314,30 @@ int isConflict(int x, int y, int w, int h)
return space_is_conflict_with_object(spacePipe, x, y, w, h);
}
-void cmdArena(char *line)
+static void cmdArena(char *line)
{
if (strncmp(line, "pipe", 4) == 0) {
cmd_teleport(line);
}
}
-void recvMsg(char *msg)
+static void recvMsg(char *msg)
{
UNUSED(msg);
}
-int destroy()
+static int destroy()
{
space_destroy_with_item(spacePipe, destroyPipe);
list_destroy(listPipe);
return 0;
}
+
+mod_sym_t modpipe_sym = { &init,
+ &draw,
+ &event,
+ &isConflict,
+ &cmdArena,
+ &recvMsg,
+ &destroy }; \ No newline at end of file
diff --git a/src/modules/modTeleport.c b/src/modules/modTeleport.c
index 289f553..0285ca0 100644
--- a/src/modules/modTeleport.c
+++ b/src/modules/modTeleport.c
@@ -49,9 +49,9 @@ static void (*fce_move_shot) (shot_t *shot, int position, int src_x,
int dist_h);
#ifndef PUBLIC_SERVER
-teleport_t *newTeleport(int x, int y, int w, int h, int layer, image_t *img)
+static teleport_t *newTeleport(int x, int y, int w, int h, int layer, image_t *img)
#else
-teleport_t *newTeleport(int x, int y, int w, int h, int layer)
+static teleport_t *newTeleport(int x, int y, int w, int h, int layer)
#endif
{
static int last_id = 0;
@@ -218,7 +218,7 @@ static void moveShotFromTeleport(shot_t *shot, teleport_t *teleport, space_t *sp
distTeleport->h);
}
-int init(export_fce_t *p)
+static int init(export_fce_t *p)
{
export_fce = p;
@@ -248,7 +248,7 @@ static void action_drawteleport(space_t *space, teleport_t *teleport, void *p)
drawTeleport(teleport);
}
-int draw(int x, int y, int w, int h)
+static int draw(int x, int y, int w, int h)
{
if (spaceTeleport == NULL) {
return 0;
@@ -304,7 +304,7 @@ static void action_eventtux(space_t *space, tux_t *tux, space_t *p_spaceTeleport
space_action_from_location(p_spaceTeleport, action_eventteleporttux, tux, x, y, w, h);
}
-int event()
+static int event()
{
if (spaceTeleport == NULL) {
return 0;
@@ -320,7 +320,7 @@ int event()
return 0;
}
-int isConflict(int x, int y, int w, int h)
+static int isConflict(int x, int y, int w, int h)
{
UNUSED(x);
UNUSED(y);
@@ -334,20 +334,28 @@ int isConflict(int x, int y, int w, int h)
return 0;
}
-void cmdArena(char *line)
+static void cmdArena(char *line)
{
if (strncmp(line, "teleport", 8) == 0)
cmd_teleport(line);
}
-void recvMsg(char *msg)
+static void recvMsg(char *msg)
{
UNUSED(msg);
}
-int destroy()
+static int destroy()
{
space_destroy_with_item(spaceTeleport, destroyTeleport);
list_destroy(listTeleport);
return 0;
}
+
+mod_sym_t modteleport_sym = { &init,
+ &draw,
+ &event,
+ &isConflict,
+ &cmdArena,
+ &recvMsg,
+ &destroy }; \ No newline at end of file
diff --git a/src/modules/modWall.c b/src/modules/modWall.c
index bb451e0..2091c5e 100644
--- a/src/modules/modWall.c
+++ b/src/modules/modWall.c
@@ -52,9 +52,9 @@ static space_t *spaceImgWall;
static list_t *listWall;
#ifndef PUBLIC_SERVER
-wall_t *newWall(int x, int y, int w, int h, int img_x, int img_y, int layer, image_t *img)
+static wall_t *newWall(int x, int y, int w, int h, int img_x, int img_y, int layer, image_t *img)
#else
-wall_t *newWall(int x, int y, int w, int h, int img_x, int img_y, int layer)
+static wall_t *newWall(int x, int y, int w, int h, int img_x, int img_y, int layer)
#endif
{
static int last_id = 0;
@@ -83,7 +83,7 @@ wall_t *newWall(int x, int y, int w, int h, int img_x, int img_y, int layer)
#ifndef PUBLIC_SERVER
-void drawWall(wall_t *p)
+static void drawWall(wall_t *p)
{
assert(p != NULL);
@@ -94,7 +94,7 @@ void drawWall(wall_t *p)
p->layer);
}
-void drawListWall(list_t *list)
+static void drawListWall(list_t *list)
{
wall_t *thisWall;
int i;
@@ -110,7 +110,7 @@ void drawListWall(list_t *list)
#endif
-void destroyWall(wall_t *p)
+static void destroyWall(wall_t *p)
{
assert(p != NULL);
free(p);
@@ -242,7 +242,7 @@ static void cmd_wall(char *line)
#endif
}
-int init(export_fce_t *p)
+static int init(export_fce_t *p)
{
export_fce = p;
listWall = list_new();
@@ -259,7 +259,7 @@ static void action_drawwall(space_t *space, wall_t *wall, void *p)
drawWall(wall);
}
-int draw(int x, int y, int w, int h)
+static int draw(int x, int y, int w, int h)
{
if (spaceWall == NULL) {
return 0;
@@ -312,7 +312,7 @@ static void action_eventshot(space_t *space, shot_t *shot, space_t *p_spaceWall)
}
}
-int event()
+static int event()
{
if (spaceWall == NULL) {
return 0;
@@ -324,7 +324,7 @@ int event()
return 0;
}
-int isConflict(int x, int y, int w, int h)
+static int isConflict(int x, int y, int w, int h)
{
if (spaceWall == NULL) {
return 0;
@@ -333,19 +333,19 @@ int isConflict(int x, int y, int w, int h)
return space_is_conflict_with_object(spaceWall, x, y, w, h);
}
-void cmdArena(char *line)
+static void cmdArena(char *line)
{
if (strncmp(line, "wall", 4) == 0) {
cmd_wall(line);
}
}
-void recvMsg(char *msg)
+static void recvMsg(char *msg)
{
UNUSED(msg);
}
-int destroy()
+static int destroy()
{
space_destroy_with_item(spaceWall, destroyWall);
#ifndef PUBLIC_SERVER
@@ -354,3 +354,11 @@ int destroy()
list_destroy(listWall);
return 0;
}
+
+mod_sym_t modwall_sym = { &init,
+ &draw,
+ &event,
+ &isConflict,
+ &cmdArena,
+ &recvMsg,
+ &destroy }; \ No newline at end of file