internal void Add(object obj)
        {
            var type         = obj.GetType();
            var objectFields =
                type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
            var length = objectFields.Length;

            var groupType = typeof(GroupCore);

#if ACTORS_EVENTS_MANUAL
            var groupEv = Attribute.GetCustomAttribute(type, typeof(EventsAttribute)) as EventsAttribute;
#endif

            for (var i = 0; i < length; i++)
            {
                var myFieldInfo = objectFields[i];
                if (!myFieldInfo.FieldType.IsSubclassOf(groupType))
                {
                    continue;
                }

#if ACTORS_EVENTS_MANUAL
                // in case we are looking at the group of the derived processor we want to check it's events
                // if (inner == null)
                // {
                groupEv = Attribute.GetCustomAttribute(myFieldInfo, typeof(EventsAttribute)) as EventsAttribute;
                //}
#endif

                var groupByAttribute      = Attribute.GetCustomAttribute(myFieldInfo, typeof(GroupByAttribute)) as GroupByAttribute;
                var groupExcludeAttribute =
                    Attribute.GetCustomAttribute(myFieldInfo, typeof(ExcludeByAttribute)) as ExcludeByAttribute;
                var bindAttribute = Attribute.GetCustomAttribute(myFieldInfo, typeof(BindAttribute)) as BindAttribute;

                var includeTagsFilter = groupByAttribute != null ? groupByAttribute.filter : new int[0];
                var excludeTagsFilter = new int[0];
                var excludeCompFilter = new int[0];

                if (groupExcludeAttribute != null)
                {
                    excludeTagsFilter = groupExcludeAttribute.filter;
                    excludeCompFilter = groupExcludeAttribute.filterType;
                }

                var composition = new Composition();
                composition.excludedTags = excludeTagsFilter;
                composition.includedTags = includeTagsFilter;
                composition.GenerateExclude(excludeCompFilter);

                var types = myFieldInfo.FieldType.GetGenericArguments();

                composition.included = new ComponentMask[types.Length];
                var nextIndex = 0;
                foreach (var nextType in types)
                {
                    var componentID = Storage.TypeNames[nextType.GetHashCode()];
                    composition.included[nextIndex].generation = Storage.Generations[componentID];
                    composition.included[nextIndex].mask       = Storage.Masks[componentID];
                    composition.included[nextIndex].id         = componentID;
                    composition.includeComponents[componentID] = true;
                    nextIndex++;
                }

                composition.hash = HashCode.OfEach(myFieldInfo.FieldType.GetGenericArguments())
                                   .AndEach(composition.includedTags).And(17).AndEach(composition.excludedTags).And(31)
                                   .AndEach(excludeCompFilter);

                var group = SetupGroup(myFieldInfo.FieldType, myFieldInfo.GetValue(obj), composition, layer);
                myFieldInfo.SetValue(obj, group);

                if (bindAttribute != null)
                {
                    bindings.Add(bindAttribute.id, group);
                }


#if ACTORS_EVENTS_MANUAL
                if (groupEv != null)
                {
                    group.SetSelf(groupEv.op, obj as Processor);
                }
#endif
            }
        }
Ejemplo n.º 2
0
        public static void Setup(object b)
        {
            var type         = b.GetType();
            var objectFields =
                type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
            int length = objectFields.Length;

            var groupType = typeof(GroupCore);
            var groupBindProcAttribute = Attribute.GetCustomAttribute(type, typeof(BindAttribute)) as BindAttribute;

            for (int i = 0; i < length; i++)
            {
                var myFieldInfo = objectFields[i];

                if (myFieldInfo.FieldType.IsSubclassOf(groupType))
                {
                    // check if the group located inside of the base processor
                    var inner = Attribute.GetCustomAttribute(myFieldInfo, typeof(InnerGroupAttribute)) as InnerGroupAttribute;

                    var groupAddedAttribute    = Attribute.GetCustomAttribute(myFieldInfo, typeof(AddedAttribute)) as AddedAttribute;
                    var groupRemovedAttribute  = Attribute.GetCustomAttribute(myFieldInfo, typeof(RemovedAttribute)) as RemovedAttribute;
                    var groupReleasedAttribute = Attribute.GetCustomAttribute(myFieldInfo, typeof(ReleasedAttribute)) as ReleasedAttribute;

                    // if group is located inside of the base processor use processor filtering
                    var bindAttribute = inner != null
            ? groupBindProcAttribute
            : Attribute.GetCustomAttribute(myFieldInfo, typeof(BindAttribute)) as BindAttribute;

                    var composition = new Composition(HashCode.OfEach(myFieldInfo.FieldType.GetGenericArguments()).And(31));

                    GroupCore group;
                    if (groupAddedAttribute != null)
                    {
                        if (!groups.Added.TryGetValue(groupType, composition, out group))
                        {
                            group = groups.Added.Add(CreateGroup(groups.Added, myFieldInfo.FieldType, composition));
                        }
                    }
                    else if (groupRemovedAttribute != null)
                    {
                        if (!groups.Removed.TryGetValue(groupType, composition, out group))
                        {
                            group = groups.Removed.Add(CreateGroup(groups.Removed, myFieldInfo.FieldType, composition));
                        }
                    }
                    else if (groupReleasedAttribute != null)
                    {
                        if (!groups.Released.TryGetValue(groupType, composition, out group))
                        {
                            group = groups.Released.Add(CreateGroup(groups.Released, myFieldInfo.FieldType, composition));
                        }
                    }

                    else
                    {
                        group = SetupGroup(myFieldInfo.FieldType, composition, myFieldInfo.GetValue(b));

                        if (bindAttribute != null)
                        {
                            if (bindAttribute.id >= groups.globals.Length)
                            {
                                Array.Resize(ref groups.globals, bindAttribute.id + 5);
                            }

                            groups.globals[bindAttribute.id] = group;
                        }
                    }

                    myFieldInfo.SetValue(b, group);
                }
            }
        }
