/// <summary>
        /// Creates all possible type-based drawers and add them to the related collection.
        /// </summary>
        /// <param name="settings"></param>
        private static void CreateTargetTypeDrawers(IToolboxInspectorSettings settings)
        {
            var childrenTypesMap = new Dictionary <ToolboxTargetTypeDrawer, List <Type> >();

            targetTypeDrawers.Clear();
            for (var i = 0; i < settings.TargetTypeDrawersCount; i++)
            {
                var drawerType = settings.GetTargetTypeDrawerTypeAt(i);
                if (drawerType == null)
                {
                    if (validationEnabled)
                    {
                        ToolboxEditorLog.LogWarning("One of assigned drawer types in the " + nameof(ToolboxEditorSettings) + " is empty.");
                    }

                    continue;
                }

                var drawerInstance = Activator.CreateInstance(drawerType) as ToolboxTargetTypeDrawer;
                var targetBaseType = drawerInstance.GetTargetType();
                if (targetBaseType != null)
                {
                    if (targetTypeDrawers.ContainsKey(targetBaseType))
                    {
                        if (validationEnabled)
                        {
                            ToolboxEditorLog.LogError("Type:" + targetBaseType + " is associated to more than one ToolboxDrawer.");
                        }

                        continue;
                    }

                    targetTypeDrawers[targetBaseType] = drawerInstance;

                    if (drawerInstance.UseForChildren())
                    {
                        childrenTypesMap[drawerInstance] = targetBaseType.GetAllChildClasses();
                    }
                }
            }

            foreach (var typesMap in childrenTypesMap)
            {
                var typesDrawer = typesMap.Key;
                var targetTypes = typesMap.Value;

                for (var i = 0; i < targetTypes.Count; i++)
                {
                    var targetType = targetTypes[i];
                    if (targetTypeDrawers.ContainsKey(targetType))
                    {
                        continue;
                    }

                    targetTypeDrawers[targetType] = typesDrawer;
                }
            }
        }
示例#2
0
        internal static object GetTreePathReference(string treeField, object treeParent)
        {
            if (IsSerializableArrayElement(treeField, out var index))
            {
                if (treeParent is IList list)
                {
                    return(list[index]);
                }

                ToolboxEditorLog.LogError("Cannot parse array element properly.");
            }

            var fieldType = treeParent.GetType();
            var fieldInfo = fieldType.GetField(treeField,
                                               BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

            return(fieldInfo.GetValue(treeParent));
        }
        /// <summary>
        /// Creates all possible attribute-based drawers and add them to proper collections.
        /// </summary>
        /// <param name="settings"></param>
        private static void CreateAttributeDrawers(IToolboxInspectorSettings settings)
        {
            void AddAttributeDrawer <T>(Type drawerType, Type attributeType, Dictionary <Type, T> drawersCollection) where T : ToolboxAttributeDrawer
            {
                if (drawerType == null)
                {
                    return;
                }

                var drawerInstance = Activator.CreateInstance(drawerType) as T;

                if (drawersCollection.ContainsKey(attributeType))
                {
                    if (validationEnabled)
                    {
                        ToolboxEditorLog.LogError("Attribute:" + attributeType + " is associated to more than one ToolboxDrawer.");
                    }

                    return;
                }

                drawersCollection.Add(attributeType, drawerInstance);
            }

            Type GetDrawersGenericType(Type drawerType, Type drawerBaseType)
            {
                if (drawerType == null)
                {
                    if (validationEnabled)
                    {
                        ToolboxEditorLog.LogWarning("One of assigned drawer types in the " + nameof(ToolboxEditorSettings) + " is empty.");
                    }

                    return(null);
                }

                while (!drawerType.IsGenericType || drawerType.GetGenericTypeDefinition() != drawerBaseType)
                {
                    if (drawerType.BaseType == null)
                    {
                        return(null);
                    }

                    drawerType = drawerType.BaseType;
                }

                return(drawerType.IsGenericType ? drawerType.GetGenericArguments().FirstOrDefault() : null);
            }

            decoratorDrawers.Clear();
            for (var i = 0; i < settings.DecoratorDrawersCount; i++)
            {
                var drawerType = settings.GetDecoratorDrawerTypeAt(i);
                var targetType = GetDrawersGenericType(settings.GetDecoratorDrawerTypeAt(i), decoratorDrawerBase);
                AddAttributeDrawer(drawerType, targetType, decoratorDrawers);
            }

            conditionDrawers.Clear();
            for (var i = 0; i < settings.ConditionDrawersCount; i++)
            {
                var drawerType = settings.GetConditionDrawerTypeAt(i);
                var targetType = GetDrawersGenericType(settings.GetConditionDrawerTypeAt(i), conditionDrawerBase);
                AddAttributeDrawer(drawerType, targetType, conditionDrawers);
            }

            selfPropertyDrawers.Clear();
            for (var i = 0; i < settings.SelfPropertyDrawersCount; i++)
            {
                var drawerType = settings.GetSelfPropertyDrawerTypeAt(i);
                var targetType = GetDrawersGenericType(settings.GetSelfPropertyDrawerTypeAt(i), selfPropertyDrawerBase);
                AddAttributeDrawer(drawerType, targetType, selfPropertyDrawers);
            }

            listPropertyDrawers.Clear();
            for (var i = 0; i < settings.ListPropertyDrawersCount; i++)
            {
                var drawerType = settings.GetListPropertyDrawerTypeAt(i);
                var targetType = GetDrawersGenericType(settings.GetListPropertyDrawerTypeAt(i), listPropertyDrawerBase);
                AddAttributeDrawer(drawerType, targetType, listPropertyDrawers);
            }
        }