File: PluginManager.cpp

package info (click to toggle)
audacity 3.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 125,252 kB
  • sloc: cpp: 358,238; ansic: 75,458; lisp: 7,761; sh: 3,410; python: 1,503; xml: 1,385; perl: 854; makefile: 122
file content (1561 lines) | stat: -rw-r--r-- 49,337 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
/**********************************************************************

  Audacity: A Digital Audio Editor

  PluginManager.cpp

  Leland Lucius

*******************************************************************//*!

\file PluginManager.cpp
\brief

************************************************************************//**
\class PluginManager
\brief PluginManager maintains a list of all plug ins.  That covers modules,
effects, generators, analysis-effects, commands.  It also has functions
for shared and private configs - which need to move out.
*****************************************************************************/


#include "PluginManager.h"



#include <algorithm>

#include <wx/log.h>
#include <wx/tokenzr.h>

#include "BasicUI.h"
#include "PluginProvider.h"

#include "Internat.h" // for macro XO
#include "FileNames.h"
#include "MemoryX.h"
#include "ModuleManager.h"
#include "PlatformCompatibility.h"
#include "Base64.h"
#include "Variant.h"

///////////////////////////////////////////////////////////////////////////////
//
// PluginManager
//
///////////////////////////////////////////////////////////////////////////////

// Registry has the list of plug ins
#define REGVERKEY wxString(wxT("/pluginregistryversion"))
#define REGROOT wxString(wxT("/pluginregistry/"))
#define REGCUSTOMPATHS wxString(wxT("/providercustompaths"))

// Settings has the values of the plug in settings.
#define SETVERKEY wxString(wxT("/pluginsettingsversion"))
#define SETVERCUR wxString(wxT("1.0"))
#define SETROOT wxString(wxT("/pluginsettings/"))

#define KEY_ID                         wxT("ID")
#define KEY_PATH                       wxT("Path")
#define KEY_SYMBOL                     wxT("Symbol")
#define KEY_NAME                       wxT("Name")
#define KEY_VENDOR                     wxT("Vendor")
#define KEY_VERSION                    wxT("Version")
#define KEY_DESCRIPTION                wxT("Description")
#define KEY_LASTUPDATED                wxT("LastUpdated")
#define KEY_ENABLED                    wxT("Enabled")
#define KEY_VALID                      wxT("Valid")
#define KEY_PROVIDERID                 wxT("ProviderID")
#define KEY_EFFECTTYPE                 wxT("EffectType")
#define KEY_EFFECTFAMILY               wxT("EffectFamily")
#define KEY_EFFECTDEFAULT              wxT("EffectDefault")
#define KEY_EFFECTINTERACTIVE          wxT("EffectInteractive")
#define KEY_EFFECTREALTIME             wxT("EffectRealtime")
#define KEY_EFFECTAUTOMATABLE          wxT("EffectAutomatable")
#define KEY_EFFECTTYPE_NONE            wxT("None")
#define KEY_EFFECTTYPE_ANALYZE         wxT("Analyze")
#define KEY_EFFECTTYPE_GENERATE        wxT("Generate")
#define KEY_EFFECTTYPE_PROCESS         wxT("Process")
#define KEY_EFFECTTYPE_TOOL            wxT("Tool")
#define KEY_EFFECTTYPE_HIDDEN          wxT("Hidden")
#define KEY_IMPORTERIDENT              wxT("ImporterIdent")
//#define KEY_IMPORTERFILTER             wxT("ImporterFilter")
#define KEY_IMPORTEREXTENSIONS         wxT("ImporterExtensions")

// ============================================================================
//
// PluginManagerInterface implementation
//
// ============================================================================

const PluginID &PluginManagerInterface::DefaultRegistrationCallback(
   PluginProvider *provider, ComponentInterface *pInterface )
{
   if(auto effectDefinitionInterface = dynamic_cast<EffectDefinitionInterface*>(pInterface))
      return PluginManager::Get().RegisterPlugin(provider, effectDefinitionInterface, PluginTypeEffect);
   return PluginManager::Get().RegisterPlugin(provider, pInterface);
}

const PluginID &PluginManagerInterface::AudacityCommandRegistrationCallback(
   PluginProvider *provider, ComponentInterface *pInterface )
{
   return PluginManager::Get().RegisterPlugin(provider, pInterface);
}

RegistryPath PluginManager::GetPluginEnabledSetting( const PluginID &ID ) const
{
   auto pPlugin = GetPlugin( ID );
   if ( pPlugin )
      return GetPluginEnabledSetting( *pPlugin );
   return {};
}

RegistryPath PluginManager::GetPluginEnabledSetting(
   const PluginDescriptor &desc ) const
{
   switch ( desc.GetPluginType() ) {
      case PluginTypeModule: {
         // Retrieve optional family symbol that was recorded in
         // RegisterPlugin() for the module
         auto family = desc.GetEffectFamily();
         if ( family.empty() ) // as for built-in effect and command modules
            return {};
         else
            return wxT('/') + family + wxT("/Enable");
      }
      case PluginTypeEffect:
         // do NOT use GetEffectFamily() for this descriptor, but instead,
         // delegate to the plugin descriptor of the provider, which may
         // be different (may be empty)
         return GetPluginEnabledSetting( desc.GetProviderID() );
      default:
         return {};
   }
}

bool PluginManager::IsPluginRegistered(
   const PluginPath &path, const TranslatableString *pName)
{
   for (auto &pair : mRegisteredPlugins) {
      if (auto &descriptor = pair.second; descriptor.GetPath() == path) {
         if (pName)
            descriptor.SetSymbol(
               { descriptor.GetSymbol().Internal(), *pName });
         return true;
      }
   }
   return false;
}

bool PluginManager::IsPluginLoaded(const wxString& ID) const
{
   return mLoadedInterfaces.find(ID) != mLoadedInterfaces.end();
}

void PluginManager::RegisterPlugin(PluginDescriptor&& desc)
{
   mRegisteredPlugins[desc.GetID()] = std::move(desc);
}

const PluginID & PluginManager::RegisterPlugin(PluginProvider *provider)
{
   PluginDescriptor & plug =
      CreatePlugin(GetID(provider), provider, PluginTypeModule);
   plug.SetEffectFamily(provider->GetOptionalFamilySymbol().Internal());

   plug.SetEnabled(true);
   plug.SetValid(true);

   return plug.GetID();
}

