예제 #1
0
        private void TestPresetBank(HashSet <string> testedProperties)
        {
            var preset = GetFreshPresetTestSubject();

            ((IEditableObject)preset).BeginEdit();

            preset.Plugin.RootBank.PresetBanks.First().PresetBanks.Add(new PresetBank()
            {
                BankName = "yo mama"
            });
            preset.IsMetadataModified.Should().BeFalse("a bank was added but did not change our bank path");

            preset = GetFreshPresetTestSubject();
            (preset as IEditableObject).BeginEdit();
            var pp = new PresetBank()
            {
                BankName = "yo mama"
            };

            preset.Plugin.RootBank.PresetBanks.First().PresetBanks.Add(pp);
            preset.PresetBank = pp;
            preset.IsMetadataModified.Should().BeTrue("the bank was changed and should trigger a change");

            preset = GetFreshPresetTestSubject();
            (preset as IEditableObject).BeginEdit();
            preset.Plugin.RootBank.PresetBanks.First().First().BankName = "diz changed";
            preset.IsMetadataModified.Should().BeTrue("the bank name was changed and should trigger a change");

            testedProperties.Add("PresetBank");
        }
예제 #2
0
        protected async Task <int> ProcessPresetDirectory(string presetDirectory, PresetBank bank, bool persist = true)
        {
            var count = 0;

            if (!Directory.Exists(presetDirectory))
            {
                return(count);
            }

            Logger.Debug($"ProcessPresetDirectory {presetDirectory}");
            var dirInfo = new DirectoryInfo(presetDirectory);

            foreach (var file in dirInfo.EnumerateFiles("*" + Extension))
            {
                count++;

                if (persist)
                {
                    var presetData = File.ReadAllText(file.FullName);
                    var presetInfo = GetPreset(file.Name, presetData, bank);
                    presetInfo.preset.SourceFile = file.FullName;
                    await DataPersistence.PersistPreset(presetInfo.preset, presetInfo.presetData);
                }
            }

            foreach (var directory in dirInfo.EnumerateDirectories())
            {
                count += await ProcessPresetDirectory(directory.FullName, bank.CreateRecursive(directory.Name),
                                                      persist);
            }

            return(count);
        }
        protected async Task GetPresetsUsingFullBank(PresetBank bank, int start, int numPresets, string sourceFile)
        {
            if (start < 0)
            {
                Logger.Error("GetPresets start index is less than 0, ignoring. This is probably a bug or a " +
                             "misconfiguration. Please report this including the full log file.");
                return;
            }

            var endIndex = start + numPresets;

            if (endIndex > PluginInstance.Plugin.PluginInfo.ProgramCount)
            {
                Logger.Error(
                    $"Tried to retrieve presets between the index {start} and {endIndex}, but this would exceed maximum " +
                    $"program count of {PluginInstance.Plugin.PluginInfo.ProgramCount}, ignoring. You might wish to " +
                    "report this as a bug.");
                return;
            }

            for (var index = start; index < endIndex; index++)
            {
                PluginInstance.SetProgram(index);

                var preset = new PresetParserMetadata
                {
                    PresetName = PluginInstance.GetCurrentProgramName(),
                    BankPath   = bank.BankPath,
                    SourceFile = sourceFile + ":" + index,
                    Plugin     = PluginInstance.Plugin
                };

                await DataPersistence.PersistPreset(preset, PluginInstance.GetChunk(false));
            }
        }
        private async Task OnAddBankCommandExecute()
        {
            var newBank = new PresetBank {
                BankName = ""
            };
            var viewModel = _viewModelFactory.CreateViewModel <PresetBankViewModel>((
                                                                                        plugin: Plugin, presetBank: newBank, parentBank: SelectedTreeNode));

            viewModel.Title       = "Add Preset Bank";
            viewModel.SavedAsync += ViewModelOnSavedAsync;
            await _visualizerService.ShowDialogAsync(viewModel);
        }
예제 #5
0
        protected async Task <int> ProcessD16PkgArchive(string archiveName, PresetBank bank, bool persist = true)
        {
            var count = 0;

            Logger.Debug($"ProcessD16PKGArchive {archiveName}");

            if (!File.Exists(archiveName))
            {
                Logger.Error($"Could not find the file {archiveName}");
                return(0);
            }

            using (var archive = ZipFile.OpenRead(archiveName))
            {
                var entry = archive.GetEntry("content");

                if (entry == null)
                {
                    Logger.Error($"Could not find the entry 'content' within archive {archiveName}");
                    return(0);
                }
                var contentStream = entry.Open();

                var contentArchive = new ZipArchive(contentStream);

                foreach (var presetEntry in contentArchive.Entries)
                {
                    if (presetEntry.Name == "__desc__")
                    {
                        continue;
                    }

                    count++;

                    if (persist)
                    {
                        var ms = new MemoryStream();
                        presetEntry.Open().CopyTo(ms);
                        ms.Seek(0, SeekOrigin.Begin);

                        var presetData = Encoding.UTF8.GetString(ms.ToArray());

                        var presetInfo = GetPreset(presetEntry.Name, presetData, bank);
                        presetInfo.preset.SourceFile = archiveName + ":" + presetEntry.Name;
                        await DataPersistence.PersistPreset(presetInfo.preset, presetInfo.presetData);
                    }
                }
            }

            return(count);
        }
