diff --git a/Libraries/UniversalEditor.Essential/Common/Reflection.cs b/Libraries/UniversalEditor.Essential/Common/Reflection.cs index 3a8d789e..f2122347 100644 --- a/Libraries/UniversalEditor.Essential/Common/Reflection.cs +++ b/Libraries/UniversalEditor.Essential/Common/Reflection.cs @@ -333,127 +333,84 @@ namespace UniversalEditor.Common #region Object Models private static ObjectModelReference[] mvarAvailableObjectModels = null; - public static ObjectModelReference[] GetAvailableObjectModels() + public static ObjectModelReference[] GetAvailableObjectModels(Accessor accessor = null) { if (mvarAvailableObjectModels == null) Initialize(); - return mvarAvailableObjectModels; + if (accessor == null) + { + return mvarAvailableObjectModels; + } + List list = new List(); + Association[] assocs = GetAvailableAssociations(accessor); + for (int i = 0; i < assocs.Length; i++) + { + for (int j = 0; j < assocs[i].ObjectModels.Count; j++) + { + if (list.Contains(assocs[i].ObjectModels[j])) + continue; + list.Add(assocs[i].ObjectModels[j]); + } + } + return list.ToArray(); } - public static T GetAvailableObjectModel(string filename) where T : ObjectModel + public static T GetAvailableObjectModel(string filename) where T : ObjectModel, new() { return GetAvailableObjectModel(new FileAccessor(filename)); } - public static T GetAvailableObjectModel(Accessor accessor) where T : ObjectModel + public static T GetAvailableObjectModel(Accessor accessor) where T : ObjectModel, new() { - ObjectModelReference[] omrs = GetAvailableObjectModels(accessor); - if (omrs.Length == 0) + if (GetAvailableObjectModel(accessor, out T objectToFill)) + return objectToFill; + return null; + } + public static bool GetAvailableObjectModel(string filename, out T objectToFill) where T : ObjectModel, new() + { + return GetAvailableObjectModel(new FileAccessor(filename), out objectToFill); + } + public static bool GetAvailableObjectModel(Accessor accessor, out T objectToFill) where T : ObjectModel, new() + { + Initialize(); + + Association[] assocs = GetAvailableAssociations(accessor); + if (assocs.Length == 0) { - // we failed to find an object model from the accessor, so let's try and - // force the loading of the object model we're told to load in the first place - - Type type = typeof(T); - ObjectModel om = (ObjectModel)type.Assembly.CreateInstance(type.FullName); - ObjectModelReference omr = om.MakeReference(); - - DataFormatReference[] dfrs = GetAvailableDataFormats(omr); - - for (int i = 0; i < dfrs.Length; i++) - { - DataFormatReference dfr = dfrs[i]; - try - { - DataFormat df = dfr.Create(); - Document.Load(om, df, accessor); - break; - } - catch (InvalidDataFormatException ex) - { - accessor.Close(); - continue; - } - catch (NotImplementedException ex) - { - accessor.Close(); - continue; - } - } - return (T)om; + objectToFill = null; + return false; } - foreach (ObjectModelReference omr in omrs) - { - if (omr.Type == typeof(T)) - { - ObjectModel om = (T)omr.Create(); - DataFormatReference[] dfrs = GetAvailableDataFormats(accessor, omr); - foreach (DataFormatReference dfr in dfrs) + ObjectModel om = new T(); + + for (int i = 0; i < assocs.Length; i++) + { + if (assocs[i].ObjectModels.Contains(om.GetType()) || assocs[i].ObjectModels.Contains(om.MakeReference().ID)) + { + if (assocs[i].DataFormats.Count > 0) { - DataFormat df = dfr.Create(); + DataFormat df = assocs[i].DataFormats[0].Create(); Document doc = new Document(om, df, accessor); try { doc.InputAccessor.Open(); doc.Load(); doc.InputAccessor.Close(); - break; + + objectToFill = (T)doc.ObjectModel; + return true; } - catch (InvalidDataFormatException ex) + catch (DataFormatException) { - if (!(Array.IndexOf(dfrs, df) < dfrs.Length - 1)) - { - throw ex; - } doc.InputAccessor.Close(); } - catch (ObjectModelNotSupportedException ex) + catch (NotSupportedException) { - if (!(Array.IndexOf(dfrs, df) < dfrs.Length - 1)) - { - throw ex; - } doc.InputAccessor.Close(); } } - return (T)om; } } - return null; - } - public static bool GetAvailableObjectModel(string filename, ref T objectToFill) where T : ObjectModel - { - return GetAvailableObjectModel(new FileAccessor(filename), ref objectToFill); - } - public static bool GetAvailableObjectModel(Accessor accessor, ref T objectToFill) where T : ObjectModel - { - ObjectModel om = (T)objectToFill; - DataFormatReference[] dfrs = GetAvailableDataFormats(accessor); - if (dfrs.Length == 0) - { - return false; - } - - for (int i = 0; i < dfrs.Length; i++) - { - DataFormat df = dfrs[i].Create(); - Document doc = new Document(om, df, accessor); - try - { - doc.InputAccessor.Open(); - doc.Load(); - doc.InputAccessor.Close(); - - return true; - } - catch (DataFormatException) - { - doc.InputAccessor.Close(); - } - catch (NotSupportedException) - { - doc.InputAccessor.Close(); - } - } - return true; + objectToFill = null; + return false; } public static ObjectModelReference[] GetAvailableObjectModels(DataFormatReference dfr) @@ -474,28 +431,6 @@ namespace UniversalEditor.Common } return list.ToArray(); } - public static ObjectModelReference[] GetAvailableObjectModels(Accessor accessor, DataFormatCapabilities capabilities = DataFormatCapabilities.All) - { - ObjectModelReference[] array = GetAvailableObjectModels(); - DataFormatReference[] dfs = GetAvailableDataFormats(accessor); - List list = new List(); - if (dfs.Length == 0) return list.ToArray(); - - foreach (ObjectModelReference om in array) - { - if (om == null) continue; - - foreach (DataFormatReference df in dfs) - { - if (df == null) continue; - if ((df.Capabilities[om.Type] & capabilities) == capabilities) - { - list.Add(om); - } - } - } - return list.ToArray(); - } public static ObjectModelReference GetAvailableObjectModelByTypeName(string TypeName) { ObjectModelReference[] omrs = GetAvailableObjectModels(); @@ -566,27 +501,28 @@ namespace UniversalEditor.Common #endregion #region Data Formats private static DataFormatReference[] mvarAvailableDataFormats = null; - public static DataFormatReference[] GetAvailableDataFormats() + public static DataFormatReference[] GetAvailableDataFormats(Accessor accessor = null) { if (mvarAvailableDataFormats == null) Initialize(); - return mvarAvailableDataFormats; - } - - public static DataFormatReference[] GetAvailableDataFormats(string filename) - { - Association[] associations = Association.FromCriteria(new AssociationCriteria() { FileName = filename }); - List list = new List(); - foreach (Association association in associations) + if (accessor == null) { - for (int i = 0; i < association.DataFormats.Count; i++) + return mvarAvailableDataFormats; + } + List list = new List(); + Association[] assocs = GetAvailableAssociations(accessor); + for (int i = 0; i < assocs.Length; i++) + { + for (int j = 0; j < assocs[i].DataFormats.Count; j++) { - list.Add(association.DataFormats[i]); + if (list.Contains(assocs[i].DataFormats[j])) + continue; + list.Add(assocs[i].DataFormats[j]); } } - list.Sort(new Comparison(_DataFormatReferenceComparer)); return list.ToArray(); } - public static DataFormatReference[] GetAvailableDataFormats(Accessor accessor) + + public static Association[] GetAvailableAssociations(Accessor accessor) { bool needsOpen = false; if (!accessor.IsOpen) @@ -600,21 +536,12 @@ namespace UniversalEditor.Common List listAssocs = new List(associations); listAssocs.Sort(); - List list = new List(); - foreach (Association association in listAssocs) - { - for (int i = 0; i < association.DataFormats.Count; i++) - { - list.Add(association.DataFormats[i]); - } - } - if (needsOpen) { // close the accessor since we're done with it accessor.Close(); } - return list.ToArray(); + return listAssocs.ToArray(); } public static DataFormatReference[] GetAvailableDataFormats(ObjectModelReference objectModelReference) @@ -724,41 +651,5 @@ namespace UniversalEditor.Common } #endif - public static ObjectModel GetAvailableObjectModel(byte[] data, string filename, string ObjectModelTypeName) - { - return GetAvailableObjectModel(new MemoryAccessor(data, filename), ObjectModelTypeName); - } - public static ObjectModel GetAvailableObjectModel(Accessor accessor, string ObjectModelTypeName) - { - long resetpos = accessor.Position; - - DataFormatReference[] dfrs = GetAvailableDataFormats(accessor); - foreach (DataFormatReference dfr in dfrs) - { - ObjectModelReference[] omrs = GetAvailableObjectModels(dfr); - foreach (ObjectModelReference omr in omrs) - { - if (omr.Type.FullName == ObjectModelTypeName) - { - ObjectModel om = omr.Create(); - if (om == null) return null; - - DataFormat df = dfr.Create(); - try - { - Document.Load(om, df, accessor); - } - catch (InvalidDataFormatException) - { - accessor.Position = resetpos; - break; - } - - return om; - } - } - } - return null; - } } } diff --git a/Libraries/UniversalEditor.Essential/ObjectModels/FileSystem/FileSystemObjectModel.cs b/Libraries/UniversalEditor.Essential/ObjectModels/FileSystem/FileSystemObjectModel.cs index 0ec8549d..45ba5429 100644 --- a/Libraries/UniversalEditor.Essential/ObjectModels/FileSystem/FileSystemObjectModel.cs +++ b/Libraries/UniversalEditor.Essential/ObjectModels/FileSystem/FileSystemObjectModel.cs @@ -80,10 +80,10 @@ namespace UniversalEditor.ObjectModels.FileSystem foreach (string fileName in fileNames) { FileAccessor accessor = new FileAccessor(fileName); - FileSystemObjectModel fsom1 = UniversalEditor.Common.Reflection.GetAvailableObjectModel(accessor); - if (fsom1 == null) continue; - - fsom1.CopyTo(fsom); + if (UniversalEditor.Common.Reflection.GetAvailableObjectModel(accessor, out FileSystemObjectModel fsom1)) + { + fsom1.CopyTo(fsom); + } } return fsom; } diff --git a/Plugins/UniversalEditor.Plugins.Concertroid/ObjectModels/Concertroid/Producer.cs b/Plugins/UniversalEditor.Plugins.Concertroid/ObjectModels/Concertroid/Producer.cs index e885de0c..4153dd98 100644 --- a/Plugins/UniversalEditor.Plugins.Concertroid/ObjectModels/Concertroid/Producer.cs +++ b/Plugins/UniversalEditor.Plugins.Concertroid/ObjectModels/Concertroid/Producer.cs @@ -76,10 +76,12 @@ namespace UniversalEditor.ObjectModels.Concertroid string[] fileNames = System.IO.Directory.GetFiles(ConfigurationPath, "*.library", System.IO.SearchOption.AllDirectories); foreach (string fileName in fileNames) { - LibraryObjectModel library = UniversalEditor.Common.Reflection.GetAvailableObjectModel(fileName); - foreach (Producer p in library.Producers) + if (UniversalEditor.Common.Reflection.GetAvailableObjectModel(fileName, out LibraryObjectModel library)) { - list.Add(p); + foreach (Producer p in library.Producers) + { + list.Add(p); + } } } } diff --git a/Plugins/UniversalEditor.Plugins.Microsoft.VisualStudio/DataFormats/Solution/Microsoft/VisualStudio/VisualStudioSolutionDataFormat.cs b/Plugins/UniversalEditor.Plugins.Microsoft.VisualStudio/DataFormats/Solution/Microsoft/VisualStudio/VisualStudioSolutionDataFormat.cs index c8c873ff..6681d870 100644 --- a/Plugins/UniversalEditor.Plugins.Microsoft.VisualStudio/DataFormats/Solution/Microsoft/VisualStudio/VisualStudioSolutionDataFormat.cs +++ b/Plugins/UniversalEditor.Plugins.Microsoft.VisualStudio/DataFormats/Solution/Microsoft/VisualStudio/VisualStudioSolutionDataFormat.cs @@ -120,10 +120,12 @@ namespace UniversalEditor.DataFormats.Solution.Microsoft.VisualStudio projectFileName = projectFileName.Replace('\\', System.IO.Path.DirectorySeparatorChar); if (System.IO.File.Exists(projectFileName)) { - ProjectObjectModel project = UniversalEditor.Common.Reflection.GetAvailableObjectModel(projectFileName); - project.Title = projectTitle; - sol.Projects.Add(project); - lastProject = project; + if (UniversalEditor.Common.Reflection.GetAvailableObjectModel(projectFileName, out ProjectObjectModel project)) + { + project.Title = projectTitle; + sol.Projects.Add(project); + lastProject = project; + } } else { diff --git a/Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Audio/Synthesized/MusicXML/MusicXMLDataFormat.cs b/Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Audio/Synthesized/MusicXML/MusicXMLDataFormat.cs index c040e620..413f9124 100644 --- a/Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Audio/Synthesized/MusicXML/MusicXMLDataFormat.cs +++ b/Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Audio/Synthesized/MusicXML/MusicXMLDataFormat.cs @@ -161,7 +161,10 @@ namespace UniversalEditor.DataFormats.Multimedia.Audio.Synthesized.MusicXML if (file.FileName != null) { string fileName = Path.MakeAbsolutePath(tag_elTagScorePartChild.Attributes["href"].Value, System.IO.Path.GetDirectoryName(file.FileName)); - track.Synthesizer = Reflection.GetAvailableObjectModel(new FileAccessor(fileName)); + if (Reflection.GetAvailableObjectModel(new FileAccessor(fileName), out VoicebankObjectModel synth)) + { + track.Synthesizer = synth; + } } } }