const PluginID & PluginManager::RegisterPlugin(
   PluginProvider *provider, ComponentInterface *command)
{
   PluginDescriptor & plug = CreatePlugin(GetID(command), command, (PluginType)PluginTypeAudacityCommand);

   plug.SetProviderID(PluginManager::GetID(provider));

   plug.SetEnabled(true);
   plug.SetValid(true);

   return plug.GetID();
}

const PluginID & PluginManager::RegisterPlugin(
   PluginProvider *provider, EffectDefinitionInterface *effect, int type)
{
   PluginDescriptor & plug = CreatePlugin(GetID(effect), effect, (PluginType)type);

   plug.SetProviderID(PluginManager::GetID(provider));

   plug.SetEffectType(effect->GetClassification());
   plug.SetEffectFamily(effect->GetFamily().Internal());
   plug.SetEffectInteractive(effect->IsInteractive());
   plug.SetEffectDefault(effect->IsDefault());
   plug.SetRealtimeSupport(effect->RealtimeSupport());
   plug.SetEffectAutomatable(effect->SupportsAutomation());

   plug.SetEnabled(true);
   plug.SetValid(true);

   return plug.GetID();
}

void PluginManager::FindFilesInPathList(const wxString & pattern,
                                        const FilePaths & pathList,
                                        FilePaths & files,
                                        bool directories)
{
   
   wxLogNull nolog;

   // Why bother...
   if (pattern.empty())
   {
      return;
   }

   // TODO:  We REALLY need to figure out the "Audacity" plug-in path(s)

   FilePaths paths;

   // Add the "per-user" plug-ins directory
   {
      const wxFileName &ff = FileNames::PlugInDir();
      paths.push_back(ff.GetFullPath());
   }
 
   // Add the "Audacity" plug-ins directory
   wxFileName ff = wxString { PlatformCompatibility::GetExecutablePath() };
#if defined(__WXMAC__)
   // Path ends for example in "Audacity.app/Contents/MacOSX"
   //ff.RemoveLastDir();
   //ff.RemoveLastDir();
   // just remove the MacOSX part.
   ff.RemoveLastDir();
#endif
   ff.AppendDir(wxT("plug-ins"));
   paths.push_back(ff.GetPath());

   // Weed out duplicates
   for (const auto &filePath : pathList)
   {
      ff = filePath;
      const wxString path{ ff.GetFullPath() };
      if (paths.Index(path, wxFileName::IsCaseSensitive()) == wxNOT_FOUND)
      {
         paths.push_back(path);
      }
   }

   // Find all matching files in each path
   for (size_t i = 0, cnt = paths.size(); i < cnt; i++)
   {
      ff = paths[i] + wxFILE_SEP_PATH + pattern;
      wxDir::GetAllFiles(ff.GetPath(), &files, ff.GetFullName(), directories ? wxDIR_DEFAULT : wxDIR_FILES);
   }

   return;
}

bool PluginManager::HasConfigGroup(ConfigurationType type, const PluginID & ID,
   const RegistryPath & group)
{
   return HasGroup(Group(type, ID, group));
}

bool PluginManager::GetConfigSubgroups(ConfigurationType type,
   const PluginID & ID, const RegistryPath & group, RegistryPaths & subgroups)
{
   return GetSubgroups(Group(type, ID, group), subgroups);
}

bool PluginManager::HasConfigValue(ConfigurationType type, const PluginID & ID,
      const RegistryPath & group, const RegistryPath & key)
{
   return HasConfigValue(Key(type, ID, group, key));
}

bool PluginManager::GetConfigValue(ConfigurationType type, const PluginID & ID,
   const RegistryPath & group, const RegistryPath & key,
   ConfigReference var, ConfigConstReference defval)
{
   return GetConfigValue(Key(type, ID, group, key), var, defval);
}

bool PluginManager::SetConfigValue(ConfigurationType type, const PluginID & ID,
   const RegistryPath & group, const RegistryPath & key,
   ConfigConstReference value)
{
   return SetConfigValue(Key(type, ID, group, key), value);
}

bool PluginManager::RemoveConfigSubgroup(ConfigurationType type,
   const PluginID & ID, const RegistryPath & group)
{
   bool result = GetSettings()->DeleteGroup(Group(type, ID, group));
   if (result)
   {
      GetSettings()->Flush();
   }

   return result;
}

bool PluginManager::RemoveConfig(ConfigurationType type, const PluginID & ID,
   const RegistryPath & group, const RegistryPath & key)
{
   bool result = GetSettings()->DeleteEntry(Key(type, ID, group, key));
   if (result)
   {
      GetSettings()->Flush();
   }

   return result;
}

// ============================================================================
//
// PluginManager
//
// ============================================================================

// ----------------------------------------------------------------------------
// Creation/Destruction
// ----------------------------------------------------------------------------

PluginManager::PluginManager()
{
   mSettings = NULL;
}

PluginManager::~PluginManager()
{
   // Ensure termination (harmless if already done)
   Terminate();
}

void PluginManager::InitializePlugins()
{
   ModuleManager & moduleManager = ModuleManager::Get();
   //ModuleManager::DiscoverProviders was called earlier, so we
   //can be sure that providers are already loaded

   //Check all known plugins to ensure they are still valid.
   for (auto it = mRegisteredPlugins.begin(); it != mRegisteredPlugins.end();) {
      auto &pluginDesc = it->second;
      const auto pluginType = pluginDesc.GetPluginType();
      if(pluginType == PluginTypeNone || pluginType == PluginTypeModule)
      {
         ++it;
         continue;
      }

      if(!moduleManager.CheckPluginExist(pluginDesc.GetProviderID(), pluginDesc.GetPath()))
         it = mRegisteredPlugins.erase(it);
      else
         ++it;
   }

   Save();
}

// ----------------------------------------------------------------------------
// PluginManager implementation
// ----------------------------------------------------------------------------

static PluginManager::ConfigFactory sFactory;

// ============================================================================
//
// Return reference to singleton
//
// (Thread-safe...no active threading during construction or after destruction)
// ============================================================================

PluginManager & PluginManager::Get()
{
   static PluginManager instance;
   return instance;
}

