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

Last change on this file since 217 was 217, checked in by Nishi, on Oct 3, 2024 at 4:50:16 AM

add a script to create specific installer

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