예제 #6
0
        private async Task ProcessPreset(FileSystemInfo file, PresetBank bank)
        {
            var presetData = File.ReadAllBytes(file.FullName);
            var sourceFile = file.FullName;


            var preset = new PresetParserMetadata
            {
                PresetName = file.Name.Replace(".h2p", ""), Plugin = PluginInstance.Plugin, BankPath = bank.BankPath,
                SourceFile = sourceFile
            };

            var metadata = ExtractMetadata(Encoding.UTF8.GetString(presetData));

            if (metadata.ContainsKey("Author"))
            {
                preset.Author = metadata["Author"];
            }

            var comments = new List <string>();

            if (metadata.ContainsKey("Description") && metadata["Description"].Length > 0)
            {
                comments.Add(metadata["Description"]);
            }

            if (metadata.ContainsKey("Usage") && metadata["Usage"].Length > 0)
            {
                comments.Add(metadata["Usage"]);
            }

            preset.Comment = string.Join(Environment.NewLine, comments);

            if (metadata.ContainsKey("Categories") && metadata["Categories"].Length > 0)
            {
                preset.Types.AddRange(ExtractTypes(metadata["Categories"]));
            }

            if (metadata.ContainsKey("Features") && metadata["Features"].Length > 0)
            {
                preset.Characteristics.AddRange(ExtractModes(metadata["Features"]));
            }

            if (metadata.ContainsKey("Character") && metadata["Character"].Length > 0)
            {
                preset.Characteristics.AddRange(ExtractModes(metadata["Character"]));
            }

            await DataPersistence.PersistPreset(preset, presetData);
        }
예제 #7
0
        protected virtual (PresetParserMetadata preset, byte[] presetData) GetPreset(string name, string presetData,
                                                                                     PresetBank presetBank)
        {
            var pluginState = new XElement("PluginState");

            pluginState.SetAttributeValue("application", XmlPluginName);
            var parametersState = new XElement("ParametersState");
            var midiControlMap  = new XElement("MidiControlMap");

            midiControlMap.SetAttributeValue("name", XmlPluginName);

            pluginState.Add(parametersState);

            var xmlPreset     = XDocument.Parse(presetData);
            var presetElement = xmlPreset.Element("Preset");

            presetElement.SetAttributeValue("name", name.Replace(Extension, ""));

            var preset = new PresetParserMetadata
            {
                PresetName = name.Replace(Extension, ""), Plugin = PluginInstance.Plugin, BankPath = presetBank.BankPath
            };

            var tagsAttribute = presetElement.Attribute("tags");

            if (!(tagsAttribute is null))
            {
                var modes = GetModes(tagsAttribute.Value);

                foreach (var modeName in modes)
                {
                    preset.Characteristics.Add(new Characteristic {
                        CharacteristicName = modeName
                    });
                }
            }

            presetElement.SetAttributeValue("tags", null);
            presetElement.SetAttributeValue("version", null);


            parametersState.Add(presetElement);


            var ms = RecursiveVC2Parser.WriteVC2(pluginState.ToString());

            return(preset, ms.ToArray());
        }
        protected async Task GetPresets(PresetBank bank, int start, int numPresets, string sourceFile)
        {
            switch (PresetSaveMode)
            {
            case PresetSaveModes.Fallback:
            case PresetSaveModes.BankTrickery:
                await GetPresetsUsingBankTrickery(bank, start, numPresets, sourceFile);

                break;

            case PresetSaveModes.FullBank:
            case PresetSaveModes.None:
                await GetPresetsUsingFullBank(bank, start, numPresets, sourceFile);

                break;
            }
        }
