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

Last change on this file since 312 was 312, checked in by Nishi, on Oct 14, 2024 at 3:17:37 AM

works on os2

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