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

Last change on this file since 391 was 366, checked in by Nishi, on Oct 17, 2024 at 9:54:56 AM

update stuff

  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1/* $Id: module.c 366 2024-10-17 00:54:56Z 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__) || defined(__DOS__)
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 <nwadv.h>
36#include <nwthread.h>
37#else
38#include <windows.h>
39#include <direct.h>
40#endif
41#else
42#include <dlfcn.h>
43#endif
44
45void* tw_module_load(const char* path) {
46 char* p = getcwd(NULL, 0);
47 void* lib;
48 char tmp[512];
49#ifdef __OS2__
50 HMODULE mod;
51#elif defined(__NETWARE__)
52 unsigned int* hnd = malloc(sizeof(*hnd));
53#endif
54 chdir(config.server_root);
55#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
56#ifdef __OS2__
57 if(DosLoadModule(tmp, 512, path, &mod) != NO_ERROR) {
58 return NULL;
59 }
60 lib = (void*)mod;
61#elif defined(__NETWARE__)
62 *hnd = FindNLMHandle(path);
63 if(*hnd == 0){
64 free(hnd);
65 hnd = NULL;
66 }
67 lib = (void*)hnd;
68#else
69 lib = LoadLibraryA(path);
70#endif
71#else
72 lib = dlopen(path, RTLD_LAZY);
73#endif
74 if(lib == NULL) {
75 cm_log("Module", "Could not load %s", path);
76 }
77 chdir(p);
78 free(p);
79 return lib;
80}
81
82void* tw_module_symbol(void* mod, const char* sym) {
83#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
84#ifdef __OS2__
85 void* ret;
86 APIRET rc;
87 if((rc = DosQueryProcAddr((HMODULE)mod, 0, sym, (PFN*)&ret)) != NO_ERROR) {
88 cm_log("Module", "OS/2 error %d", (int)rc);
89 return NULL;
90 }
91 return ret;
92#elif defined(__NETWARE__)
93 return ImportSymbol(*(unsigned int*)mod, sym);
94#else
95 return GetProcAddress(mod, sym);
96#endif
97#else
98 return dlsym(mod, sym);
99#endif
100}
101
102int tw_module_init(void* mod) {
103 tw_mod_init_t mod_init = (tw_mod_init_t)tw_module_symbol(mod, "mod_init");
104 if(mod_init == NULL) {
105 cm_log("Module", "Could not find a init call");
106 return 1;
107 } else {
108 struct tw_tool tools;
109 tw_init_tools(&tools);
110 return mod_init(&config, &tools);
111 }
112}
113#endif
114
115void tw_add_version(const char* string) {
116 if(config.extension == NULL) {
117 config.extension = cm_strcat(" ", string);
118 } else {
119 char* tmp = config.extension;
120 config.extension = cm_strcat3(tmp, " ", string);
121 free(tmp);
122 }
123}
124
125void tw_add_define(const char* string) {
126 int i;
127 for(i = 0; config.defined[i] != NULL; i++) {
128 if(strcmp(config.defined[i], string) == 0) {
129 return;
130 }
131 }
132 for(i = 0; config.defined[i] != NULL; i++)
133 ;
134 config.defined[i] = cm_strdup(string);
135 config.defined[i + 1] = NULL;
136}
137
138void tw_delete_define(const char* string) {
139 int i;
140 for(i = 0; config.defined[i] != NULL; i++) {
141 if(strcmp(config.defined[i], string) == 0) {
142 free(config.defined[i]);
143 for(; config.defined[i] != NULL; i++) {
144 config.defined[i] = config.defined[i + 1];
145 }
146 break;
147 }
148 }
149}
150
151void tw_init_tools(struct tw_tool* tools) {
152 tools->log = cm_log;
153 tools->add_version = tw_add_version;
154 tools->add_define = tw_add_define;
155}
Note: See TracBrowser for help on using the repository browser.