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

Last change on this file since 187 was 187, checked in by Nishi, on Sep 28, 2024 at 9:07:34 AM

kinda works

  • Property svn:keywords set to Id
File size: 2.1 KB
RevLine 
[17]1/* $Id: module.c 187 2024-09-28 00:07:34Z nishi $ */
2
[18]3#define SOURCE
4
[17]5#include "tw_module.h"
6
7#include "tw_config.h"
8
9#include <cm_string.h>
10#include <cm_log.h>
11
[156]12#include <string.h>
[17]13#include <unistd.h>
14#include <stdlib.h>
15
[182]16extern struct tw_config config;
17
[187]18#if defined(_PSP) || defined(__PPU__)
[182]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
[17]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
[25]40 lib = dlopen(path, RTLD_LAZY);
[17]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
[182]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
[18]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}
[17]80
[156]81void tw_add_define(const char* string) {
82 int i;
[159]83 for(i = 0; config.defined[i] != NULL; i++) {
84 if(strcmp(config.defined[i], string) == 0) {
85 return;
86 }
87 }
[156]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 }
[159]102 break;
[156]103 }
104 }
105}
106
[18]107void tw_init_tools(struct tw_tool* tools) {
108 tools->log = cm_log;
109 tools->add_version = tw_add_version;
[156]110 tools->add_define = tw_add_define;
[18]111}
Note: See TracBrowser for help on using the repository browser.