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

Last change on this file since 215 was 215, checked in by Nishi, on Oct 3, 2024 at 4:24:43 AM

can be compiled using bcc32 now

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