mirror of
https://git.code.sf.net/p/quake/quakeforge
synced 2025-02-18 09:51:40 +00:00
static plugins now actually work
This commit is contained in:
parent
8b2da8cbec
commit
742bb9de91
4 changed files with 67 additions and 33 deletions
|
@ -107,6 +107,7 @@ extern struct cvar_s *fs_pluginpath;
|
||||||
*/
|
*/
|
||||||
plugin_t *PI_LoadPlugin (const char *, const char *);
|
plugin_t *PI_LoadPlugin (const char *, const char *);
|
||||||
qboolean PI_UnloadPlugin (plugin_t *);
|
qboolean PI_UnloadPlugin (plugin_t *);
|
||||||
|
void PI_RegisterPlugins (plugin_list_t *);
|
||||||
void PI_Init (void);
|
void PI_Init (void);
|
||||||
void PI_Shutdown (void);
|
void PI_Shutdown (void);
|
||||||
|
|
||||||
|
|
|
@ -93,6 +93,7 @@ CD_f (void)
|
||||||
int
|
int
|
||||||
CDAudio_Init (void)
|
CDAudio_Init (void)
|
||||||
{
|
{
|
||||||
|
PI_RegisterPlugins (cd_plugin_list);
|
||||||
cd_plugin = Cvar_Get ("cd_plugin", "null", CVAR_ARCHIVE, NULL,
|
cd_plugin = Cvar_Get ("cd_plugin", "null", CVAR_ARCHIVE, NULL,
|
||||||
"CD Plugin to use");
|
"CD Plugin to use");
|
||||||
cdmodule = PI_LoadPlugin ("cd", cd_plugin->string);
|
cdmodule = PI_LoadPlugin ("cd", cd_plugin->string);
|
||||||
|
|
|
@ -84,6 +84,8 @@ S_Init (void)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PI_RegisterPlugins (snd_output_list);
|
||||||
|
PI_RegisterPlugins (snd_render_list);
|
||||||
snd_output_module = PI_LoadPlugin ("snd_output", snd_output->string);
|
snd_output_module = PI_LoadPlugin ("snd_output", snd_output->string);
|
||||||
if (!snd_output_module) {
|
if (!snd_output_module) {
|
||||||
Sys_Printf ("Loading of sound output module: %s failed!\n",
|
Sys_Printf ("Loading of sound output module: %s failed!\n",
|
||||||
|
|
|
@ -55,13 +55,21 @@ static const char rcsid[] =
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "QF/cvar.h"
|
#include "QF/cvar.h"
|
||||||
|
#include "QF/hash.h"
|
||||||
#include "QF/plugin.h"
|
#include "QF/plugin.h"
|
||||||
#include "QF/sys.h"
|
#include "QF/sys.h"
|
||||||
|
|
||||||
#include "compat.h"
|
#include "compat.h"
|
||||||
|
|
||||||
cvar_t *fs_pluginpath;
|
cvar_t *fs_pluginpath;
|
||||||
|
|
||||||
|
hashtab_t *registered_plugins;
|
||||||
|
|
||||||
|
static const char *
|
||||||
|
get_key (void *pl, void *unused)
|
||||||
|
{
|
||||||
|
return ((plugin_list_t *)pl)->name;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
PI_InitCvars (void)
|
PI_InitCvars (void)
|
||||||
|
@ -80,6 +88,7 @@ void
|
||||||
PI_Init (void)
|
PI_Init (void)
|
||||||
{
|
{
|
||||||
PI_InitCvars ();
|
PI_InitCvars ();
|
||||||
|
registered_plugins = Hash_NewTable (253, get_key, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -91,62 +100,74 @@ plugin_t *
|
||||||
PI_LoadPlugin (const char *type, const char *name)
|
PI_LoadPlugin (const char *type, const char *name)
|
||||||
{
|
{
|
||||||
char realname[4096];
|
char realname[4096];
|
||||||
|
char plugin_name[1024];
|
||||||
char plugin_info_name[1024];
|
char plugin_info_name[1024];
|
||||||
char *tmpname;
|
char *tmpname;
|
||||||
void *dlhand = NULL;
|
void *dlhand = NULL;
|
||||||
plugin_t *plugin = NULL;
|
plugin_t *plugin = NULL;
|
||||||
P_PluginInfo plugin_info = NULL;
|
P_PluginInfo plugin_info = NULL;
|
||||||
|
plugin_list_t *pl;
|
||||||
|
|
||||||
if (!name)
|
if (!name)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
tmpname = strrchr (name, '/'); // Get the base name, don't allow paths
|
tmpname = strrchr (name, '/'); // Get the base name, don't allow paths
|
||||||
|
|
||||||
// Build the plugin info name
|
// Build the plugin name
|
||||||
snprintf (plugin_info_name, sizeof (plugin_info_name), "%s_%s_PluginInfo",
|
snprintf (plugin_name, sizeof (plugin_name), "%s_%s", type, name);
|
||||||
type, name);
|
pl = Hash_Find (registered_plugins, plugin_name);
|
||||||
// Build the path to the file to load
|
if (pl) {
|
||||||
|
plugin_info = pl->info;
|
||||||
|
}
|
||||||
|
if (!plugin_info) {
|
||||||
|
// Build the plugin info name
|
||||||
|
snprintf (plugin_info_name, sizeof (plugin_info_name),
|
||||||
|
"%s_%s_PluginInfo", type, name);
|
||||||
|
// Build the path to the file to load
|
||||||
#if defined(HAVE_DLOPEN)
|
#if defined(HAVE_DLOPEN)
|
||||||
snprintf (realname, sizeof (realname), "%s/lib%s_%s.so",
|
snprintf (realname, sizeof (realname), "%s/lib%s_%s.so",
|
||||||
fs_pluginpath->string, type, (tmpname ? tmpname + 1 : name));
|
fs_pluginpath->string, type,
|
||||||
|
(tmpname ? tmpname + 1 : name));
|
||||||
#elif defined(_WIN32)
|
#elif defined(_WIN32)
|
||||||
snprintf (realname, sizeof (realname), "%s/QF%s_%s.dll",
|
snprintf (realname, sizeof (realname), "%s/QF%s_%s.dll",
|
||||||
fs_pluginpath->string, type, (tmpname ? tmpname + 1 : name));
|
fs_pluginpath->string, type,
|
||||||
|
(tmpname ? tmpname + 1 : name));
|
||||||
#else
|
#else
|
||||||
# error "No shared library support. FIXME"
|
# error "No shared library support. FIXME"
|
||||||
return NULL;
|
return NULL;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(HAVE_DLOPEN)
|
#if defined(HAVE_DLOPEN)
|
||||||
if (!(dlhand = dlopen (realname, RTLD_GLOBAL | RTLD_NOW))) {
|
if (!(dlhand = dlopen (realname, RTLD_GLOBAL | RTLD_NOW))) {
|
||||||
// lib not found
|
// lib not found
|
||||||
Sys_Printf ("Could not load plugin \"%s\": %s\n", realname,
|
Sys_Printf ("Could not load plugin \"%s\": %s\n", realname,
|
||||||
dlerror ());
|
dlerror ());
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
#elif defined (_WIN32)
|
#elif defined (_WIN32)
|
||||||
if (!(dlhand = LoadLibrary (realname))) { // lib not found
|
if (!(dlhand = LoadLibrary (realname))) { // lib not found
|
||||||
Sys_Printf ("Could not load plugin \"%s\".\n", realname);
|
Sys_Printf ("Could not load plugin \"%s\".\n", realname);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(HAVE_DLOPEN)
|
#if defined(HAVE_DLOPEN)
|
||||||
if (!(plugin_info = dlsym (dlhand, plugin_info_name))) {
|
if (!(plugin_info = dlsym (dlhand, plugin_info_name))) {
|
||||||
// info function not found
|
// info function not found
|
||||||
dlclose (dlhand);
|
dlclose (dlhand);
|
||||||
Sys_Printf ("Plugin info function not found\n");
|
Sys_Printf ("Plugin info function not found\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
#elif defined (_WIN32)
|
#elif defined (_WIN32)
|
||||||
if (!(plugin_info = (P_PluginInfo) GetProcAddress (dlhand,
|
if (!(plugin_info = (P_PluginInfo) GetProcAddress (dlhand,
|
||||||
plugin_info_name))) {
|
plugin_info_name))) {
|
||||||
// info function not found
|
// info function not found
|
||||||
FreeLibrary (dlhand);
|
FreeLibrary (dlhand);
|
||||||
Sys_Printf ("Plugin info function not found\n");
|
Sys_Printf ("Plugin info function not found\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
if (!(plugin = plugin_info ())) { // Something went badly wrong
|
if (!(plugin = plugin_info ())) { // Something went badly wrong
|
||||||
#if defined(HAVE_DLOPEN)
|
#if defined(HAVE_DLOPEN)
|
||||||
|
@ -173,9 +194,18 @@ PI_UnloadPlugin (plugin_t *plugin)
|
||||||
} else {
|
} else {
|
||||||
Sys_Printf ("Warning: No shutdown function for type %d plugin!\n", plugin->type);
|
Sys_Printf ("Warning: No shutdown function for type %d plugin!\n", plugin->type);
|
||||||
}
|
}
|
||||||
|
if (!plugin->handle) // we didn't load it
|
||||||
|
return true;
|
||||||
#if defined(HAVE_DLOPEN)
|
#if defined(HAVE_DLOPEN)
|
||||||
return (dlclose (plugin->handle) == 0);
|
return (dlclose (plugin->handle) == 0);
|
||||||
#elif defined (_WIN32)
|
#elif defined (_WIN32)
|
||||||
return (FreeLibrary (plugin->handle) == 0);
|
return (FreeLibrary (plugin->handle) == 0);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
PI_RegisterPlugins (plugin_list_t *plugins)
|
||||||
|
{
|
||||||
|
while (plugins->name)
|
||||||
|
Hash_Add (registered_plugins, plugins++);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue