Exemple #1
0
        protected internal override void InternalSystemAwake()
        {
            List <EudiMap.TupleItem> tempItems         = new List <EudiMap.TupleItem>();
            List <FieldInfo>         tempCorrectFields = new List <FieldInfo>();

            var type        = GetType();
            var fields      = type.GetFields();
            var fieldsCount = fields.Length;

            for (int i = 0; i < fieldsCount; i++)
            {
                var field             = fields[i];
                var hasTupleAttribute = Attribute.IsDefined(field, typeof(InjectTuplesAttribute));
                if (hasTupleAttribute)
                {
                    var item = new EudiMap.TupleItem();
                    item.Attribute = (InjectTuplesAttribute)Attribute.GetCustomAttribute(field, typeof(InjectTuplesAttribute));
                    item.Type      = field.FieldType;
                    tempItems.Add(item);
                    tempCorrectFields.Add(field);
                }
            }

            _linkedMap    = Eudi.EntitiesManager.MapManager.GetMap(tempItems.ToArray());
            MatchEntities = Eudi.EntitiesManager.MapManager.GetEntities(_linkedMap);

            // Set fields value from the map
            foreach (var field in tempCorrectFields)
            {
                var isGeneric  = field.FieldType.IsGenericType;
                var wantedType = field.FieldType;

                var obj = _linkedMap.GetAssignedObject(wantedType);
                if (obj == null)
                {
                    Debug.LogError(new Exception($"Field {field.Name} (c:{field.DeclaringType?.Name}) was set to null!"));
                }
                field.SetValue(this, obj);
            }

            if (_linkedMap == null || tempItems.Count == 0)
            {
                Debug.LogWarning($"!!! System {GetType().FullName} got no `InjectTuple` attribute.");
                tempItems?.Clear();
                tempItems = null; //< remove reference
            }
        }
        public static bool IsValid(Type originalType, EudiMap.TupleItem item)
        {
            if (item.Type.IsGenericType)
            {
                if (item.Type.GetGenericTypeDefinition() == typeof(EudiMatchArray <>))
                {
                    var newType = originalType;
                    if (typeof(WrapperModuleEntity).IsAssignableFrom(newType))
                    {
                        Type baseType = newType.BaseType;
                        while (baseType != null)
                        {
                            if (baseType.IsGenericType &&
                                baseType.GetGenericTypeDefinition() == typeof(WrapperModuleEntity <>))
                            {
                                newType = baseType.GetGenericArguments()[0];
                                break;
                            }

                            baseType = baseType.BaseType;

                            if (baseType == typeof(System.Object))
                            {
                                return(false);
                            }
                        }
                    }

                    var dataArrayType = typeof(EudiMatchArray <>).MakeGenericType(newType);
                    return(dataArrayType == item.Type);
                }
            }
            else
            {
                if (item.Type == typeof(TransformAccessArray) &&
                    originalType == typeof(Transform))
                {
                    return(true);
                }
            }

            return(false);
        }