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

Last change on this file since 348 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
Line 
1/* $Id: module.c 347 2024-10-15 16:33:28Z nishi $ */
2
3#define SOURCE
4
5#include "tw_module.h"
6
7#include "tw_config.h"
8
9#include <cm_string.h>
10#include <cm_log.h>
11
12#include <string.h>
13#include <stdlib.h>
14#if !defined(_MSC_VER) && !defined(__BORLANDC__)
15#include <unistd.h>
16#endif
17
18extern struct tw_config config;
19
20#if defined(_PSP) || defined(__PPU__) || defined(__ps2sdk__) || defined(__NeXT__)
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
29#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
30#ifdef __OS2__
31#define INCL_DOSMODULEMGR
32#define INCL_DOSERRORS
33#include <os2.h>
34#elif defined(__NETWARE__)
35#include <dlfcn.h>
36#else
37#include <windows.h>
38#include <direct.h>
39#endif
40#else
41#include <dlfcn.h>
42#endif
43
44void* tw_module_load(const char* path) {
45 char* p = getcwd(NULL, 0);
46 void* lib;
47 char tmp[512];
48#ifdef __OS2__
49 HMODULE mod;
50#elif defined(__NETWARE__)
51 unsigned int* hnd = malloc(sizeof(*hnd));
52#endif
53 chdir(config.server_root);
54#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
55#ifdef __OS2__
56 if(DosLoadModule(tmp, 512, path, &mod) != NO_ERROR) {
57 return NULL;
58 }
59 lib = (void*)mod;
60#elif defined(__NETWARE__)
61 lib = dlopen(path, RTLD_LAZY);
62#else
63 lib = LoadLibraryA(path);
64#endif
65#else
66 lib = dlopen(path, RTLD_LAZY);
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) {
77#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
78#ifdef __OS2__
79 void* ret;
80 APIRET rc;
81 if((rc = DosQueryProcAddr((HMODULE)mod, 0, sym, (PFN*)&ret)) != NO_ERROR) {
82 cm_log("Module", "OS/2 error %d", (int)rc);
83 return NULL;
84 }
85 return ret;
86#elif defined(__NETWARE__)
87 return dlsym(mod, sym);
88#else
89 return GetProcAddress(mod, sym);
90#endif
91#else
92 return dlsym(mod, sym);
93#endif
94}
95
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) {
99 cm_log("Module", "Could not find a init call");
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
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}
118
119void tw_add_define(const char* string) {
120 int i;
121 for(i = 0; config.defined[i] != NULL; i++) {
122 if(strcmp(config.defined[i], string) == 0) {
123 return;
124 }
125 }
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 }
140 break;
141 }
142 }
143}
144
145void tw_init_tools(struct tw_tool* tools) {
146 tools->log = cm_log;
147 tools->add_version = tw_add_version;
148 tools->add_define = tw_add_define;
149}
Note: See TracBrowser for help on using the repository browser.