Exemple #1
0
        public void RegisterComponent(IGameComponent component)
        {
            List <IGameSystem> systemsToRegisterTo;

            if (!_systemToComponentMapper.TryGetValue(component.GetType(), out systemsToRegisterTo))
            {
                return;
            }

            foreach (var system in systemsToRegisterTo)
            {
                system.RegisterComponent(component);
            }
        }
Exemple #2
0
        public override void OnDiffComponent(IGameEntity leftEntity, IGameComponent leftComponent, IGameEntity rightEntity, IGameComponent rightComponent)
        {
            bool diff;

            var comp = leftComponent as IComparableComponent;

            // ReSharper disable once PossibleNullReferenceException
            diff = !comp.IsApproximatelyEqual(rightComponent);

            if (diff)
            {
                _logger.InfoFormat("cmd seq {0} component diff key[{1}], type[{2}],\n local {3}],\n remote[{4}]",
                                   remoteCmdSeq, leftEntity.EntityKey, leftComponent.GetType(), leftComponent, rightComponent);
                IsDiff = true;
            }
        }
        public static void InjectDependenciesIntoIGameComponent(Core gameCore, IGameComponent gameComponent)
        {
            var gameComponentType = gameComponent.GetType();
            var logger            = LogManager.GetLogger(gameComponentType.FullName, gameComponentType);

            foreach (var propertyInfo in gameComponentType.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
            {
#pragma warning disable CS0618 // Type or member is obsolete
                var gameComponentDependencyAttribute = propertyInfo.GetCustomAttribute <GameComponentDependencyAttribute>();
                var gameServiceDependencyAttribute   = propertyInfo.GetCustomAttribute <GameServiceDependencyAttribute>();
#pragma warning restore CS0618 // Type or member is obsolete
                var injectAttribute = propertyInfo.GetCustomAttribute <InjectAttribute>();
                var setMethod       = propertyInfo.GetSetMethod(true);
                var propertyType    = propertyInfo.PropertyType;

                if (injectAttribute != null && setMethod != null)
                {
                    if (propertyType.GetInterfaces().Contains(typeof(IGameComponent)))
                    {
                        setMethod.Invoke(gameComponent, new object[] { gameCore.GetComponent(propertyType) });
                        logger.Trace("Injected {0} => {1}.", propertyType.FullName, propertyInfo.Name);
                    }
                    else if (propertyType.GetInterfaces().Contains(typeof(IGameService)))
                    {
                        setMethod.Invoke(gameComponent, new object[] { gameCore.GetService(propertyType) });
                        logger.Trace("Injected {0} => {1}.", propertyType.FullName, propertyInfo.Name);
                    }
                    else
                    {
                        logger.Error("Failed to injected {0} into {1}.", propertyType.FullName, propertyInfo.Name);
                    }
                }
                else if (gameComponentDependencyAttribute != null && setMethod != null)
                {
                    setMethod.Invoke(gameComponent, new object[] { gameCore.GetComponent(propertyType) });
                    logger.Trace("Injected {0} => {1}.", propertyType.FullName, propertyInfo.Name);
                }
                else if (gameServiceDependencyAttribute != null && setMethod != null)
                {
                    setMethod.Invoke(gameComponent, new object[] { gameCore.GetService(propertyType) });
                    logger.Trace("Injected {0} => {1}.", propertyType.FullName, propertyInfo.Name);
                }
            }
        }
        public static void AddComponent(IGameComponent component, bool addServices)
        {
            IGameComponent repl;

            if (ServiceHelperHooks.ReplacementComponents.TryGetValue(component.GetType().FullName, out repl))
            {
                if (repl is IServiceWrapper)
                {
                    ((IServiceWrapper)repl).Wrap(component);
                }
                else
                {
                    (component as IDisposable)?.Dispose();
                }
                component = repl;
            }

            orig_AddComponent(component, addServices);
        }
Exemple #5
0
 public static void AddComponent(IGameComponent component, bool addServices)
 {
     lock (ServiceHelper.Mutex)
     {
         if (!addServices)
         {
             ServiceHelper.InjectServices((object)component);
         }
         ServiceHelper.Game.Components.Add(component);
         if (addServices)
         {
             ServiceHelper.AddService((object)component);
         }
         if (!TraceFlags.TraceContentLoad)
         {
             return;
         }
         Logger.Log("ServiceHelper", LogSeverity.Information, component.GetType().Name + " loaded");
     }
 }
        public static bool ReloadShader(this IGameComponent component, Func <ShaderProgram> GetNew, Action <ShaderProgram> SetNew, Logger log = null)
        {
            try
            {
                ShaderProgram p = GetNew();

                if (p == null)
                {
                    throw new InvalidOperationException("ReloadShader() returned null, but didn't throw an exception");
                }

                SetNew(p);
                return(true);
            }
            catch (Exception ex)
            {
                if (log != null)
                {
                    log.Warn("Could not reload shader program {0}: {1}", component.GetType().Name, ex.GetType().Name + ": " + ex.Message);
                }
            }
            return(false);
        }
Exemple #7
0
        /// <summary>
        /// Registers a component with this ComponentManager.
        /// </summary>
        /// <param name="component">The component to register.</param>
        internal void RegisterComponent(IGameComponent component)
        {
            Type listType;

            this.components.Add(component);

            // Get all additional interfaces to link the component into all corresponding type specific lists.
            foreach (Type componentInterface in component.GetType().GetInterfaces())
            {
                listType = typeof(List <>).MakeGenericType(componentInterface);

                // Do not use a type specific list for the IGameComponent interface since all components are forced to implement it.
                if ((!this.InterfacesToIgnore.Contains(componentInterface)) && (!componentInterface.FullName.StartsWith("System.")))
                {
                    if (!this.componentsByType.TryGetValue(componentInterface, out IList typeSpecificComponents))
                    {
                        // Type specific list for this interface does not yet exist.
                        // Create list and add to dictionary.
                        typeSpecificComponents = (IList)Activator.CreateInstance(listType);
                        this.componentsByType.Add(componentInterface, typeSpecificComponents);
                    }
                    // At this point the type specific list definitely exists. Just add the component.
                    _ = typeSpecificComponents.Add(component);

                    // Is the component specific list sortable?
                    if (typeof(IComparable <>).MakeGenericType(componentInterface).IsAssignableFrom(componentInterface))
                    {
                        // Somehow the current interface implements inherits from the generic ICompareable<T> interface with T being the current interface itself.
                        // Since this pattern ensures that the type specific list is sortable, sort it.
                        // Sort the component specific list by getting the correspondig Sort() method and invoking it.
                        MethodInfo mi = listType.GetMethod("Sort", new Type[] { });
                        _ = mi.Invoke(typeSpecificComponents, null);
                    }
                }
            }
        }
 public void OnRightComponentMissing(IGameEntity leftEntity, IGameEntity rightEntity, IGameComponent leftComponent)
 {
     _logger.InfoFormat("cmd seq {0} remote component missing {1} {2}", _remoteCmdSeq, leftEntity.EntityKey, leftComponent.GetType());
     IsDiff = true;
 }
Exemple #9
0
 public void OnRightComponentMissing(IGameEntity leftEntity, IGameEntity rightEntity, IGameComponent leftComponent)
 {
     _logger.DebugFormat("remove component {0}:{1}", leftEntity.EntityKey, leftComponent.GetType());
     leftEntity.RemoveComponent(leftComponent.GetComponentId());
 }
Exemple #10
0
 public void OnLeftComponentMissing(IGameEntity leftEntity, IGameEntity rightEntity, IGameComponent rightComponent)
 {
     _logger.DebugFormat("add component {0}:{1}", leftEntity.EntityKey, rightComponent.GetType());
     var leftComponent = (ILatestComponent)leftEntity.AddComponent(rightComponent.GetComponentId(), rightComponent);
 }
Exemple #11
0
        public override void OnDiffComponent(IGameEntity leftEntity, IGameComponent leftComponent,
                                             IGameEntity rightEntity, IGameComponent rightComponent)
        {
            _logger.DebugFormat("rewind component field {0}:{1}", leftEntity.EntityKey, rightComponent.GetType());
            var left = leftComponent as IRewindableComponent;

            left.RewindTo(rightComponent);
        }
        public static void RegisterCommandsForIGameComponent(Core gameCore, IGameComponent gameComponent)
        {
            var delegateTypeFactory = DelegateTypeFactory.Instance;
            var gameComponentType   = gameComponent.GetType();
            var logger = LogManager.GetLogger(gameComponentType.FullName, gameComponentType);

            foreach (var methodInfo in gameComponentType.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
            {
#pragma warning disable CS0618 // Type or member is obsolete
                var registeredCommandAttribute = methodInfo.GetCustomAttribute <RegisteredCommandAttribute>();
#pragma warning restore CS0618 // Type or member is obsolete
                var commandAttribute = methodInfo.GetCustomAttribute <CommandAttribute>();

                if (commandAttribute != null)
                {
                    var commandDescriptionInfo = new CommandDescriptionInfo()
                    {
                        Name        = commandAttribute.Name,
                        Description = methodInfo.GetCustomAttribute <DescriptionAttribute>()?.Description,
                        ReturnType  = methodInfo.ReturnType?.Name
                    };
                    foreach (var paramInfo in methodInfo.GetParameters())
                    {
                        commandDescriptionInfo.Parameters.Add(new CommandParamDescriptionInfo
                        {
                            Name        = paramInfo.Name,
                            ValueType   = paramInfo.ParameterType.Name,
                            Description = paramInfo.GetCustomAttribute <DescriptionAttribute>()?.Description
                        });
                    }

                    var delegateType    = delegateTypeFactory.CreateDelegateType(methodInfo);
                    var commandDelegate = Delegate.CreateDelegate(delegateType, gameComponent, methodInfo.Name);

                    gameCore.RegisterCommandHandler(commandAttribute.Name, commandDelegate, commandDescriptionInfo);
                    logger.Trace("Registered command {0} => {1}({2})", commandAttribute.Name, methodInfo.Name, string.Join(", ", commandDescriptionInfo.Parameters.Select(e => e.ValueType)));
                }
                else if (registeredCommandAttribute != null)
                {
                    var commandDescriptionInfo = new CommandDescriptionInfo()
                    {
                        Name        = registeredCommandAttribute.Name,
                        Description = methodInfo.GetCustomAttribute <DescriptionAttribute>()?.Description,
                        ReturnType  = methodInfo.ReturnType?.Name
                    };
                    foreach (var paramInfo in methodInfo.GetParameters())
                    {
                        commandDescriptionInfo.Parameters.Add(new CommandParamDescriptionInfo
                        {
                            Name        = paramInfo.Name,
                            ValueType   = paramInfo.ParameterType.Name,
                            Description = paramInfo.GetCustomAttribute <DescriptionAttribute>()?.Description
                        });
                    }

                    var delegateType    = delegateTypeFactory.CreateDelegateType(methodInfo);
                    var commandDelegate = Delegate.CreateDelegate(delegateType, gameComponent, methodInfo.Name);

                    gameCore.RegisterCommandHandler(registeredCommandAttribute.Name, commandDelegate, commandDescriptionInfo);
                    logger.Trace("Registered command {0} => {1}({2})", registeredCommandAttribute.Name, methodInfo.Name, string.Join(", ", commandDescriptionInfo.Parameters.Select(e => e.ValueType)));
                }
            }
        }
Exemple #13
0
 public override void OnLeftComponentMissing(IGameEntity leftEntity, IGameEntity rightEntity, IGameComponent rightComponent)
 {
     _logger.InfoFormat("cmd seq {0} local component missing {1} {2}", remoteCmdSeq, leftEntity.EntityKey, rightComponent.GetType());
     IsDiff = true;
 }
 public override void OnLeftComponentMissing(IGameEntity leftEntity, IGameEntity rightEntity, IGameComponent rightComponent)
 {
     _logger.DebugFormat("add component {0}:{1}", leftEntity.EntityKey, rightComponent.GetType());
     leftEntity.AddComponent(rightComponent.GetComponentId());
 }