Ejemplo n.º 3
0
        public static void Setup(object b)
        {
            var type         = b.GetType();
            var objectFields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
            int length       = objectFields.Length;

            var groupType = typeof(GroupCore);

                        #if ACTORS_EVENTS_MANUAL
            var groupEv = Attribute.GetCustomAttribute(type, typeof(EventsAttribute)) as EventsAttribute;
                        #endif
            var groupByProcAttribute      = Attribute.GetCustomAttribute(type, typeof(GroupByAttribute)) as GroupByAttribute;
            var groupExcludeProcAttribute = Attribute.GetCustomAttribute(type, typeof(ExcludeAttribute)) as ExcludeAttribute;
            var groupBindProcAttribute    = Attribute.GetCustomAttribute(type, typeof(BindAttribute)) as BindAttribute;


            for (int i = 0; i < length; i++)
            {
                var myFieldInfo = objectFields[i];

                if (myFieldInfo.FieldType.IsSubclassOf(groupType))
                {
                    // check if the group located inside of the base processor
                    var inner = Attribute.GetCustomAttribute(myFieldInfo, typeof(InnerGroupAttribute)) as InnerGroupAttribute;

                                        #if ACTORS_EVENTS_MANUAL
                    // in case we are looking at the group of the derived processor we want to check it's events
                    if (inner == null)
                    {
                        groupEv = Attribute.GetCustomAttribute(myFieldInfo, typeof(EventsAttribute)) as EventsAttribute;
                    }
                                        #endif
                    // if group is located inside of the base processor use processor filtering
                    var groupByAttribute      = inner != null ? groupByProcAttribute : Attribute.GetCustomAttribute(myFieldInfo, typeof(GroupByAttribute)) as GroupByAttribute;
                    var groupExcludeAttribute = inner != null ? groupExcludeProcAttribute : Attribute.GetCustomAttribute(myFieldInfo, typeof(ExcludeAttribute)) as ExcludeAttribute;
                    var bindAttribute         = inner != null ? groupBindProcAttribute : Attribute.GetCustomAttribute(myFieldInfo, typeof(BindAttribute)) as BindAttribute;


                    var includeTagsFilter = groupByAttribute != null ? groupByAttribute.filter : new int[0];
                    var excludeTagsFilter = new int[0];
                    var excludeCompFilter = new int[0];

                    if (groupExcludeAttribute != null)
                    {
                        excludeTagsFilter = groupExcludeAttribute.filter;
                        excludeCompFilter = groupExcludeAttribute.filterType;
                    }

                    var composition = new Composition();
                    composition.excludeTags = excludeTagsFilter;
                    composition.includeTags = includeTagsFilter;
                    composition.AddTypesExclude(excludeCompFilter);


                    composition.hash = HashCode.OfEach(myFieldInfo.FieldType.GetGenericArguments()).AndEach(composition.includeTags).And(17).AndEach(composition.excludeTags).And(31).AndEach(excludeCompFilter);

                    var group = SetupGroup(myFieldInfo.FieldType, composition, myFieldInfo.GetValue(b));
                    myFieldInfo.SetValue(b, group);

                    if (bindAttribute != null)
                    {
                        if (bindAttribute.id >= groups.globals.Length)
                        {
                            Array.Resize(ref groups.globals, bindAttribute.id + 5);
                        }

                        groups.globals[bindAttribute.id] = group;
                    }

                                        #if ACTORS_EVENTS_MANUAL
                    if (groupEv != null)
                    {
                        group.SetSelf(groupEv.op, b as Processor);
                    }
                                        #endif
                }
            }
        }