示例#1
0
        static void FillEntityView(EntityView entityView, object[] implementors, RemoveEntityImplementor removeEntity,
                                   string entityDescriptorName)
        {
            for (int index = 0; index < implementors.Length; index++)
            {
                var implementor = implementors[index];

                if (implementor != null)
                {
                    var type = implementor.GetType();

                    Type[] interfaces;
                    if (_cachedTypes.TryGetValue(type, out interfaces) == false)
                    {
                        interfaces = _cachedTypes[type] = type.GetInterfacesEx();
                    }

                    for (int iindex = 0; iindex < interfaces.Length; iindex++)
                    {
                        var componentType = interfaces[iindex];
#if DEBUG && !PROFILER
                        Tuple <object, int> implementorHolder;

                        if (implementorsByType.TryGetValue(componentType, out implementorHolder) == true)
                        {
                            implementorHolder.item2++;
                        }
                        else
                        {
                            implementorsByType[componentType] = new Tuple <object, int>(implementor, 1);
                        }
#else
                        implementorsByType[componentType] = implementor;
#endif
                    }
                }
#if DEBUG && !PROFILER
                else
                {
                    Utility.Console.LogError(NULL_IMPLEMENTOR_ERROR.FastConcat(entityView.ToString()));
                }
#endif
            }

            int count;

            //Very efficent way to collect the fields of every EntityViewType
            KeyValuePair <Type, CastedAction <EntityView> >[] setters =
                FasterList <KeyValuePair <Type, CastedAction <EntityView> > > .NoVirt.ToArrayFast(entityView.entityViewBlazingFastReflection, out count);

            var removeEntityComponentType = typeof(IRemoveEntityComponent);

            for (int i = 0; i < count; i++)
            {
                var  keyValuePair = setters[i];
                Type fieldType    = keyValuePair.Key;

                if (fieldType != removeEntityComponentType)
                {
#if DEBUG && !PROFILER
                    Tuple <object, int> component;
#else
                    object component;
#endif

                    if (implementorsByType.TryGetValue(fieldType, out component) == false)
                    {
                        Exception e = new Exception(NOT_FOUND_EXCEPTION + " Component Type: " + fieldType.Name + " - EntityView: " +
                                                    entityView.GetType().Name + " - EntityDescriptor " + entityDescriptorName);

                        throw e;
                    }
#if DEBUG && !PROFILER
                    if (component.item2 > 1)
                    {
                        Utility.Console.LogError(DUPLICATE_IMPLEMENTOR_ERROR.FastConcat(
                                                     "Component Type: ", fieldType.Name, " implementor: ",
                                                     component.item1.ToString()) + " - EntityView: " +
                                                 entityView.GetType().Name + " - EntityDescriptor " + entityDescriptorName);
                    }
#endif
#if DEBUG && !PROFILER
                    keyValuePair.Value.Call(entityView, component.item1);
#else
                    keyValuePair.Value.Call(entityView, component);
#endif
                }
                else
                {
                    keyValuePair.Value.Call(entityView, removeEntity);
                }
            }

            implementorsByType.Clear();
        }
    public static void FillEntityView <T>(this IEntityBuilder entityBuilder
                                          , ref T entityView
                                          , FasterList <KeyValuePair <Type, ActionCast <T> > > entityViewBlazingFastReflection
                                          , object[] implementors)
    {
        int count;

        //efficient way to collect the fields of every EntityViewType
        var setters =
            FasterList <KeyValuePair <Type, ActionCast <T> > >
            .NoVirt.ToArrayFast(entityViewBlazingFastReflection, out count);

        for (var index = 0; index < implementors.Length; index++)
        {
            var implementor = implementors[index];

            if (implementor != null)
            {
                var type = implementor.GetType();

                Type[] interfaces;
                if (_cachedTypes.TryGetValue(type, out interfaces) == false)
                {
                    interfaces = _cachedTypes[type] = type.GetInterfacesEx();
                }

                for (var iindex = 0; iindex < interfaces.Length; iindex++)
                {
                    var componentType = interfaces[iindex];
#if DEBUG && !PROFILER
                    Tuple <object, int> implementorData;

                    if (implementorsByType.TryGetValue(componentType, out implementorData))
                    {
                        implementorData.numberOfImplementations++;
                        implementorsByType[componentType] = implementorData;
                    }
                    else
                    {
                        implementorsByType[componentType] = new Tuple <object, int>(implementor, 1);
                    }
#else
                    implementorsByType[componentType] = implementor;
#endif
                }
            }
#if DEBUG && !PROFILER
            else
            {
                Svelto.Console.Log(NULL_IMPLEMENTOR_ERROR.FastConcat(" entityView ",
                                                                     entityBuilder.GetEntityType().ToString()));
            }
#endif
        }

        for (var i = 0; i < count; i++)
        {
            var fieldSetter = setters[i];
            var fieldType   = fieldSetter.Key;

#if DEBUG && !PROFILER
            Tuple <object, int> component;
#else
            object component;
#endif

            if (implementorsByType.TryGetValue(fieldType, out component) == false)
            {
                var e = new ECSException(NOT_FOUND_EXCEPTION + " Component Type: " + fieldType.Name +
                                         " - EntityView: " + entityBuilder.GetEntityType().Name);

                throw e;
            }
#if DEBUG && !PROFILER
            if (component.numberOfImplementations > 1)
            {
                throw new ECSException(DUPLICATE_IMPLEMENTOR_ERROR.FastConcat(
                                           "Component Type: ", fieldType.Name, " implementor: ",
                                           component.implementorType.ToString()) + " - EntityView: " +
                                       entityBuilder.GetEntityType().Name);
            }
#endif
#if DEBUG && !PROFILER
            fieldSetter.Value(ref entityView, component.implementorType);
#else
            fieldSetter.Value(ref entityView, component);
#endif
        }

        implementorsByType.Clear();
    }
