PluginManager.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * PluginManager.h
  3. * ---------------
  4. * Purpose: Plugin management
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #pragma once
  10. #include "openmpt/all/BuildSettings.hpp"
  11. OPENMPT_NAMESPACE_BEGIN
  12. constexpr int32 PLUGMAGIC(char a, char b, char c, char d) noexcept
  13. {
  14. return static_cast<int32>((static_cast<uint32>(a) << 24) | (static_cast<uint32>(b) << 16) | (static_cast<uint32>(c) << 8) | (static_cast<uint32>(d) << 0));
  15. }
  16. //#define kBuzzMagic PLUGMAGIC('B', 'u', 'z', 'z')
  17. inline constexpr int32 kDmoMagic = PLUGMAGIC('D', 'X', 'M', 'O');
  18. class CSoundFile;
  19. class IMixPlugin;
  20. struct SNDMIXPLUGIN;
  21. enum PluginArch : int;
  22. struct VSTPluginLib
  23. {
  24. public:
  25. enum PluginCategory : uint8
  26. {
  27. // Same plugin categories as defined in VST SDK
  28. catUnknown = 0,
  29. catEffect, // Simple Effect
  30. catSynth, // VST Instrument (Synths, samplers,...)
  31. catAnalysis, // Scope, Tuner, ...
  32. catMastering, // Dynamics, ...
  33. catSpacializer, // Panners, ...
  34. catRoomFx, // Delays and Reverbs
  35. catSurroundFx, // Dedicated surround processor
  36. catRestoration, // Denoiser, ...
  37. catOfflineProcess, // Offline Process
  38. catShell, // Plug-in is container of other plug-ins
  39. catGenerator, // Tone Generator, ...
  40. // Custom categories
  41. catDMO, // DirectX media object plugin
  42. catHidden, // For internal plugins that should not be visible to the user (e.g. because they only exist for legacy reasons)
  43. numCategories
  44. };
  45. public:
  46. using CreateProc = IMixPlugin *(*)(VSTPluginLib &factory, CSoundFile &sndFile, SNDMIXPLUGIN *mixStruct);
  47. IMixPlugin *pPluginsList = nullptr; // Pointer to first plugin instance (this instance carries pointers to other instances)
  48. CreateProc Create; // Factory to call for this plugin
  49. mpt::PathString libraryName; // Display name
  50. mpt::PathString dllPath; // Full path name
  51. #ifdef MODPLUG_TRACKER
  52. mpt::ustring tags; // User tags
  53. CString vendor;
  54. #endif // MODPLUG_TRACKER
  55. int32 pluginId1 = 0; // Plugin type (kEffectMagic, kDmoMagic, ...)
  56. int32 pluginId2 = 0; // Plugin unique ID
  57. PluginCategory category = catUnknown;
  58. const bool isBuiltIn : 1;
  59. bool isInstrument : 1;
  60. bool useBridge : 1, shareBridgeInstance : 1, modernBridge : 1;
  61. protected:
  62. mutable uint8 dllArch = 0;
  63. public:
  64. VSTPluginLib(CreateProc factoryProc, bool isBuiltIn, const mpt::PathString &dllPath, const mpt::PathString &libraryName
  65. #ifdef MODPLUG_TRACKER
  66. , const mpt::ustring &tags = mpt::ustring(), const CString &vendor = CString()
  67. #endif // MODPLUG_TRACKER
  68. )
  69. : Create(factoryProc)
  70. , libraryName(libraryName), dllPath(dllPath)
  71. #ifdef MODPLUG_TRACKER
  72. , tags(tags)
  73. , vendor(vendor)
  74. #endif // MODPLUG_TRACKER
  75. , category(catUnknown)
  76. , isBuiltIn(isBuiltIn), isInstrument(false)
  77. , useBridge(false), shareBridgeInstance(true), modernBridge(true)
  78. {
  79. }
  80. #ifdef MPT_WITH_VST
  81. // Get native phost process arch encoded as plugin arch
  82. static uint8 GetNativePluginArch();
  83. static mpt::ustring GetPluginArchName(uint8 arch);
  84. static mpt::ustring GetPluginArchNameUser(uint8 arch);
  85. // Check whether a plugin can be hosted inside OpenMPT or requires bridging
  86. uint8 GetDllArch(bool fromCache = true) const;
  87. mpt::ustring GetDllArchName(bool fromCache = true) const;
  88. mpt::ustring GetDllArchNameUser(bool fromCache = true) const;
  89. bool IsNative(bool fromCache = true) const;
  90. // Check if a plugin is native, and if it is currently unknown, assume that it is native. Use this function only for performance reasons
  91. // (e.g. if tons of unscanned plugins would slow down generation of the plugin selection dialog)
  92. bool IsNativeFromCache() const;
  93. #endif // MPT_WITH_VST
  94. void WriteToCache() const;
  95. uint32 EncodeCacheFlags() const
  96. {
  97. // Format: 00000000.0000000M.AAAAAASB.CCCCCCCI
  98. return (isInstrument ? 1 : 0)
  99. | (category << 1)
  100. | (useBridge ? 0x100 : 0)
  101. | (shareBridgeInstance ? 0x200 : 0)
  102. | ((dllArch / 8) << 10)
  103. | (modernBridge ? 0x10000 : 0)
  104. ;
  105. }
  106. void DecodeCacheFlags(uint32 flags)
  107. {
  108. category = static_cast<PluginCategory>((flags & 0xFF) >> 1);
  109. if(category >= numCategories)
  110. {
  111. category = catUnknown;
  112. }
  113. if(flags & 1)
  114. {
  115. isInstrument = true;
  116. category = catSynth;
  117. }
  118. useBridge = (flags & 0x100) != 0;
  119. shareBridgeInstance = (flags & 0x200) != 0;
  120. dllArch = ((flags >> 10) & 0x3F) * 8;
  121. modernBridge = (flags & 0x10000) != 0;
  122. }
  123. };
  124. class CVstPluginManager
  125. {
  126. #ifndef NO_PLUGINS
  127. protected:
  128. #if defined(MPT_WITH_DMO)
  129. bool MustUnInitilizeCOM = false;
  130. #endif
  131. std::vector<VSTPluginLib *> pluginList;
  132. public:
  133. CVstPluginManager();
  134. ~CVstPluginManager();
  135. using iterator = std::vector<VSTPluginLib *>::iterator;
  136. using const_iterator = std::vector<VSTPluginLib *>::const_iterator;
  137. iterator begin() { return pluginList.begin(); }
  138. const_iterator begin() const { return pluginList.begin(); }
  139. iterator end() { return pluginList.end(); }
  140. const_iterator end() const { return pluginList.end(); }
  141. void reserve(size_t num) { pluginList.reserve(num); }
  142. size_t size() const { return pluginList.size(); }
  143. bool IsValidPlugin(const VSTPluginLib *pLib) const;
  144. VSTPluginLib *AddPlugin(const mpt::PathString &dllPath, bool maskCrashes, const mpt::ustring &tags = mpt::ustring(), bool fromCache = true, bool *fileFound = nullptr);
  145. bool RemovePlugin(VSTPluginLib *);
  146. bool CreateMixPlugin(SNDMIXPLUGIN &, CSoundFile &);
  147. void OnIdle();
  148. static void ReportPlugException(const mpt::ustring &msg);
  149. protected:
  150. void EnumerateDirectXDMOs();
  151. #else // NO_PLUGINS
  152. public:
  153. const VSTPluginLib **begin() const { return nullptr; }
  154. const VSTPluginLib **end() const { return nullptr; }
  155. void reserve(size_t) { }
  156. size_t size() const { return 0; }
  157. void OnIdle() {}
  158. #endif // NO_PLUGINS
  159. };
  160. OPENMPT_NAMESPACE_END