/
dd_plugin.c
308 lines (259 loc) · 7.85 KB
/
dd_plugin.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
/**
* @file dd_plugin.c
* Plugin subsystem. @ingroup base
*
* @todo Convert to C++, rename.
*
* @authors Copyright © 2003-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 "library.h"
#endif
#include "de_base.h"
#include "de_console.h"
#include "de_defs.h"
#include "dd_pinit.h"
#define HOOKMASK(x) ((x) & 0xffffff)
typedef struct {
int exclude;
struct {
hookfunc_t func;
pluginid_t pluginId;
} list[MAX_HOOKS]; /// @todo Remove arbitrary MAX_HOOKS.
} hookreg_t;
typedef Library* PluginHandle;
static Library* hInstPlug[MAX_PLUGS]; /// @todo Remove arbitrary MAX_PLUGS.
static hookreg_t hooks[NUM_HOOK_TYPES];
static pluginid_t currentPlugin = 0; // none
static PluginHandle* findFirstUnusedPluginHandle(void)
{
int i;
for(i = 0; i < MAX_PLUGS; ++i)
{
if(!hInstPlug[i]) return &hInstPlug[i];
}
return 0; // none available
}
static int loadPlugin(void* libraryFile, const char* fileName, const char* pluginPath, void* param)
{
Library* plugin;
PluginHandle* handle;
void (*initializer)(void);
filename_t name;
pluginid_t plugId;
DENG_UNUSED(libraryFile); // this is not C++...
DENG_UNUSED(param);
DENG_ASSERT(fileName && fileName[0]);
DENG_ASSERT(pluginPath && pluginPath[0]);
if(strcasestr("/bin/audio_", pluginPath))
{
// Do not touch audio plugins at this point.
return true;
}
plugin = Library_New(pluginPath);
if(!plugin)
{
Con_Message(" loadPlugin: Did not load \"%s\" (%s).\n", pluginPath, Library_LastError());
return 0; // Continue iteration.
}
if(!strcmp(Library_Type(plugin), "deng-plugin/audio"))
{
// Audio plugins will be loaded later, on demand.
Library_Delete(plugin);
return 0;
}
initializer = Library_Symbol(plugin, "DP_Initialize");
if(!initializer)
{
DEBUG_Message((" loadPlugin: \"%s\" does not export entrypoint DP_Initialize, ignoring.\n", pluginPath));
// Clearly not a Doomsday plugin.
Library_Delete(plugin);
return 0; // Continue iteration.
}
// Assign a handle and ID to the plugin.
handle = findFirstUnusedPluginHandle();
plugId = handle - hInstPlug + 1;
if(!handle)
{
DEBUG_Message((" loadPlugin: Failed acquiring new handle for \"%s\", ignoring.\n", pluginPath));
Library_Delete(plugin);
return 0; // Continue iteration.
}
// This seems to be a Doomsday plugin.
_splitpath(pluginPath, NULL, NULL, name, NULL);
Con_Message(" (id:%i) %s\n", plugId, name);
*handle = plugin;
DD_SetActivePluginId(plugId);
initializer();
DD_SetActivePluginId(0);
return 0; // Continue iteration.
}
static boolean unloadPlugin(PluginHandle* handle)
{
assert(handle);
if(!*handle) return false;
Library_Delete(*handle);
*handle = 0;
return true;
}
void Plug_LoadAll(void)
{
Con_Message("Initializing plugins...\n");
Library_IterateAvailableLibraries(loadPlugin, 0);
}
void Plug_UnloadAll(void)
{
int i;
for(i = 0; i < MAX_PLUGS && hInstPlug[i]; ++i)
{
unloadPlugin(&hInstPlug[i]);
}
}
int Plug_AddHook(int hookType, hookfunc_t hook)
{
int i, type = HOOKMASK(hookType);
/**
* The current plugin must be set before calling this. The engine has the
* responsibility to call DD_SetActivePluginId() whenever it passes control
* to a plugin, and then set it back to zero after it gets control back.
*/
DENG_ASSERT(DD_ActivePluginId() != 0);
// The type must be good.
if(type < 0 || type >= NUM_HOOK_TYPES)
return false;
// Exclusive hooks.
if(hookType & HOOKF_EXCLUSIVE)
{
hooks[type].exclude = true;
memset(hooks[type].list, 0, sizeof(hooks[type].list));
}
else if(hooks[type].exclude)
{
// An exclusive hook has closed down this list.
return false;
}
for(i = 0; i < MAX_HOOKS && hooks[type].list[i].func; ++i) {};
if(i == MAX_HOOKS)
return false; // No more hooks allowed!
// Add the hook. If the plugin is unidentified the ID will be zero.
hooks[type].list[i].func = hook;
hooks[type].list[i].pluginId = DD_ActivePluginId();
return true;
}
int Plug_RemoveHook(int hookType, hookfunc_t hook)
{
int i, type = HOOKMASK(hookType);
/*
if(currentPlugin)
{
LegacyCore_PrintfLogFragmentAtLevel(DE2_LOG_WARNING,
"Plug_RemoveHook: Failed to remove hook %p of type %i; currently processing a hook.\n",
hook, hookType);
return false;
}
*/
// The type must be good.
if(type < 0 || type >= NUM_HOOK_TYPES)
return false;
for(i = 0; i < MAX_HOOKS; ++i)
{
if(hooks[type].list[i].func != hook)
continue;
hooks[type].list[i].func = 0;
hooks[type].list[i].pluginId = 0;
if(hookType & HOOKF_EXCLUSIVE)
{ // Exclusive hook removed; allow normal hooks.
hooks[type].exclude = false;
}
return true;
}
return false;
}
int Plug_CheckForHook(int hookType)
{
size_t i;
for(i = 0; i < MAX_HOOKS; ++i)
if(hooks[hookType].list[i].func)
return true;
return false;
}
void DD_SetActivePluginId(pluginid_t id)
{
currentPlugin = id;
}
int DD_CallHooks(int hookType, int parm, void *data)
{
int ret = 0;
boolean allGood = true;
pluginid_t oldPlugin = DD_ActivePluginId();
// Try all the hooks.
{ int i;
for(i = 0; i < MAX_HOOKS; ++i)
{
if(!hooks[hookType].list[i].func)
continue;
DD_SetActivePluginId(hooks[hookType].list[i].pluginId);
if(hooks[hookType].list[i].func(hookType, parm, data))
{ // One hook executed; return nonzero from this routine.
ret = 1;
}
else
allGood = false;
}}
DD_SetActivePluginId(oldPlugin);
if(ret && allGood)
ret |= 2;
return ret;
}
pluginid_t DD_ActivePluginId(void)
{
return currentPlugin;
}
void* DD_FindEntryPoint(pluginid_t pluginId, const char* fn)
{
/*
#if WIN32
HINSTANCE* handle = &hInstPlug[pluginId-1];
void* adr = (void*)GetProcAddress(*handle, fn);
if(!adr)
{
LPVOID lpMsgBuf;
DWORD dw = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
0, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, 0);
if(lpMsgBuf)
{
Con_Printf("DD_FindEntryPoint: Error locating \"%s\" #%d: %s", fn, dw, (char*)lpMsgBuf);
LocalFree(lpMsgBuf); lpMsgBuf = 0;
}
}
return adr;
#elif UNIX
*/
void* addr = 0;
int plugIndex = pluginId - 1;
assert(plugIndex >= 0 && plugIndex < MAX_PLUGS);
addr = Library_Symbol(hInstPlug[plugIndex], fn);
if(!addr)
{
Con_Message("DD_FindEntryPoint: Error locating address of \"%s\" (%s).\n", fn,
Library_LastError());
}
return addr;
//#endif
}