Beispiel #1
0
        private static bool AllowRegisterType(Type type)
        {
            var typeFlags = AssemblyUtilities.GetAssemblyTypeFlag(type.Assembly);

            if ((typeFlags & AssemblyTypeFlags.UnityEditorTypes) == AssemblyTypeFlags.UnityEditorTypes)
            {
                return(false);
            }

            if ((typeFlags & AssemblyTypeFlags.UserEditorTypes) == AssemblyTypeFlags.UserEditorTypes)
            {
                return(false);
            }

            if (type.IsGenericType)
            {
                foreach (var parameter in type.GetGenericArguments())
                {
                    if (!AllowRegisterType(parameter))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Beispiel #2
0
        private void DrawOdinInspector()
        {
            EnsureInitialized();

            if (this.Tree == null)
            {
                base.OnInspectorGUI();
                return;
            }
            else if (this.Tree.RootPropertyCount == 0)
            {
                var assemblyTypeFlag = AssemblyUtilities.GetAssemblyTypeFlag(this.target.GetType().Assembly);

                if (assemblyTypeFlag == AssemblyTypeFlags.UnityTypes || assemblyTypeFlag == AssemblyTypeFlags.UnityEditorTypes)
                {
                    this.MockUnityGenericInspector();
                    return;
                }
            }


#if PREFAB_DEBUG
            this.PrefabModificationsDebug();
#endif

            if (Event.current.type == EventType.Layout)
            {
                this.tree.DrawMonoScriptObjectField = !ForceHideMonoScriptInEditor &&
                                                      this.tree.UnitySerializedObject != null &&
                                                      this.tree.TargetType != null &&
                                                      GeneralDrawerConfig.Instance.ShowMonoScriptInEditor &&
                                                      !this.tree.TargetType.IsDefined(typeof(HideMonoScriptAttribute), true);
            }

            this.DrawTree();

            //NetworkBehaviour networkBehaviour = this.target as NetworkBehaviour;

            //if (networkBehaviour != null && !networkBehaviour.GetType().IsDefined(typeof(HideNetworkBehaviourFieldsAttribute), true))
            //{
            //    EditorGUILayout.LabelField(networkChannelLabel, GUIHelper.TempContent(networkBehaviour.GetNetworkChannel().ToString()));
            //    EditorGUILayout.LabelField(networkSendIntervalLabel, GUIHelper.TempContent(networkBehaviour.GetNetworkSendInterval().ToString()));
            //}

            if (UnityNetworkingUtility.NetworkBehaviourType != null && UnityNetworkingUtility.NetworkBehaviourType.IsAssignableFrom(this.target.GetType()))
            {
                if (this.target.GetType().IsDefined <HideNetworkBehaviourFieldsAttribute>(true) == false)
                {
                    EditorGUILayout.LabelField(networkChannelLabel, GUIHelper.TempContent(UnityNetworkingUtility.GetNetworkChannel(this.target as MonoBehaviour).ToString()));
                    EditorGUILayout.LabelField(networkSendIntervalLabel, GUIHelper.TempContent(UnityNetworkingUtility.GetNetworkingInterval(this.target as MonoBehaviour).ToString()));
                }
            }

            this.RepaintWarmup();
            this.RepaintIfRequested();
            GUIHelper.CurrentWindow.wantsMouseMove = true;
        }
Beispiel #3
0
        private void DrawOdinInspector()
        {
            EnsureInitialized();

            if (this.Tree == null)
            {
                base.OnInspectorGUI();
                return;
            }
            else if (this.Tree.RootPropertyCount == 0)
            {
                var assemblyTypeFlag = AssemblyUtilities.GetAssemblyTypeFlag(this.target.GetType().Assembly);

                if (assemblyTypeFlag == AssemblyTypeFlags.UnityTypes || assemblyTypeFlag == AssemblyTypeFlags.UnityEditorTypes)
                {
                    this.MockUnityGenericInspector();
                    return;
                }
            }

#if PREFAB_DEBUG
            this.PrefabModificationsDebug();
#endif

            if (Event.current.type == EventType.Layout)
            {
                this.tree.DrawMonoScriptObjectField = !ForceHideMonoScriptInEditor &&
                                                      this.tree.UnitySerializedObject != null &&
                                                      this.tree.TargetType != null &&
                                                      GeneralDrawerConfig.Instance.ShowMonoScriptInEditor &&
                                                      !this.tree.TargetType.IsDefined(typeof(HideMonoScriptAttribute), true);
            }

            this.DrawTree();

            NetworkBehaviour networkBehaviour = this.target as NetworkBehaviour;

            if (networkBehaviour != null)
            {
                EditorGUILayout.LabelField(networkChannelLabel, GUIHelper.TempContent(networkBehaviour.GetNetworkChannel().ToString()));
                EditorGUILayout.LabelField(networkSendIntervalLabel, GUIHelper.TempContent(networkBehaviour.GetNetworkSendInterval().ToString()));
            }

            this.RepaintIfRequested();
            GUIHelper.CurrentWindow.wantsMouseMove = true;

            if (GUIHelper.CurrentWindow == EditorWindow.focusedWindow)
            {
                //if ((Event.current.mousePosition - this.prevMousePosition).sqrMagnitude > 2)
                //{
                //    this.Repaint();
                //    this.prevMousePosition = Event.current.mousePosition;
                //}
            }
        }
Beispiel #4
0
            //private bool showNotSerializableLabel;
            //private static GUIContent isNotSerializableLabel = new GUIContent("Not serializable ");

            public void PrependType(Type type)
            {
                if (this.ChildNodes == null)
                {
                    this.ChildNodes = new List <TypeTreeNode>();
                }
                var specialType = type == typeof(NullType) ? InstanceCreator.Type : type;
                var flag        = AssemblyUtilities.GetAssemblyTypeFlag(specialType.Assembly);
                var node        = this.ChildNodes.FirstOrDefault(x => x.AssemblyTypeFlag == flag);

                if (node == null)
                {
                    node = new TypeTreeNode()
                    {
                        AssemblyTypeFlag = flag
                    };
                    this.ChildNodes.Insert(0, node);
                }

                if (node.ChildNodes == null)
                {
                    node.ChildNodes = new List <TypeTreeNode>();
                }

                if (string.IsNullOrEmpty(specialType.Namespace))
                {
                    node.ChildNodes.Insert(0, new TypeTreeNode()
                    {
                        Type = type
                    });
                }
                else
                {
                    var nsNode = node.ChildNodes.FirstOrDefault(x => x.Namespace == specialType.Namespace);
                    if (nsNode == null)
                    {
                        nsNode = new TypeTreeNode()
                        {
                            Namespace = specialType.Namespace
                        };
                        node.ChildNodes.Insert(0, nsNode);
                    }

                    if (nsNode.ChildNodes == null)
                    {
                        nsNode.ChildNodes = new List <TypeTreeNode>();
                    }

                    nsNode.ChildNodes.Insert(0, new TypeTreeNode()
                    {
                        Type = type
                    });
                }
            }
Beispiel #5
0
        public static IEnumerable <Assembly> GetResolverAssemblies()
        {
            return(AppDomain.CurrentDomain.GetAssemblies().Where(a =>
            {
                if (a.IsDefined(typeof(ContainsOdinResolversAttribute), true))
                {
                    return true;
                }

                var flag = AssemblyUtilities.GetAssemblyTypeFlag(a);

                return (flag & AssemblyTypeFlags.CustomTypes) != 0;
            }));
        }
Beispiel #6
0
        private static void AddTypeToGroups(Type type, Type preExistingEditorType)
        {
            var assemblyType = AssemblyUtilities.GetAssemblyTypeFlag(type.Assembly);

            TypeGroup group;

            switch (assemblyType)
            {
            case AssemblyTypeFlags.UserTypes:
            case AssemblyTypeFlags.UserEditorTypes:
                group = UserTypesRootGroup;
                break;

            case AssemblyTypeFlags.PluginTypes:
            case AssemblyTypeFlags.PluginEditorTypes:
                group = PluginTypesRootGroup;
                break;

            case AssemblyTypeFlags.UnityTypes:
            case AssemblyTypeFlags.UnityEditorTypes:
                group = UnityTypesRootGroup;
                break;

            case AssemblyTypeFlags.OtherTypes:
            // If we hit one of the below flags, or the default case, something actually went wrong.
            // We don't care, though - just shove it into the other types category.
            case AssemblyTypeFlags.All:
            case AssemblyTypeFlags.GameTypes:
            case AssemblyTypeFlags.EditorTypes:
            case AssemblyTypeFlags.CustomTypes:
            case AssemblyTypeFlags.None:
            default:
                group = OtherTypesRootGroup;
                break;
            }

            if (type.Namespace != null)
            {
                string[] groups = type.Namespace.Split('.');

                for (int i = 0; i < groups.Length; i++)
                {
                    group = group.GetChildGroup(groups[i]);
                }
            }

            group.SubTypes.Add(new TypeGroup.TypePair(type, preExistingEditorType));
        }
Beispiel #7
0
      private void OnSerializedType(Type type)
      {
          var typeFlags = AssemblyUtilities.GetAssemblyTypeFlag(type.Assembly);

          if ((typeFlags & AssemblyTypeFlags.UnityEditorTypes) == AssemblyTypeFlags.UnityEditorTypes)
          {
              return;
          }

          if ((typeFlags & AssemblyTypeFlags.UserEditorTypes) == AssemblyTypeFlags.UserEditorTypes)
          {
              return;
          }

          this.RegisterType(type);
      }
Beispiel #8
0
      private void OnLocatedEmitType(Type type, ISerializationPolicy policy)
      {
          var typeFlags = AssemblyUtilities.GetAssemblyTypeFlag(type.Assembly);

          if ((typeFlags & AssemblyTypeFlags.UnityEditorTypes) == AssemblyTypeFlags.UnityEditorTypes)
          {
              return;
          }

          if ((typeFlags & AssemblyTypeFlags.UserEditorTypes) == AssemblyTypeFlags.UserEditorTypes)
          {
              return;
          }

          this.RegisterType(type, policy);
      }
        private static bool ContinueAddingAttributesFor(Type type)
        {
            if (type == null)
            {
                return(false);
            }
            var flag = AssemblyUtilities.GetAssemblyTypeFlag(type.Assembly);

            if (flag == AssemblyTypeFlags.OtherTypes)
            {
                return(false);
            }
            if (flag == AssemblyTypeFlags.UnityTypes)
            {
                return(false);
            }
            return(true);
        }
Beispiel #10
0
      private void OnLocatedFormatter(IFormatter formatter, ISerializationPolicy policy)
      {
          var typeFlags = AssemblyUtilities.GetAssemblyTypeFlag(formatter.SerializedType.Assembly);

          if ((typeFlags & AssemblyTypeFlags.UnityEditorTypes) == AssemblyTypeFlags.UnityEditorTypes)
          {
              return;
          }

          if ((typeFlags & AssemblyTypeFlags.UserEditorTypes) == AssemblyTypeFlags.UserEditorTypes)
          {
              return;
          }

          var type = formatter.SerializedType;

          if (type != null)
          {
              this.RegisterType(type, policy);
          }
      }
Beispiel #11
0
        /// <summary>
        /// Gets the default editor that this type would have, if no custom editor was set for this type in particular. This is calculated using the value of <see cref="InspectorConfig.DefaultEditorBehaviour"/>.
        /// </summary>
        /// <param name="drawnType">The drawn type to get the default editor for.</param>
        /// <returns>The editor that would draw this type by default, or null, if there is no default ODIN-defined editor for the drawn type.</returns>
        /// <exception cref="System.ArgumentNullException">drawnType is null</exception>
        public static Type GetDefaultEditorType(Type drawnType)
        {
            if (drawnType == null)
            {
                throw new ArgumentNullException("drawnType");
            }

            if (!InspectorTypeDrawingConfigDrawer.OdinCanCreateEditorFor(drawnType))
            {
                return(null);
            }

            Type editorType;

            if (!HardCodedDefaultEditors.TryGetValue(drawnType, out editorType))
            {
                if (InspectorConfig.Instance.DefaultEditorBehaviour == InspectorDefaultEditors.None)
                {
                    return(null);
                }

                var assemblyTypeFlag = AssemblyUtilities.GetAssemblyTypeFlag(drawnType.Assembly);

                bool useSirenixInspector;

                switch (assemblyTypeFlag)
                {
                case AssemblyTypeFlags.UserTypes:
                case AssemblyTypeFlags.UserEditorTypes:
                    useSirenixInspector = (InspectorConfig.Instance.DefaultEditorBehaviour & InspectorDefaultEditors.UserTypes) == InspectorDefaultEditors.UserTypes;
                    break;

                case AssemblyTypeFlags.PluginTypes:
                case AssemblyTypeFlags.PluginEditorTypes:
                    useSirenixInspector = (InspectorConfig.Instance.DefaultEditorBehaviour & InspectorDefaultEditors.PluginTypes) == InspectorDefaultEditors.PluginTypes;
                    break;

                case AssemblyTypeFlags.UnityTypes:
                case AssemblyTypeFlags.UnityEditorTypes:
                    useSirenixInspector = (InspectorConfig.Instance.DefaultEditorBehaviour & InspectorDefaultEditors.UnityTypes) == InspectorDefaultEditors.UnityTypes;
                    break;

                case AssemblyTypeFlags.OtherTypes:
                // If we hit one of the below flags, or the default case, something actually went wrong.
                // We don't care, though - just shove it into the other types category.
                case AssemblyTypeFlags.All:
                case AssemblyTypeFlags.GameTypes:
                case AssemblyTypeFlags.EditorTypes:
                case AssemblyTypeFlags.CustomTypes:
                case AssemblyTypeFlags.None:
                default:
                    useSirenixInspector = (InspectorConfig.Instance.DefaultEditorBehaviour & InspectorDefaultEditors.OtherTypes) == InspectorDefaultEditors.OtherTypes;
                    break;
                }

                if (useSirenixInspector)
                {
                    editorType = OdinEditorType;
                }
            }

            return(editorType);
        }
Beispiel #12
0
        private bool ProcessProject(out List <Type> serializedTypes)
        {
            serializedTypes = null;

            HashSet <Type> seenSerializedTypes = new HashSet <Type>();

            Action <Type> registerType = null;

            registerType = (type) =>
            {
                if (typeof(UnityEngine.Object).IsAssignableFrom(type))
                {
                    return;
                }
                if (type.IsAbstract || type.IsInterface)
                {
                    return;
                }
                if (type.IsGenericType && (type.IsGenericTypeDefinition || !type.IsFullyConstructedGenericType()))
                {
                    return;
                }

                seenSerializedTypes.Add(type);

                if (type.IsGenericType)
                {
                    foreach (var arg in type.GetGenericArguments())
                    {
                        registerType(arg);
                    }
                }
            };

            Action <Type> onLocatedEmitType = (type) =>
            {
                var typeFlags = AssemblyUtilities.GetAssemblyTypeFlag(type.Assembly);

                if ((typeFlags & AssemblyTypeFlags.PluginEditorTypes) == AssemblyTypeFlags.PluginEditorTypes ||
                    (typeFlags & AssemblyTypeFlags.UnityEditorTypes) == AssemblyTypeFlags.UnityEditorTypes ||
                    (typeFlags & AssemblyTypeFlags.UserEditorTypes) == AssemblyTypeFlags.UserEditorTypes ||
                    (typeFlags & AssemblyTypeFlags.EditorTypes) == AssemblyTypeFlags.EditorTypes)
                {
                    return;
                }

                registerType(type);
            };

            Action <Type> onSerializedType = (type) =>
            {
                // We need variants of serializers for enums specifically
                if (!type.IsEnum)
                {
                    return;
                }

                var typeFlags = AssemblyUtilities.GetAssemblyTypeFlag(type.Assembly);

                if ((typeFlags & AssemblyTypeFlags.PluginEditorTypes) == AssemblyTypeFlags.PluginEditorTypes ||
                    (typeFlags & AssemblyTypeFlags.UnityEditorTypes) == AssemblyTypeFlags.UnityEditorTypes ||
                    (typeFlags & AssemblyTypeFlags.UserEditorTypes) == AssemblyTypeFlags.UserEditorTypes ||
                    (typeFlags & AssemblyTypeFlags.EditorTypes) == AssemblyTypeFlags.EditorTypes)
                {
                    return;
                }

                registerType(type);
            };

            Action <IFormatter> onLocatedFormatter = (formatter) =>
            {
                var typeFlags = AssemblyUtilities.GetAssemblyTypeFlag(formatter.SerializedType.Assembly);

                if ((typeFlags & AssemblyTypeFlags.PluginEditorTypes) == AssemblyTypeFlags.PluginEditorTypes ||
                    (typeFlags & AssemblyTypeFlags.UnityEditorTypes) == AssemblyTypeFlags.UnityEditorTypes ||
                    (typeFlags & AssemblyTypeFlags.UserEditorTypes) == AssemblyTypeFlags.UserEditorTypes ||
                    (typeFlags & AssemblyTypeFlags.EditorTypes) == AssemblyTypeFlags.EditorTypes)
                {
                    return;
                }

                var type = formatter.SerializedType;

                if (type != null)
                {
                    registerType(type);
                }
            };

            FormatterLocator.OnLocatedEmittableFormatterForType += onLocatedEmitType;
            FormatterLocator.OnLocatedFormatter += onLocatedFormatter;
            Serializer.OnSerializedType         += onSerializedType;

            try
            {
                UnitySerializationUtility.ForceEditorModeSerialization = true;

                if (!this.ProcessScenes())
                {
                    Debug.Log("Project scan canceled while scanning scenes.");
                    return(false);
                }

                if (!this.ProcessAssets())

                {
                    Debug.Log("Project scan canceled while scanning assets.");
                    return(false);
                }

                serializedTypes = seenSerializedTypes.ToList();

                return(true);
            }
            finally
            {
                UnitySerializationUtility.ForceEditorModeSerialization = false;

                FormatterLocator.OnLocatedEmittableFormatterForType -= onLocatedEmitType;
                FormatterLocator.OnLocatedFormatter -= onLocatedFormatter;
                Serializer.OnSerializedType         -= onSerializedType;

                EditorUtility.ClearProgressBar(); // Just to be sure we don't forget that
            }
        }
Beispiel #13
0
        private TypeTreeNode GetTypeTree(Type type)
        {
            TypeTreeNode rootNode;

            if (typeTrees.TryGetValue(type, out rootNode) == false)
            {
                var typeFlag = AssemblyUtilities.GetAssemblyTypeFlag(type.Assembly);

                var additionalTypes = new List <Type>()
                {
                    typeof(NullType),
                    type,
                    typeof(string),
                    typeof(List <>).MakeGenericType(type),
                    type.MakeArrayType(),
                    type.MakeArrayType(2),
                    type.MakeArrayType(3)
                };

                var additions = additionalTypes
                                .Select(x => new
                {
                    type = x,
                    flag = (x == typeof(NullType) || x.IsArray) ? typeFlag : AssemblyUtilities.GetAssemblyTypeFlag(x.Assembly)
                })
                                .ToArray();

                rootNode = typeTrees[type] = new TypeTreeNode()
                {
                    ChildNodes = AssemblyUtilities
                                 .GetAllAssemblies()
                                 .Select(x => new { assembly = x, flag = AssemblyUtilities.GetAssemblyTypeFlag(x) })
                                 .GroupBy(a => a.flag)
                                 .Select(a => new TypeTreeNode()
                    {
                        AssemblyTypeFlag = a.Key,
                        IsVisible        = (a.Key & AssemblyTypeFlags.UserTypes) != 0,
                        ChildNodes       = AssemblyUtilities
                                           .GetTypes(a.Key)
                                           .Where(x => ((a.Key & AssemblyTypeFlags.UserTypes) != 0 || x.IsPublic) && x != type) // Only include public types or types from User assembly.
                                           .Prepend(additions.Where(x => x.flag == a.Key).Select(x => x.type))
                                           .Select(t => CanCreateInstance(type, ref t) ? t : null)
                                           .Where(t => t != null)
                                           .Distinct()
                                           .GroupBy(t => ((t == typeof(NullType) || t.IsArray) ? type : t).Namespace ?? "ALSO!GROUP!ME")
                                           .Select(g => new TypeTreeNode()
                        {
                            Namespace  = (g.Key == "ALSO!GROUP!ME") ? null : g.Key,
                            ChildNodes = g
                                         .Select(t => new TypeTreeNode()
                            {
                                Type = t
                            })
                                         .Where(x => x.Type == null || x.Type.GetNiceName().Contains('$') == false)
                                         .OrderBy(t => t.Type == typeof(NullType))
                                         .ThenBy(t => t.Type == null ? "" : t.Type.Name)
                                         .ToList()
                        })
                                           .Where(x => x.Type == null || x.Type.GetNiceName().Contains('$') == false)
                                           .OrderBy(x => x.Namespace)
                                           .ThenBy(x => x.Type)
                                           .ToList()
                    })
                                 .OrderBy(x => x.AssemblyTypeFlag)
                                 .ToList()
                };
                rootNode.IsVisible = true;
                rootNode.Initialize(this, null);
            }
            else
            {
                rootNode.Initialize(this, null);
            }

            this.hideFoldoutLabels = rootNode.EnumerateTree().Count(x => x.Type != null) < 20;

            if (this.hideFoldoutLabels)
            {
                foreach (var item in rootNode.EnumerateTree())
                {
                    item.IsVisible = true;
                }
            }
            else
            {
                var firstCollection = rootNode.EnumerateTree().FirstOrDefault(x => x.ChildNodes != null && x.ChildNodes.Count > 0);
                if (firstCollection != null)
                {
                    firstCollection.IsVisible = true;
                }
            }

            var firstTypeNode = rootNode.EnumerateTree().FirstOrDefault(x => x.Type != null);

            if (firstTypeNode != null)
            {
                firstTypeNode.ForceSetSelected = true;
            }

            if (this.autoSelectFirst && firstTypeNode != null)
            {
                this.chosenType = firstTypeNode.Type;
            }

            return(rootNode);
        }
            public void UpdateSearch(string search, DisplayType displayType)
            {
                this.IsSearchVisible    = false;
                this.AllSubTypesVisible = true;

                this.SubTypesVisible.SetLength(this.SubTypes.Count);

                foreach (var group in this.SubGroups)
                {
                    group.UpdateSearch(search, displayType);

                    if (group.IsSearchVisible)
                    {
                        this.IsSearchVisible = true;
                    }

                    if (!group.AllSubTypesVisible)
                    {
                        this.AllSubTypesVisible = false;
                    }
                }

                bool searchIsNullOrWhitespace = search.IsNullOrWhitespace();

                if (searchIsNullOrWhitespace)
                {
                    if (displayType == DisplayType.AllUnityObjects)
                    {
                        this.IsSearchVisible    = true;
                        this.AllSubTypesVisible = true;

                        for (int i = 0; i < this.SubTypesVisible.Count; i++)
                        {
                            this.SubTypesVisible[i] = true;
                        }

                        return;
                    }
                }

                for (int i = 0; i < this.SubTypes.Count; i++)
                {
                    Type type = this.SubTypes[i].DrawnType;

                    if ((displayType == DisplayType.AllScriptableObjects && !typeof(ScriptableObject).IsAssignableFrom(type)) ||
                        (displayType == DisplayType.AllComponents && !typeof(Component).IsAssignableFrom(type)) ||
                        (displayType == DisplayType.UserScripts && !((AssemblyUtilities.GetAssemblyTypeFlag(type.Assembly) & AssemblyTypeFlags.CustomTypes) != 0)))
                    {
                        this.SubTypesVisible[i] = false;
                        this.AllSubTypesVisible = false;
                        continue;
                    }

                    if (searchIsNullOrWhitespace || type.FullName.Contains(search, StringComparison.InvariantCultureIgnoreCase))
                    {
                        this.IsSearchVisible    = true;
                        this.SubTypesVisible[i] = true;
                    }
                    else
                    {
                        this.SubTypesVisible[i] = false;
                        this.AllSubTypesVisible = false;
                    }
                }
            }