-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
library.cpp
239 lines (202 loc) · 6.18 KB
/
library.cpp
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
/**
* @file library.cpp
* Dynamic libraries. @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>
*/
#include <de/App>
#include <de/NativeFile>
#include <de/LibraryFile>
#include <de/Library>
#include "de_base.h"
#include "m_misc.h"
#include "api_def.h"
#include "api_console.h"
#include "api_filesys.h"
#include "api_material.h"
#include "api_mpe.h"
#include "api_sound.h"
struct library_s { // typedef Library
Str* path; ///< de::FS path of the library (e.g., "/bin/doom.dll").
de::LibraryFile* file; ///< File where the plugin has been loaded from.
bool isGamePlugin; ///< Is this a game plugin? (only one should be in use at a time)
std::string typeId; ///< deng2 library type ID e.g., "deng-plugin/game".
library_s() : path(0), file(0), isGamePlugin(false) {}
};
static ddstring_t* lastError;
typedef QList<Library*> LoadedLibs;
static LoadedLibs loadedLibs;
void Library_Init(void)
{
lastError = Str_NewStd();
}
void Library_Shutdown(void)
{
Str_Delete(lastError); lastError = 0;
/// @todo Unload all remaining libraries?
}
void Library_ReleaseGames(void)
{
#ifdef UNIX
foreach(Library* lib, loadedLibs)
{
if(lib->isGamePlugin)
{
LegacyCore_PrintfLogFragmentAtLevel(DE2_LOG_DEBUG,
"Library_ReleaseGames: Closing '%s'\n", Str_Text(lib->path));
// Close the Library.
DENG_ASSERT(lib->file);
lib->file->clear();
}
}
#endif
}
#ifdef UNIX
static void reopenLibraryIfNeeded(Library* lib)
{
DENG_ASSERT(lib);
if(!lib->file->loaded())
{
LegacyCore_PrintfLogFragmentAtLevel(DE2_LOG_DEBUG,
"reopenLibraryIfNeeded: Opening '%s'\n", Str_Text(lib->path));
// Make sure the Library gets opened again now.
lib->file->library();
DENG_ASSERT(lib->file->loaded());
Library_PublishAPIs(lib);
}
}
#endif
Library* Library_New(const char* filePath)
{
try
{
Str_Clear(lastError);
de::LibraryFile& libFile = DENG2_APP->rootFolder().locate<de::LibraryFile>(filePath);
if(libFile.library().type() == de::Library::DEFAULT_TYPE)
{
// This is just a shared library, not a plugin.
// We don't have to keep it loaded.
libFile.clear();
Str_Set(lastError, "not a Doomsday plugin");
return 0;
}
// Create the Library instance.
Library* lib = new Library;
lib->file = &libFile;
lib->path = Str_Set(Str_NewStd(), filePath);
lib->typeId = libFile.library().type().toStdString();
loadedLibs.append(lib);
// Symbols from game plugins conflict with each other, so we have to
// keep track of games.
if(libFile.library().type() == "deng-plugin/game")
{
lib->isGamePlugin = true;
}
Library_PublishAPIs(lib);
return lib;
}
catch(const de::Error& er)
{
Str_Set(lastError, er.asText().toAscii().constData());
LOG_WARNING("Library_New: Error opening \"%s\": ") << filePath << er.asText();
return 0;
}
}
void Library_Delete(Library *lib)
{
DENG_ASSERT(lib);
// Unload the library from memory.
lib->file->clear();
Str_Delete(lib->path);
loadedLibs.removeOne(lib);
delete lib;
}
const char* Library_Type(const Library* lib)
{
DENG_ASSERT(lib);
return &lib->typeId[0];
}
de::LibraryFile& Library_File(Library* lib)
{
DENG_ASSERT(lib);
return *lib->file;
}
void Library_PublishAPIs(Library *lib)
{
de::Library &library = lib->file->library();
if(library.hasSymbol("deng_API"))
{
de::Library::deng_API setAPI = library.DENG2_SYMBOL(deng_API);
#define PUBLISH(X) setAPI(X.api.id, &X)
PUBLISH(_api_Base);
PUBLISH(_api_B);
PUBLISH(_api_Busy);
PUBLISH(_api_Con);
PUBLISH(_api_Def);
PUBLISH(_api_F);
PUBLISH(_api_MPE);
PUBLISH(_api_Material);
PUBLISH(_api_Player);
PUBLISH(_api_Plug);
PUBLISH(_api_S);
PUBLISH(_api_Svg);
PUBLISH(_api_Thinker);
PUBLISH(_api_Uri);
PUBLISH(_api_W);
#ifdef __CLIENT__
// Client-only APIs.
PUBLISH(_api_FR);
#endif
}
}
void* Library_Symbol(Library* lib, const char* symbolName)
{
try
{
DENG_ASSERT(lib);
#ifdef UNIX
reopenLibraryIfNeeded(lib);
#endif
return lib->file->library().address(symbolName);
}
catch(const de::Library::SymbolMissingError& er)
{
Str_Set(lastError, er.asText().toAscii().constData());
return 0;
}
}
const char* Library_LastError(void)
{
return Str_Text(lastError);
}
int Library_IterateAvailableLibraries(int (*func)(void *, const char *, const char *, void *), void *data)
{
const de::FS::Index& libs = DENG2_APP->fileSystem().indexFor(DENG2_TYPE_NAME(de::LibraryFile));
DENG2_FOR_EACH_CONST(de::FS::Index, i, libs)
{
de::LibraryFile* lib = static_cast<de::LibraryFile*>(i->second);
const de::NativeFile* src = dynamic_cast<const de::NativeFile*>(lib->source());
if(src)
{
int result = func(lib, src->name().toUtf8().constData(),
lib->path().toUtf8().constData(), data);
if(result) return result;
}
}
return 0;
}