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

Last change on this file since 313 was 313, checked in by Nishi, on Oct 14, 2024 at 8:22:06 AM

dll does not work somehow

  • Property svn:keywords set to Id
File size: 2.9 KB
RevLine 
[17]1/* $Id: module.c 313 2024-10-13 23:22:06Z 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
[219]29#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[312]30#ifdef __OS2__
31#define INCL_DOSMODULEMGR
[313]32#define INCL_DOSERRORS
[312]33#include <os2.h>
34#else
[17]35#include <windows.h>
[217]36#include <direct.h>
[312]37#endif
[17]38#else
39#include <dlfcn.h>
40#endif
41
42void* tw_module_load(const char* path) {
43 char* p = getcwd(NULL, 0);
[212]44 void* lib;
[312]45 char tmp[512];
[313]46#ifdef __OS2__
47 HMODULE mod;
48#endif
[17]49 chdir(config.server_root);
[219]50#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[312]51#ifdef __OS2__
[313]52 if(DosLoadModule(tmp, 512, path, &mod) != NO_ERROR){
53 return NULL;
54 }
55 lib = (void*)mod;
[312]56#else
[17]57 lib = LoadLibraryA(path);
[312]58#endif
[17]59#else
[25]60 lib = dlopen(path, RTLD_LAZY);
[17]61#endif
62 if(lib == NULL) {
63 cm_log("Module", "Could not load %s", path);
64 }
65 chdir(p);
66 free(p);
67 return lib;
68}
69
70void* tw_module_symbol(void* mod, const char* sym) {
[219]71#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[312]72#ifdef __OS2__
73 void* ret;
[313]74 APIRET rc;
75 if((rc = DosQueryProcAddr((HMODULE)mod, 0, sym, (PFN*)&ret)) != NO_ERROR){
76 cm_log("Module", "OS/2 error %d", (int)rc);
77 return NULL;
78 }
[312]79 return ret;
80#else
[17]81 return GetProcAddress(mod, sym);
[312]82#endif
[17]83#else
84 return dlsym(mod, sym);
85#endif
86}
87
[182]88int tw_module_init(void* mod) {
89 tw_mod_init_t mod_init = (tw_mod_init_t)tw_module_symbol(mod, "mod_init");
90 if(mod_init == NULL) {
[313]91 cm_log("Module", "Could not find a init call");
[182]92 return 1;
93 } else {
94 struct tw_tool tools;
95 tw_init_tools(&tools);
96 return mod_init(&config, &tools);
97 }
98}
99#endif
100
[18]101void tw_add_version(const char* string) {
102 if(config.extension == NULL) {
103 config.extension = cm_strcat(" ", string);
104 } else {
105 char* tmp = config.extension;
106 config.extension = cm_strcat3(tmp, " ", string);
107 free(tmp);
108 }
109}
[17]110
[156]111void tw_add_define(const char* string) {
112 int i;
[159]113 for(i = 0; config.defined[i] != NULL; i++) {
114 if(strcmp(config.defined[i], string) == 0) {
115 return;
116 }
117 }
[156]118 for(i = 0; config.defined[i] != NULL; i++)
119 ;
120 config.defined[i] = cm_strdup(string);
121 config.defined[i + 1] = NULL;
122}
123
124void tw_delete_define(const char* string) {
125 int i;
126 for(i = 0; config.defined[i] != NULL; i++) {
127 if(strcmp(config.defined[i], string) == 0) {
128 free(config.defined[i]);
129 for(; config.defined[i] != NULL; i++) {
130 config.defined[i] = config.defined[i + 1];
131 }
[159]132 break;
[156]133 }
134 }
135}
136
[18]137void tw_init_tools(struct tw_tool* tools) {
138 tools->log = cm_log;
139 tools->add_version = tw_add_version;
[156]140 tools->add_define = tw_add_define;
[18]141}
Note: See TracBrowser for help on using the repository browser.