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

Last change on this file since 200 was 189, checked in by Nishi, on Sep 29, 2024 at 9:37:00 AM

will try to fix ps2 later

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