static plugins now actually work

This commit is contained in:
Bill Currie 2001-10-10 16:22:41 +00:00
parent 8b2da8cbec
commit 742bb9de91
4 changed files with 67 additions and 33 deletions

View file

@ -107,6 +107,7 @@ extern struct cvar_s *fs_pluginpath;
*/
plugin_t *PI_LoadPlugin (const char *, const char *);
qboolean PI_UnloadPlugin (plugin_t *);
void PI_RegisterPlugins (plugin_list_t *);
void PI_Init (void);
void PI_Shutdown (void);

View file

@ -93,6 +93,7 @@ CD_f (void)
int
CDAudio_Init (void)
{
PI_RegisterPlugins (cd_plugin_list);
cd_plugin = Cvar_Get ("cd_plugin", "null", CVAR_ARCHIVE, NULL,
"CD Plugin to use");
cdmodule = PI_LoadPlugin ("cd", cd_plugin->string);

View file

@ -84,6 +84,8 @@ S_Init (void)
return;
}
PI_RegisterPlugins (snd_output_list);
PI_RegisterPlugins (snd_render_list);
snd_output_module = PI_LoadPlugin ("snd_output", snd_output->string);
if (!snd_output_module) {
Sys_Printf ("Loading of sound output module: %s failed!\n",

View file

@ -55,6 +55,7 @@ static const char rcsid[] =
#endif
#include "QF/cvar.h"
#include "QF/hash.h"
#include "QF/plugin.h"
#include "QF/sys.h"
@ -62,6 +63,13 @@ static const char rcsid[] =
cvar_t *fs_pluginpath;
hashtab_t *registered_plugins;
static const char *
get_key (void *pl, void *unused)
{
return ((plugin_list_t *)pl)->name;
}
void
PI_InitCvars (void)
@ -80,6 +88,7 @@ void
PI_Init (void)
{
PI_InitCvars ();
registered_plugins = Hash_NewTable (253, get_key, 0, 0);
}
void
@ -91,27 +100,38 @@ plugin_t *
PI_LoadPlugin (const char *type, const char *name)
{
char realname[4096];
char plugin_name[1024];
char plugin_info_name[1024];
char *tmpname;
void *dlhand = NULL;
plugin_t *plugin = NULL;
P_PluginInfo plugin_info = NULL;
plugin_list_t *pl;
if (!name)
return NULL;
tmpname = strrchr (name, '/'); // Get the base name, don't allow paths
// Build the plugin name
snprintf (plugin_name, sizeof (plugin_name), "%s_%s", type, name);
pl = Hash_Find (registered_plugins, plugin_name);
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);
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)
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)
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
# error "No shared library support. FIXME"
return NULL;
@ -147,6 +167,7 @@ PI_LoadPlugin (const char *type, const char *name)
return NULL;
}
#endif
}
if (!(plugin = plugin_info ())) { // Something went badly wrong
#if defined(HAVE_DLOPEN)
@ -173,9 +194,18 @@ PI_UnloadPlugin (plugin_t *plugin)
} else {
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)
return (dlclose (plugin->handle) == 0);
#elif defined (_WIN32)
return (FreeLibrary (plugin->handle) == 0);
#endif
}
void
PI_RegisterPlugins (plugin_list_t *plugins)
{
while (plugins->name)
Hash_Add (registered_plugins, plugins++);
}