Exemple #1
0
        /// <summary>
        ///		Getter for the singleton instance for testing purposes.
        ///		This overload will be used for unit testing.
        /// </summary>
        /// <returns>The instance of the <see cref="IEventAggregator"/></returns>
        internal static IEventAggregator GetInstance(IResolveInstances instanceResolver, bool createNewInstance)
        {
            if (_instance == null || (_instance != null && createNewInstance))
            {
                // first call to the event aggregator, lets initialize a new instance.
                _instance = new EventAggregator(instanceResolver, true);
            }

            return(_instance);
        }
Exemple #2
0
        /// <summary>
        ///		Private constructor for the singleton.
        /// </summary>
        private EventAggregator(IResolveInstances instanceResolver, bool preventLogging = false)
        {
            _preventLogging  = preventLogging;
            _hookRegistry    = new PrioritizedEventListenerDictionary <Type, EventListenerRegistration>(new EventListenerRegistrationComparer());
            _commandRegistry = new Dictionary <string, List <ICommandListener> >();

            // Get all instances and try to gather all event listeners.
            _plugins = instanceResolver.ResolveInstances <IPlugin>();

            if (_plugins == null || _plugins.Count == 0)
            {
                LogUtility.Warning("[SK] No IPlugin instances found.");
                return;
            }

            foreach (var plugin in _plugins)
            {
                try
                {
                    var authors   = plugin.getAuthors();
                    var formatted = string.Join(", ", authors);
                    var lastIndex = formatted.LastIndexOf(", ");
                    if (lastIndex > 0)
                    {
                        formatted = formatted.Remove(lastIndex, 2);
                        formatted = formatted.Insert(lastIndex, " & ");
                    }
                    LogUtility.Out("[SK] Loading plugin " + plugin.getPluginName() + " created by " + formatted);

                    plugin.onLoad();
                    plugin.RegisterEventListeners(this);
                    plugin.RegisterCommandListeners(this);
                }
                catch (Exception exception)
                {
                    var wrappedException = new PluginInitializationException(plugin.GetType().AssemblyQualifiedName, exception.Message, exception);
                    LogUtility.Exception(wrappedException);
                }
            }

            LogUtility.Out("[SK] EventAggregator initialized");
        }