void PluginManager::Initialize(ConfigFactory factory)
{
   sFactory = move(factory);

   // Always load the registry first
   Load();

   // And force load of setting to verify it's accessible
   GetSettings();

   auto &mm = ModuleManager::Get();
   mm.DiscoverProviders();
   for (auto& [id, module] : mm.Providers()) {
      RegisterPlugin(module.get());
      // Allow the module to auto-register children
      module->AutoRegisterPlugins(*this);
   }

   InitializePlugins();
}

void PluginManager::Terminate()
{
   // Get rid of all non-module(effects?) plugins first
   for(auto& p : mRegisteredPlugins)
   {
      auto& desc = p.second;
      if(desc.GetPluginType() == PluginTypeEffect)
         mLoadedInterfaces.erase(desc.GetID());
   }

   // Now get rid of others
   mRegisteredPlugins.clear();
   mLoadedInterfaces.clear();
}

bool PluginManager::DropFile(const wxString &fileName)
{
   using namespace BasicUI;
   auto &mm = ModuleManager::Get();
   const wxFileName src{ fileName };

   for (auto &plug : PluginsOfType(PluginTypeModule)) {
      auto module = static_cast<PluginProvider *>
         (mm.CreateProviderInstance(plug.GetID(), plug.GetPath()));
      if (! module)
         continue;

      const auto &ff = module->InstallPath();
      const auto &extensions = module->GetFileExtensions();
      if ( !ff.empty() &&
          extensions.Index(src.GetExt(), false) != wxNOT_FOUND ) {
         TranslatableString errMsg;
         // Do dry-run test of the file format
         unsigned nPlugIns =
            module->DiscoverPluginsAtPath(fileName, errMsg, {});
         if (nPlugIns) {
            // File contents are good for this module, so check no others.
            // All branches of this block return true, even in case of
            // failure for other reasons, to signal that other drag-and-drop
            // actions should not be tried.

            // Find path to copy it
            wxFileName dst;
            dst.AssignDir( ff );
            dst.SetFullName( src.GetFullName() );
            if ( dst.Exists() ) {
               // Query whether to overwrite
               bool overwrite = (MessageBoxResult::Yes == ShowMessageBox(
                  XO("Overwrite the plug-in file %s?")
                     .Format( dst.GetFullPath() ),
                  MessageBoxOptions{}
                     .Caption(XO("Plug-in already exists"))
                     .ButtonStyle(Button::YesNo)));
               if ( !overwrite )
                  return true;
            }

            // Move the file or subtree
            bool copied = false;
            auto dstPath = dst.GetFullPath();
            if ( src.FileExists() )
               // A simple one-file plug-in
               copied = FileNames::DoCopyFile(
                  src.GetFullPath(), dstPath, true );
            else {
               // A sub-folder
               // such as for some VST packages
               // Recursive copy needed -- to do
               return true;
            }

            if (!copied) {
               ShowMessageBox(
                  XO("Plug-in file is in use. Failed to overwrite") );
               return true;
            }

            // Register for real
            std::vector<PluginID> ids;
            std::vector<wxString> names;
            nPlugIns = module->DiscoverPluginsAtPath(dstPath, errMsg,
               [&](PluginProvider *provider, ComponentInterface *ident)
                                                     -> const PluginID& {
                  // Register as by default, but also collecting the PluginIDs
                  // and names
                  auto &id = PluginManagerInterface::DefaultRegistrationCallback(
                        provider, ident);
                  ids.push_back(id);
                  names.push_back( ident->GetSymbol().Translation() );
                  return id;
               });
            if ( ! nPlugIns ) {
               // Unlikely after the dry run succeeded
               ShowMessageBox(
                  XO("Failed to register:\n%s").Format( errMsg ) );
               return true;
            }

            // Ask whether to enable the plug-ins
            if (auto nIds = ids.size()) {
               auto message = XPC(
               /* i18n-hint A plug-in is an optional added program for a sound
                effect, or generator, or analyzer */
                  "Enable this plug-in?\n",
                  "Enable these plug-ins?\n",
                  0,
                  "plug-ins"
               )( nIds );
               for (const auto &name : names)
                  message.Join( Verbatim( name ), wxT("\n") );
               bool enable = (MessageBoxResult::Yes == ShowMessageBox(
                  message,
                  MessageBoxOptions{}
                     .Caption(XO("Enable new plug-ins"))
                     .ButtonStyle(Button::YesNo)));
               for (const auto &id : ids)
                  mRegisteredPlugins[id].SetEnabled(enable);
               // Make changes to enabled status persist:
               this->Save();
               this->NotifyPluginsChanged();
            }

            return true;
         }
      }
   }
   
   return false;
}

void PluginManager::Load()
{
   // Create/Open the registry
   auto pRegistry = sFactory(FileNames::PluginRegistry());
   auto &registry = *pRegistry;

   // If this group doesn't exist then we have something that's not a registry.
   // We should probably warn the user, but it's pretty unlikely that this will happen.
   if (!registry.HasGroup(REGROOT))
   {
      // Must start over
      // This DeleteAll affects pluginregistry.cfg only, not audacity.cfg
      // That is, the memory of on/off states of effect (and generator,
      // analyzer, and tool) plug-ins
      registry.Clear();
      registry.Flush();
      return;
   }

   // Check for a registry version that we can understand
   // TODO: Should also check for a registry file that is newer than
   // what we can understand.
   mRegver = registry.Read(REGVERKEY);
   if (Regver_lt(mRegver, "1.1")) {
      // Conversion code here, for when registry version changes.

      // We iterate through the effects, possibly updating their info.
      wxString group = GetPluginTypeString(PluginTypeEffect);
      wxString cfgPath = REGROOT + group + wxCONFIG_PATH_SEPARATOR;
      wxArrayString groupsToDelete;

      auto cfgGroup = registry.BeginGroup(cfgPath);
      for(const auto& groupName : registry.GetChildGroups())
      {
         auto effectGroup = registry.BeginGroup(groupName);
         wxString effectSymbol = registry.Read(KEY_SYMBOL, "");
         wxString effectVersion = registry.Read(KEY_VERSION, "");


         // For 2.3.0 the plugins we distribute have moved around.
         // So we upped the registry version number to 1.1.
         // These particular config edits were originally written to fix Bug 1914.
         if (Regver_le(mRegver, "1.0")) {
            // Nyquist prompt is a built-in that has moved to the tools menu.
            if (effectSymbol == NYQUIST_PROMPT_ID) {
               registry.Write(KEY_EFFECTTYPE, "Tool");
            // Old version of SDE was in Analyze menu.  Now it is in Tools.
            // We don't want both the old and the new.
            } else if ((effectSymbol == "Sample Data Export") && (effectVersion == "n/a")) {
               groupsToDelete.push_back(cfgPath + groupName);
            // Old version of SDI was in Generate menu.  Now it is in Tools.
            } else if ((effectSymbol == "Sample Data Import") && (effectVersion == "n/a")) {
               groupsToDelete.push_back(cfgPath + groupName);
            }
         }
      }
      // Doing the deletion within the search loop risked skipping some items,
      // hence the delayed delete.
      for (unsigned int i = 0; i < groupsToDelete.size(); i++) {
         registry.DeleteGroup(groupsToDelete[i]);
      }
      // Updates done.  Make sure we read the updated data later.
      registry.Flush();
   }

   // Load all provider plugins first
   LoadGroup(&registry, PluginTypeModule);

   // Now the rest
   LoadGroup(&registry, PluginTypeEffect);
   LoadGroup(&registry, PluginTypeAudacityCommand );
   LoadGroup(&registry, PluginTypeExporter);
   LoadGroup(&registry, PluginTypeImporter);

   LoadGroup(&registry, PluginTypeStub);
   return;
}

