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

Last change on this file since 156 was 156, checked in by Nishi, on Sep 25, 2024 at 9:28:10 PM

release 1.07

  • Property svn:keywords set to Id
File size: 1.8 KB
Line 
1/* $Id: module.c 156 2024-09-25 12:28:10Z 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 ;
63 config.defined[i] = cm_strdup(string);
64 config.defined[i + 1] = NULL;
65}
66
67void tw_delete_define(const char* string) {
68 int i;
69 for(i = 0; config.defined[i] != NULL; i++) {
70 if(strcmp(config.defined[i], string) == 0) {
71 free(config.defined[i]);
72 for(; config.defined[i] != NULL; i++) {
73 config.defined[i] = config.defined[i + 1];
74 }
75 }
76 }
77}
78
79void tw_init_tools(struct tw_tool* tools) {
80 tools->log = cm_log;
81 tools->add_version = tw_add_version;
82 tools->add_define = tw_add_define;
83}
84
85int tw_module_init(void* mod) {
86 tw_mod_init_t mod_init = (tw_mod_init_t)tw_module_symbol(mod, "mod_init");
87 if(mod_init == NULL) {
88 cm_log("Module", "Could not init a module");
89 return 1;
90 } else {
91 struct tw_tool tools;
92 tw_init_tools(&tools);
93 return mod_init(&config, &tools);
94 }
95}
Note: See TracBrowser for help on using the repository browser.