예제 #9
0
        private async Task <int> H2PScanBank(PresetBank bank, DirectoryInfo directory, bool persist)
        {
            var count = 0;

            foreach (var file in directory.EnumerateFiles("*.h2p"))
            {
                count++;
                if (!persist)
                {
                    continue;
                }

                await ProcessPreset(file, bank);
            }

            foreach (var subDirectory in directory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly))
            {
                var subBank = bank.CreateRecursive(subDirectory.Name);
                count += await H2PScanBank(subBank, subDirectory, persist);
            }

            return(count);
        }
        protected async Task GetPresetsUsingBankTrickery(PresetBank bank, int start, int numPresets, string sourceFile)
        {
            if (start < 0)
            {
                Logger.Error("GetPresets start index is less than 0, ignoring. This is probably a bug or a " +
                             "misconfiguration. Please report this including the full log file.");
                return;
            }

            var endIndex = start + numPresets;

            if (endIndex > PluginInstance.Plugin.PluginInfo.ProgramCount)
            {
                Logger.Error(
                    $"Tried to retrieve presets between the index {start} and {endIndex}, but this would exceed maximum " +
                    $"program count of {PluginInstance.Plugin.PluginInfo.ProgramCount}, ignoring. You might wish to " +
                    "report this as a bug.");
                return;
            }

            for (var index = start; index < endIndex; index++)
            {
                PluginInstance.SetProgram(0);
                PluginInstance.PerformIdleLoop(10);

                var programBackup = PluginInstance.GetChunk(true);
                PluginInstance.SetProgram(index);
                PluginInstance.PerformIdleLoop(10);

                var programName    = PluginInstance.GetCurrentProgramName();
                var fullSourceFile = sourceFile + ":" + index;
                var vstPreset      = new PresetParserMetadata
                {
                    SourceFile = fullSourceFile,
                    BankPath   = bank.BankPath,
                    PresetName = programName,
                    Plugin     = PluginInstance.Plugin
                };


                var realProgram = PluginInstance.GetChunk(true);
                PluginInstance.SetProgram(0);
                PluginInstance.PerformIdleLoop(10);

                PluginInstance.SetChunk(realProgram, true);
                PluginInstance.PerformIdleLoop(10);

                var presetData = PluginInstance.GetChunk(false);

                // Restore original program 0
                PluginInstance.SetChunk(programBackup, true);
                PluginInstance.PerformIdleLoop(10);

                var hash = HashUtils.getIxxHash(presetData);

                if (PresetHashes.ContainsKey(hash))
                {
                    Logger.Warning(
                        $"Skipping program {index} with name {programName} because a program with the same data " +
                        $"was already added ({PresetHashes[hash]}. Please report this if you think if it's a bug.");
                }
                else
                {
                    PresetHashes.Add(hash, fullSourceFile + " " + programName);
                    await DataPersistence.PersistPreset(vstPreset, presetData);
                }
            }
        }
예제 #11
0
        public async Task <int> ScanPresetXml(XElement rootElement, string fileName, PresetBank presetBank,
                                              bool persist = true)
        {
            var count       = 0;
            var directories = rootElement.Elements("Directory");

            foreach (var directory in directories)
            {
                var bankNameElement = directory.Attribute("Name");

                if (bankNameElement == null)
                {
                    bankNameElement = directory.Attribute("name");

                    if (bankNameElement == null)
                    {
                        Logger.Warning($"A directory within {fileName} contains no name attribute. Maybe the file is corrupt?");
                        continue;
                    }
                }

                var subBank = presetBank.CreateRecursive(bankNameElement.Value);
                count += await ScanPresetXml(directory, fileName, subBank, persist);
            }

            var presets = rootElement.Elements("preset");

            foreach (var presetElement in presets)
            {
                var nameAttribute = presetElement.Attribute("name");

                if (nameAttribute == null)
                {
                    nameAttribute = presetElement.Attribute("Name");

                    if (nameAttribute == null)
                    {
                        Logger.Error($"A preset in the file {fileName} has no name attribute. Ignoring the preset, " +
                                     "as PresetMagician does not know how to identify the name of the preset.");
                        continue;
                    }
                }

                count++;

                if (persist)
                {
                    var sourceFile = fileName + ":" + presetBank.BankPath + "/" + nameAttribute.Value;
                    var presetName = nameAttribute.Value;
                    if (SourceFileDuplicates.ContainsKey(sourceFile))
                    {
                        SourceFileDuplicates[sourceFile]++;
                        sourceFile += ":" + (SourceFileDuplicates[sourceFile] + 1);
                        presetName += "-1";
                        Logger.Warning(
                            $"The preset file {fileName} contains a duplicate preset name in {presetBank.BankPath}/" +
                            $"{nameAttribute.Value}. " + Environment.NewLine + Environment.NewLine +
                            "PresetMagician has no reliable way to check which preset is which " +
                            "(think of it as two files with exactly the same name in the same directory). " +
                            Environment.NewLine + Environment.NewLine +
                            $"PresetMagician will use {presetName} for it; however, it could happen that the preset " +
                            "data gets mixed up between these duplicate presets." +
                            Environment.NewLine + Environment.NewLine +
                            "To avoid this, please rename the duplicates to an unique name.");
                    }
                    else
                    {
                        SourceFileDuplicates.Add(sourceFile, 0);
                    }

                    var preset = new PresetParserMetadata
                    {
                        PresetName = presetName, Plugin = PluginInstance.Plugin,
                        BankPath   = presetBank.BankPath,
                        SourceFile = sourceFile
                    };

                    var base64 = presetElement.Value.Trim().Replace("-", "/").Replace("$", "");

                    await DataPersistence.PersistPreset(preset, Convert.FromBase64String(base64));
                }
            }

            return(count);
        }