void PluginManager::LoadGroup(audacity::BasicSettings *pRegistry, PluginType type)
{
#ifdef __WXMAC__
   // Bug 1590: On Mac, we should purge the registry of Nyquist plug-ins
   // bundled with other versions of Audacity, assuming both versions
   // were properly installed in /Applications (or whatever it is called in
   // your locale)

   const auto fullExePath =
      wxString { PlatformCompatibility::GetExecutablePath() };

   // Strip rightmost path components up to *.app
   wxFileName exeFn{ fullExePath };
   exeFn.SetEmptyExt();
   exeFn.SetName(wxString{});
   while(exeFn.GetDirCount() && !exeFn.GetDirs().back().EndsWith(".app"))
      exeFn.RemoveLastDir();

   const auto goodPath = exeFn.GetPath();

   if(exeFn.GetDirCount())
      exeFn.RemoveLastDir();
   const auto possiblyBadPath = exeFn.GetPath();

   auto AcceptPath = [&](const wxString &path) {
      if (!path.StartsWith(possiblyBadPath))
         // Assume it's not under /Applications
         return true;
      if (path.StartsWith(goodPath))
         // It's bundled with this executable
         return true;
      return false;
   };
#else
   auto AcceptPath = [](const wxString&){ return true; };
#endif

   wxString strVal;
   bool boolVal;
   wxString cfgPath = REGROOT + GetPluginTypeString(type) + wxCONFIG_PATH_SEPARATOR;

   const auto cfgGroup = pRegistry->BeginGroup(cfgPath);
   for(const auto& group : pRegistry->GetChildGroups())
   {
      PluginDescriptor plug;
      const auto effectGroup = pRegistry->BeginGroup(group);

      auto groupName = ConvertID(group);

      // Bypass group if the ID is already in use
      if (mRegisteredPlugins.count(groupName))
         continue;

      // Set the ID and type
      plug.SetID(groupName);
      plug.SetPluginType(type);

      // Get the provider ID and bypass group if not found
      if (!pRegistry->Read(KEY_PROVIDERID, &strVal, {}))
      {
         // Bypass group if the provider isn't valid
         if (!strVal.empty() && !mRegisteredPlugins.count(strVal))
            continue;
      }
      plug.SetProviderID(PluginID(strVal));

      // Get the path (optional)
      pRegistry->Read(KEY_PATH, &strVal, {});
      if (!AcceptPath(strVal))
         // Ignore the obsolete path in the config file, during session,
         // but don't remove it from the file.  Maybe you really want to
         // switch back to the other version of Audacity and lose nothing.
         continue;
      plug.SetPath(strVal);

      /*
       // PRL: Ignore names  written in configs before 2.3.0!
       // use Internal string only!  Let the present version of Audacity map
       // that to a user-visible string.
      // Get the name and bypass group if not found
      if (!pRegistry->Read(KEY_NAME, &strVal))
      {
         continue;
      }
      plug.SetName(strVal);
       */

      // Get the symbol...Audacity 2.3.0 or later requires it
      // bypass group if not found
      // Note, KEY_SYMBOL started getting written to config files in 2.1.0.
      // KEY_NAME (now ignored) was written before that, but only for VST
      // effects.
      if (!pRegistry->Read(KEY_SYMBOL, &strVal))
         continue;

      // Related to Bug2778: config file only remembered an internal name,
      // so this symbol may not contain the correct TranslatableString.
      // See calls to IsPluginRegistered which can correct that.
      plug.SetSymbol(strVal);

      // Get the version and bypass group if not found
      if (!pRegistry->Read(KEY_VERSION, &strVal))
      {
         continue;
      }
      plug.SetVersion(strVal);

      // Get the vendor and bypass group if not found
      if (!pRegistry->Read(KEY_VENDOR, &strVal))
      {
         continue;
      }
      plug.SetVendor( strVal );

#if 0
      // This was done before version 2.2.2, but the value was not really used
      // But absence of a value will cause early versions to skip the group
      // Therefore we still write a blank to keep pluginregistry.cfg
      // backwards-compatible

      // Get the description and bypass group if not found
      if (!pRegistry->Read(KEY_DESCRIPTION, &strVal))
      {
         continue;
      }
#endif

      // Is it enabled...default to no if not found
      pRegistry->Read(KEY_ENABLED, &boolVal, false);
      plug.SetEnabled(boolVal);

      // Is it valid...default to no if not found
      pRegistry->Read(KEY_VALID, &boolVal, false);
      plug.SetValid(boolVal);

      switch (type)
      {
         case PluginTypeModule:
         {
            // Nothing to do here yet
         }
         break;

         case PluginTypeEffect:
         {
            // Get the effect type and bypass group if not found
            if (!pRegistry->Read(KEY_EFFECTTYPE, &strVal))
               continue;

            if (strVal == KEY_EFFECTTYPE_NONE)
               plug.SetEffectType(EffectTypeNone);
            else if (strVal == KEY_EFFECTTYPE_ANALYZE)
               plug.SetEffectType(EffectTypeAnalyze);
            else if (strVal == KEY_EFFECTTYPE_GENERATE)
               plug.SetEffectType(EffectTypeGenerate);
            else if (strVal == KEY_EFFECTTYPE_PROCESS)
               plug.SetEffectType(EffectTypeProcess);
            else if (strVal == KEY_EFFECTTYPE_TOOL)
               plug.SetEffectType(EffectTypeTool);
            else if (strVal == KEY_EFFECTTYPE_HIDDEN)
               plug.SetEffectType(EffectTypeHidden);
            else
               continue;

            // Get the effect family and bypass group if not found
            if (!pRegistry->Read(KEY_EFFECTFAMILY, &strVal))
            {
               continue;
            }
            plug.SetEffectFamily(strVal);

            // Is it a default (above the line) effect and bypass group if not found
            if (!pRegistry->Read(KEY_EFFECTDEFAULT, &boolVal))
            {
               continue;
            }
            plug.SetEffectDefault(boolVal);

            // Is it an interactive effect and bypass group if not found
            if (!pRegistry->Read(KEY_EFFECTINTERACTIVE, &boolVal))
            {
               continue;
            }
            plug.SetEffectInteractive(boolVal);

            // Is it a realtime capable effect and bypass group if not found
            if (!pRegistry->Read(KEY_EFFECTREALTIME, &strVal))
            {
               continue;
            }
            plug.DeserializeRealtimeSupport(strVal);

            // Does the effect support automation...bypass group if not found
            if (!pRegistry->Read(KEY_EFFECTAUTOMATABLE, &boolVal))
            {
               continue;
            }
            plug.SetEffectAutomatable(boolVal);
         }
         break;

         case PluginTypeImporter:
         {
            // Get the importer identifier and bypass group if not found
            if (!pRegistry->Read(KEY_IMPORTERIDENT, &strVal))
            {
               continue;
            }
            plug.SetImporterIdentifier(strVal);

            // Get the importer extensions and bypass group if not found
            if (!pRegistry->Read(KEY_IMPORTEREXTENSIONS, &strVal))
            {
               continue;
            }
            FileExtensions extensions;
            wxStringTokenizer tkr(strVal, wxT(":"));
            while (tkr.HasMoreTokens())
            {
               extensions.push_back(tkr.GetNextToken());
            }
            plug.SetImporterExtensions(extensions);
         }
         break;

         case PluginTypeStub:
         {
            // Nothing additional for stubs
         }
         break;

         // Not used by 2.1.1 or greater and should be removed after a few releases past 2.1.0.
         case PluginTypeNone:
         {
            // Used for stub groups
         }
         break;

         default:
         {
            continue;
         }
      }

      // Everything checked out...accept the plugin
      mRegisteredPlugins[groupName] = std::move(plug);
   }

   return;
}

