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

Last change on this file since 359 was 359, checked in by Nishi, on Oct 16, 2024 at 11:34:51 PM

add dos supportr

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