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

Last change on this file since 17 was 17, checked in by Nishi, on Sep 14, 2024 at 2:41:07 AM

module system kinda works

  • Property svn:keywords set to Id
File size: 1.0 KB
RevLine 
[17]1/* $Id: module.c 17 2024-09-13 17:41:07Z nishi $ */
2
3#include "tw_module.h"
4
5#include "tw_config.h"
6
7#include <cm_string.h>
8#include <cm_log.h>
9
10#include <unistd.h>
11#include <stdlib.h>
12
13#ifdef __MINGW32__
14#include <windows.h>
15#else
16#include <dlfcn.h>
17#endif
18
19extern struct tw_config config;
20
21void* tw_module_load(const char* path) {
22 char* p = getcwd(NULL, 0);
23 chdir(config.server_root);
24 void* lib;
25#ifdef __MINGW32__
26 lib = LoadLibraryA(path);
27#else
28 lib = dlopen(path, DL_LAZY);
29#endif
30 if(lib == NULL) {
31 cm_log("Module", "Could not load %s", path);
32 }
33 chdir(p);
34 free(p);
35 return lib;
36}
37
38void* tw_module_symbol(void* mod, const char* sym) {
39#ifdef __MINGW32__
40 return GetProcAddress(mod, sym);
41#else
42 return dlsym(mod, sym);
43#endif
44}
45
46void tw_init_tools(struct tw_tool* tools) { tools->log = cm_log; }
47
48int tw_module_init(void* mod) {
49 tw_mod_init_t mod_init = (tw_mod_init_t)tw_module_symbol(mod, "mod_init");
50 if(mod_init == NULL) {
51 cm_log("Module", "Could not init a module");
52 return 1;
53 } else {
54 struct tw_tool tools;
55 tw_init_tools(&tools);
56 return mod_init(&config, &tools);
57 }
58}
Note: See TracBrowser for help on using the repository browser.