void PluginManager::Save()
{
   // Create/Open the registry
   auto pRegistry = sFactory(FileNames::PluginRegistry());
   auto &registry = *pRegistry;

   // Clear pluginregistry.cfg (not audacity.cfg)
   registry.Clear();

   // Save the individual groups
   SaveGroup(&registry, PluginTypeEffect);
   SaveGroup(&registry, PluginTypeExporter);
   SaveGroup(&registry, PluginTypeAudacityCommand);
   SaveGroup(&registry, PluginTypeImporter);
   SaveGroup(&registry, PluginTypeStub);

   // Not used by 2.1.1 or greater, but must save to allow users to switch between 2.1.0
   // and 2.1.1+.  This should be removed after a few releases past 2.1.0.
   //SaveGroup(&registry, PluginTypeNone);

   // And now the providers
   SaveGroup(&registry, PluginTypeModule);

   // Write the version string
   registry.Write(REGVERKEY, REGVERCUR);

   // Just to be safe
   registry.Flush();

   mRegver = REGVERCUR;
}

void PluginManager::NotifyPluginsChanged()
{
   Publisher<PluginsChangedMessage>::Publish({});
}

const PluginRegistryVersion &PluginManager::GetRegistryVersion() const
{
   return mRegver;
}


PluginPaths PluginManager::ReadCustomPaths(const PluginProvider& provider)
{
   auto group = mSettings->BeginGroup(REGCUSTOMPATHS);
   const auto key = GetID(&provider);
   const auto paths = mSettings->Read(key, wxString{});
   const auto wxarr = wxSplit(paths, ';');
   return PluginPaths(wxarr.begin(), wxarr.end());
}

void PluginManager::StoreCustomPaths(const PluginProvider& provider, const PluginPaths& paths)
{
   auto group = mSettings->BeginGroup(REGCUSTOMPATHS);
   const auto key = GetID(&provider);
   wxArrayString wxarr;
   std::copy(paths.begin(), paths.end(), std::back_inserter(wxarr));
   mSettings->Write(key, wxJoin(wxarr, ';'));
}

