Portability fixes. plugin.c will, in all likelihood, work on Windows now.

This commit is contained in:
Jeff Teunissen 2001-06-27 08:34:48 +00:00
parent 06227cf37d
commit 76e28aeaaa

View file

@ -42,6 +42,10 @@
# include <strings.h> # include <strings.h>
#endif #endif
#ifdef HAVE_WINDOWS_H
# include <windows.h>
#endif
#ifdef HAVE_DLFCN_H #ifdef HAVE_DLFCN_H
# include <dlfcn.h> # include <dlfcn.h>
#endif #endif
@ -86,7 +90,6 @@ PI_Shutdown (void)
plugin_t * plugin_t *
PI_LoadPlugin (char *type, char *name) PI_LoadPlugin (char *type, char *name)
{ {
#ifdef HAVE_DLOPEN
char realname[4096]; char realname[4096];
char *tmpname; char *tmpname;
void *dlhand = NULL; void *dlhand = NULL;
@ -99,31 +102,55 @@ PI_LoadPlugin (char *type, char *name)
tmpname = strrchr (name, '/'); // Get the base name, don't allow paths tmpname = strrchr (name, '/'); // Get the base name, don't allow paths
// Build the path to the file to load // Build the path to the file to load
#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)
snprintf (realname, sizeof (realname), "%s/QF%s_%s.dll",
fs_pluginpath->string, type, (tmpname ? tmpname + 1 : name));
#else
# error "No shared library support. FIXME"
return NULL;
#endif
#if defined(HAVE_DLOPEN)
if (!(dlhand = dlopen (realname, RTLD_LAZY))) { // lib not found if (!(dlhand = dlopen (realname, RTLD_LAZY))) { // lib not found
Con_Printf ("%s\n", dlerror()); Con_Printf ("Could not load plugin \"%s\": %s\n", realname, dlerror ());
return NULL; return NULL;
} }
#elif defined (_WIN32)
if (!(dlhand = LoadLibrary (realname))) { // lib not found
Con_Printf ("Could not load plugin \"%s\".\n", realname);
return NULL;
}
#endif
#if defined(HAVE_DLOPEN)
if (!(plugin_info = dlsym (dlhand, "PluginInfo"))) { // info function not found if (!(plugin_info = dlsym (dlhand, "PluginInfo"))) { // info function not found
dlclose (dlhand); dlclose (dlhand);
Con_Printf ("info function not found\n"); Con_Printf ("Plugin info function not found\n");
return NULL; return NULL;
} }
#elif defined (_WIN32)
if (!(plugin_info = GetProcAddress (dlhand, "PluginInfo"))) { // info function not found
FreeLibrary (dlhand);
Con_Printf ("Plugin info function not found\n");
return NULL;
}
#endif
if (!(plugin = plugin_info ())) { // Something went badly wrong if (!(plugin = plugin_info ())) { // Something went badly wrong
#if defined(HAVE_DLOPEN)
dlclose (dlhand); dlclose (dlhand);
Con_Printf ("soemthing went badly wrong\n"); #elif defined (_WIN32)
FreeLibrary (dlhand);
#endif
Con_Printf ("Something went badly wrong.\n");
return NULL; return NULL;
} }
plugin->handle = dlhand; plugin->handle = dlhand;
return plugin; return plugin;
#else
return NULL; // no plugin support
#endif
} }
qboolean qboolean
@ -137,9 +164,9 @@ PI_UnloadPlugin (plugin_t *plugin)
} else { } else {
Con_Printf ("Warning: No shutdown function for plugin!"); Con_Printf ("Warning: No shutdown function for plugin!");
} }
#ifdef HAVE_DLOPEN #if defined(HAVE_DLOPEN)
return (dlclose (plugin->handle) == 0); return (dlclose (plugin->handle) == 0);
#else #elif defined (_WIN32)
return false; return (FreeLibrary (plugin->handle) == 0);
#endif #endif
} }