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

Last change on this file since 176 was 159, checked in by Nishi, on Sep 25, 2024 at 9:56:45 PM

some fix

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