/
library.c
349 lines (304 loc) · 7.87 KB
/
library.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* @file library.c
* Dynamic libraries implementation. @ingroup base
*
* @authors Copyright © 2006-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2009-2012 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#ifdef UNIX
# include <sys/types.h>
# include <unistd.h>
# include <dirent.h>
# include <dlfcn.h>
# include <string.h>
# include <stdbool.h>
#endif
#include "de_base.h"
#include "de_filesys.h"
#include "m_misc.h"
#include "m_args.h"
#ifdef WIN32
# include "de_platform.h"
#endif
#define MAX_LIBRARIES 64 /// @todo Replace with a dynamic list.
#ifdef UNIX
typedef void* handle_t;
#endif
#ifdef WIN32
typedef HINSTANCE handle_t;
#endif
struct library_s {
ddstring_t* path;
handle_t handle;
boolean isGamePlugin;
};
#ifdef UNIX
static filename_t appDir; /// @todo Use ddstring_t
#endif
static ddstring_t* lastError;
static Library* loadedLibs[MAX_LIBRARIES];
#ifdef UNIX
static void getBundlePath(char* path, size_t len)
{
if(ArgCheckWith("-libdir", 1))
{
strncpy(path, ArgNext(), len);
return;
}
if(ArgCheckWith("-appdir", 1))
{
dd_snprintf(path, len, "%s/%s", appDir, ArgNext());
return;
}
/*
#ifdef MACOSX
// This is the default location where bundles are.
dd_snprintf(path, len, "%s/Bundles", appDir);
#endif
*/
#ifdef UNIX
# ifdef DENG_LIBRARY_DIR
strncpy(path, DENG_LIBRARY_DIR, len);
# else
// Assume they are in the cwd.
strncpy(path, appDir, len);
# endif
// Check Unix-specific config files.
DD_Unix_GetConfigValue("paths", "libdir", path, len);
#endif
}
#endif
static void addToLoaded(Library* lib)
{
int i;
for(i = 0; i < MAX_LIBRARIES; ++i)
{
if(!loadedLibs[i])
{
loadedLibs[i] = lib;
return;
}
}
assert(false);
}
static void removeFromLoaded(Library* lib)
{
int i;
for(i = 0; i < MAX_LIBRARIES; ++i)
{
if(loadedLibs[i] == lib)
{
loadedLibs[i] = 0;
return;
}
}
assert(false);
}
void Library_Init(void)
{
lastError = Str_NewStd();
#ifdef UNIX
if(!getcwd(appDir, sizeof(appDir)))
{
strcpy(appDir, "");
}
#endif
}
void Library_Shutdown(void)
{
Str_Delete(lastError); lastError = 0;
/// @todo Unload all remaining libraries?
}
void Library_ReleaseGames(void)
{
#ifdef UNIX
int i;
for(i = 0; i < MAX_LIBRARIES; ++i)
{
Library* lib = loadedLibs[i];
if(!lib) continue;
if(lib->isGamePlugin && lib->handle)
{
LegacyCore_PrintfLogFragmentAtLevel(de2LegacyCore, DE2_LOG_DEBUG,
"Library_ReleaseGames: Closing '%s'\n", Str_Text(lib->path));
dlclose(lib->handle);
lib->handle = 0;
}
}
#endif
}
#ifdef UNIX
static void reopenLibraryIfNeeded(Library* lib)
{
assert(lib);
if(!lib->handle)
{
LegacyCore_PrintfLogFragmentAtLevel(de2LegacyCore, DE2_LOG_DEBUG,
"reopenLibraryIfNeeded: Opening '%s'\n", Str_Text(lib->path));
lib->handle = dlopen(Str_Text(lib->path), RTLD_NOW);
assert(lib->handle);
}
}
#endif
Library* Library_New(const char *fileName)
{
Library* lib = 0;
handle_t handle = 0;
#ifdef UNIX
filename_t bundlePath; /// @todo Use ddstring_t
#ifdef MACOSX
char* ptr;
#endif
getBundlePath(bundlePath, FILENAME_T_MAXLEN);
if(bundlePath[strlen(bundlePath) - 1] != '/')
M_StrCat(bundlePath, "/", FILENAME_T_MAXLEN);
#ifdef MACOSX
M_StrCat(bundlePath, fileName, FILENAME_T_MAXLEN);
M_StrCat(bundlePath, "/", FILENAME_T_MAXLEN);
#endif
M_StrCat(bundlePath, fileName, FILENAME_T_MAXLEN);
#ifdef MACOSX
{ const char* ext = F_FindFileExtension(bundlePath);
if(ext && stricmp(ext, "dylib") && stricmp(ext, "bundle")) {
// Not a dynamic library... We already know this will fail.
Str_Set(lastError, "not a dynamic library");
return NULL;
}}
// Get rid of the ".bundle" in the end.
if(NULL != (ptr = strrchr(bundlePath, '.')))
*ptr = '\0';
#endif
Str_Clear(lastError);
handle = dlopen(bundlePath, RTLD_NOW);
if(!handle)
{
Str_Set(lastError, dlerror());
printf("Library_New: Error opening \"%s\" (%s).\n", bundlePath, Library_LastError());
return 0;
}
#endif
#ifdef WIN32
Str_Clear(lastError);
handle = LoadLibrary(WIN_STRING(fileName));
if(!handle)
{
Str_Set(lastError, DD_Win32_GetLastErrorMessage());
printf("Library_New: Error opening \"%s\" (%s).\n", fileName, Library_LastError());
return 0;
}
#endif
// Create the Library instance.
lib = calloc(1, sizeof(*lib));
lib->handle = handle;
lib->path = Str_NewStd();
#ifdef UNIX
Str_Set(lib->path, bundlePath);
#endif
#ifdef WIN32
Str_Set(lib->path, fileName);
#endif
addToLoaded(lib);
// Symbols from game plugins conflict with each other, so we have to
// keep track of them.
/// @todo Needs a more generic way to detect the type of plugin.
if(Library_Symbol(lib, "G_RegisterGames"))
{
lib->isGamePlugin = true;
}
return lib;
}
void Library_Delete(Library *lib)
{
assert(lib);
if(lib->handle)
{
#ifdef UNIX
dlclose(lib->handle);
#endif
#ifdef WIN32
FreeLibrary(lib->handle);
#endif
}
Str_Delete(lib->path);
removeFromLoaded(lib);
free(lib);
}
void* Library_Symbol(Library* lib, const char* symbolName)
{
void* ptr = 0;
assert(lib);
#ifdef UNIX
reopenLibraryIfNeeded(lib);
ptr = dlsym(lib->handle, symbolName);
if(!ptr)
{
Str_Set(lastError, dlerror());
}
#endif
#ifdef WIN32
ptr = (void*)GetProcAddress(lib->handle, symbolName);
if(!ptr)
{
Str_Set(lastError, DD_Win32_GetLastErrorMessage());
}
#endif
return ptr;
}
const char* Library_LastError(void)
{
return Str_Text(lastError);
}
void Library_AddSearchDir(const char *dir)
{
/// @todo Implement this (and use it in the lookup)
}
int Library_IterateAvailableLibraries(int (*func)(const char *, void *), void *data)
{
#ifdef UNIX
struct dirent* entry = NULL;
filename_t bundlePath;
DIR* dir = NULL;
// This is the default location where bundles are.
getBundlePath(bundlePath, FILENAME_T_MAXLEN);
dir = opendir(bundlePath);
if(!dir)
{
Str_Set(lastError, strerror(errno));
printf("Library_IterateAvailableLibraries: Error opening \"%s\" (%s).\n",
bundlePath, Library_LastError());
return 0;
}
while((entry = readdir(dir)))
{
if(!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) continue;
#ifdef MACOSX
// Mac plugins are bundled in a subdir.
if(entry->d_type != DT_REG && entry->d_type != DT_DIR &&
entry->d_type != DT_LNK) continue;
#else
// Also include symlinks.
if(entry->d_type != DT_REG && entry->d_type != DT_LNK) continue;
#endif
if(func(entry->d_name, data)) break;
}
closedir(dir);
#endif
#ifdef WIN32
printf("TODO: a similar routine should be in dd_winit.c; move the code here\n");
#endif
return 0;
}