forked from Sneeds-Feed-and-Seed/sneedacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPluginManager.cpp
2093 lines (1758 loc) · 59.1 KB
/
PluginManager.cpp
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
/**********************************************************************
Sneedacity: 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 "sneedacity/ModuleInterface.h"
#include "SneedacityFileConfig.h"
#include "Internat.h" // for macro XO
#include "FileNames.h"
#include "MemoryX.h"
#include "ModuleManager.h"
#include "PlatformCompatibility.h"
#include "widgets/SneedacityMessageBox.h"
///////////////////////////////////////////////////////////////////////////////
//
// Plugindescriptor
//
///////////////////////////////////////////////////////////////////////////////
PluginDescriptor::PluginDescriptor()
{
mPluginType = PluginTypeNone;
mEnabled = false;
mValid = false;
mInstance = nullptr;
mEffectType = EffectTypeNone;
mEffectInteractive = false;
mEffectDefault = false;
mEffectLegacy = false;
mEffectRealtime = false;
mEffectAutomatable = false;
}
PluginDescriptor::~PluginDescriptor()
{
}
PluginDescriptor &PluginDescriptor::operator =(PluginDescriptor &&) = default;
bool PluginDescriptor::IsInstantiated() const
{
return mInstance != nullptr;
}
ComponentInterface *PluginDescriptor::GetInstance()
{
if (!mInstance)
{
if (GetPluginType() == PluginTypeModule)
mInstance = ModuleManager::Get().CreateProviderInstance(GetID(), GetPath());
else
{
muInstance = ModuleManager::Get().CreateInstance(GetProviderID(), GetPath());
mInstance = muInstance.get();
}
}
return mInstance;
}
void PluginDescriptor::SetInstance(std::unique_ptr<ComponentInterface> instance)
{
muInstance = std::move(instance);
mInstance = muInstance.get();
}
PluginType PluginDescriptor::GetPluginType() const
{
return mPluginType;
}
const PluginID & PluginDescriptor::GetID() const
{
return mID;
}
const PluginID & PluginDescriptor::GetProviderID() const
{
return mProviderID;
}
const PluginPath & PluginDescriptor::GetPath() const
{
return mPath;
}
const ComponentInterfaceSymbol & PluginDescriptor::GetSymbol() const
{
return mSymbol;
}
wxString PluginDescriptor::GetUntranslatedVersion() const
{
return mVersion;
}
wxString PluginDescriptor::GetVendor() const
{
return mVendor;
}
bool PluginDescriptor::IsEnabled() const
{
return mEnabled;
}
bool PluginDescriptor::IsValid() const
{
return mValid;
}
void PluginDescriptor::SetPluginType(PluginType type)
{
mPluginType = type;
}
void PluginDescriptor::SetID(const PluginID & ID)
{
mID = ID;
}
void PluginDescriptor::SetProviderID(const PluginID & providerID)
{
mProviderID = providerID;
}
void PluginDescriptor::SetPath(const PluginPath & path)
{
mPath = path;
}
void PluginDescriptor::SetSymbol(const ComponentInterfaceSymbol & symbol)
{
mSymbol = symbol;
}
void PluginDescriptor::SetVersion(const wxString & version)
{
mVersion = version;
}
void PluginDescriptor::SetVendor(const wxString & vendor)
{
mVendor = vendor;
}
void PluginDescriptor::SetEnabled(bool enable)
{
mEnabled = enable;
}
void PluginDescriptor::SetValid(bool valid)
{
mValid = valid;
}
// Effects
wxString PluginDescriptor::GetEffectFamily() const
{
return mEffectFamily;
}
EffectType PluginDescriptor::GetEffectType() const
{
return mEffectType;
}
bool PluginDescriptor::IsEffectInteractive() const
{
return mEffectInteractive;
}
bool PluginDescriptor::IsEffectDefault() const
{
return mEffectDefault;
}
bool PluginDescriptor::IsEffectLegacy() const
{
return mEffectLegacy;
}
bool PluginDescriptor::IsEffectRealtime() const
{
return mEffectRealtime;
}
bool PluginDescriptor::IsEffectAutomatable() const
{
return mEffectAutomatable;
}
void PluginDescriptor::SetEffectFamily(const wxString & family)
{
mEffectFamily = family;
}
void PluginDescriptor::SetEffectType(EffectType type)
{
mEffectType = type;
}
void PluginDescriptor::SetEffectInteractive(bool interactive)
{
mEffectInteractive = interactive;
}
void PluginDescriptor::SetEffectDefault(bool dflt)
{
mEffectDefault = dflt;
}
void PluginDescriptor::SetEffectLegacy(bool legacy)
{
mEffectLegacy = legacy;
}
void PluginDescriptor::SetEffectRealtime(bool realtime)
{
mEffectRealtime = realtime;
}
void PluginDescriptor::SetEffectAutomatable(bool automatable)
{
mEffectAutomatable = automatable;
}
// Importer
const wxString & PluginDescriptor::GetImporterIdentifier() const
{
return mImporterIdentifier;
}
void PluginDescriptor::SetImporterIdentifier(const wxString & identifier)
{
mImporterIdentifier = identifier;
}
const FileExtensions & PluginDescriptor::GetImporterExtensions()
const
{
return mImporterExtensions;
}
void PluginDescriptor::SetImporterExtensions( FileExtensions extensions )
{
mImporterExtensions = std::move( extensions );
}
///////////////////////////////////////////////////////////////////////////////
//
// PluginManager
//
///////////////////////////////////////////////////////////////////////////////
// Registry has the list of plug ins
#define REGVERKEY wxString(wxT("/pluginregistryversion"))
#define REGVERCUR wxString(wxT("1.1"))
#define REGROOT wxString(wxT("/pluginregistry/"))
// 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(
ModuleInterface *provider, ComponentInterface *pInterface )
{
EffectDefinitionInterface * pEInterface = dynamic_cast<EffectDefinitionInterface*>(pInterface);
if( pEInterface )
return PluginManager::Get().RegisterPlugin(provider, pEInterface, PluginTypeEffect);
ComponentInterface * pCInterface = dynamic_cast<ComponentInterface*>(pInterface);
if( pCInterface )
return PluginManager::Get().RegisterPlugin(provider, pCInterface);
static wxString empty;
return empty;
}
const PluginID &PluginManagerInterface::SneedacityCommandRegistrationCallback(
ModuleInterface *provider, ComponentInterface *pInterface )
{
ComponentInterface * pCInterface = dynamic_cast<ComponentInterface*>(pInterface);
if( pCInterface )
return PluginManager::Get().RegisterPlugin(provider, pCInterface);
static wxString empty;
return empty;
}
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 : mPlugins) {
if (auto &descriptor = pair.second; descriptor.GetPath() == path) {
if (pName)
descriptor.SetSymbol(
{ descriptor.GetSymbol().Internal(), *pName });
return true;
}
}
return false;
}
const PluginID & PluginManager::RegisterPlugin(ModuleInterface *module)
{
PluginDescriptor & plug = CreatePlugin(GetID(module), module, PluginTypeModule);
plug.SetEffectFamily(module->GetOptionalFamilySymbol().Internal());
plug.SetEnabled(true);
plug.SetValid(true);
return plug.GetID();
}
const PluginID & PluginManager::RegisterPlugin(ModuleInterface *provider, ComponentInterface *command)
{
PluginDescriptor & plug = CreatePlugin(GetID(command), command, (PluginType)PluginTypeSneedacityCommand);
plug.SetProviderID(PluginManager::GetID(provider));
plug.SetEnabled(true);
plug.SetValid(true);
return plug.GetID();
}
const PluginID & PluginManager::RegisterPlugin(ModuleInterface *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.SetEffectRealtime(effect->SupportsRealtime());
plug.SetEffectAutomatable(effect->SupportsAutomation());
plug.SetEnabled(true);
plug.SetValid(true);
return plug.GetID();
}
const PluginID & PluginManager::RegisterPlugin(ModuleInterface *provider, ImporterInterface *importer)
{
PluginDescriptor & plug = CreatePlugin(GetID(importer), importer, PluginTypeImporter);
plug.SetProviderID(PluginManager::GetID(provider));
plug.SetImporterIdentifier(importer->GetPluginStringID());
plug.SetImporterExtensions(importer->GetSupportedExtensions());
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 "Sneedacity" 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 "Sneedacity" plug-ins directory
wxFileName ff = PlatformCompatibility::GetExecutablePath();
#if defined(__WXMAC__)
// Path ends for example in "Sneedacity.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::HasSharedConfigGroup(const PluginID & ID, const RegistryPath & group)
{
return HasGroup(SharedGroup(ID, group));
}
bool PluginManager::GetSharedConfigSubgroups(const PluginID & ID, const RegistryPath & group, RegistryPaths & subgroups)
{
return GetSubgroups(SharedGroup(ID, group), subgroups);
}
bool PluginManager::GetSharedConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key, wxString & value, const wxString & defval)
{
return GetConfig(SharedKey(ID, group, key), value, defval);
}
bool PluginManager::GetSharedConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key, int & value, int defval)
{
return GetConfig(SharedKey(ID, group, key), value, defval);
}
bool PluginManager::GetSharedConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key, bool & value, bool defval)
{
return GetConfig(SharedKey(ID, group, key), value, defval);
}
bool PluginManager::GetSharedConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key, float & value, float defval)
{
return GetConfig(SharedKey(ID, group, key), value, defval);
}
bool PluginManager::GetSharedConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key, double & value, double defval)
{
return GetConfig(SharedKey(ID, group, key), value, defval);
}
bool PluginManager::SetSharedConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key, const wxString & value)
{
return SetConfig(SharedKey(ID, group, key), value);
}
bool PluginManager::SetSharedConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key, const int & value)
{
return SetConfig(SharedKey(ID, group, key), value);
}
bool PluginManager::SetSharedConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key, const bool & value)
{
return SetConfig(SharedKey(ID, group, key), value);
}
bool PluginManager::SetSharedConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key, const float & value)
{
return SetConfig(SharedKey(ID, group, key), value);
}
bool PluginManager::SetSharedConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key, const double & value)
{
return SetConfig(SharedKey(ID, group, key), value);
}
bool PluginManager::RemoveSharedConfigSubgroup(const PluginID & ID, const RegistryPath & group)
{
bool result = GetSettings()->DeleteGroup(SharedGroup(ID, group));
if (result)
{
GetSettings()->Flush();
}
return result;
}
bool PluginManager::RemoveSharedConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key)
{
bool result = GetSettings()->DeleteEntry(SharedKey(ID, group, key));
if (result)
{
GetSettings()->Flush();
}
return result;
}
bool PluginManager::HasPrivateConfigGroup(const PluginID & ID, const RegistryPath & group)
{
return HasGroup(PrivateGroup(ID, group));
}
bool PluginManager::GetPrivateConfigSubgroups(const PluginID & ID, const RegistryPath & group, RegistryPaths & subgroups)
{
return GetSubgroups(PrivateGroup(ID, group), subgroups);
}
bool PluginManager::GetPrivateConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key, wxString & value, const wxString & defval)
{
return GetConfig(PrivateKey(ID, group, key), value, defval);
}
bool PluginManager::GetPrivateConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key, int & value, int defval)
{
return GetConfig(PrivateKey(ID, group, key), value, defval);
}
bool PluginManager::GetPrivateConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key, bool & value, bool defval)
{
return GetConfig(PrivateKey(ID, group, key), value, defval);
}
bool PluginManager::GetPrivateConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key, float & value, float defval)
{
return GetConfig(PrivateKey(ID, group, key), value, defval);
}
bool PluginManager::GetPrivateConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key, double & value, double defval)
{
return GetConfig(PrivateKey(ID, group, key), value, defval);
}
bool PluginManager::SetPrivateConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key, const wxString & value)
{
return SetConfig(PrivateKey(ID, group, key), value);
}
bool PluginManager::SetPrivateConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key, const int & value)
{
return SetConfig(PrivateKey(ID, group, key), value);
}
bool PluginManager::SetPrivateConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key, const bool & value)
{
return SetConfig(PrivateKey(ID, group, key), value);
}
bool PluginManager::SetPrivateConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key, const float & value)
{
return SetConfig(PrivateKey(ID, group, key), value);
}
bool PluginManager::SetPrivateConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key, const double & value)
{
return SetConfig(PrivateKey(ID, group, key), value);
}
bool PluginManager::RemovePrivateConfigSubgroup(const PluginID & ID, const RegistryPath & group)
{
bool result = GetSettings()->DeleteGroup(PrivateGroup(ID, group));
if (result)
{
GetSettings()->Flush();
}
return result;
}
bool PluginManager::RemovePrivateConfig(const PluginID & ID, const RegistryPath & group, const RegistryPath & key)
{
bool result = GetSettings()->DeleteEntry(PrivateKey(ID, group, key));
if (result)
{
GetSettings()->Flush();
}
return result;
}
// ============================================================================
//
// PluginManager
//
// ============================================================================
// The one and only PluginManager
std::unique_ptr<PluginManager> PluginManager::mInstance{};
// ----------------------------------------------------------------------------
// Creation/Destruction
// ----------------------------------------------------------------------------
PluginManager::PluginManager()
{
mSettings = NULL;
}
PluginManager::~PluginManager()
{
// Ensure termination (harmless if already done)
Terminate();
}
// ----------------------------------------------------------------------------
// PluginManager implementation
// ----------------------------------------------------------------------------
// ============================================================================
//
// Return reference to singleton
//
// (Thread-safe...no active threading during construction or after destruction)
// ============================================================================
PluginManager & PluginManager::Get()
{
if (!mInstance)
{
mInstance.reset(safenew PluginManager);
}
return *mInstance;
}
void PluginManager::Initialize()
{
// Always load the registry first
Load();
// And force load of setting to verify it's accessible
GetSettings();
// Then look for providers (they may autoregister plugins)
ModuleManager::Get().DiscoverProviders();
// And finally check for updates
#ifndef EXPERIMENTAL_EFFECT_MANAGEMENT
CheckForUpdates();
#else
const bool kFast = true;
CheckForUpdates( kFast );
#endif
}
void PluginManager::Terminate()
{
// Get rid of all non-module plugins first
PluginMap::iterator iter = mPlugins.begin();
while (iter != mPlugins.end())
{
PluginDescriptor & plug = iter->second;
if (plug.GetPluginType() == PluginTypeEffect)
{
mPlugins.erase(iter++);
continue;
}
++iter;
}
// Now get rid of the modules
iter = mPlugins.begin();
while (iter != mPlugins.end())
{
mPlugins.erase(iter++);
}
}
bool PluginManager::DropFile(const wxString &fileName)
{
auto &mm = ModuleManager::Get();
const wxFileName src{ fileName };
for (auto &plug : PluginsOfType(PluginTypeModule)) {
auto module = static_cast<ModuleInterface *>
(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 = (wxYES == ::SneedacityMessageBox(
XO("Overwrite the plug-in file %s?")
.Format( dst.GetFullPath() ),
XO("Plug-in already exists"),
wxYES_NO ) );
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) {
::SneedacityMessageBox(
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,
[&](ModuleInterface *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
::SneedacityMessageBox(
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 = (wxYES == ::SneedacityMessageBox(
message,
XO("Enable new plug-ins"),
wxYES_NO ) );
for (const auto &id : ids)
mPlugins[id].SetEnabled(enable);
// Make changes to enabled status persist:
this->Save();
}
return true;
}
}
}
return false;
}
void PluginManager::Load()
{
// Create/Open the registry
auto pRegistry = SneedacityFileConfig::Create(
{}, {}, FileNames::PluginRegistry());
auto ®istry = *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
registry.DeleteAll();
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.
wxString regver = registry.Read(REGVERKEY);
if (regver < REGVERCUR )
{
// Conversion code here, for when registry version changes.
// We iterate through the effects, possibly updating their info.
wxString groupName;
long groupIndex;
wxString group = GetPluginTypeString(PluginTypeEffect);
wxString cfgPath = REGROOT + group + wxCONFIG_PATH_SEPARATOR;
wxArrayString groupsToDelete;
registry.SetPath(cfgPath);
for (bool cont = registry.GetFirstGroup(groupName, groupIndex);
cont;
registry.SetPath(cfgPath),
cont = registry.GetNextGroup(groupName, groupIndex))
{
registry.SetPath(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 <= "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]);
}
registry.SetPath("");
registry.Write(REGVERKEY, REGVERCUR);
// Updates done. Make sure we read the updated data later.
registry.Flush();
}
// Load all provider plugins first
LoadGroup(®istry, PluginTypeModule);
// Now the rest
LoadGroup(®istry, PluginTypeEffect);
LoadGroup(®istry, PluginTypeSneedacityCommand );
LoadGroup(®istry, PluginTypeExporter);
LoadGroup(®istry, PluginTypeImporter);
LoadGroup(®istry, PluginTypeStub);
return;
}
void PluginManager::LoadGroup(FileConfig *pRegistry, PluginType type)
{
#ifdef __WXMAC__
// Bug 1590: On Mac, we should purge the registry of Nyquist plug-ins
// bundled with other versions of Sneedacity, assuming both versions
// were properly installed in /Applications (or whatever it is called in
// your locale)
const auto fullExePath = 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 groupName;
long groupIndex;
wxString group = GetPluginTypeString(type);
wxString cfgPath = REGROOT + group + wxCONFIG_PATH_SEPARATOR;
pRegistry->SetPath(cfgPath);