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

Last change on this file since 127 was 25, checked in by Nishi, on Sep 15, 2024 at 12:10:29 AM

fix dylib load

  • Property svn:keywords set to Id
File size: 1.3 KB
Line 
1/* $Id: module.c 25 2024-09-14 15:10:29Z 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 <unistd.h>
13#include <stdlib.h>
14
15#ifdef __MINGW32__
16#include <windows.h>
17#else
18#include <dlfcn.h>
19#endif
20
21extern struct tw_config config;
22
23void* tw_module_load(const char* path) {
24 char* p = getcwd(NULL, 0);
25 chdir(config.server_root);
26 void* lib;
27#ifdef __MINGW32__
28 lib = LoadLibraryA(path);
29#else
30 lib = dlopen(path, RTLD_LAZY);
31#endif
32 if(lib == NULL) {
33 cm_log("Module", "Could not load %s", path);
34 }
35 chdir(p);
36 free(p);
37 return lib;
38}
39
40void* tw_module_symbol(void* mod, const char* sym) {
41#ifdef __MINGW32__
42 return GetProcAddress(mod, sym);
43#else
44 return dlsym(mod, sym);
45#endif
46}
47
48void tw_add_version(const char* string) {
49 if(config.extension == NULL) {
50 config.extension = cm_strcat(" ", string);
51 } else {
52 char* tmp = config.extension;
53 config.extension = cm_strcat3(tmp, " ", string);
54 free(tmp);
55 }
56}
57
58void tw_init_tools(struct tw_tool* tools) {
59 tools->log = cm_log;
60 tools->add_version = tw_add_version;
61}
62
63int tw_module_init(void* mod) {
64 tw_mod_init_t mod_init = (tw_mod_init_t)tw_module_symbol(mod, "mod_init");
65 if(mod_init == NULL) {
66 cm_log("Module", "Could not init a module");
67 return 1;
68 } else {
69 struct tw_tool tools;
70 tw_init_tools(&tools);
71 return mod_init(&config, &tools);
72 }
73}
Note: See TracBrowser for help on using the repository browser.