void PluginManager::SaveGroup(audacity::BasicSettings *pRegistry, PluginType type)
{
   wxString group = GetPluginTypeString(type);
   for (auto &pair : mRegisteredPlugins) {
      auto & plug = pair.second;

      if (plug.GetPluginType() != type)
      {
         continue;
      }

      const auto pluginGroup = pRegistry->BeginGroup(REGROOT + group + wxCONFIG_PATH_SEPARATOR + ConvertID(plug.GetID()));

      pRegistry->Write(KEY_PATH, plug.GetPath());

      // See comments with the corresponding load-time call to SetSymbol().
      pRegistry->Write(KEY_SYMBOL, plug.GetSymbol().Internal());

      // PRL:  Writing KEY_NAME which is no longer read, but older Audacity
      // versions expect to find it.
      pRegistry->Write(KEY_NAME, plug.GetSymbol().Msgid().MSGID());

      pRegistry->Write(KEY_VERSION, plug.GetUntranslatedVersion());
      pRegistry->Write(KEY_VENDOR, plug.GetVendor());
      // Write a blank -- see comments in LoadGroup:
      pRegistry->Write(KEY_DESCRIPTION, wxString{});
      pRegistry->Write(KEY_PROVIDERID, plug.GetProviderID());
      pRegistry->Write(KEY_ENABLED, plug.IsEnabled());
      pRegistry->Write(KEY_VALID, plug.IsValid());

      switch (type)
      {
         case PluginTypeModule:
         break;

         case PluginTypeEffect:
         {
            EffectType etype = plug.GetEffectType();
            wxString stype;
            if (etype == EffectTypeNone)
               stype = KEY_EFFECTTYPE_NONE;
            else if (etype == EffectTypeAnalyze)
               stype = KEY_EFFECTTYPE_ANALYZE;
            else if (etype == EffectTypeGenerate)
               stype = KEY_EFFECTTYPE_GENERATE;
            else if (etype == EffectTypeProcess)
               stype = KEY_EFFECTTYPE_PROCESS;
            else if (etype == EffectTypeTool)
               stype = KEY_EFFECTTYPE_TOOL;
            else if (etype == EffectTypeHidden)
               stype = KEY_EFFECTTYPE_HIDDEN;

            pRegistry->Write(KEY_EFFECTTYPE, stype);
            pRegistry->Write(KEY_EFFECTFAMILY, plug.GetEffectFamily());
            pRegistry->Write(KEY_EFFECTDEFAULT, plug.IsEffectDefault());
            pRegistry->Write(KEY_EFFECTINTERACTIVE, plug.IsEffectInteractive());
            pRegistry->Write(KEY_EFFECTREALTIME, plug.SerializeRealtimeSupport());
            pRegistry->Write(KEY_EFFECTAUTOMATABLE, plug.IsEffectAutomatable());
         }
         break;

         case PluginTypeImporter:
         {
            pRegistry->Write(KEY_IMPORTERIDENT, plug.GetImporterIdentifier());
            const auto & extensions = plug.GetImporterExtensions();
            wxString strExt;
            for (size_t i = 0, cnt = extensions.size(); i < cnt; i++)
            {
               strExt += extensions[i] + wxT(":");
            }
            strExt.RemoveLast(1);
            pRegistry->Write(KEY_IMPORTEREXTENSIONS, strExt);
         }
         break;

         default:
         break;
      }
   }

   return;
}

// Here solely for the purpose of Nyquist Workbench until
// a better solution is devised.
const PluginID & PluginManager::RegisterPlugin(
   std::unique_ptr<EffectDefinitionInterface> effect, PluginType type)
{
   PluginDescriptor & plug =
      CreatePlugin(GetID(effect.get()), effect.get(), type);

   plug.SetEffectType(effect->GetType());
   plug.SetEffectFamily(effect->GetFamily().Internal());
   plug.SetEffectInteractive(effect->IsInteractive());
   plug.SetEffectDefault(effect->IsDefault());
   plug.SetRealtimeSupport(effect->RealtimeSupport());
   plug.SetEffectAutomatable(effect->SupportsAutomation());
   
   plug.SetEffectLegacy(true);
   plug.SetEnabled(true);
   plug.SetValid(true);

   mLoadedInterfaces[plug.GetID()] = std::move(effect);

   return plug.GetID();
}

void PluginManager::UnregisterPlugin(const PluginID & ID)
{
   mRegisteredPlugins.erase(ID);
   mLoadedInterfaces.erase(ID);
}

int PluginManager::GetPluginCount(PluginType type)
{
   return count_if(mRegisteredPlugins.begin(), mRegisteredPlugins.end(), [type](auto &pair){
      return pair.second.GetPluginType() == type; });
}

const PluginDescriptor *PluginManager::GetPlugin(const PluginID & ID) const
{
   if (auto iter = mRegisteredPlugins.find(ID); iter != mRegisteredPlugins.end())
      return &iter->second;

   auto iter2 = make_iterator_range(mEffectPluginsCleared)
      .find_if([&ID](const PluginDescriptor& plug) {
         return plug.GetID() == ID;
      });
   if (iter2 != mEffectPluginsCleared.end())
      return &(*iter2);

   return nullptr;
}

void PluginManager::Iterator::Advance(bool incrementing)
{
   const auto end = mPm.mRegisteredPlugins.end();
   if (incrementing && mIterator != end)
      ++mIterator;
   bool all = mPluginType == PluginTypeNone && mEffectType == EffectTypeNone;
   for (; mIterator != end; ++mIterator) {
      auto &plug = mIterator->second;
      if (!all && !(plug.IsValid() && plug.IsEnabled()))
         continue;
      auto plugType = plug.GetPluginType();
      if ((mPluginType == PluginTypeNone || (plugType & mPluginType)) &&
         (mEffectType == EffectTypeNone || plug.GetEffectType() == mEffectType)) {
         if (!all && (plugType & PluginTypeEffect)) {
            // This preference may be written by EffectsPrefs
            auto setting = mPm.GetPluginEnabledSetting( plug );
            if (!(setting.empty() || gPrefs->Read( setting, true )))
               continue;
         }
         // Pause iteration at this match
         break;
      }
   }
}

PluginManager::Iterator::Iterator(PluginManager &manager)
: mPm{ manager }
, mIterator{ manager.mRegisteredPlugins.begin() }
{   
}

PluginManager::Iterator::Iterator(PluginManager &manager, int type)
: mPm{ manager }
, mIterator{ manager.mRegisteredPlugins.begin() }
, mPluginType{ type }
{
   Advance(false);
}

PluginManager::Iterator::Iterator(PluginManager &manager, EffectType type)
: mPm{ manager }
, mIterator{ manager.mRegisteredPlugins.begin() }
, mEffectType{ type }
{
   Advance(false);
}

auto PluginManager::Iterator::operator ++() -> Iterator &
{
   Advance(true);
   return *this;
}

bool PluginManager::IsPluginEnabled(const PluginID & ID)
{
   if (auto iter = mRegisteredPlugins.find(ID); iter == mRegisteredPlugins.end())
      return false;
   else
      return iter->second.IsEnabled();
}

void PluginManager::EnablePlugin(const PluginID & ID, bool enable)
{
   if (auto iter = mRegisteredPlugins.find(ID); iter == mRegisteredPlugins.end())
      return;
   else
      iter->second.SetEnabled(enable);
}

const ComponentInterfaceSymbol&
PluginManager::GetSymbol(const PluginID& ID) const
{
   if (auto iter = mRegisteredPlugins.find(ID); iter == mRegisteredPlugins.end()) {
      static ComponentInterfaceSymbol empty;
      return empty;
   }
   else
      return iter->second.GetSymbol();
}

TranslatableString PluginManager::GetName(const PluginID& ID) const
{
   return GetSymbol(ID).Msgid();
}

