summaryrefslogtreecommitdiff
path: root/src/base
diff options
context:
space:
mode:
authororoborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e>2008-07-28 16:33:48 +0000
committeroroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e>2008-07-28 16:33:48 +0000
commit554234f4f238305ee82504bdbb51096efaea74f0 (patch)
treed3ca5826ff970de0abbcb96a370a8adcac80744d /src/base
parentf1fca3a63e37866dbcc84ffbf5927697fc60c263 (diff)
- moduly si medzi sebou mozu exportovat a imporotvat funkcie
- na premiestnovanie striel a tuxov v moduloch modPipe a modTetelport sa pouzivaju funkcie z modulu modMove - uhladeny kod v module.c ;) git-svn-id: http://opensvn.csie.org/tuxanci_ng@176 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e
Diffstat (limited to 'src/base')
-rw-r--r--src/base/exportFunction.c73
-rw-r--r--src/base/exportFunction.h12
-rwxr-xr-xsrc/base/modules.c130
-rw-r--r--src/base/modules.h6
4 files changed, 187 insertions, 34 deletions
diff --git a/src/base/exportFunction.c b/src/base/exportFunction.c
new file mode 100644
index 0000000..955c0e1
--- /dev/null
+++ b/src/base/exportFunction.c
@@ -0,0 +1,73 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#include "main.h"
+#include "list.h"
+#include "exportFunction.h"
+
+typedef struct export_fce_item_struct
+{
+ char *name;
+ void *function;
+} export_fce_item_t;
+
+static list_t *listExportFce;
+
+static export_fce_item_t* newExportFceItem(char *name, void *function)
+{
+ export_fce_item_t *new;
+
+ new = malloc( sizeof(export_fce_item_t) );
+ new->name = strdup(name);
+ new->function = function;
+
+ return new;
+}
+
+static void destroyExportFce(export_fce_item_t *p)
+{
+ free(p->name);
+ free(p);
+}
+
+void initExortFunction()
+{
+ listExportFce = newList();
+}
+
+void addToExportFce(char *name, void *function)
+{
+ printf("add to export function %s\n", name);
+
+ addList(listExportFce, newExportFceItem(name, function) );
+}
+
+void* getExportFce(char *name)
+{
+ int i;
+
+ for( i = 0 ; i < listExportFce->count ; i++ )
+ {
+ export_fce_item_t *this;
+
+ this = (export_fce_item_t *)listExportFce->list[i];
+
+ if( strcmp(this->name, name) == 0 )
+ {
+ return this->function;
+ }
+ }
+
+ return NULL;
+}
+
+void quitExportFunction()
+{
+ destroyListItem(listExportFce, destroyExportFce);
+}
+
+
+
diff --git a/src/base/exportFunction.h b/src/base/exportFunction.h
new file mode 100644
index 0000000..5f3f1d0
--- /dev/null
+++ b/src/base/exportFunction.h
@@ -0,0 +1,12 @@
+
+#ifndef EXPORT_FUNCTION_H
+
+#define EXPORT_FUNCTION_H
+
+extern void initExortFunction();
+extern void addToExportFce(char *name, void *function);
+extern void* getExportFce(char *name);
+extern void quitExportFunction();
+
+#endif
+
diff --git a/src/base/modules.c b/src/base/modules.c
index fb692cf..e84cd30 100755
--- a/src/base/modules.c
+++ b/src/base/modules.c
@@ -15,6 +15,7 @@
#include "arena.h"
#include "arenaFile.h"
#include "proto.h"
+#include "exportFunction.h"
#ifndef PUBLIC_SERVER
#include "interface.h"
@@ -29,6 +30,10 @@ static export_fce_t export_fce =
.fce_addLayer = addLayer,
.fce_getImage = getImage,
#endif
+ .fce_loadDepModule = loadDepModule,
+ .fce_addToExportFce = addToExportFce,
+ .fce_getExportFce = getExportFce,
+
.fce_getValue = getArenaValue,
.fce_getNetTypeGame = getNetTypeGame,
.fce_getTuxProportion = getTuxProportion,
@@ -81,25 +86,22 @@ static void* getFce(module_t *p, char *s)
return (void *)ret;
}
-static module_t* newModule(char *name)
+static void* mapImage(char *name)
{
- module_t *ret;
-
- assert( name != NULL );
+ void *image;
- ret = malloc( sizeof(module_t) );
#ifndef __WIN32__
- ret->image = dlopen (name, RTLD_LAZY);
+ image = dlopen (name, RTLD_LAZY);
- if(!ret->image)
+ if( image == NULL )
{
fputs (dlerror(), stderr);
- free(ret);
return NULL;
}
#else
HINSTANCE image;
image = LoadLibrary((LPCSTR) name);
+
if (!image)
{
LPVOID msg;
@@ -120,10 +122,53 @@ static module_t* newModule(char *name)
FreeLibrary(image);
return NULL;
}
- ret->image = image;
//FreeLibrary(image); //to tu nema bejt
#endif
- ret->file = strdup(name);
+
+ 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, "I dont map %s file !\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
@@ -140,13 +185,10 @@ static module_t* newModule(char *name)
if( ret->fce_init(&export_fce) != 0 )
{
fprintf(stderr, "init module failed !\n");
-#ifndef __WIN32__
- dlclose(ret->image);
-#else
- FreeLibrary(ret->image);
-#endif
- free(ret->file);
+ unmapImage(image);
+ free(ret->name);
free(ret);
+
return NULL;
}
@@ -158,13 +200,8 @@ static int destroyModule(module_t *p)
assert( p != NULL );
p->fce_destroy();
-
- free(p->file);
-#ifndef __WIN32__
- dlclose(p->image);
-#else
- FreeLibrary(p->image);
-#endif
+ unmapImage(p->image);
+ free(p->name);
free(p);
printf("destroy module..\n");
@@ -175,23 +212,39 @@ static int destroyModule(module_t *p)
void initModule()
{
listModule = newList();
+ initExortFunction();
+}
+
+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)
{
- char str[STR_PATH_SIZE];
module_t *module;
- sprintf(str, PATH_MODULES "%s" MODULE_TYPE_UNIX, name); // linux
-#ifdef __WIN32__
- sprintf(str, PATH_MODULES "%s" MODULE_TYPE_WIN, name); // windows
-#endif
-#ifdef APPLE
- sprintf(str, PATH_MODULES "%s" MODULE_TYPE_APPLE, name); // apple
-#endif
- accessExistFile(str);
+ if( isModuleLoaded(name) )
+ {
+ fprintf(stderr, "dupicit loaded module %s !\n", name);
+ return -1;
+ }
- module = newModule(str);
+ module = newModule(name);
if( module == NULL )
{
@@ -204,6 +257,16 @@ int loadModule(char *name)
return 0;
}
+int loadDepModule(char *name)
+{
+ if( isModuleLoaded(name) )
+ {
+ return 0;
+ }
+
+ return loadModule(name);
+}
+
void cmdModule(char *s)
{
int i;
@@ -280,4 +343,5 @@ int recvMsgModule(char *msg)
void quitModule()
{
destroyListItem(listModule, destroyModule);
+ quitExportFunction();
}
diff --git a/src/base/modules.h b/src/base/modules.h
index d7e440f..8036a6a 100644
--- a/src/base/modules.h
+++ b/src/base/modules.h
@@ -30,6 +30,9 @@ typedef struct export_fce_s
#endif
int (*fce_getNetTypeGame)();
+ int (*fce_loadDepModule)(void *name);
+ void (*fce_addToExportFce)(char *name, void *function);
+ void* (*fce_getExportFce)(char *name);
void (*fce_getTuxProportion)(tux_t *tux, int *x,int *y, int *w, int *h);
void (*fce_setTuxProportion)(tux_t *tux, int x, int y);
@@ -52,7 +55,7 @@ typedef struct export_fce_s
typedef struct module_s
{
- char *file;
+ char *name;
void *image;
int (*fce_init)(export_fce_t *p);
@@ -69,6 +72,7 @@ typedef struct module_s
extern void initModule();
extern int loadModule(char *name);
+extern int loadDepModule(char *name);
#ifndef PUBLIC_SERVER
extern void drawModule(int x, int y, int w, int h);
#endif