示例#1
0
        /// <summary>
        /// Registers a component.
        /// </summary>
        /// <exception cref="InvalidOperationException">Is thrown when the component was incompatible with another component.</exception>
        public static EngineHandle RegisterComponent <T>(T component) where T : class, IEngineComponent
        {
            var conflictingComponents = Components.Where(comp => !component.IsCompatibleTo(comp) || !comp.IsCompatibleTo(component));

            if (conflictingComponents.Any())
            {
                PostGlobalLogMessage(string.Format("Error registering engine component {0}: incompatibility to existing component(s) {1} detected.",
                                                   component.GetType().FullName, string.Join(", ", conflictingComponents.Select(c => c.GetType().FullName))), LogMessageKind.Error, LogMessagePriority.Engine);
                throw new InvalidOperationException("This component is not compatible to an already registered one.");
            }

            OnComponentAdded(component);
            LinkedListNode <IEngineComponent> node;

            lock (ComponentsLocker) node = Components.AddLast(component);
            var handle = new EngineHandle(Components, node, ComponentsLocker);

            handle.Disposed += (sender, e) => OnComponentRemoved(((EngineHandle)sender).Component);
            PostGlobalLogMessage(string.Format("Engine component {0} successfully registered.", component.GetType().FullName),
                                 LogMessageKind.Information, LogMessagePriority.Engine);
            return(handle);
        }