Exemple #1
0
        public static void Inject(EcsWorld world, IEcsSystem system)
        {
            var systemType = system.GetType();

            if (!Attribute.IsDefined(systemType, typeof(EcsInjectAttribute)))
            {
                return;
            }
            var worldType  = world.GetType();
            var filterType = typeof(EcsFilter);

            foreach (var f in systemType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                // EcsWorld
                if (f.FieldType.IsAssignableFrom(worldType) && !f.IsStatic)
                {
                    f.SetValue(system, world);
                }
                // EcsFilter
#if DEBUG
                if (f.FieldType == filterType)
                {
                    throw new Exception(
                              string.Format("Cant use EcsFilter type at \"{0}\" system for dependency injection, use generic version instead", system));
                }
#endif
                if (f.FieldType.IsSubclassOf(filterType) && !f.IsStatic)
                {
                    f.SetValue(system, world.GetFilter(f.FieldType));
                }
            }
        }
        public static void Inject(EcsWorld world, IEcsSystem system)
        {
            var worldType            = world.GetType();
            var systemType           = system.GetType();
            var ecsFilter            = typeof(EcsFilter);
            var ecsIndex             = typeof(int);
            var attrEcsWorld         = typeof(EcsWorldAttribute);
            var attrEcsFilterInclude = typeof(EcsFilterIncludeAttribute);
            var attrEcsFilterExclude = typeof(EcsFilterExcludeAttribute);
            var attrEcsIndex         = typeof(EcsIndexAttribute);

            foreach (var f in systemType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                // [EcsWorld]
                if (f.FieldType.IsAssignableFrom(worldType) && !f.IsStatic && Attribute.IsDefined(f, attrEcsWorld))
                {
                    f.SetValue(system, world);
                }

                // [EcsFilterInclude]
                if (f.FieldType == ecsFilter && !f.IsStatic)
                {
                    EcsComponentMask includeMask = null;
                    var standardFilterIncDefined = Attribute.IsDefined(f, attrEcsFilterInclude);
                    if (standardFilterIncDefined)
                    {
                        includeMask = new EcsComponentMask();
                        var components = ((EcsFilterIncludeAttribute)Attribute.GetCustomAttribute(f, attrEcsFilterInclude)).Components;
                        for (var i = 0; i < components.Length; i++)
                        {
                            includeMask.SetBit(world.GetComponentIndex(components[i]), true);
                        }
                    }
                    EcsComponentMask excludeMask = null;
                    var standardFilterExcDefined = Attribute.IsDefined(f, attrEcsFilterExclude);
                    if (standardFilterExcDefined)
                    {
                        excludeMask = new EcsComponentMask();
                        var components = ((EcsFilterExcludeAttribute)Attribute.GetCustomAttribute(f, attrEcsFilterExclude)).Components;
                        for (var i = 0; i < components.Length; i++)
                        {
                            excludeMask.SetBit(world.GetComponentIndex(components[i]), true);
                        }
                    }
#if DEBUG && !ECS_PERF_TEST
                    if (standardFilterIncDefined && includeMask.IsEmpty())
                    {
                        throw new Exception("Include filter cant be empty at system: " + systemType.Name);
                    }
                    if (standardFilterExcDefined && excludeMask.IsEmpty())
                    {
                        throw new Exception("Exclude filter cant be empty at system: " + systemType.Name);
                    }
                    if (!standardFilterIncDefined && standardFilterExcDefined)
                    {
                        throw new Exception("EcsFilterExclude can be applied only as pair to EcsFilterInclude at system: " + systemType.Name);
                    }
                    if (includeMask != null && excludeMask != null && includeMask.IsIntersects(excludeMask))
                    {
                        throw new Exception("Exclude and include filters are intersected at system: " + systemType.Name);
                    }
#endif
                    if (standardFilterIncDefined)
                    {
                        f.SetValue(system, world.GetFilter(includeMask, excludeMask ?? new EcsComponentMask()));
                    }
                }

                // [EcsIndex]
                if (f.FieldType == ecsIndex && !f.IsStatic && Attribute.IsDefined(f, attrEcsIndex))
                {
                    var component = ((EcsIndexAttribute)Attribute.GetCustomAttribute(f, attrEcsIndex)).Component;
                    f.SetValue(system, world.GetComponentIndex(component));
                }
            }
        }