示例#3
0
        static void FillEntityView(EntityView entityView
                                   , object[]   implementors
                                   , string entityDescriptorName)
        {
            int count;

            //Very efficent way to collect the fields of every EntityViewType
            var setters =
                FasterList <KeyValuePair <Type, CastedAction <EntityView> > >
                .NoVirt.ToArrayFast(entityView.entityViewBlazingFastReflection, out count);

            if (count == 0)
            {
                return;
            }

            for (var index = 0; index < implementors.Length; index++)
            {
                var implementor = implementors[index];

                if (implementor != null)
                {
                    var type = implementor.GetType();

                    Type[] interfaces;
                    if (_cachedTypes.TryGetValue(type, out interfaces) == false)
                    {
                        interfaces = _cachedTypes[type] = type.GetInterfacesEx();
                    }

                    for (var iindex = 0; iindex < interfaces.Length; iindex++)
                    {
                        var componentType = interfaces[iindex];
#if DEBUG && !PROFILER
                        Tuple <object, int> implementorHolder;

                        if (implementorsByType.TryGetValue(componentType, out implementorHolder))
                        {
                            implementorHolder.numberOfImplementations++;
                        }
                        else
                        {
                            implementorsByType[componentType] = new Tuple <object, int>(implementor, 1);
                        }
#else
                        implementorsByType[componentType] = implementor;
#endif
                    }
                }
#if DEBUG && !PROFILER
                else
                {
                    Console.LogError(NULL_IMPLEMENTOR_ERROR.FastConcat(entityView.ToString()));
                }
#endif
            }

            for (var i = 0; i < count; i++)
            {
                var fieldSetter = setters[i];
                var fieldType   = fieldSetter.Key;

#if DEBUG && !PROFILER
                Tuple <object, int> component;
#else
                object component;
#endif

                if (implementorsByType.TryGetValue(fieldType, out component) == false)
                {
                    var e = new Exception(NOT_FOUND_EXCEPTION + " Component Type: " + fieldType.Name +
                                          " - EntityView: " +
                                          entityView.GetType().Name + " - EntityDescriptor " + entityDescriptorName);

                    throw e;
                }
#if DEBUG && !PROFILER
                if (component.numberOfImplementations > 1)
                {
                    Console.LogError(DUPLICATE_IMPLEMENTOR_ERROR.FastConcat(
                                         "Component Type: ", fieldType.Name,
                                         " implementor: ",
                                         component.implementorType.ToString()) +
                                     " - EntityView: " +
                                     entityView.GetType().Name + " - EntityDescriptor " + entityDescriptorName);
                }
#endif
#if DEBUG && !PROFILER
                fieldSetter.Value.Call(entityView, component.implementorType);
#else
                fieldSetter.Value.Call(entityView, component);
#endif
            }

            implementorsByType.Clear();
        }