protected override Task <bool> SaveAsync()
        {
            Plugin.EndEdit();

            _remotePluginInstance?.UnloadPlugin();

            try
            {
                _dataPersisterService.SavePresetsForPlugin(Plugin);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }

            return(base.SaveAsync());
        }
Example #2
0
        protected override async Task <bool> SaveAsync()
        {
            await TaskHelper.Run(() =>
            {
                _globalService.GlobalTypes.EndEdit();
                _globalService.GlobalCharacteristics.EndEdit();

                foreach (var plugin in _globalService.Plugins)
                {
                    plugin.EndEdit();
                    _dataPersisterService.SavePresetsForPlugin(plugin);
                    _dataPersisterService.SavePlugin(plugin);
                }
            });

            return(await base.SaveAsync());
        }
Example #3
0
        private async Task AnalyzePlugins(IList <Plugin> pluginsToScan, CancellationToken cancellationToken)
        {
            foreach (var plugin in pluginsToScan)
            {
                if (!plugin.IsPresent)
                {
                    continue;
                }

                if (!plugin.HasMetadata)
                {
                    continue;
                }

                LogTo.Debug($"Begin analysis of {plugin.DllFilename}");
                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                _globalFrontendService.SelectedPlugin = plugin;

                try
                {
                    using (var remotePluginInstance = _remoteVstService.GetRemotePluginInstance(plugin))
                    {
                        _applicationService.UpdateApplicationOperationStatus(
                            pluginsToScan.IndexOf(plugin), $"Scanning {plugin.DllFilename}");


                        if (!plugin.HasMetadata)
                        {
                            if (plugin.LoadError)
                            {
                                LogTo.Debug($"Skipping {plugin.DllPath} because a load error occured");
                            }
                            else
                            {
                                throw new Exception(
                                          $"Plugin {plugin.DllPath} has no metadata and was loaded correctly.");
                            }
                        }

                        if (plugin.PresetParser == null)
                        {
                            throw new Exception(
                                      $"Plugin {plugin.DllPath} has no preset parser. Please report this as a bug.");
                        }


                        var wasLoaded = remotePluginInstance.IsLoaded;
                        plugin.PresetParser.PluginInstance  = remotePluginInstance;
                        plugin.PresetParser.DataPersistence = _presetDataPersisterService;
                        await _presetDataPersisterService.OpenDatabase();

                        _presetDataPersisterService.PresetUpdated += ContextOnPresetUpdated;
                        _currentPluginIndex = pluginsToScan.IndexOf(plugin);
                        _currentPlugin      = plugin;

                        plugin.PresetParser.RootBank = plugin.RootBank.First();

                        plugin.PresetParser.Logger.MirrorTo(plugin.Logger);
                        _totalPresets       = plugin.PresetParser.GetNumPresets();
                        _currentPresetIndex = 0;

                        await plugin.PresetParser.DoScan();

                        await _presetDataPersisterService.CloseDatabase();

                        _dataPersisterService.SavePresetsForPlugin(plugin);

                        await _dispatcherService.InvokeAsync(() => { plugin.NativeInstrumentsResource.Load(plugin); });

                        if (GlobalService.RuntimeConfiguration.AutoCreateResources &&
                            _resourceGeneratorService.ShouldCreateScreenshot(remotePluginInstance))
                        {
                            plugin.Logger.Debug(
                                $"Auto-generating resources for {plugin.DllFilename} - Opening Editor");
                            _applicationService.UpdateApplicationOperationStatus(
                                pluginsToScan.IndexOf(plugin),
                                $"Auto-generating resources for {plugin.DllFilename} - Opening Editor");
                            if (!remotePluginInstance.IsLoaded)
                            {
                                await remotePluginInstance.LoadPlugin();
                            }

                            remotePluginInstance.OpenEditorHidden();
                            _dispatcherService.Invoke(() => Application.Current.MainWindow.BringWindowToTop());
                            await Task.Delay(1000);
                        }

                        await _dispatcherService.InvokeAsync(() =>
                        {
                            if (GlobalService.RuntimeConfiguration.AutoCreateResources &&
                                _resourceGeneratorService.NeedToGenerateResources(remotePluginInstance))
                            {
                                plugin.Logger.Debug(
                                    $"Auto-generating resources for {plugin.DllFilename} - Creating screenshot and applying magic");
                                _applicationService.UpdateApplicationOperationStatus(
                                    pluginsToScan.IndexOf(plugin),
                                    $"Auto-generating resources for {plugin.DllFilename} - Creating screenshot and applying magic");

                                _resourceGeneratorService.AutoGenerateResources(remotePluginInstance);
                            }
                        });

                        wasLoaded = remotePluginInstance.IsLoaded;


                        _applicationService.UpdateApplicationOperationStatus(
                            pluginsToScan.IndexOf(plugin),
                            $"{plugin.DllFilename} - Updating Database");
                        _dataPersisterService.SavePlugin(plugin);


                        if (wasLoaded)
                        {
                            plugin.Logger.Debug($"Unloading {plugin.DllFilename}");
                            remotePluginInstance.UnloadPlugin();
                        }
                    }
                }
                catch (Exception e)
                {
                    plugin.LogPluginError("loading presets", e);

                    var errorMessage =
                        $"Unable to analyze {plugin.DllFilename} because of {e.GetType().FullName}: {e.Message}";
                    _applicationService.AddApplicationOperationError(errorMessage + " - see plugin log for details");
                }

                if (plugin.PresetParser != null && plugin.PresetParser.Logger.HasLoggedEntries(LogLevel.Error))
                {
                    var errors = plugin.PresetParser.Logger.GetFilteredLogEntries(new List <LogLevel> {
                        LogLevel.Error
                    });

                    foreach (var error in errors)
                    {
                        _applicationService.AddApplicationOperationError(error.Message);
                    }
                }

                // Remove the event handler here, so we can be absolutely sure we removed this.
                _presetDataPersisterService.PresetUpdated -= ContextOnPresetUpdated;

                LogTo.Debug($"End analysis of {plugin.DllFilename}");
            }
        }
