public void Draw(bool parentIncluded, HashSet <string> typeExceptions)
            {
                using (new EditorGUI.DisabledScope(!parentIncluded))
                {
                    var included = parentIncluded && !typeExceptions.Contains(FullName);
                    using (new GUILayout.HorizontalScope())
                    {
                        EditorGUILayout.LabelField(m_DisplayName);
                        var indentedRect = EditorGUI.IndentedRect(Rect.zero);
                        var nowIncluded  = EditorGUILayout.Toggle("", included, GUILayout.Width(k_ToggleWidth + indentedRect.x));

                        if (included && !nowIncluded)
                        {
                            typeExceptions.Add(FullName);
                            RuntimeSerializationSettingsUtils.SaveSettings();
                        }

                        if (!included && nowIncluded)
                        {
                            typeExceptions.Remove(FullName);
                            RuntimeSerializationSettingsUtils.SaveSettings();
                        }
                    }
                }
            }
        public override void OnActivate(string searchContext, VisualElement rootElement)
        {
            base.OnActivate(searchContext, rootElement);
            RuntimeSerializationSettingsUtils.ClearExceptionCache();
            m_AssemblyRows.Clear();
            ReflectionUtils.ForEachAssembly(assembly =>
            {
                if (CodeGenUtils.IsTestAssembly(assembly))
                {
                    return;
                }

                if (k_IgnoredAssemblies.Contains(assembly.GetName().Name))
                {
                    return;
                }

                var assemblyRow = new AssemblyRow(assembly);
                if (assemblyRow.GetTypeCount() > 0)
                {
                    m_AssemblyRows.Add(assemblyRow);
                }
            });

            m_AssemblyRows.Sort((a, b) => a.DisplayName.CompareTo(b.DisplayName));
        }
        static bool ShouldProcess(ICompiledAssembly compiledAssembly)
        {
            if (!compiledAssembly.RequiresCodegen())
            {
                return(false);
            }

            var assemblyName = compiledAssembly.Name;

            if (assemblyName == CodeGenUtils.ExternalPropertyBagAssemblyName)
            {
                return(false);
            }

            // TODO: Debug type load exception and other assembly-specific issues
            if (k_IgnoredAssemblies.Contains(assemblyName))
            {
                return(false);
            }

            if (RuntimeSerializationSettingsUtils.GetAssemblyExceptions().Contains(assemblyName))
            {
                return(false);
            }

            if (CodeGenUtils.IsTestAssembly(compiledAssembly))
            {
                return(false);
            }

            return(true);
        }
            public void Draw(HashSet <string> assemblyExceptions, HashSet <string> namespaceExceptions, HashSet <string> typeExceptions)
            {
                var included = !assemblyExceptions.Contains(FullName);

                using (new GUILayout.HorizontalScope())
                {
                    var wasExpanded = m_Expanded;
                    m_Expanded = EditorGUILayout.Foldout(m_Expanded, DisplayName, true);
                    if (wasExpanded != m_Expanded && Event.current.alt)
                    {
                        m_RootNamespaceGroup.SetExpandedRecursively(m_Expanded);
                    }

                    var indentedRect = EditorGUI.IndentedRect(Rect.zero);
                    var nowIncluded  = EditorGUILayout.Toggle("", included, GUILayout.Width(k_ToggleWidth + indentedRect.x));

                    if (included && !nowIncluded)
                    {
                        assemblyExceptions.Add(FullName);
                        if (Event.current.alt)
                        {
                            m_RootNamespaceGroup.GetAllNamespacesRecursively(namespaceExceptions);
                        }

                        RuntimeSerializationSettingsUtils.SaveSettings();
                    }

                    if (!included && nowIncluded)
                    {
                        assemblyExceptions.Remove(FullName);
                        if (Event.current.alt)
                        {
                            m_RootNamespaceGroup.RemoveAllNamespacesRecursively(namespaceExceptions);
                        }

                        RuntimeSerializationSettingsUtils.SaveSettings();
                    }
                }

                if (m_Expanded)
                {
                    using (new EditorGUI.IndentLevelScope())
                    {
                        using (new EditorGUI.DisabledScope(!included))
                        {
                            DrawButtons(namespaceExceptions, "namespaces", m_GetAllNamespaces, m_RemoveAllNamespaces);
                            DrawButtons(namespaceExceptions, "types", m_GetAllTypes, m_RemoveAllTypes);
                        }

                        foreach (var kvp in m_RootNamespaceGroup.Children)
                        {
                            kvp.Value.Draw(kvp.Key, included, namespaceExceptions, typeExceptions);
                        }
                    }
                }
            }
