using Jacobi.Vst.Core; using Jacobi.Vst.Host.Interop; using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Runtime.CompilerServices; using System.Text.Json.Serialization; using System.Windows; using static QuikDawEditor.EDITING.StaticProperties; using System.Windows.Threading; namespace QuikDawEditor.VST; public class VstPluginReference : INotifyPropertyChanged { public bool IsSynth { get { return myContext.PluginInfo.Flags.HasFlag(VstPluginFlags.IsSynth); } } public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged([CallerMemberName] String propertyName = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public VstPluginReference() { } private string _VstDllFileFullPath; public string VstDllFileFullPath { get { return _VstDllFileFullPath; } set { _VstDllFileFullPath = value; VstDllFile = Path.GetFileName(value); } } public string PluginName { get; set; } public string PluginCategory { get; set; } public bool IsNotVst { get { return PluginCategory.Contains("has no exported 'VSTPluginMain' or 'main' function"); } } [JsonIgnore(Condition = JsonIgnoreCondition.Always)] public int VSTVersion { get; set; } [JsonIgnore(Condition = JsonIgnoreCondition.Always)] public string VstDllFile { get; set; } public string pluginID; public bool isNotifying = false; private bool _IsSelected = false; [JsonIgnore(Condition = JsonIgnoreCondition.Always)] public bool IsSelected { get { return _IsSelected; } set { _IsSelected = value; isNotifying = true; NotifyPropertyChanged(nameof(IsSelected)); isNotifying = false; } } //private bool _IsActive = false; //public bool IsActive { get { return _IsActive && ReturnedWithoutError; } set { _IsActive = value; NotifyPropertyChanged(nameof(IsActive)); } } private bool _ReturnedWithoutError = true; public bool ReturnedWithoutError { get { return _ReturnedWithoutError; } set { _ReturnedWithoutError = value; NotifyPropertyChanged(nameof(ReturnedWithoutError)); } } [JsonIgnore(Condition = JsonIgnoreCondition.Always)] private bool _SkipLoadingPlugin = false; public bool SkipLoadingPlugin { get { return _SkipLoadingPlugin; } set { _SkipLoadingPlugin = value; switch (value) { case true: if (!Properties.Settings.Default.SkipLoadingPlugins.Contains(VstDllFileFullPath)) Properties.Settings.Default.SkipLoadingPlugins.Add(VstDllFileFullPath); break; case false: if (Properties.Settings.Default.SkipLoadingPlugins.Contains(VstDllFileFullPath)) Properties.Settings.Default.SkipLoadingPlugins.Remove(VstDllFileFullPath); break; } Properties.Settings.Default.Save(); NotifyPropertyChanged(nameof(SkipLoadingPlugin)); } } public ObservableCollection VstParameters { get; set; } = new ObservableCollection(); public ObservableCollection VstPrograms = new ObservableCollection(); public byte[] PluginChunk { get; set; } public VstPluginContext myContext; internal HostCommandStub myHostCmdStub; public int PluginVersion; public string Flags; public void GetContextInfo() { try { HostCommandStub hcs = new HostCommandStub(); VstPluginContext ctx = null; File.AppendAllText(VSTLoadLogFileName, this.VstDllFileFullPath); try { ctx = VstPluginContext.Create(VstDllFileFullPath, hcs); ctx.AcceptPluginInfoData(true); PluginName = ctx.PluginCommandStub.Commands.GetEffectName(); PluginCategory = ctx.PluginCommandStub.Commands.GetCategory().ToString(); VSTVersion = ctx.PluginCommandStub.Commands.GetVstVersion(); PluginVersion = ctx.PluginInfo.PluginVersion; Flags = ctx.PluginInfo.Flags.ToString(); //pluginID = ctx.PluginCommandStub.PluginContext.PluginInfo.PluginID.ToString(); //MessageBox.Show("got pluginid=" + pluginID); Debug.WriteLine("Loaded Plugin: " + PluginName + ":::" + VSTVersion.ToString()); } catch (Exception ex) { //MessageBox.Show("Error creating vst context...."); Debug.WriteLine("Had an error loading Vst:\n" + ex.Message + "\n"); try { Application.Current.Dispatcher.Invoke(() => { ((EditorWindow)(App.Current.MainWindow)) .ShowLocalMessage("**************************\nEncountered error loading Vst:\n" + this.VstDllFileFullPath + "\n\"" + ex.Message + "\"\n******************************"); }, DispatcherPriority.Normal); } catch(Exception ex2) { Debug.WriteLine ("couldn't display message: " + ex2.Message); } this.PluginCategory = ex.Message.Replace(this.VstDllFileFullPath, "").Trim("'".ToCharArray()); this.ReturnedWithoutError = false; this.PluginName += " (not loaded)"; } ctx?.Dispose(); hcs = null; File.AppendAllText(VSTLoadLogFileName, "***OK\n"); } //catch (Exception ex) { ((EditorWindow)(App.Current.MainWindow)).ShowLocalMessage("Was unable to load vst: " + this.VstDllFileFullPath + "\n" + ex.Message); } catch { ((EditorWindow)(App.Current.MainWindow)).ShowLocalMessage("Was totally unable to load vst: "); } } public ActiveVstPlugin CreateActivePlugin() { ActiveVstPlugin newAP = new ActiveVstPlugin(); newAP.VstDllFileFullPath = VstDllFileFullPath; newAP.VstDllFile = VstDllFile; newAP.PluginName = PluginName; newAP.PluginCategory = PluginCategory; newAP.VstParameters = new ObservableCollection(VstParameters); newAP.VstPrograms = new ObservableCollection(VstPrograms); //newAP.myHostCmdStub.PluginContext.PluginInfo = newAP.pluginID = Guid.NewGuid().ToString(); return newAP; } //public void Close() //{ // // myContext.PluginCommandStub.Commands.MainsChanged(false); // *********added // this.CloseEditor(); // //if (myContext != null) myContext.PluginCommandStub.Commands.Close(); //} }