CommandID PluginManager::GetCommandIdentifier(const PluginID& ID) const
{
   const auto name = GetSymbol(ID).Internal();
   return EffectDefinitionInterface::GetSquashedName(name);
}

const PluginID&
PluginManager::GetByCommandIdentifier(const CommandID& strTarget)
{
   static PluginID empty;
   if (strTarget.empty()) // set GetCommandIdentifier to wxT("") to not show an
                          // effect in Batch mode
   {
      return empty;
   }

   // Effects OR Generic commands...
   for (auto& plug :
        PluginsOfType(PluginTypeEffect | PluginTypeAudacityCommand))
   {
      auto& ID = plug.GetID();
      if (GetCommandIdentifier(ID) == strTarget)
         return ID;
   }
   return empty;
}

ComponentInterface *PluginManager::Load(const PluginID & ID)
{
   if(auto it = mLoadedInterfaces.find(ID); it != mLoadedInterfaces.end())
      return it->second.get();

   if(auto it = mRegisteredPlugins.find(ID); it != mRegisteredPlugins.end())
   {
      auto& desc = it->second;
      if(desc.GetPluginType() == PluginTypeModule)
         //it's very likely that this code path is not used
         return ModuleManager::Get().CreateProviderInstance(desc.GetID(), desc.GetPath());
      
      if(auto provider = ModuleManager::Get().CreateProviderInstance(desc.GetProviderID(), wxEmptyString))
      {
         auto pluginInterface = provider->LoadPlugin(desc.GetPath());
         auto result = pluginInterface.get();
         mLoadedInterfaces[desc.GetID()] = std::move(pluginInterface);
         return result;
      }
   }
   return nullptr;
}

void PluginManager::ClearEffectPlugins()
{
   mEffectPluginsCleared.clear();

   for ( auto it = mRegisteredPlugins.cbegin(); it != mRegisteredPlugins.cend(); )
   {
      const auto& desc = it->second;
      const auto type = desc.GetPluginType();

      if (type == PluginTypeEffect || type == PluginTypeStub)
      {
         mEffectPluginsCleared.push_back(desc);
         it = mRegisteredPlugins.erase(it);
      }
      else
      {
         ++it;
      }
   }

   // Repeat what usually happens at startup
   // This prevents built-in plugins to appear in the plugin validation list
   for (auto& [_, provider] : ModuleManager::Get().Providers())
      provider->AutoRegisterPlugins(*this);

   // Remove auto registered plugins from "cleared" list
   for ( auto it = mEffectPluginsCleared.begin(); it != mEffectPluginsCleared.end(); )
   {
      if ( mRegisteredPlugins.find(it->GetID()) != mRegisteredPlugins.end() )
         it = mEffectPluginsCleared.erase(it);
      else
         ++it;
   }
}

std::map<wxString, std::vector<wxString>> PluginManager::CheckPluginUpdates()
{
   wxArrayString pathIndex;
   for (auto &pair : mRegisteredPlugins) {
      auto &plug = pair.second;

      // Bypass 2.1.0 placeholders...remove this after a few releases past 2.1.0
      if (plug.GetPluginType() != PluginTypeNone)
         pathIndex.push_back(plug.GetPath().BeforeFirst(wxT(';')));
   }

   // Scan for NEW ones.
   //
   // Because we use the plugins "path" as returned by the providers, we can actually
   // have multiple providers report the same path since, at this point, they only
   // know that the path might possibly be one supported by the provider.
   //
   // When the user enables the plugin, each provider that reported it will be asked
   // to register the plugin.

   auto& moduleManager = ModuleManager::Get();
   std::map<wxString, std::vector<wxString>> newPaths;
   for(auto& [id, provider] : moduleManager.Providers())
   {
      const auto paths = provider->FindModulePaths(*this);
      for(const auto& path : paths)
      {
         const auto modulePath = path.BeforeFirst(';');
         if (!make_iterator_range(pathIndex).contains(modulePath) ||
            make_iterator_range(mEffectPluginsCleared).any_of([&modulePath](const PluginDescriptor& plug) {
               return plug.GetPath().BeforeFirst(wxT(';')) == modulePath;
            })
         )
         {
            newPaths[modulePath].push_back(id);
         }
      }
   }

   return newPaths;
}

PluginID PluginManager::GetID(const PluginProvider *provider)
{
   return ModuleManager::GetID(provider);
}

PluginID PluginManager::GetID(const ComponentInterface *command)
{
   return wxString::Format(wxT("%s_%s_%s_%s_%s"),
                           GetPluginTypeString(PluginTypeAudacityCommand),
                           wxEmptyString,
                           command->GetVendor().Internal(),
                           command->GetSymbol().Internal(),
                           command->GetPath());
}

PluginID PluginManager::OldGetID(const EffectDefinitionInterface* effect)
{
   return wxString::Format(wxT("%s_%s_%s_%s_%s"),
                           GetPluginTypeString(PluginTypeEffect),
                           effect->GetFamily().Internal(),
                           effect->GetVendor().Internal(),
                           effect->GetSymbol().Internal(),
                           effect->GetPath());
}

PluginID PluginManager::GetID(const EffectDefinitionInterface* effect)
{
   return wxJoin(wxArrayStringEx{
      GetPluginTypeString(PluginTypeEffect),
      effect->GetFamily().Internal(),
      effect->GetVendor().Internal(),
      effect->GetSymbol().Internal(),
      effect->GetPath()
   }, '_');
}

Identifier PluginManager::GetEffectNameFromID(const PluginID &ID)
{
   auto strings = wxSplit(ID, '_');
   if (strings.size() == 5)
      return strings[3];
   return {};
}

// This string persists in configuration files
// So config compatibility will break if it is changed across Audacity versions
wxString PluginManager::GetPluginTypeString(PluginType type)
{
   wxString str;

   switch (type)
   {
   default:
   case PluginTypeNone:
      str = wxT("Placeholder");
      break;
   case PluginTypeStub:
      str = wxT("Stub");
      break;
   case PluginTypeEffect:
      str = wxT("Effect");
      break;
   case PluginTypeAudacityCommand:
      str = wxT("Generic");
      break;
   case PluginTypeExporter:
      str = wxT("Exporter");
      break;
   case PluginTypeImporter:
      str = wxT("Importer");
      break;
   case PluginTypeModule:
      str = ModuleManager::GetPluginTypeString();
      break;
   }

   return str;
}

