From e231c66f04ba7d21129e5d9465a79afa7b733eef Mon Sep 17 00:00:00 2001 From: Moritz Maxeiner <moritz@ucworks.org> Date: Fri, 2 Nov 2018 10:02:02 +0100 Subject: [PATCH] Do not ignore manually specified plugins, use generic std::filesystem functionality --- SrcBehaviorLoader/PluginLoader.cpp | 61 ++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/SrcBehaviorLoader/PluginLoader.cpp b/SrcBehaviorLoader/PluginLoader.cpp index 5adabb6..11e3a5a 100644 --- a/SrcBehaviorLoader/PluginLoader.cpp +++ b/SrcBehaviorLoader/PluginLoader.cpp @@ -134,6 +134,12 @@ bool WinSetEnv(const char* name, const char* toWhat){ } #endif +bool is_shared_library(std::filesystem::path const& p) +{ + auto extension = p.extension(); + return extension == ".so" || extension == ".dylib" || extension == ".dll"; +} + const char* PluginLoader::addDllPath(std::string f) { //Get the directory of the DLL/*.so and add it to the PATH env variable. @@ -180,26 +186,41 @@ std::vector<std::string> PluginLoader::searchDirectoriesForPlugins(std::vector<s //Search directories std::vector<std::string> filesFromFolders; - for (auto f: list) { - std::string file = f; - try { - if (!file.empty() && file[file.size() - 1] == '/') { - for (auto& p : std::filesystem::directory_iterator(file)) { - std::string s = p.path().string(); - if(validSuffix(s, suffix)) - filesFromFolders.push_back(s); - } - } - else { - if(validSuffix(f, suffix)) - filesFromFolders.push_back(f); - } - } - catch (...){ - qWarning() << "Could not read file/directory: " << file.c_str(); - } - } - + for (auto f: list) + { + auto path = std::filesystem::path(f); + if (path.empty()) + { + continue; + } + if (std::filesystem::is_directory(path)) + { + try + { + for (auto& e : std::filesystem::directory_iterator(path)) + { + auto p = e.path(); + if (is_shared_library(p) && p.replace_extension().extension() == ".robo_tracker") + { + filesFromFolders.push_back(p.string()); + } + } + } + catch (std::filesystem::filesystem_error const& e) + { + qWarning() << e.what(); + } + } + else if (is_shared_library(path)) + { + filesFromFolders.push_back(f); + } + else + { + qWarning() << "Neither a directory, nor a shared library:" << f.data(); + } + } + return filesFromFolders; } -- GitLab