The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. static StringArray readDeadMansPedalFile (const File& file)
  16. {
  17. StringArray lines;
  18. file.readLines (lines);
  19. lines.removeEmptyStrings();
  20. return lines;
  21. }
  22. PluginDirectoryScanner::PluginDirectoryScanner (KnownPluginList& listToAddTo,
  23. AudioPluginFormat& formatToLookFor,
  24. FileSearchPath directoriesToSearch,
  25. const bool recursive,
  26. const File& deadMansPedal,
  27. bool allowPluginsWhichRequireAsynchronousInstantiation)
  28. : list (listToAddTo),
  29. format (formatToLookFor),
  30. deadMansPedalFile (deadMansPedal),
  31. allowAsync (allowPluginsWhichRequireAsynchronousInstantiation)
  32. {
  33. directoriesToSearch.removeRedundantPaths();
  34. setFilesOrIdentifiersToScan (format.searchPathsForPlugins (directoriesToSearch, recursive, allowAsync));
  35. }
  36. PluginDirectoryScanner::~PluginDirectoryScanner()
  37. {
  38. list.scanFinished();
  39. }
  40. //==============================================================================
  41. void PluginDirectoryScanner::setFilesOrIdentifiersToScan (const StringArray& filesOrIdentifiers)
  42. {
  43. filesOrIdentifiersToScan = filesOrIdentifiers;
  44. // If any plugins have crashed recently when being loaded, move them to the
  45. // end of the list to give the others a chance to load correctly..
  46. for (auto& crashed : readDeadMansPedalFile (deadMansPedalFile))
  47. for (int j = filesOrIdentifiersToScan.size(); --j >= 0;)
  48. if (crashed == filesOrIdentifiersToScan[j])
  49. filesOrIdentifiersToScan.move (j, -1);
  50. applyBlacklistingsFromDeadMansPedal (list, deadMansPedalFile);
  51. nextIndex.set (filesOrIdentifiersToScan.size());
  52. }
  53. String PluginDirectoryScanner::getNextPluginFileThatWillBeScanned() const
  54. {
  55. return format.getNameOfPluginFromIdentifier (filesOrIdentifiersToScan [nextIndex.get() - 1]);
  56. }
  57. void PluginDirectoryScanner::updateProgress()
  58. {
  59. progress = (1.0f - nextIndex.get() / (float) filesOrIdentifiersToScan.size());
  60. }
  61. bool PluginDirectoryScanner::scanNextFile (bool dontRescanIfAlreadyInList,
  62. String& nameOfPluginBeingScanned)
  63. {
  64. const int index = --nextIndex;
  65. if (index >= 0)
  66. {
  67. auto file = filesOrIdentifiersToScan [index];
  68. if (file.isNotEmpty() && ! (dontRescanIfAlreadyInList && list.isListingUpToDate (file, format)))
  69. {
  70. nameOfPluginBeingScanned = format.getNameOfPluginFromIdentifier (file);
  71. OwnedArray<PluginDescription> typesFound;
  72. // Add this plugin to the end of the dead-man's pedal list in case it crashes...
  73. auto crashedPlugins = readDeadMansPedalFile (deadMansPedalFile);
  74. crashedPlugins.removeString (file);
  75. crashedPlugins.add (file);
  76. setDeadMansPedalFile (crashedPlugins);
  77. list.scanAndAddFile (file, dontRescanIfAlreadyInList, typesFound, format);
  78. // Managed to load without crashing, so remove it from the dead-man's-pedal..
  79. crashedPlugins.removeString (file);
  80. setDeadMansPedalFile (crashedPlugins);
  81. if (typesFound.size() == 0 && ! list.getBlacklistedFiles().contains (file))
  82. failedFiles.add (file);
  83. }
  84. }
  85. updateProgress();
  86. return index > 0;
  87. }
  88. bool PluginDirectoryScanner::skipNextFile()
  89. {
  90. updateProgress();
  91. return --nextIndex > 0;
  92. }
  93. void PluginDirectoryScanner::setDeadMansPedalFile (const StringArray& newContents)
  94. {
  95. if (deadMansPedalFile.getFullPathName().isNotEmpty())
  96. deadMansPedalFile.replaceWithText (newContents.joinIntoString ("\n"), true, true);
  97. }
  98. void PluginDirectoryScanner::applyBlacklistingsFromDeadMansPedal (KnownPluginList& list, const File& file)
  99. {
  100. // If any plugins have crashed recently when being loaded, move them to the
  101. // end of the list to give the others a chance to load correctly..
  102. for (auto& crashedPlugin : readDeadMansPedalFile (file))
  103. list.addToBlacklist (crashedPlugin);
  104. }
  105. } // namespace juce