bool PluginManager::IsPluginAvailable(const PluginDescriptor& plug)
{
   const auto& providerID = plug.GetProviderID();
   auto provider = ModuleManager::Get().CreateProviderInstance(providerID, wxEmptyString);

   if (provider == nullptr)
   {
      wxLogWarning("Unable to find a provider for '%s'", providerID);
      return false;
   }

   if (provider->CheckPluginExist(plug.GetPath()) == false)
   {
      wxLogWarning("Plugin '%s' does not exist", plug.GetID());
      return false;
   }

   return true;
}

PluginDescriptor & PluginManager::CreatePlugin(const PluginID & id,
                                               ComponentInterface *ident,
                                               PluginType type)
{
   // This will either create a NEW entry or replace an existing entry
   PluginDescriptor & plug = mRegisteredPlugins[id];

   plug.SetPluginType(type);

   plug.SetID(id);
   plug.SetPath(ident->GetPath());
   plug.SetSymbol(ident->GetSymbol());
   plug.SetVendor(ident->GetVendor().Internal());
   plug.SetVersion(ident->GetVersion());

   return plug;
}

audacity::BasicSettings *PluginManager::GetSettings()
{
   if (!mSettings)
   {
      mSettings = sFactory(FileNames::PluginSettings());

      // Check for a settings version that we can understand
      if (mSettings->HasEntry(SETVERKEY))
      {
         wxString setver = mSettings->Read(SETVERKEY, SETVERKEY);
         if (setver < SETVERCUR )
         {
            // This is where we'd put in conversion code when the
            // settings version changes.
            //
            // Should also check for a settings file that is newer than
            // what we can understand.
         }
      }
      else
      {
         // Make sure is has a version string
         mSettings->Write(SETVERKEY, SETVERCUR);
         mSettings->Flush();
      }
   }

   return mSettings.get();
}

bool PluginManager::HasGroup(const RegistryPath & groupName)
{
   auto settings = GetSettings();

   if(!settings->HasGroup(groupName))
      return false;
   
   auto group = settings->BeginGroup(groupName);
   return !settings->GetChildGroups().empty() || !settings->GetChildKeys().empty();
}

bool PluginManager::GetSubgroups(const RegistryPath & groupName, RegistryPaths & subgroups)
{
   if (groupName.empty() || !HasGroup(groupName))
   {
      return false;
   }

   auto group = GetSettings()->BeginGroup(groupName);
   for(const auto& name : GetSettings()->GetChildGroups())
      subgroups.push_back(name);

   return true;
}

bool PluginManager::HasConfigValue(const RegistryPath & key)
{
   return GetSettings()->Exists(key);
}

template<typename T> class TD;

bool PluginManager::GetConfigValue(
   const RegistryPath & key, ConfigReference var, ConfigConstReference defval)
{
   using namespace Variant;
   if (key.empty())
      return false;
   const auto visitor = [&](const auto var){
      const auto pVar = &var.get();
      // precondition is that defval wraps same type as var
      using Type = typename decltype(var)::type;
      const auto pDefval =
         std::get_if<std::reference_wrapper<const Type>>(&defval);
      //TD<decltype(pDefval)> defType;
      //return true;
      return GetSettings()->Read(key, pVar, pDefval->get());
   };
   return Visit(visitor, var);
}

bool PluginManager::SetConfigValue(
   const RegistryPath & key, ConfigConstReference value)
{
   using namespace Variant;
   if (key.empty())
      return false;
   const auto visitor = [&](const auto value){
      return GetSettings()->Write(key, value.get()) && GetSettings()->Flush();
   };
   return Visit(visitor, value);
}

/* Return value is a key for lookup in a config file */
RegistryPath PluginManager::SettingsPath(
   ConfigurationType type, const PluginID & ID)
{
   bool shared = (type == ConfigurationType::Shared);

   // All the strings reported by PluginDescriptor and used in this function
   // persist in the plugin settings configuration file, so they should not
   // be changed across Audacity versions, or else compatibility of the
   // configuration files will break.

   if (auto iter = mRegisteredPlugins.find(ID); iter == mRegisteredPlugins.end())
      return {};
   else {
      const PluginDescriptor & plug = iter->second;
      
      wxString id = GetPluginTypeString(plug.GetPluginType()) +
                    wxT("_") +
                    plug.GetEffectFamily() + // is empty for non-Effects
                    wxT("_") +
                    plug.GetVendor() +
                    wxT("_") +
                    (shared ? wxString{} : plug.GetSymbol().Internal());

      return SETROOT +
             ConvertID(id) +
             wxCONFIG_PATH_SEPARATOR +
             (shared ? wxT("shared") : wxT("private")) +
             wxCONFIG_PATH_SEPARATOR;
   }
}

/* Return value is a key for lookup in a config file */
RegistryPath PluginManager::Group( ConfigurationType type,
   const PluginID & ID, const RegistryPath & group) 
{
   auto path = SettingsPath(type, ID);

   wxFileName ff(group);
   if (!ff.GetName().empty())
   {
      path += ff.GetFullPath(wxPATH_UNIX) + wxCONFIG_PATH_SEPARATOR;
   }

   return path;
}

/* Return value is a key for lookup in a config file */
RegistryPath PluginManager::Key(ConfigurationType type, const PluginID & ID,
   const RegistryPath & group, const RegistryPath & key)
{
   auto path = Group(type, ID, group);
   if (path.empty())
   {
      return path;
   }

   return path + key;
}

// Sanitize the ID...not the best solution, but will suffice until this
// is converted to XML.  We use base64 encoding to preserve case.
wxString PluginManager::ConvertID(const PluginID & ID)
{
   if (ID.StartsWith(wxT("base64:")))
   {
      wxString id = ID.Mid(7);
      ArrayOf<char> buf{ id.length() / 4 * 3 };
      id =  wxString::FromUTF8(buf.get(), Base64::Decode(id, buf.get()));
      return id;
   }

   const wxCharBuffer & buf = ID.ToUTF8();
   return wxT("base64:") + Base64::Encode(buf, strlen(buf));
}

// This is defined out-of-line here, to keep ComponentInterface free of other
// #include directives.
TranslatableString ComponentInterface::GetName() const
{
   return GetSymbol().Msgid();
}