Example #4
0
        protected override async Task ExecuteAsync(object parameter)
        {
            var pluginPresets = from item in _globalFrontendService.PresetExportList
                                group item by item.Plugin
                                into pluginGroup
                                let first = pluginGroup.First()
                                            select new
            {
                first.Plugin,
                Presets = pluginGroup.Select(gi => new { Preset = gi })
            };

            int totalPresets  = _globalFrontendService.PresetExportList.Count;
            int currentPreset = 0;

            _applicationService.StartApplicationOperation(this, "Exporting Presets",
                                                          totalPresets);
            var progress = _applicationService.GetApplicationProgress();

            await TaskHelper.Run(async() =>
            {
                var exportDirectory = _globalService.RuntimeConfiguration
                                      .NativeInstrumentsUserContentDirectory;

                if (!Directory.Exists(exportDirectory))
                {
                    LogTo.Warning($"Directory {exportDirectory} does not exist, using the default");
                }

                foreach (var pluginPreset in pluginPresets)
                {
                    Preset lastPreset = null;
                    try
                    {
                        await _presetDataPersisterService.OpenDatabase();

                        if (pluginPreset.Plugin.PresetParser == null)
                        {
                            _applicationService.AddApplicationOperationError(
                                $"Unable to update export presets for {pluginPreset.Plugin.PluginName} because it has no Preset Parser. Try to force-reload metadata for this plugin.");
                            continue;
                        }


                        using (var remotePluginInstance =
                                   _remoteVstService.GetRemotePluginInstance(pluginPreset.Plugin, false))
                        {
                            foreach (var preset in pluginPreset.Presets)
                            {
                                lastPreset = preset.Preset;
                                currentPreset++;
                                _applicationService.UpdateApplicationOperationStatus(
                                    currentPreset,
                                    $"Exporting {pluginPreset.Plugin.PluginName} - {preset.Preset.Metadata.PresetName}");

                                if (progress.CancellationToken.IsCancellationRequested)
                                {
                                    return;
                                }

                                var presetData = await _presetDataPersisterService.GetPresetData(preset.Preset);

                                if (preset.Preset.PresetBank == null)
                                {
                                    preset.Preset.PresetBank = preset.Preset.Plugin.RootBank;
                                    LogTo.Warning(
                                        $"Preset {preset.Preset.Metadata.PresetName} has no preset bank, using none.");
                                }

                                var presetExportInfo = new PresetExportInfo(preset.Preset)
                                {
                                    FolderMode           = _globalService.RuntimeConfiguration.FolderExportMode,
                                    OverwriteMode        = _globalService.RuntimeConfiguration.FileOverwriteMode,
                                    UserContentDirectory = exportDirectory
                                };

                                if (!presetExportInfo.CanExport())
                                {
                                    _applicationService.AddApplicationOperationError(
                                        $"Cannot export {preset.Preset.Plugin} -{preset.Preset.Metadata.PresetName}. " +
                                        $"Reason: {presetExportInfo.CannotExportReason}");
                                    continue;
                                }

                                pluginPreset.Plugin.PresetParser.PluginInstance = remotePluginInstance;

                                if (_globalService.RuntimeConfiguration.ExportWithAudioPreviews &&
                                    pluginPreset.Plugin.PluginType == Plugin.PluginTypes.Instrument)
                                {
                                    await remotePluginInstance.LoadPlugin().ConfigureAwait(false);
                                    pluginPreset.Plugin.PresetParser.OnPluginLoad();
                                    remotePluginInstance.SetChunk(presetData, false);

                                    remotePluginInstance.ExportNksAudioPreview(presetExportInfo, presetData,
                                                                               preset.Preset.Plugin.GetAudioPreviewDelay());
                                }

                                remotePluginInstance.ExportNks(presetExportInfo, presetData);


                                pluginPreset.Plugin.PresetParser.OnAfterPresetExport();
                                preset.Preset.LastExported = DateTime.Now;
                                preset.Preset.UpdateLastExportedMetadata();
                            }

                            if (remotePluginInstance.IsLoaded)
                            {
                                pluginPreset.Plugin.PresetParser.OnPluginUnload();
                            }

                            remotePluginInstance.UnloadPlugin();
                        }
                    }
                    catch (Exception e)
                    {
                        var errorMessage =
                            $"Unable to export presets for {pluginPreset.Plugin.PluginName} because of {e.GetType().FullName}: {e.Message}. ";

                        if (lastPreset != null)
                        {
                            errorMessage +=
                                $"The preset causing the error was: {lastPreset.Metadata.PresetName} in bank path {lastPreset.Metadata.BankPath}";
                        }

                        _applicationService.AddApplicationOperationError(
                            errorMessage
                            );
                        LogTo.Debug(e.StackTrace);
                    }

                    await _presetDataPersisterService.CloseDatabase();

                    _dataPersisterService.SavePresetsForPlugin(pluginPreset.Plugin);
                }
            });

            _applicationService.StopApplicationOperation("Export completed");
        }
        public NewModels.Plugin MigratePlugin(Plugin oldPlugin)
        {
            _presetDataPersister.OpenDatabase().Wait();
            var newPlugin = new NewModels.Plugin();

            if (oldPlugin.PluginLocation != null)
            {
                newPlugin.PluginLocation = new NewModels.PluginLocation();
                MigratePluginLocation(oldPlugin, oldPlugin.PluginLocation, newPlugin.PluginLocation);
            }

            foreach (var preset in oldPlugin.Presets)
            {
                var data = _dbContext.GetPresetData(preset);

                if (data == null)
                {
                    // The preset data is null for some reason, skip this one
                    continue;
                }

                UpdateCounter++;
                var currentPreset = oldPlugin.Presets.IndexOf(preset) + 1;

                if (UpdateCounter > 111)
                {
                    MigrationProgressUpdated?.Invoke(this,
                                                     new MigrationProgessEventArgs(
                                                         $"({CurrentPlugin}/{OldPlugins.Count}) {oldPlugin.PluginName} Preset {currentPreset} / {oldPlugin.Presets.Count}"));
                    UpdateCounter = 0;
                }

                var newPreset = new NewModels.Preset();
                newPreset.Plugin = newPlugin;
                newPlugin.Presets.Add(newPreset);

                MigratePreset(preset, newPreset);


                newPreset.Plugin = newPlugin;
                newPreset.OriginalMetadata.Plugin = newPlugin;
                _presetDataPersister.PersistPreset(newPreset.OriginalMetadata, data, true).Wait();
            }


            var propertiesToMigrate = new HashSet <string>
            {
                nameof(Plugin.PluginType),
                nameof(Plugin.PluginName),
                nameof(Plugin.LastKnownGoodDllPath),
                nameof(Plugin.PluginVendor),
                nameof(Plugin.VstPluginId),
                nameof(Plugin.IsEnabled),
                nameof(Plugin.IsReported),
                nameof(Plugin.IsSupported),
                nameof(Plugin.DontReport),
                nameof(Plugin.DefaultControllerAssignments),
            };

            foreach (var propertyName in propertiesToMigrate)
            {
                var oldValue = PropertyHelper.GetPropertyValue(oldPlugin, propertyName);
                PropertyHelper.SetPropertyValue(newPlugin, propertyName, oldValue);
            }

            MigrateModes(oldPlugin.DefaultModes, newPlugin.DefaultCharacteristics);
            MigrateTypes(oldPlugin.DefaultTypes, newPlugin.DefaultTypes);

            if (oldPlugin.PluginInfo != null)
            {
                newPlugin.PluginInfo = new NewModels.VstPluginInfoSurrogate();
            }

            MigratePluginInfo(oldPlugin.PluginInfo, newPlugin.PluginInfo);
            MigratePluginCapabilities(oldPlugin.PluginCapabilities, newPlugin.PluginCapabilities);
            MigrateBankFiles(oldPlugin.AdditionalBankFiles, newPlugin.AdditionalBankFiles);

            _dataPersister.SavePlugin(newPlugin);
            _dataPersister.SavePresetsForPlugin(newPlugin);
            _presetDataPersister.CloseDatabase().Wait();
            return(newPlugin);
        }