Skip to content

Commit

Permalink
Refactor: Rely on library type ID to detect audio plugins
Browse files Browse the repository at this point in the history
Instead of checking the filename to see if a plugin is an audio plugin,
use the type identifier ("deng-plugin/audio" for audio plugins).

The audio plugins still use the "audio_" prefix so that AudioDriver can
locate them without checking each plugin's type, which requires loading
them into memory.
  • Loading branch information
skyjake committed Oct 18, 2012
1 parent a56d0a5 commit de35f54
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 21 deletions.
6 changes: 0 additions & 6 deletions doomsday/engine/api/dd_plugin.h
Expand Up @@ -40,12 +40,6 @@
*/
#define DENG_PLUGIN_GLOBAL(name) __DengPlugin_##name

#if defined(UNIX) && !defined(MACOSX)
# define DENG_AUDIO_PLUGIN_NAME_PREFIX "libaudio_"
#else
# define DENG_AUDIO_PLUGIN_NAME_PREFIX "audio_"
#endif

#define MAX_HOOKS 16
#define HOOKF_EXCLUSIVE 0x01000000

Expand Down
10 changes: 10 additions & 0 deletions doomsday/engine/portable/include/library.h
Expand Up @@ -82,6 +82,16 @@ Library* Library_New(const char* filePath);

void Library_Delete(Library* lib);

/**
* Returns the type identifier of the library.
* @see de::Library
*
* @param lib Library instance.
*
* @return Type identifier string, e.g., "deng-plugin/game".
*/
const char* Library_Type(const Library* lib);

/**
* Looks up a symbol from the library.
*
Expand Down
21 changes: 15 additions & 6 deletions doomsday/engine/portable/src/audiodriver.c
Expand Up @@ -124,20 +124,29 @@ static void importInterfaces(driver_t* d)

static int audioPluginFinder(const char* fileName, const char* absPath, void* ptr)
{
int i;
Str* path = (Str*) ptr;
if(!strncmp(fileName, Str_Text(path), Str_Length(path)) /* matching name? */ &&
(strlen(fileName) == Str_Length(path) /* no extension? */
|| fileName[Str_Length(path)] == '.' /* extension follows right away? */))

// Check if the filename matches: [*]audio_name[.ext]
// (a regex or pattern match routine wouldn't hurt...)

for(i = 0; i <= strlen(fileName) - Str_Size(path); ++i)
{
Str_Set(path, absPath);
return true; // Found it!
const char* fn = fileName + i;
if(!strncmp(fn, Str_Text(path), Str_Length(path)) /* matching name? */ &&
(strlen(fn) == Str_Length(path) /* no extension? */
|| fn[Str_Length(path)] == '.' /* extension follows right away? */))
{
Str_Set(path, absPath);
return true; // Found it!
}
}
return false; // Keep looking...
}

static AutoStr* findAudioPluginPath(const char* name)
{
AutoStr* path = Str_Appendf(AutoStr_New(), "%s%s", DENG_AUDIO_PLUGIN_NAME_PREFIX, name);
AutoStr* path = Str_Appendf(AutoStr_New(), "audio_%s", name);
if(Library_IterateAvailableLibraries(audioPluginFinder, path))
{
// The full path of the library was returned in @a path.
Expand Down
14 changes: 7 additions & 7 deletions doomsday/engine/portable/src/dd_plugin.c
Expand Up @@ -69,20 +69,20 @@ static int loadPlugin(const char* fileName, const char* pluginPath, void* param)
DENG_ASSERT(fileName && fileName[0]);
DENG_ASSERT(pluginPath && pluginPath[0]);

if(!strncmp(fileName, DENG_AUDIO_PLUGIN_NAME_PREFIX,
strlen(DENG_AUDIO_PLUGIN_NAME_PREFIX)))
{
// The audio plugins are loaded later on demand by AudioDriver.
return false;
}

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)
{
Expand Down
11 changes: 11 additions & 0 deletions doomsday/engine/portable/src/library.cpp
Expand Up @@ -32,6 +32,7 @@ struct library_s { // typedef Library
Str* path;
de::LibraryFile* libFile;
bool isGamePlugin;
std::string typeId;

library_s() : path(0), libFile(0), isGamePlugin(false) {}
};
Expand Down Expand Up @@ -109,6 +110,7 @@ Library* Library_New(const char* filePath)
Library* lib = new Library;
lib->libFile = &libFile;
lib->path = Str_Set(Str_NewStd(), filePath);
lib->typeId = libFile.library().type().toStdString();
loadedLibs.append(lib);

// Symbols from game plugins conflict with each other, so we have to
Expand All @@ -131,12 +133,21 @@ Library* Library_New(const char* filePath)
void Library_Delete(Library *lib)
{
DENG_ASSERT(lib);

// Unload the library from memory.
lib->libFile->clear();

Str_Delete(lib->path);
loadedLibs.removeOne(lib);
delete lib;
}

const char* Library_Type(const Library* lib)
{
DENG_ASSERT(lib);
return &lib->typeId[0];
}

void* Library_Symbol(Library* lib, const char* symbolName)
{
try
Expand Down
6 changes: 4 additions & 2 deletions doomsday/libdeng2/include/de/core/library.h
Expand Up @@ -133,8 +133,10 @@ namespace de
virtual ~Library();

/**
* Returns the type identifier of the library. This affects how libdeng2
* will treat the library.
* Returns the type identifier of the library. This affects how
* libdeng2 will treat the library. The type is determined
* automatically when the library is first loaded, and can then be
* queried at any time even after the library has been unloaded.
*/
const String& type() const { return _type; }

Expand Down

0 comments on commit de35f54

Please sign in to comment.