| 1 |
/* |
| 2 |
* plugin.c |
| 3 |
* |
| 4 |
* This file is a part of NSIS. |
| 5 |
* |
| 6 |
* Copyright (C) 1999-2026 Nullsoft and Contributors |
| 7 |
* |
| 8 |
* Licensed under the zlib/libpng license (the "License"); |
| 9 |
* you may not use this file except in compliance with the License. |
| 10 |
* |
| 11 |
* Licence details can be found in the file COPYING. |
| 12 |
* |
| 13 |
* This software is provided 'as-is', without any express or implied |
| 14 |
* warranty. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "plugin.h" |
| 18 |
|
| 19 |
#ifdef NSIS_CONFIG_PLUGIN_SUPPORT |
| 20 |
|
| 21 |
typedef struct _loaded_plugin |
| 22 |
{ |
| 23 |
struct _loaded_plugin* next; |
| 24 |
NSISPLUGINCALLBACK proc; |
| 25 |
HMODULE dll; |
| 26 |
} |
| 27 |
loaded_plugin; |
| 28 |
|
| 29 |
static loaded_plugin* g_plugins = 0; // not thread safe! |
| 30 |
|
| 31 |
void NSISCALL Plugins_SendMsgToAllPlugins(int msg) |
| 32 |
{ |
| 33 |
loaded_plugin* p; |
| 34 |
|
| 35 |
for (p = g_plugins; p; p = p->next) |
| 36 |
{ |
| 37 |
p->proc(msg); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
void NSISCALL Plugins_UnloadAll() |
| 42 |
{ |
| 43 |
loaded_plugin* p = g_plugins; |
| 44 |
|
| 45 |
Plugins_SendMsgToAllPlugins(NSPIM_UNLOAD); |
| 46 |
|
| 47 |
while (p) |
| 48 |
{ |
| 49 |
loaded_plugin* oldp = p; |
| 50 |
p = oldp->next; |
| 51 |
FreeLibrary(oldp->dll); |
| 52 |
GlobalFree(oldp); |
| 53 |
} |
| 54 |
|
| 55 |
g_plugins = NULL; |
| 56 |
} |
| 57 |
|
| 58 |
BOOL NSISCALL Plugins_CanUnload(HANDLE pluginHandle) |
| 59 |
{ |
| 60 |
loaded_plugin* p; |
| 61 |
|
| 62 |
for (p = g_plugins; p; p = p->next) |
| 63 |
{ |
| 64 |
if (p->dll == pluginHandle) |
| 65 |
{ |
| 66 |
return FALSE; |
| 67 |
} |
| 68 |
} |
| 69 |
return TRUE; |
| 70 |
} |
| 71 |
|
| 72 |
int NSISCALL RegisterPluginCallback(HMODULE pluginHandle, NSISPLUGINCALLBACK proc) |
| 73 |
{ |
| 74 |
loaded_plugin* p; |
| 75 |
|
| 76 |
if (!Plugins_CanUnload(pluginHandle)) |
| 77 |
{ |
| 78 |
// already registered |
| 79 |
return 1; |
| 80 |
} |
| 81 |
|
| 82 |
p = (loaded_plugin*) GlobalAlloc(GPTR, sizeof(loaded_plugin)); |
| 83 |
if (p) |
| 84 |
{ |
| 85 |
p->proc = proc; |
| 86 |
p->dll = pluginHandle; |
| 87 |
p->next = g_plugins; |
| 88 |
|
| 89 |
g_plugins = p; |
| 90 |
|
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
return -1; |
| 95 |
} |
| 96 |
|
| 97 |
#endif /* #ifdef NSIS_CONFIG_PLUGIN_SUPPORT */ |