Ejemplo n.º 5
0
        static ILPostProcessResult GeneratePropertyBags(AssemblyDefinition compiledAssembly, string[] defines)
        {
            var context = new Context(compiledAssembly.MainModule, defines);

            // TODO: collection pools
            var fields     = new List <FieldInfo>();
            var properties = new List <PropertyInfo>();

#if UNITY_2020_2_OR_NEWER
            var editorAssemblyPath = Assembly.GetAssembly(typeof(EditorApplication)).Location;
            var editorPath         = editorAssemblyPath.Substring(0, editorAssemblyPath.Length - k_EditorAssemblyPathSuffix);
#else
            var editorPath = EditorApplication.applicationContentsPath;
#endif
            var serializableContainerTypes = new HashSet <Type>();
            var assemblyExceptions         = RuntimeSerializationSettingsUtils.GetAssemblyExceptions();
            var namespaceExceptions        = RuntimeSerializationSettingsUtils.GetNamespaceExceptions();
            var typeExceptions             = RuntimeSerializationSettingsUtils.GetTypeExceptions();
            ReflectionUtils.ForEachAssembly(assembly =>
            {
                Console.WriteLine($"Process assembly {assembly.FullName}");
                if (assembly.IsDynamic)
                {
                    return;
                }

                if (!CodeGenUtils.IsBuiltInAssembly(assembly, editorPath))
                {
                    return;
                }

                if (assemblyExceptions.Contains(assembly.FullName))
                {
                    return;
                }

                PostProcessAssembly(namespaceExceptions, typeExceptions, assembly, fields, properties, serializableContainerTypes);
            });

            serializableContainerTypes.ExceptWith(k_IgnoredTypes);

            if (serializableContainerTypes.Count == 0)
            {
                return(null);
            }

            GeneratePropertyBagsForSerializableTypes(context, serializableContainerTypes);

            return(CreatePostProcessResult(compiledAssembly));
        }
        public override void OnGUI(string searchContext)
        {
            base.OnGUI(searchContext);
            GUILayout.Label("AOT Code Generation Exceptions", EditorStyles.boldLabel);
            var assemblyExceptions  = RuntimeSerializationSettingsUtils.GetAssemblyExceptions();
            var namespaceExceptions = RuntimeSerializationSettingsUtils.GetNamespaceExceptions();
            var typeExceptions      = RuntimeSerializationSettingsUtils.GetTypeExceptions();

            DrawButtons(assemblyExceptions, "assemblies", m_ExcludeAllAssemblies, k_ClearExceptions);
            DrawButtons(namespaceExceptions, "namespaces", m_ExcludeAllNamespaces, k_ClearExceptions);
            DrawButtons(typeExceptions, "types", m_ExcludeAllTypes, k_ClearExceptions);

            foreach (var assemblyRow in m_AssemblyRows)
            {
                assemblyRow.Draw(assemblyExceptions, namespaceExceptions, typeExceptions);
            }
        }
        static void DrawButtons(HashSet <string> exceptions, string type, Action <HashSet <string> > excludeAll, Action <HashSet <string> > includeAll)
        {
            using (new GUILayout.HorizontalScope())
            {
                GUILayout.Space(EditorGUI.indentLevel * k_Indent);
                if (GUILayout.Button($"Exclude all {type}"))
                {
                    excludeAll(exceptions);
                    RuntimeSerializationSettingsUtils.SaveSettings();
                }

                if (GUILayout.Button($"Include all {type}"))
                {
                    includeAll(exceptions);
                    RuntimeSerializationSettingsUtils.SaveSettings();
                }
            }
        }
        static ILPostProcessResult ProcessAssembly(AssemblyDefinition compiledAssembly)
        {
            var module         = compiledAssembly.MainModule;
            var componentTypes = new List <TypeContainer>();

#if UNITY_2020_2_OR_NEWER
            var editorAssemblyPath = Assembly.GetAssembly(typeof(EditorApplication)).Location;
            var editorPath         = editorAssemblyPath.Substring(0, editorAssemblyPath.Length - k_EditorAssemblyPathSuffix);
#else
            var editorPath = EditorApplication.applicationContentsPath;
#endif

            var assemblyExceptions  = RuntimeSerializationSettingsUtils.GetAssemblyExceptions();
            var namespaceExceptions = RuntimeSerializationSettingsUtils.GetNamespaceExceptions();
            var typeExceptions      = RuntimeSerializationSettingsUtils.GetTypeExceptions();
            ReflectionUtils.ForEachAssembly(assembly =>
            {
                if (assembly.IsDynamic)
                {
                    return;
                }

                if (!CodeGenUtils.IsBuiltInAssembly(assembly, editorPath) && !k_PlayerAssemblies.Contains(assembly.GetName().Name))
                {
                    return;
                }

                if (assemblyExceptions.Contains(assembly.FullName))
                {
                    return;
                }

                PostProcessAssembly(namespaceExceptions, typeExceptions, module, assembly, componentTypes);
            });

            PostProcessGenericMethodWrapper(componentTypes, module);

            return(CreatePostProcessResult(compiledAssembly));
        }
            void Draw(string prefix, string name, bool parentIncluded, HashSet <string> namespaceExceptions, HashSet <string> typeExceptions)
            {
                using (new EditorGUI.DisabledScope(!parentIncluded))
                {
                    var fullName = name;
                    if (!string.IsNullOrEmpty(prefix))
                    {
                        fullName = $"{prefix}.{name}";
                    }

                    var included = parentIncluded && !namespaceExceptions.Contains(fullName);
                    using (new GUILayout.HorizontalScope())
                    {
                        var wasExpanded = m_Expanded;
                        m_Expanded = EditorGUILayout.Foldout(m_Expanded, name, true);
                        if (wasExpanded != m_Expanded && Event.current.alt)
                        {
                            SetExpandedRecursively(m_Expanded);
                        }

                        var indentedRect = EditorGUI.IndentedRect(Rect.zero);
                        var nowIncluded  = EditorGUILayout.Toggle("", included, GUILayout.Width(k_ToggleWidth + indentedRect.x));

                        if (included && !nowIncluded)
                        {
                            namespaceExceptions.Add(fullName);
                            if (Event.current.alt)
                            {
                                GetAllNamespacesRecursively(prefix, name, namespaceExceptions);
                                GetAllTypesRecursively(typeExceptions);
                            }

                            RuntimeSerializationSettingsUtils.SaveSettings();
                        }

                        if (!included && nowIncluded)
                        {
                            namespaceExceptions.Remove(fullName);
                            if (Event.current.alt)
                            {
                                RemoveAllNamespacesRecursively(prefix, name, namespaceExceptions);
                                RemoveAllTypesRecursively(typeExceptions);
                            }

                            RuntimeSerializationSettingsUtils.SaveSettings();
                        }
                    }

                    if (m_Expanded)
                    {
                        using (new EditorGUI.IndentLevelScope())
                        {
                            using (new EditorGUI.DisabledScope(!included))
                            {
                                DrawButtons(namespaceExceptions, "namespaces", m_GetAllNamespacesRecursively, m_RemoveAllNamespacesRecursively);
                                DrawButtons(typeExceptions, "types", m_GetAllTypesRecursively, m_RemoveAllTypesRecursively);
                            }

                            foreach (var kvp in m_Children)
                            {
                                kvp.Value.Draw(fullName, kvp.Key, included, namespaceExceptions, typeExceptions);
                            }

                            foreach (var typeRow in m_Types)
                            {
                                typeRow.Draw(included, typeExceptions);
                            }
                        }
                    }
                }
            }
        static void PostProcessAssembly(AssemblyDefinition assembly, HashSet <TypeDefinition> serializableTypes)
        {
            var namespaceExceptions = RuntimeSerializationSettingsUtils.GetNamespaceExceptions();
            var typeExceptions      = RuntimeSerializationSettingsUtils.GetTypeExceptions();

            foreach (var module in assembly.Modules)
            {
                foreach (var type in module.GetTypes())
                {
                    if (type.IsAbstract || type.IsInterface)
                    {
                        continue;
                    }

                    if (type.HasGenericParameters && !type.IsGenericInstance)
                    {
                        continue;
                    }

                    var(hasSkipGenerationAttribute, hasCompilerGeneratedAttribute) = CodeGenUtils.GetSerializationAttributes(type);
                    if (hasSkipGenerationAttribute || hasCompilerGeneratedAttribute)
                    {
                        continue;
                    }

                    if (!CodeGenUtils.IsAssignableToComponent(type))
                    {
#if INCLUDE_ALL_SERIALIZABLE_CONTAINERS
                        if (!CodeGenUtils.IsSerializableContainer(type))
#endif
                        continue;
                    }

                    var typeName = type.FullName;
                    if (string.IsNullOrEmpty(typeName))
                    {
                        continue;
                    }

                    if (typeExceptions.Contains(typeName))
                    {
                        continue;
                    }

                    var partOfNamespaceException = false;
                    var typeNamespace            = type.Namespace;
                    if (!string.IsNullOrEmpty(typeNamespace))
                    {
                        foreach (var exception in namespaceExceptions)
                        {
                            if (typeNamespace.Contains(exception))
                            {
                                partOfNamespaceException = true;
                                break;
                            }
                        }
                    }

                    if (partOfNamespaceException)
                    {
                        continue;
                    }

                    serializableTypes.Add(type);
                    PostProcessType(type, serializableTypes);
                }
            }
        }