diff --git a/Nuclex.Support (x86).csproj b/Nuclex.Support (x86).csproj index b5ce502..c5fcf5c 100644 --- a/Nuclex.Support (x86).csproj +++ b/Nuclex.Support (x86).csproj @@ -84,7 +84,9 @@ RingMemoryStream.cs - + + TransformingReadOnlyCollection.cs + LicenseKey.cs @@ -114,6 +116,13 @@ PathHelper.cs + + + + + + + diff --git a/Source/Collections/ObservableCollection.Test.cs b/Source/Collections/ObservableCollection.Test.cs index 6f395d7..17ca953 100644 --- a/Source/Collections/ObservableCollection.Test.cs +++ b/Source/Collections/ObservableCollection.Test.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Collections/ObservableCollection.cs b/Source/Collections/ObservableCollection.cs index 01371b1..2e9e347 100644 --- a/Source/Collections/ObservableCollection.cs +++ b/Source/Collections/ObservableCollection.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Collections/PairPriorityQueue.Test.cs b/Source/Collections/PairPriorityQueue.Test.cs index 251dd99..69c5d4d 100644 --- a/Source/Collections/PairPriorityQueue.Test.cs +++ b/Source/Collections/PairPriorityQueue.Test.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Collections/PairPriorityQueue.cs b/Source/Collections/PairPriorityQueue.cs index dde43de..c3f316b 100644 --- a/Source/Collections/PairPriorityQueue.cs +++ b/Source/Collections/PairPriorityQueue.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Collections/Parentable.cs b/Source/Collections/Parentable.cs index cc17891..ecee738 100644 --- a/Source/Collections/Parentable.cs +++ b/Source/Collections/Parentable.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Collections/ParentingCollection.cs b/Source/Collections/ParentingCollection.cs index b1afad6..e981fdb 100644 --- a/Source/Collections/ParentingCollection.cs +++ b/Source/Collections/ParentingCollection.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Collections/PriorityItemPair.cs b/Source/Collections/PriorityItemPair.cs index ca8ebe8..8415d5d 100644 --- a/Source/Collections/PriorityItemPair.cs +++ b/Source/Collections/PriorityItemPair.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Collections/PriorityQueue.Test.cs b/Source/Collections/PriorityQueue.Test.cs index c24e908..02374bf 100644 --- a/Source/Collections/PriorityQueue.Test.cs +++ b/Source/Collections/PriorityQueue.Test.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Collections/PriorityQueue.cs b/Source/Collections/PriorityQueue.cs index 2917492..934d12e 100644 --- a/Source/Collections/PriorityQueue.cs +++ b/Source/Collections/PriorityQueue.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as @@ -196,15 +196,17 @@ namespace Nuclex.Support.Collections { while(child < this.count) { - if( + bool needsToBeMoved = ((child + 1) < this.count) && - (this.comparer.Compare(heap[child], this.heap[child + 1]) < 0) - ) + (this.comparer.Compare(heap[child], this.heap[child + 1]) < 0); + + if(needsToBeMoved) ++child; this.heap[index] = this.heap[child]; index = child; child = getLeftChild(index); + } bubbleUp(index, item); diff --git a/Source/Collections/RingMemoryStream.Test.cs b/Source/Collections/RingMemoryStream.Test.cs index eb298e5..2668739 100644 --- a/Source/Collections/RingMemoryStream.Test.cs +++ b/Source/Collections/RingMemoryStream.Test.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Collections/RingMemoryStream.cs b/Source/Collections/RingMemoryStream.cs index 3a3b13c..a443990 100644 --- a/Source/Collections/RingMemoryStream.cs +++ b/Source/Collections/RingMemoryStream.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Collections/TransformingReadOnlyCollection.Interfaces.cs b/Source/Collections/TransformingReadOnlyCollection.Interfaces.cs index acc3a47..f3685ba 100644 --- a/Source/Collections/TransformingReadOnlyCollection.Interfaces.cs +++ b/Source/Collections/TransformingReadOnlyCollection.Interfaces.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Collections/TransformingReadOnlyCollection.cs b/Source/Collections/TransformingReadOnlyCollection.cs index dfabc36..6170381 100644 --- a/Source/Collections/TransformingReadOnlyCollection.cs +++ b/Source/Collections/TransformingReadOnlyCollection.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Licensing/LicenseKey.Test.cs b/Source/Licensing/LicenseKey.Test.cs index 112942a..16f9f0f 100644 --- a/Source/Licensing/LicenseKey.Test.cs +++ b/Source/Licensing/LicenseKey.Test.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Licensing/LicenseKey.cs b/Source/Licensing/LicenseKey.cs index 38d2a33..df8ce7e 100644 --- a/Source/Licensing/LicenseKey.cs +++ b/Source/Licensing/LicenseKey.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Packing/ArevaloRectanglePacker.Test.cs b/Source/Packing/ArevaloRectanglePacker.Test.cs index 47f4bb5..3efc7bf 100644 --- a/Source/Packing/ArevaloRectanglePacker.Test.cs +++ b/Source/Packing/ArevaloRectanglePacker.Test.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Packing/ArevaloRectanglePacker.cs b/Source/Packing/ArevaloRectanglePacker.cs index 433ab5a..9f39397 100644 --- a/Source/Packing/ArevaloRectanglePacker.cs +++ b/Source/Packing/ArevaloRectanglePacker.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Packing/CygonRectanglePacker.Test.cs b/Source/Packing/CygonRectanglePacker.Test.cs index b098572..8ceae5e 100644 --- a/Source/Packing/CygonRectanglePacker.Test.cs +++ b/Source/Packing/CygonRectanglePacker.Test.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Packing/CygonRectanglePacker.cs b/Source/Packing/CygonRectanglePacker.cs index 2e7ff87..610631a 100644 --- a/Source/Packing/CygonRectanglePacker.cs +++ b/Source/Packing/CygonRectanglePacker.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Packing/OutOfSpaceException.cs b/Source/Packing/OutOfSpaceException.cs index afbe9f5..aacc264 100644 --- a/Source/Packing/OutOfSpaceException.cs +++ b/Source/Packing/OutOfSpaceException.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Packing/RectanglePacker.Test.cs b/Source/Packing/RectanglePacker.Test.cs index 20c09d4..d2498fd 100644 --- a/Source/Packing/RectanglePacker.Test.cs +++ b/Source/Packing/RectanglePacker.Test.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Packing/RectanglePacker.cs b/Source/Packing/RectanglePacker.cs index 5617376..00d2ba2 100644 --- a/Source/Packing/RectanglePacker.cs +++ b/Source/Packing/RectanglePacker.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Packing/SimpleRectanglePacker.Test.cs b/Source/Packing/SimpleRectanglePacker.Test.cs index e9ad060..2344a8d 100644 --- a/Source/Packing/SimpleRectanglePacker.Test.cs +++ b/Source/Packing/SimpleRectanglePacker.Test.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Packing/SimpleRectanglePacker.cs b/Source/Packing/SimpleRectanglePacker.cs index 56abb53..27ba460 100644 --- a/Source/Packing/SimpleRectanglePacker.cs +++ b/Source/Packing/SimpleRectanglePacker.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Parsing/CommandLineParser.cs b/Source/Parsing/CommandLineParser.cs index a5131d0..51e8c9c 100644 --- a/Source/Parsing/CommandLineParser.cs +++ b/Source/Parsing/CommandLineParser.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/PathHelper.Test.cs b/Source/PathHelper.Test.cs index 55fc69b..326b488 100644 --- a/Source/PathHelper.Test.cs +++ b/Source/PathHelper.Test.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Plugins/Attributes.cs b/Source/Plugins/Attributes.cs new file mode 100644 index 0000000..d5d9204 --- /dev/null +++ b/Source/Plugins/Attributes.cs @@ -0,0 +1,38 @@ +#region CPL License +/* +Nuclex Framework +Copyright (C) 2002-2008 Nuclex Development Labs + +This library is free software; you can redistribute it and/or +modify it under the terms of the IBM Common Public License as +published by the IBM Corporation; either version 1.0 of the +License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +IBM Common Public License for more details. + +You should have received a copy of the IBM Common Public +License along with this library +*/ +#endregion + +using System; + +namespace Nuclex.Support.Plugins { + + /// Attribute that prevents a class from being seen by the PluginHost + /// + /// When this attribute is attached to a class it will be invisible to the + /// PluginHost and not become accessable as a plugin. + /// + [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] + public class NoPluginAttribute : System.Attribute { + + /// Initializes an instance of the NoPluginAttributes + public NoPluginAttribute() : base() { } + + } + +} // namespace Nuclex.Support.Plugins diff --git a/Source/Plugins/Employer.cs b/Source/Plugins/Employer.cs new file mode 100644 index 0000000..37b5207 --- /dev/null +++ b/Source/Plugins/Employer.cs @@ -0,0 +1,48 @@ +#region CPL License +/* +Nuclex Framework +Copyright (C) 2002-2008 Nuclex Development Labs + +This library is free software; you can redistribute it and/or +modify it under the terms of the IBM Common Public License as +published by the IBM Corporation; either version 1.0 of the +License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +IBM Common Public License for more details. + +You should have received a copy of the IBM Common Public +License along with this library +*/ +#endregion + +using System; + +namespace Nuclex.Support.Plugins { + + /// Plugin type employer + /// + /// This class is used by the plugin host to assess whether a concrete type found + /// in a plugin assembly is suited to be processed the plugin user. If so, + /// the employer can employ the type. Employing can typically mean to create an + /// instance of the type in the plugin assembly or to build a runtime-factory + /// that can create instances of the type when it is needed. + /// + public abstract class Employer { + + /// Determines whether the type suites the employer's requirements + /// Type which will be assessed + /// True if the type can be employed + public virtual bool CanEmploy(Type type) { + return Support.HasDefaultConstructor(type); + } + + /// Employs the specified plugin type + /// Type to be employed + public abstract void Employ(Type type); + + } + +} // namespace Nuclex.Support.Plugins diff --git a/Source/Plugins/FactoryEmployer.cs b/Source/Plugins/FactoryEmployer.cs new file mode 100644 index 0000000..858bffb --- /dev/null +++ b/Source/Plugins/FactoryEmployer.cs @@ -0,0 +1,116 @@ +#region CPL License +/* +Nuclex Framework +Copyright (C) 2002-2008 Nuclex Development Labs + +This library is free software; you can redistribute it and/or +modify it under the terms of the IBM Common Public License as +published by the IBM Corporation; either version 1.0 of the +License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +IBM Common Public License for more details. + +You should have received a copy of the IBM Common Public +License along with this library +*/ +#endregion + +using System; +using System.Collections.Generic; + +namespace Nuclex.Support.Plugins { + + /// Abstract factory + /// Interface or base class of the product of the factory + public interface IFactory { + + /// The concrete type as implemented by the factory instance + Type ConcreteType { get; } + + /// Creates a new instance of the type to which the factory is specialized + /// The newly created instance + T CreateInstance(); + + } + + /// Employer to create factories of suiting types found in plugins + /// Interface or base class that the types need to implement + /// + /// + /// This employer will not directly instanciate any compatible types found in + /// plugin assemblies, but generated runtime-factories of these types, enabling the + /// user to decide when and how many instances of a type will be created. + /// + /// + /// This approach has the advantage that it enables even assemblies that were not + /// intended to be plugins can be loaded as plugins, without risking an instanciation + /// or complex and possibly heavy-weight types. The disadvantage is that the + /// runtime-factory can not provide decent informationa about the plugin type like + /// a human-readable name, capabilities or an icon. + /// + /// + public class FactoryEmployer : Employer { + + #region class Factory + + /// Concrete factory for the types in a plugin assembly + private class Factory : IFactory { + + /// Initializes a factory and configures it for the specified product + /// Type of which the factory creates instances + public Factory(Type type) { + this.concreteType = type; + } + + /// The concrete type as produced by the factory + public Type ConcreteType { + get { return this.concreteType; } + } + + /// Create a new instance of the type that the factory is configured to + /// The newly created instance + public T CreateInstance() { + return (T)Activator.CreateInstance(this.concreteType); + } + + /// Concrete product which the factory instance creates + private Type concreteType; + + } + + #endregion // class Factory + + /// Initializes a new FactoryEmployer + public FactoryEmployer() { + this.employedFactories = new List>(); + } + + /// List of all factories that the instance employer has created + public List> Factories { + get { return this.employedFactories; } + } + + /// Determines whether the type suites the employer's requirements + /// Type which will be assessed + /// True if the type can be employed + public override bool CanEmploy(Type type) { + return + Support.HasDefaultConstructor(type) && + typeof(T).IsAssignableFrom(type); + } + + /// Employs the specified plugin type + /// Type to be employed + public override void Employ(Type type) { + this.employedFactories.Add(new Factory(type)); + } + + /// All factories that the instance employer has created + private List> employedFactories; + + } + +} // namespace Nuclex.Support.Plugins diff --git a/Source/Plugins/InstanceEmployer.cs b/Source/Plugins/InstanceEmployer.cs new file mode 100644 index 0000000..b9c2d51 --- /dev/null +++ b/Source/Plugins/InstanceEmployer.cs @@ -0,0 +1,79 @@ +#region CPL License +/* +Nuclex Framework +Copyright (C) 2002-2008 Nuclex Development Labs + +This library is free software; you can redistribute it and/or +modify it under the terms of the IBM Common Public License as +published by the IBM Corporation; either version 1.0 of the +License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +IBM Common Public License for more details. + +You should have received a copy of the IBM Common Public +License along with this library +*/ +#endregion + +using System; +using System.Collections.Generic; + +namespace Nuclex.Support.Plugins { + + /// Employer that directly creates instances of the types in a plugin + /// Interface or base class required for the employed types + /// + /// + /// This employer directly creates an instance of any type in a plugin assembly that + /// implements or is derived from the type the generic InstanceEmployer is instanced + /// to. This is useful when the plugin user already has a special plugin interface + /// through which additional informations about a plugin type can be queried or + /// when actually exactly one instance per plugin type is wanted (think of the + /// prototype pattern for example) + /// + /// + /// Because this employer blindly creates an instance of any compatible type found + /// in a plugin assembly it should be used with care. If big types with high + /// construction time or huge memory requirements are loaded this can become + /// a real resource hog. The intention of this employer was to let the plugin user + /// define his own factory interface which possibly provides further details about + /// the type the factory is reponsible for (like a description field). This + /// factory would then be implemented on the plugin side. + /// + /// + public class InstanceEmployer : Employer { + + /// Initializes a new instance employer + public InstanceEmployer() { + this.employedInstances = new List(); + } + + /// All instances that have been employed + public List Instances { + get { return this.employedInstances; } + } + + /// Determines whether the type suites the employer's requirements + /// Type that is checked for employability + /// True if the type can be employed + public override bool CanEmploy(Type type) { + return + Support.HasDefaultConstructor(type) && + typeof(T).IsAssignableFrom(type); + } + + /// Employs the specified plugin type + /// Type to be employed + public override void Employ(Type type) { + this.employedInstances.Add((T)Activator.CreateInstance(type)); + } + + /// All instances employed by the instance employer + private List employedInstances; + + } + +} // namespace Nuclex.Support.Plugins diff --git a/Source/Plugins/PluginHelper.cs b/Source/Plugins/PluginHelper.cs new file mode 100644 index 0000000..0417dba --- /dev/null +++ b/Source/Plugins/PluginHelper.cs @@ -0,0 +1,43 @@ +#region CPL License +/* +Nuclex Framework +Copyright (C) 2002-2008 Nuclex Development Labs + +This library is free software; you can redistribute it and/or +modify it under the terms of the IBM Common Public License as +published by the IBM Corporation; either version 1.0 of the +License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +IBM Common Public License for more details. + +You should have received a copy of the IBM Common Public +License along with this library +*/ +#endregion + +using System; + +namespace Nuclex.Support.Plugins { + + /// Supporting functions for the assembly + internal static class Support { + + /// Determines whether the given type has a default constructor + /// Type which is to be checked + /// True if the type has a default constructor + public static bool HasDefaultConstructor(Type type) { + System.Reflection.ConstructorInfo[] constructors = type.GetConstructors(); + + foreach(System.Reflection.ConstructorInfo constructor in constructors) + if(constructor.IsPublic && (constructor.GetParameters().Length != 0)) + return true; + + return false; + } + + } + +} // namespace Nuclex.Support.Plugins diff --git a/Source/Plugins/PluginHost.cs b/Source/Plugins/PluginHost.cs new file mode 100644 index 0000000..d04f6d3 --- /dev/null +++ b/Source/Plugins/PluginHost.cs @@ -0,0 +1,110 @@ +#region CPL License +/* +Nuclex Framework +Copyright (C) 2002-2008 Nuclex Development Labs + +This library is free software; you can redistribute it and/or +modify it under the terms of the IBM Common Public License as +published by the IBM Corporation; either version 1.0 of the +License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +IBM Common Public License for more details. + +You should have received a copy of the IBM Common Public +License along with this library +*/ +#endregion + +using System; +using System.Reflection; + +namespace Nuclex.Support.Plugins { + + /// Integration host for plugins + /// + /// This class is created by the party that is interested in loading plugins, + /// herein referred to as the "plugin user". The plugin host will monitor a + /// repository and react to any assembly being loaded into that repository by + /// iterating over all types (as in classes and structures) found in the + /// assembly and using the employer to do whatever the plugin user intends + /// to do with the types found in that assembly + /// + public class PluginHost { + + /// Initializes a plugin host using a new repository + /// Employer used assess and employ the plugin types + public PluginHost(Employer employer) + : this(employer, new PluginRepository()) { } + + /// Initializes the plugin using an existing repository + /// Employer used assess and employ the plugin types + /// Repository in which plugins will be stored + public PluginHost(Employer employer, PluginRepository repository) { + this.employer = employer; + this.repository = repository; + + foreach(Assembly assembly in this.repository.LoadedPlugins) + employAssemblyTypes(assembly); + + this.repository.AssemblyLoaded += new AssemblyLoadEventHandler(assemblyLoadHandler); + } + + /// The repository containing all loaded plugins + public PluginRepository Repository { + get { return this.repository; } + } + + /// The employer that is used by this plugin integration host + public Employer Employer { + get { return this.employer; } + } + + /// Responds to a new plugin being loaded into the repository + /// Repository into which the assembly was loaded + /// Event arguments; contains the loaded assembly + private void assemblyLoadHandler(object sender, AssemblyLoadEventArgs arguments) { + employAssemblyTypes(arguments.LoadedAssembly); + } + + /// Employs all employable types in an assembly + /// Assembly whose types to assess and to employ + private void employAssemblyTypes(Assembly assembly) { + + // Iterate all types contained in the assembly + foreach(Type type in assembly.GetTypes()) { + + // We'll ignore abstract and non-public types + if(!type.IsPublic || type.IsAbstract) + continue; + + // Types that have been tagged with the [NoPlugin] attribute will be ignored + object[] attributes = type.GetCustomAttributes(true); + foreach(object attribute in attributes) + if(attribute is NoPluginAttribute) + continue; + + // Type seems to be acceptable, assess and possibly employ it + try { + if(this.employer.CanEmploy(type)) + this.employer.Employ(type); + } + catch(Exception exception) { + System.Console.WriteLine( + "Could not employ " + type.ToString() + ": " + exception.Message + ); + } + + } + } + + /// Employs and manages types in the loaded plugin assemblies + private Employer employer; + /// Repository containing all plugins loaded, shared with other hosts + private PluginRepository repository; + + } + +} // namespace Nuclex.Support.Plugins diff --git a/Source/Plugins/PluginRepository.cs b/Source/Plugins/PluginRepository.cs new file mode 100644 index 0000000..25bb2f3 --- /dev/null +++ b/Source/Plugins/PluginRepository.cs @@ -0,0 +1,119 @@ +#region CPL License +/* +Nuclex Framework +Copyright (C) 2002-2008 Nuclex Development Labs + +This library is free software; you can redistribute it and/or +modify it under the terms of the IBM Common Public License as +published by the IBM Corporation; either version 1.0 of the +License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +IBM Common Public License for more details. + +You should have received a copy of the IBM Common Public +License along with this library +*/ +#endregion + +using System; +using System.Collections.Generic; +using System.Reflection; +using System.IO; + +namespace Nuclex.Support.Plugins { + + /// Stores loaded plugins + /// + /// This class manages a set of assemblies that have been dynamically loaded + /// as plugins. It usually is shared by multiple PluginHosts that handle + /// different interfaces of one plugin type. + /// + public class PluginRepository { + + /// Triggered whenever a new assembly is loaded into this repository + public event AssemblyLoadEventHandler AssemblyLoaded; + + /// Initializes a new instance of the plugin repository + public PluginRepository() { + this.assemblies = new List(); + } + + /// Loads all plugins matching a wildcard specification + /// Path of one or more plugins via wildcard + /// + /// This function always assumes that a plugin is optional. This means that + /// even when you specify a unique file name and a matching file is not found, + /// no exception will be raised and the error is silently ignored. + /// + public void AddFiles(string wildcard) { + string directory = Path.GetDirectoryName(wildcard); + string search = Path.GetFileName(wildcard); + + // We'll scan the specified directory for all files matching the specified + // wildcard. If only a single file is specified, only that file will match + // the supposed wildcard and everything works as expected + string[] assemblyFiles = Directory.GetFiles(directory, search); + foreach(string assemblyFile in assemblyFiles) { + + // A lot of errors can occur when attempting to load an assembly... + try { + AddAssembly(Assembly.LoadFile(assemblyFile)); + } + // File not found - Most likely a missing dependency of the assembly we + // attempted to load since the assembly itself has been found by the GetFiles() method + catch(DllNotFoundException exception) { + System.Console.WriteLine( + "Assembly not found, missing dependencies? " + exception.Message + ); + } + // Unauthorized acccess - Either the assembly is not trusted because it contains + // code that imposes a security risk on the system or a user rights problem + catch(UnauthorizedAccessException exception) { + System.Console.WriteLine( + "Not authorized, user rights problem? " + exception.Message + ); + } + // Bad image format - This exception is often thrown when the assembly we + // attempted to load requires a different version of the .NET framework + catch(BadImageFormatException exception) { + System.Console.WriteLine( + "Not a .NET assembly or wrong runtime version. " + exception.Message + ); + } + // Unknown error - Our last resort is to show a default error message + catch(Exception exception) { + System.Console.WriteLine( + "Failed to load plugin. " + exception.Message + ); + } + + } + } + + /// Adds the specified assembly to the repository + /// + /// Also used internally, so any assembly that is to be put into the repository, + /// not matter how, wanders through this method + /// + public void AddAssembly(Assembly assembly) { + this.assemblies.Add(assembly); + + // Trigger event in case any subscribers have been registered + if(AssemblyLoaded != null) + AssemblyLoaded(this, new AssemblyLoadEventArgs(assembly)); + } + + /// List of all loaded plugin assemblies in the repository + public List LoadedAssemblies { + get { return this.assemblies; } + } + + /// Loaded plugin assemblies + private List assemblies; + + } + +} // namespace Nuclex.Support.Plugins diff --git a/Source/Scheduling/AbortedException.cs b/Source/Scheduling/AbortedException.cs index 5e730f9..6048d42 100644 --- a/Source/Scheduling/AbortedException.cs +++ b/Source/Scheduling/AbortedException.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Scheduling/IAbortable.cs b/Source/Scheduling/IAbortable.cs index b054de0..16d6255 100644 --- a/Source/Scheduling/IAbortable.cs +++ b/Source/Scheduling/IAbortable.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Scheduling/Operation.cs b/Source/Scheduling/Operation.cs index 33bbbe6..582fdb2 100644 --- a/Source/Scheduling/Operation.cs +++ b/Source/Scheduling/Operation.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Scheduling/QueueOperation.Test.cs b/Source/Scheduling/QueueOperation.Test.cs index 1ed8924..88bf4eb 100644 --- a/Source/Scheduling/QueueOperation.Test.cs +++ b/Source/Scheduling/QueueOperation.Test.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Scheduling/QueueOperation.cs b/Source/Scheduling/QueueOperation.cs index d237862..55c6ca8 100644 --- a/Source/Scheduling/QueueOperation.cs +++ b/Source/Scheduling/QueueOperation.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Scheduling/ThreadCallbackOperation.cs b/Source/Scheduling/ThreadCallbackOperation.cs index e6fdaa2..73f826c 100644 --- a/Source/Scheduling/ThreadCallbackOperation.cs +++ b/Source/Scheduling/ThreadCallbackOperation.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Scheduling/ThreadOperation.cs b/Source/Scheduling/ThreadOperation.cs index 4e137eb..d61d41b 100644 --- a/Source/Scheduling/ThreadOperation.cs +++ b/Source/Scheduling/ThreadOperation.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Serialization/BinarySerializer.Test.cs b/Source/Serialization/BinarySerializer.Test.cs index cabac5e..3698986 100644 --- a/Source/Serialization/BinarySerializer.Test.cs +++ b/Source/Serialization/BinarySerializer.Test.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Serialization/BinarySerializer.cs b/Source/Serialization/BinarySerializer.cs index bc3d518..0425e7d 100644 --- a/Source/Serialization/BinarySerializer.cs +++ b/Source/Serialization/BinarySerializer.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Serialization/IBinarySerializable.cs b/Source/Serialization/IBinarySerializable.cs index c665fb7..977ed05 100644 --- a/Source/Serialization/IBinarySerializable.cs +++ b/Source/Serialization/IBinarySerializable.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/SpatialPartitioning/BoundingRectangle.cs b/Source/SpatialPartitioning/BoundingRectangle.cs index 1c1167f..1a86c17 100644 --- a/Source/SpatialPartitioning/BoundingRectangle.cs +++ b/Source/SpatialPartitioning/BoundingRectangle.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/SpatialPartitioning/RTree2.cs b/Source/SpatialPartitioning/RTree2.cs index 76118bc..cf32fb6 100644 --- a/Source/SpatialPartitioning/RTree2.cs +++ b/Source/SpatialPartitioning/RTree2.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/SpatialPartitioning/RTreeLeaf2.cs b/Source/SpatialPartitioning/RTreeLeaf2.cs index 8c0a715..d7dd67d 100644 --- a/Source/SpatialPartitioning/RTreeLeaf2.cs +++ b/Source/SpatialPartitioning/RTreeLeaf2.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/SpatialPartitioning/RTreeNode2.cs b/Source/SpatialPartitioning/RTreeNode2.cs index 5df3d5a..c09cda6 100644 --- a/Source/SpatialPartitioning/RTreeNode2.cs +++ b/Source/SpatialPartitioning/RTreeNode2.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/SpatialPartitioning/SpatialIndex2.cs b/Source/SpatialPartitioning/SpatialIndex2.cs index 9596462..272c0ae 100644 --- a/Source/SpatialPartitioning/SpatialIndex2.cs +++ b/Source/SpatialPartitioning/SpatialIndex2.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Tracking/IStatusReporter.cs b/Source/Tracking/IStatusReporter.cs index 6c00741..6055051 100644 --- a/Source/Tracking/IStatusReporter.cs +++ b/Source/Tracking/IStatusReporter.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Tracking/IdleStateEventArgs.cs b/Source/Tracking/IdleStateEventArgs.cs index 7d180e4..df8ec05 100644 --- a/Source/Tracking/IdleStateEventArgs.cs +++ b/Source/Tracking/IdleStateEventArgs.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Tracking/Internal/ObservedWeightedProgression.cs b/Source/Tracking/Internal/ObservedWeightedProgression.cs index d59a92b..4079ac7 100644 --- a/Source/Tracking/Internal/ObservedWeightedProgression.cs +++ b/Source/Tracking/Internal/ObservedWeightedProgression.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Tracking/Internal/WeightedProgressionWrapperCollection.cs b/Source/Tracking/Internal/WeightedProgressionWrapperCollection.cs index 51bda13..84a1500 100644 --- a/Source/Tracking/Internal/WeightedProgressionWrapperCollection.cs +++ b/Source/Tracking/Internal/WeightedProgressionWrapperCollection.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Tracking/ProgressUpdateEventArgs.cs b/Source/Tracking/ProgressUpdateEventArgs.cs index a9ff914..828d48a 100644 --- a/Source/Tracking/ProgressUpdateEventArgs.cs +++ b/Source/Tracking/ProgressUpdateEventArgs.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Tracking/Progression.cs b/Source/Tracking/Progression.cs index 6814f91..6f03e44 100644 --- a/Source/Tracking/Progression.cs +++ b/Source/Tracking/Progression.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Tracking/ProgressionTracker.Test.cs b/Source/Tracking/ProgressionTracker.Test.cs index 413a64c..568e54e 100644 --- a/Source/Tracking/ProgressionTracker.Test.cs +++ b/Source/Tracking/ProgressionTracker.Test.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Tracking/ProgressionTracker.cs b/Source/Tracking/ProgressionTracker.cs index 6df07bf..399d14f 100644 --- a/Source/Tracking/ProgressionTracker.cs +++ b/Source/Tracking/ProgressionTracker.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Tracking/SetProgression.Test.cs b/Source/Tracking/SetProgression.Test.cs index 9d794f4..67e8cdb 100644 --- a/Source/Tracking/SetProgression.Test.cs +++ b/Source/Tracking/SetProgression.Test.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Tracking/SetProgression.cs b/Source/Tracking/SetProgression.cs index 099c867..4d53f4e 100644 --- a/Source/Tracking/SetProgression.cs +++ b/Source/Tracking/SetProgression.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Tracking/StatusReportEventArgs.cs b/Source/Tracking/StatusReportEventArgs.cs index d571f23..68a6ccb 100644 --- a/Source/Tracking/StatusReportEventArgs.cs +++ b/Source/Tracking/StatusReportEventArgs.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/Tracking/WeightedProgression.cs b/Source/Tracking/WeightedProgression.cs index b248663..8fe6c63 100644 --- a/Source/Tracking/WeightedProgression.cs +++ b/Source/Tracking/WeightedProgression.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as diff --git a/Source/WeakReference.cs b/Source/WeakReference.cs index 28c427e..3ddba72 100644 --- a/Source/WeakReference.cs +++ b/Source/WeakReference.cs @@ -1,7 +1,7 @@ #region CPL License /* Nuclex Framework -Copyright (C) 2002-2007 Nuclex Development Labs +Copyright (C) 2002-2008 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as