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

Last change on this file since 269 was 219, checked in by Nishi, on Oct 3, 2024 at 5:40:37 AM

add watcom

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