source: Main/trunk/Server/module.c@ 347

Last change on this file since 347 was 347, checked in by Nishi, on Oct 16, 2024 at 1:33:28 AM

does not really work

  • Property svn:keywords set to Id
File size: 3.2 KB
RevLine 
[17]1/* $Id: module.c 347 2024-10-15 16:33:28Z nishi $ */
2
[18]3#define SOURCE
4
[17]5#include "tw_module.h"
6
7#include "tw_config.h"
8
9#include <cm_string.h>
10#include <cm_log.h>
11
[156]12#include <string.h>
[212]13#include <stdlib.h>
[215]14#if !defined(_MSC_VER) && !defined(__BORLANDC__)
[17]15#include <unistd.h>
[212]16#endif
[17]17
[182]18extern struct tw_config config;
19
[339]20#if defined(_PSP) || defined(__PPU__) || defined(__ps2sdk__) || defined(__NeXT__)
[182]21void* tw_module_load(const char* path) { return NULL; }
22
23void* tw_module_symbol(void* mod, const char* sym) { return NULL; }
24
25int tw_module_init(void* mod) { return 1; }
26
27#else
28
[219]29#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[312]30#ifdef __OS2__
31#define INCL_DOSMODULEMGR
[313]32#define INCL_DOSERRORS
[312]33#include <os2.h>
[315]34#elif defined(__NETWARE__)
[347]35#include <dlfcn.h>
[312]36#else
[17]37#include <windows.h>
[217]38#include <direct.h>
[312]39#endif
[17]40#else
41#include <dlfcn.h>
42#endif
43
44void* tw_module_load(const char* path) {
45 char* p = getcwd(NULL, 0);
[212]46 void* lib;
[312]47 char tmp[512];
[313]48#ifdef __OS2__
49 HMODULE mod;
[315]50#elif defined(__NETWARE__)
51 unsigned int* hnd = malloc(sizeof(*hnd));
[313]52#endif
[17]53 chdir(config.server_root);
[219]54#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[312]55#ifdef __OS2__
[315]56 if(DosLoadModule(tmp, 512, path, &mod) != NO_ERROR) {
[313]57 return NULL;
58 }
59 lib = (void*)mod;
[315]60#elif defined(__NETWARE__)
[347]61 lib = dlopen(path, RTLD_LAZY);
[312]62#else
[17]63 lib = LoadLibraryA(path);
[312]64#endif
[17]65#else
[25]66 lib = dlopen(path, RTLD_LAZY);
[17]67#endif
68 if(lib == NULL) {
69 cm_log("Module", "Could not load %s", path);
70 }
71 chdir(p);
72 free(p);
73 return lib;
74}
75
76void* tw_module_symbol(void* mod, const char* sym) {
[219]77#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[312]78#ifdef __OS2__
79 void* ret;
[313]80 APIRET rc;
[315]81 if((rc = DosQueryProcAddr((HMODULE)mod, 0, sym, (PFN*)&ret)) != NO_ERROR) {
[313]82 cm_log("Module", "OS/2 error %d", (int)rc);
83 return NULL;
84 }
[312]85 return ret;
[315]86#elif defined(__NETWARE__)
[347]87 return dlsym(mod, sym);
[312]88#else
[17]89 return GetProcAddress(mod, sym);
[312]90#endif
[17]91#else
92 return dlsym(mod, sym);
93#endif
94}
95
[182]96int tw_module_init(void* mod) {
97 tw_mod_init_t mod_init = (tw_mod_init_t)tw_module_symbol(mod, "mod_init");
98 if(mod_init == NULL) {
[313]99 cm_log("Module", "Could not find a init call");
[182]100 return 1;
101 } else {
102 struct tw_tool tools;
103 tw_init_tools(&tools);
104 return mod_init(&config, &tools);
105 }
106}
107#endif
108
[18]109void tw_add_version(const char* string) {
110 if(config.extension == NULL) {
111 config.extension = cm_strcat(" ", string);
112 } else {
113 char* tmp = config.extension;
114 config.extension = cm_strcat3(tmp, " ", string);
115 free(tmp);
116 }
117}
[17]118
[156]119void tw_add_define(const char* string) {
120 int i;
[159]121 for(i = 0; config.defined[i] != NULL; i++) {
122 if(strcmp(config.defined[i], string) == 0) {
123 return;
124 }
125 }
[156]126 for(i = 0; config.defined[i] != NULL; i++)
127 ;
128 config.defined[i] = cm_strdup(string);
129 config.defined[i + 1] = NULL;
130}
131
132void tw_delete_define(const char* string) {
133 int i;
134 for(i = 0; config.defined[i] != NULL; i++) {
135 if(strcmp(config.defined[i], string) == 0) {
136 free(config.defined[i]);
137 for(; config.defined[i] != NULL; i++) {
138 config.defined[i] = config.defined[i + 1];
139 }
[159]140 break;
[156]141 }
142 }
143}
144
[18]145void tw_init_tools(struct tw_tool* tools) {
146 tools->log = cm_log;
147 tools->add_version = tw_add_version;
[156]148 tools->add_define = tw_add_define;
[18]149}
Note: See TracBrowser for help on using the repository browser.