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

Last change on this file since 214 was 212, checked in by Nishi, on Oct 3, 2024 at 2:44:55 AM

compiles on vc6

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