/// <summary> /// Refreshes the plugin store /// </summary> /// <returns></returns> public static PluginStore RefreshPuginStore() { IconSet.Clear(); var pluginStore = new PluginStore(); var pluginDirs = Directory.GetDirectories(PluginsDirectory); var cumulativeFileCount = new int[pluginDirs.Length]; var totalFiles = 0; for (var i = 0; i < pluginDirs.Length; i++) { var currentFileCount = Directory.GetFiles(pluginDirs[i]).Length; cumulativeFileCount[i] = currentFileCount; if (i > 0) cumulativeFileCount[i] = cumulativeFileCount[i] + cumulativeFileCount[i - 1]; totalFiles += currentFileCount; } cumulativeFileCount[0] = 0; // Raise start of plugin store refresh NotifyOnPluginStoreRefreshStarted(); for (var i = 0; i < pluginDirs.Length; i++) { var files = Directory.GetFiles(pluginDirs[i]); LoadPluginsInDirectory(ref pluginStore, pluginDirs[i], files, totalFiles, cumulativeFileCount[i]); } // Save the new plugin store var xml = new XmlSerializer(typeof(PluginStore)); var storeFile = new StreamWriter(PluginStoreFile); xml.Serialize(storeFile, pluginStore); storeFile.Close(); // Raise event that refresh has been completed NotifyOnPluginStoreRefreshCompleted(); return pluginStore; }
public MainWindow() { InitializeComponent(); _pluginStore = PluginStoreManager.LoadPuginStore(); LoadPluginsList(); }
/// <summary> /// Inspects assemblies and executables in the specified directory using /// reflection and searches for controls with the PluginAttribute marked /// against its type. Updates the plugin store with discovered plugins. /// </summary> /// <param name="pluginStore"></param> /// <param name="directory"></param> /// <param name="files"></param> /// <param name="totalFiles"> </param> /// <param name="currentCumulate"> </param> private static void LoadPluginsInDirectory(ref PluginStore pluginStore, string directory, IList<string> files, int totalFiles, int currentCumulate) { for (var i = 0; i < files.Count; i++) { var file = files[i]; // Process only dll and exe files if (!file.EndsWith(".dll") && !file.EndsWith(".exe")) continue; try { var asm = Assembly.LoadFile(file); var types = asm.GetTypes(); foreach (var t in types) { if (!t.IsSubclassOf(typeof(Control))) continue; var pluginAttributes = t.GetCustomAttributes(typeof(PluginAttribute), false); if (pluginAttributes.Length <= 0) continue; var pluginAttribute = (PluginAttribute)pluginAttributes[0]; // Check if an icon file is present var iconFile = Path.Combine(directory, t.Name + ".ico"); if (!File.Exists(iconFile)) iconFile = @"\Resources\Icons\YinYang.ico"; var currentPlugin = new PluginInfo { Name = pluginAttribute.Name, Description = pluginAttribute.Description, Type = t.FullName, AssemblyFile = file.Replace(AppDomain.CurrentDomain.BaseDirectory, ""), InstallPath = directory.Replace(AppDomain.CurrentDomain.BaseDirectory, ""), Icon = iconFile.Replace(AppDomain.CurrentDomain.BaseDirectory, "") }; pluginStore.Plugins.Add(currentPlugin); NotifyOnPluginStoreRefreshProgress( new PluginStoreRefreshProgressEventArgs(currentPlugin, totalFiles, currentCumulate + i + 1)); } } // ReSharper disable EmptyGeneralCatchClause catch // ReSharper restore EmptyGeneralCatchClause { } } }
private void RefreshPluginsButtonClick(object sender, EventArgs e) { SetStatusText("Refreshing...", StatusMessageType.Busy); PluginTabs.UnloadPlugins(); _pluginStore = _refresher.RefreshPluginStore(this); LoadPluginsList(); }