Skip to content

Commit

Permalink
Improve findSearchPaths()
Browse files Browse the repository at this point in the history
  • Loading branch information
messmerd committed Dec 4, 2023
1 parent e9708d6 commit 7c81092
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
8 changes: 4 additions & 4 deletions include/ClapManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
#include "ClapInstance.h"

#include <vector>
#include <unordered_set>
#include <string_view>
#include <memory>

#include <QString>
#include <clap/clap.h>

namespace lmms
{
Expand Down Expand Up @@ -86,12 +86,12 @@ class ClapManager
void findSearchPaths();

//! Returns search paths found by prior call to findSearchPaths()
auto getSearchPaths() const -> const std::vector<std::filesystem::path>& { return m_searchPaths; }
auto searchPaths() const -> const auto& { return m_searchPaths; }

//! Finds and loads all .clap files in the provided search paths @p searchPaths
void loadClapFiles(const std::vector<std::filesystem::path>& searchPaths);
void loadClapFiles(const std::unordered_set<std::filesystem::path>& searchPaths);

std::vector<std::filesystem::path> m_searchPaths; //!< Owns all CLAP search paths; Populated by findSearchPaths()
std::unordered_set<std::filesystem::path> m_searchPaths; //!< Owns all CLAP search paths; Populated by findSearchPaths()
std::vector<ClapFile> m_files; //!< Owns all loaded .clap files; Populated by loadClapFiles()

// Non-owning plugin caches (for fast iteration/lookup)
Expand Down
41 changes: 17 additions & 24 deletions src/core/clap/ClapManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void ClapManager::initPlugins()
}
qDebug() << "Found .clap files:";
}
loadClapFiles(getSearchPaths());
loadClapFiles(searchPaths());
}

void ClapManager::findSearchPaths()
Expand All @@ -111,27 +111,20 @@ void ClapManager::findSearchPaths()
// Parses a string of paths, adding results to m_searchPaths
auto parsePaths = [this](const char* pathString) {
if (!pathString) { return; }
std::error_code ec;
auto paths = std::string_view{pathString};
auto pos = paths.find(LADSPA_PATH_SEPERATOR);
while (pos != std::string_view::npos)
std::size_t pos = 0;
do
{
auto path = expandHomeDir(paths.substr(0, pos));
if (fs::is_directory(path, ec))
{
m_searchPaths.emplace_back(std::move(path.make_preferred()));
}
paths = paths.substr(pos + 1);
if (paths.size() <= pos) { break; }
paths.remove_prefix(pos);
pos = paths.find(LADSPA_PATH_SEPERATOR);
}
if (!paths.empty())
{
auto path = expandHomeDir(paths);
if (fs::is_directory(path, ec))
if (pos == 0) { continue; }
auto path = expandHomeDir(paths.substr(0, pos));
if (std::error_code ec; fs::is_directory(path, ec))
{
m_searchPaths.emplace_back(std::move(path.make_preferred()));
m_searchPaths.emplace(std::move(path.make_preferred()));
}
}
} while (pos++ != std::string_view::npos);
};

// Use LMMS_CLAP_PATH to override all of CLAP's default search paths
Expand All @@ -152,12 +145,12 @@ void ClapManager::findSearchPaths()
auto path = expandHomeDir("~/.clap");
if (fs::is_directory(path, ec))
{
m_searchPaths.emplace_back(std::move(path.make_preferred()));
m_searchPaths.emplace(std::move(path.make_preferred()));
}
path = "/usr/lib/clap";
if (fs::is_directory(path, ec))
{
m_searchPaths.emplace_back(std::move(path.make_preferred()));
m_searchPaths.emplace(std::move(path.make_preferred()));
}
#elif defined(LMMS_BUILD_WIN32) || defined(LMMS_BUILD_WIN64)
// %COMMONPROGRAMFILES%\CLAP
Expand All @@ -168,15 +161,15 @@ void ClapManager::findSearchPaths()
auto path = fs::path{commonProgFiles} / "CLAP";
if (fs::is_directory(path, ec))
{
m_searchPaths.emplace_back(std::move(path.make_preferred()));
m_searchPaths.emplace(std::move(path.make_preferred()));
}
}
if (auto localAppData = std::getenv("LOCALAPPDATA"))
{
auto path = fs::path{localAppData} / "Programs/Common/CLAP";
if (fs::is_directory(path, ec))
{
m_searchPaths.emplace_back(std::move(path.make_preferred()));
m_searchPaths.emplace(std::move(path.make_preferred()));
}
}
#elif defined(LMMS_BUILD_APPLE)
Expand All @@ -186,17 +179,17 @@ void ClapManager::findSearchPaths()
auto path = fs::path{"/Library/Audio/Plug-Ins/CLAP"};
if (fs::is_directory(path, ec))
{
m_searchPaths.emplace_back(std::move(path.make_preferred()));
m_searchPaths.emplace(std::move(path.make_preferred()));
}
path = expandHomeDir("~/Library/Audio/Plug-Ins/CLAP");
if (fs::is_directory(path, ec))
{
m_searchPaths.emplace_back(std::move(path.make_preferred()));
m_searchPaths.emplace(std::move(path.make_preferred()));
}
#endif
}

void ClapManager::loadClapFiles(const std::vector<std::filesystem::path>& searchPaths)
void ClapManager::loadClapFiles(const std::unordered_set<std::filesystem::path>& searchPaths)
{
if (!m_files.empty()) { return; } // Cannot unload CLAP plugins yet

Expand Down

0 comments on commit 7c81092

Please sign in to comment.