Skip to content

Commit

Permalink
Fix several bugs with undocumented feature of Bass.PluginLoad() (#87)
Browse files Browse the repository at this point in the history
* Fix several bugs with undocumented feature of Bass.PluginLoad()

The first bug here is with path handling. The code splits the directory name from the file name then merges them together but does not put a file seperator between the two.

The second bug is with the state that it leaves Bass.LastError in after trying to load the plugin. If the plugin fails for any reason such as Errors.FileFormat, or Errors.Already that will get overwritten by later calls for the different platforms.

* Fix issue with multi-platform plugins in the same directory
  • Loading branch information
mdsitton committed Feb 25, 2021
1 parent 0cdc873 commit 43f9d4b
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/Bass/Shared/Bass/PInvoke/Plugin.cs
Expand Up @@ -68,11 +68,32 @@ public static int PluginLoad(string FilePath)

var dir = Path.GetDirectoryName(FilePath);
var fileName = Path.GetFileName(FilePath);

// Try for Windows, Linux/Android and OSX Libraries respectively.
return new[] { $"{dir}{fileName}.dll", $"{dir}lib{fileName}.so", $"{dir}lib{fileName}.dylib" }
.Select(PluginLoad)
.FirstOrDefault(HLib => HLib != 0);
string[] paths = new[] {
Path.Combine(dir, $"{fileName}.dll"),
Path.Combine(dir, $"lib{fileName}.so"),
Path.Combine(dir, $"lib{fileName}.dylib")
};

foreach (string path in paths)
{
// Check if the file exists before trying to load plugin otherwise Bass.LastError can be overwritten by multiple calls.
if (File.Exists(path))
{
// Only return if we have a valid handle. If we don't keep trying the other files.
var rtnVal = BASS_PluginLoad(path);

// Errors.Already means the plugin is already loaded and we have found the proper plugin, we should return in this case
if (rtnVal != 0 || Bass.LastError == Errors.Already)
{
return rtnVal;
}
}
}

// Fall back to just returning the result of BASS_PluginLoad so Bass.LastError is set correctly
return BASS_PluginLoad(FilePath);
#endif
}
#endregion
Expand Down

0 comments on commit 43f9d4b

Please sign in to comment.