/// <summary> /// Search for assemblies in given directory /// </summary> /// <param name="directory">directory</param> /// <param name="state">Search for all or only for signed assemblies</param> /// <param name="foundAssemblies">list of found assemblies</param> private void FindAssemblies(DirectoryInfo directory, AssemblySigningRequirement state, Dictionary <string, Assembly> foundAssemblies) { //foreach (DirectoryInfo subDirectory in directory.GetDirectories()) //{ // FindAssemblies(subDirectory, state, foundAssemblies); //} int currentPosition = 0; foreach (FileInfo fileInfo in directory.GetFiles("*.dll")) { if (disabledAssemblies != null && disabledAssemblies.Contains(fileInfo.Name)) { continue; } currentPosition++; try { Assembly asm = Assembly.Load(AssemblyName.GetAssemblyName(fileInfo.FullName)); string key = GetAssemblyKey(asm.FullName, state); if (key == null) { throw new UnknownFileFormatException(fileInfo.FullName); } bool sendMessage = false; if (!foundAssemblies.ContainsKey(key)) { foundAssemblies.Add(key, asm); sendMessage = true; } else if (GetVersion(asm) > GetVersion(foundAssemblies[key])) { foundAssemblies[key] = asm; sendMessage = true; } if (sendMessage) { SendDebugMessage("Loaded Assembly \"" + asm.FullName + "\" from file: " + fileInfo.FullName); if (OnPluginLoaded != null) { OnPluginLoaded(this, new PluginLoadedEventArgs(currentPosition, this.availablePluginsApproximation, string.Format("{0} Version={1}", asm.GetName().Name, GetVersion(asm)))); } } } catch (BadImageFormatException) { SendExceptionMessage(string.Format(Resources.Exceptions.non_plugin_file, fileInfo.Name)); } catch (Exception ex) { SendExceptionMessage(ex); } } }
/// <summary> /// Returns all types found in the plugins /// </summary> /// <param name="state">Load type from all plugins or from signed only</param> /// <returns></returns> public Dictionary <string, Type> LoadTypes(AssemblySigningRequirement state) { availablePluginsApproximation = AvailablePluginsApproximation(new DirectoryInfo(crypPluginsFolder)); availablePluginsApproximation += AvailablePluginsApproximation(new DirectoryInfo(CrypToolStorePluginFolder), true); int currentPosition = FindAssemblies(new DirectoryInfo(crypPluginsFolder), state, foundAssemblies, 0); FindAssemblies(new DirectoryInfo(CrypToolStorePluginFolder), state, foundAssemblies, currentPosition, true); LoadTypes(foundAssemblies); return(this.loadedTypes); }
/// <summary> /// Create a unique key for each assembly /// </summary> /// <param name="assemblyFullName">Full name of the assembly</param> /// <param name="state">Signed or unsigned</param> /// <returns>Returns the key or null if public key is null and signing is required</returns> private string GetAssemblyKey(string assemblyFullName, AssemblySigningRequirement state) { AssemblyName asmName = new AssemblyName(assemblyFullName); if (state == AssemblySigningRequirement.LoadSignedAssemblies) { if (asmName.KeyPair.PublicKey == null) { return(null); } return(asmName.Name + "__" + asmName.KeyPair.ToString()); } return(asmName.Name); }
/// <summary> /// Returns all types found in the plugins /// </summary> /// <param name="state">Load type from all plugins or from signed only</param> /// <returns></returns> public Dictionary <string, Type> LoadTypes(AssemblySigningRequirement state) { if (Directory.Exists(globalPluginStore)) { availablePluginsApproximation = AvailablePluginsApproximation(new DirectoryInfo(globalPluginStore)); FindAssemblies(new DirectoryInfo(globalPluginStore), state, foundAssemblies); } // custom plugin store is not supported yet //if (!Directory.Exists(customPluginStore)) // Directory.CreateDirectory(customPluginStore); //FindAssemblies(new DirectoryInfo(customPluginStore), state, foundAssemblies); LoadTypes(foundAssemblies); return(this.loadedTypes); }
/// <summary> /// Adds an assembly to the store /// </summary> /// <param name="buffer">byte[] of the assembly</param> /// <param name="state">Signed or unsigned</param> /// <param name="pluginStore">Global or Custom Store</param> public void AddPlugin(byte[] buffer, AssemblySigningRequirement state, PluginStore pluginStore) { try { Assembly asm = Assembly.ReflectionOnlyLoad(buffer); AssemblyName asmName = new AssemblyName(asm.FullName); string publicKeyToken = GetPublicToken(asm); if ((state == AssemblySigningRequirement.StoreSignedAssemblies) && (publicKeyToken == string.Empty)) { throw new AssemblyNotSignedException(); } string pluginStoreDirectory = GetPluginDirectory(asmName, publicKeyToken, pluginStore); if (!Directory.Exists(pluginStoreDirectory)) { Directory.CreateDirectory(pluginStoreDirectory); } WriteFile(buffer, pluginStoreDirectory, asmName); } catch (AssemblyNotSignedException ex) { if (OnExceptionOccured != null) { OnExceptionOccured(this, new PluginManagerEventArgs(ex)); } } catch { if (OnExceptionOccured != null) { OnExceptionOccured(this, new PluginManagerEventArgs(new StoreAddingException())); } } }