using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using Voile.Common.Reflection; using Voile.Patchouli.Data; namespace Voile.Patchouli.Reflection { public class VoilePluginManager { private VoilePluginManager() { } public static VoilePluginManager _instance; private static Type _dataStorageFactoryType = typeof(IVoileDataStorageFactory); private static Type _objectStorageFactoryType = typeof(IVoileObjectStorageFactory); public static VoilePluginManager GetInstance() { if (_instance == null) { _instance = new VoilePluginManager(); } return _instance; } private List _loadedAssemblies; private List _knownDataStorages; private List _knownObjectStorages; public bool ScanAssembly(Assembly assembly) { if (_loadedAssemblies == null) _loadedAssemblies = new List(); if (_loadedAssemblies.Contains(assembly)) return false; bool result = false; Type[] types = assembly.GetTypes(); foreach (Type t in types) { if (ScanType(t)) result = true; } return result; } public bool ScanType(Type t) { VoilePluginAttribute? voilePluginAttribute = t.GetCustomAttribute(); if (voilePluginAttribute == null) return false; VoilePluginIdAttribute? voilePluginIdAttribute = t.GetCustomAttribute(); if (voilePluginIdAttribute == null) return false; VoilePluginInfo child = new VoilePluginInfo(t); child.Id = voilePluginIdAttribute.Id; if (t.IsAssignableTo(_dataStorageFactoryType)) { if (_knownDataStorages == null) _knownDataStorages = new List(); child.Subsystem = VoileSubsystemType.DataStorage; _knownDataStorages.Add(child); return true; } else if (t.IsAssignableTo(_objectStorageFactoryType)) { if (_knownObjectStorages == null) _knownObjectStorages = new List(); child.Subsystem = VoileSubsystemType.ObjectStorage; _knownObjectStorages.Add(child); return true; } else { throw new VoileReflectionException(String.Format("Could not figure out plugin type of {0}", t.Name)); } } } }