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

Last change on this file since 314 was 313, checked in by Nishi, on Oct 14, 2024 at 8:22:06 AM

dll does not work somehow

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