Ejemplo n.º 1
0
        private static bool IsTypeAUserExtendedScript(AssemblyDefinition assembly, TypeReference type)
        {
            bool result;

            if (type == null)
            {
                result = false;
            }
            else if (type.FullName == "System.Object")
            {
                result = false;
            }
            else
            {
                Assembly assembly2 = null;
                if (type.Scope.Name == "UnityEngine")
                {
                    assembly2 = typeof(MonoBehaviour).Assembly;
                }
                else if (type.Scope.Name == "UnityEditor")
                {
                    assembly2 = typeof(EditorWindow).Assembly;
                }
                else if (type.Scope.Name == "UnityEngine.UI")
                {
                    assembly2 = AssemblyHelper.FindLoadedAssemblyWithName("UnityEngine.UI");
                }
                if (assembly2 != null)
                {
                    string name  = (!type.IsGenericInstance) ? type.FullName : (type.Namespace + "." + type.Name);
                    Type   type2 = assembly2.GetType(name);
                    if (type2 == typeof(MonoBehaviour) || type2.IsSubclassOf(typeof(MonoBehaviour)))
                    {
                        result = true;
                        return(result);
                    }
                    if (type2 == typeof(ScriptableObject) || type2.IsSubclassOf(typeof(ScriptableObject)))
                    {
                        result = true;
                        return(result);
                    }
                    if (type2 == typeof(ScriptedImporter) || type2.IsSubclassOf(typeof(ScriptedImporter)))
                    {
                        result = true;
                        return(result);
                    }
                }
                TypeDefinition typeDefinition = null;
                try
                {
                    typeDefinition = type.Resolve();
                }
                catch (AssemblyResolutionException)
                {
                }
                result = (typeDefinition != null && AssemblyHelper.IsTypeAUserExtendedScript(assembly, typeDefinition.BaseType));
            }
            return(result);
        }
Ejemplo n.º 2
0
        public static void ExtractAllClassesThatAreUserExtendedScripts(string path, out string[] classNamesArray, out string[] classNameSpacesArray)
        {
            List <string>           list                    = new List <string>();
            List <string>           list2                   = new List <string>();
            ReaderParameters        readerParameters        = new ReaderParameters();
            DefaultAssemblyResolver defaultAssemblyResolver = new DefaultAssemblyResolver();

            defaultAssemblyResolver.AddSearchDirectory(Path.GetDirectoryName(path));
            BuildTargetGroup activeBuildTargetGroup = EditorUserBuildSettings.activeBuildTargetGroup;
            BuildTarget      activeBuildTarget      = EditorUserBuildSettings.activeBuildTarget;

            PrecompiledAssembly[] precompiledAssemblies = InternalEditorUtility.GetPrecompiledAssemblies(true, activeBuildTargetGroup, activeBuildTarget);
            HashSet <string>      hashSet = new HashSet <string>();

            PrecompiledAssembly[] array = precompiledAssemblies;
            for (int i = 0; i < array.Length; i++)
            {
                PrecompiledAssembly precompiledAssembly = array[i];
                hashSet.Add(Path.GetDirectoryName(precompiledAssembly.Path));
            }
            foreach (string current in hashSet)
            {
                defaultAssemblyResolver.AddSearchDirectory(current);
            }
            readerParameters.AssemblyResolver = defaultAssemblyResolver;
            AssemblyDefinition assemblyDefinition = AssemblyDefinition.ReadAssembly(path, readerParameters);

            foreach (ModuleDefinition current2 in assemblyDefinition.Modules)
            {
                foreach (TypeDefinition current3 in current2.Types)
                {
                    TypeReference baseType = current3.BaseType;
                    try
                    {
                        if (AssemblyHelper.IsTypeAUserExtendedScript(assemblyDefinition, baseType))
                        {
                            list.Add(current3.Name);
                            list2.Add(current3.Namespace);
                        }
                    }
                    catch (Exception)
                    {
                        UnityEngine.Debug.LogError(string.Concat(new string[]
                        {
                            "Failed to extract ",
                            current3.FullName,
                            " class of base type ",
                            baseType.FullName,
                            " when inspecting ",
                            path
                        }));
                    }
                }
            }
            classNamesArray      = list.ToArray();
            classNameSpacesArray = list2.ToArray();
        }