Exemple #1
0
        public static DatabaseMeta      GetDatabase()
        {
            if (DatabaseMeta.database == null)
            {
                try
                {
                    if (File.Exists(DatabaseMeta.DatabasePath) == true)
                    {
                        DatabaseMeta.database = new DatabaseMeta(DatabaseMeta.DatabasePath);
                    }
                }
                catch (Exception ex)
                {
                    Debug.LogException(ex);
                }
                finally
                {
                    if (DatabaseMeta.database == null)
                    {
                        DatabaseMeta.database = new DatabaseMeta();
                    }
                }
            }

            return(DatabaseMeta.database);
        }
        public static AssemblyUsagesResult[]    CheckCompatibilities(IEnumerable <string> assembliesPath, FilterText[] filterNamespaces, string[] targetNamespaces, IEnumerable <string> unityVersions)
        {
            if (targetNamespaces.Length == 0)
            {
                Debug.LogWarning("You must target at least one namespace.");
                return(null);
            }

            int hash = 0;

            hash += assembliesPath.GetHashCode();

            for (int i = 0, max = filterNamespaces.Length; i < max; ++i)
            {
                FilterText filter = filterNamespaces[i];

                if (filter.active == true)
                {
                    hash += filter.text.GetHashCode() + filter.active.GetHashCode();
                }
            }

            for (int i = 0, max = targetNamespaces.Length; i < max; ++i)
            {
                hash += targetNamespaces[i].GetHashCode();
            }

            if (AssemblyUsages.usages == null || AssemblyUsages.lastUsagesHash != hash)
            {
                AssemblyUsages.lastUsagesHash = hash;

                using ((AssemblyUsages.debug & DebugItems.WatchTime) == 0 ? null : WatchTime.Get("Extracted usages from target assemblies"))
                {
                    AssemblyUsages.usages = AssemblyUsages.InspectAssembly(assembliesPath, filterNamespaces, targetNamespaces);
                }
            }

            List <AssemblyUsagesResult> results = new List <AssemblyUsagesResult>();
            int          processorCount         = Environment.ProcessorCount;
            Exception    threadException        = null;
            DatabaseMeta database = DatabaseMeta.GetDatabase();

            using ((AssemblyUsages.debug & DebugItems.WatchTime) == 0 ? null : WatchTime.Get("Resolving all"))
            {
                foreach (string unityVersion in unityVersions)
                {
                    try
                    {
                        UnityMeta unityMeta = database.Get(unityVersion);

                        if (unityMeta != null)
                        {
                            AssemblyUsagesResult result = new AssemblyUsagesResult()
                            {
                                assemblyUsages = usages, unityMeta = unityMeta
                            };

                            using ((AssemblyUsages.debug & DebugItems.WatchTime) == 0 ? null : WatchTime.Get("Resolved against " + unityMeta.Version))
                            {
                                result.ResolveReferences(AssemblyUsages.usages.visibleTypes, AssemblyUsages.usages.visibleFields, AssemblyUsages.usages.visibleMethods);
                            }

                            lock (results)
                            {
                                results.Add(result);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.LogException(ex);
                        threadException = ex;
                    }
                }
            }

            if (threadException != null)
            {
                throw threadException;
            }

            results.Sort(Utility.CompareVersion);

            return(results.ToArray());
        }
        public TypeDatabase(DatabaseMeta database)
        {
            Dictionary <string, Type> types = new Dictionary <string, Type>();

            for (int i = 0, max = database.UnityMeta.Length; i < max; ++i)
            {
                UnityMeta unityMeta = database.UnityMeta[i];

                for (int j = 0, max2 = unityMeta.AssembliesMeta.Length; j < max2; ++j)
                {
                    AssemblyMeta assemblyMeta = unityMeta.AssembliesMeta[j];

                    for (int k = 0, max3 = assemblyMeta.Types.Length; k < max3; ++k)
                    {
                        TypeMeta typeMeta = assemblyMeta.Types[k];
                        Type     type;

                        if (types.TryGetValue(typeMeta.FullName, out type) == false)
                        {
                            type = new Type()
                            {
                                isPublic = typeMeta.IsPublic,
                                name     = typeMeta.FullName,
                                members  = new Member[0],
                                versions = new byte[] { (byte)i }
                            };
                            types.Add(typeMeta.FullName, type);
                        }
                        else
                        {
                            int l    = 0;
                            int max4 = type.versions.Length;

                            for (; l < max4; ++l)
                            {
                                if (type.versions[l] == i)
                                {
                                    break;
                                }
                            }

                            if (l == max4)
                            {
                                Array.Resize(ref type.versions, type.versions.Length + 1);
                                type.versions[type.versions.Length - 1] = (byte)i;
                            }
                        }

                        for (int l = 0, max4 = typeMeta.Events.Length; l < max4; ++l)
                        {
                            type.Aggregate(MemberTypes.Event, typeMeta.Events[l].Name, i);
                        }

                        for (int l = 0, max4 = typeMeta.Properties.Length; l < max4; ++l)
                        {
                            type.Aggregate(MemberTypes.Property, typeMeta.Properties[l].Name, i);
                        }

                        for (int l = 0, max4 = typeMeta.Fields.Length; l < max4; ++l)
                        {
                            type.Aggregate(MemberTypes.Field, typeMeta.Fields[l].Name, i);
                        }

                        for (int l = 0, max4 = typeMeta.Methods.Length; l < max4; ++l)
                        {
                            type.Aggregate(MemberTypes.Method, typeMeta.Methods[l].Name, i);
                        }
                    }
                }
            }

            this.versions = new string[database.UnityMeta.Length];
            for (int i = 0, max = database.UnityMeta.Length; i < max; ++i)
            {
                this.versions[i] = database.UnityMeta[i].Version;
            }

            this.types = new List <Type>(types.Values).ToArray();
        }
        /// <summary>Creates a UnityMeta from a Unity installation path and automatically adds it to DatabaseMeta.</summary>
        /// <param name="unityInstallPath"></param>
        /// <returns></returns>
        public static UnityMeta Create(string unityInstallPath)
        {
            UnityMeta unityMeta;

            try
            {
                // Gather all assemblies referencing UnityEditor.
                string unityEditor = Path.Combine(unityInstallPath, @"Editor\Data\Managed\UnityEditor.dll");

                if (File.Exists(unityEditor) == false)
                {
                    Debug.LogError("Assembly at \"" + unityEditor + "\" was not found.");
                    return(null);
                }

                string[] editorAssemblies = Directory.GetFiles(Path.Combine(unityInstallPath, @"Editor\Data\Managed"), "*.dll");

                for (int i = 0, max = editorAssemblies.Length; i < max; ++i)
                {
                    using (AssemblyDefinition assemblyDef = AssemblyDefinition.ReadAssembly(editorAssemblies[i]))
                    {
                        int j    = 0;
                        int max2 = assemblyDef.Modules.Count;

                        for (; j < max2; ++j)
                        {
                            ModuleDefinition moduleDef = assemblyDef.Modules[j];

                            if (moduleDef.HasAssemblyReferences == true)
                            {
                                int k    = 0;
                                int max3 = moduleDef.AssemblyReferences.Count;

                                for (; k < max3; ++k)
                                {
                                    if (moduleDef.AssemblyReferences[k].Name == "UnityEditor")
                                    {
                                        break;
                                    }
                                }

                                if (k < max3)
                                {
                                    break;
                                }
                            }
                        }

                        if (j == max2)
                        {
                            editorAssemblies[i] = null;
                        }
                    }
                }

                string   runtimeAssembliesPath = Path.Combine(unityInstallPath, @"Editor\Data\Managed\UnityEngine");
                string[] runtimeAssemblies;

                if (Directory.Exists(runtimeAssembliesPath) == true)
                {
                    runtimeAssemblies = Directory.GetFiles(runtimeAssembliesPath, "*.dll");
                }
                else
                {
                    // Switch to Unity <=2017.1.
                    runtimeAssembliesPath = Path.Combine(unityInstallPath, @"Editor\Data\Managed\UnityEngine.dll");

                    if (File.Exists(runtimeAssembliesPath) == false)
                    {
                        Debug.LogError("Runtime assembly at \"" + runtimeAssembliesPath + "\" was not found.");
                        return(null);
                    }

                    runtimeAssemblies = new string[] { runtimeAssembliesPath };
                }

                string   extensionAssembliesPath = Path.Combine(unityInstallPath, @"Editor\Data\UnityExtensions\Unity");
                string[] extensionAssemblies;

                if (Directory.Exists(extensionAssembliesPath) == false)                 // Does not exist in Unity 4.5.
                {
                    extensionAssemblies = new string[0];
                }
                else
                {
                    extensionAssemblies = Directory.GetFiles(extensionAssembliesPath, "*.dll", SearchOption.AllDirectories);
                }

                List <AssemblyMeta> meta = new List <AssemblyMeta>();

                try
                {
                    meta.Add(new AssemblyMeta(unityEditor.Substring(unityInstallPath.Length + 1), unityEditor));
                }
                catch (Exception)
                {
                    Debug.LogError("Main editor assembly \"" + unityEditor + "\" failed extraction.");
                    throw;
                }

                for (int i = 0, max = runtimeAssemblies.Length; i < max; ++i)
                {
                    try
                    {
                        meta.Add(new AssemblyMeta(runtimeAssemblies[i].Substring(unityInstallPath.Length + 1), runtimeAssemblies[i]));
                    }
                    catch (Exception)
                    {
                        Debug.LogError("Runtime assembly \"" + runtimeAssemblies[i] + "\" failed extraction.");
                        throw;
                    }
                }

                for (int i = 0, max = editorAssemblies.Length; i < max; ++i)
                {
                    try
                    {
                        if (editorAssemblies[i] != null)
                        {
                            meta.Add(new AssemblyMeta(editorAssemblies[i].Substring(unityInstallPath.Length + 1), editorAssemblies[i]));
                        }
                    }
                    catch (Exception)
                    {
                        Debug.LogError("Editor assembly \"" + editorAssemblies[i] + "\" failed extraction.");
                        throw;
                    }
                }

                for (int i = 0, max = extensionAssemblies.Length; i < max; ++i)
                {
                    try
                    {
                        meta.Add(new AssemblyMeta(extensionAssemblies[i].Substring(unityInstallPath.Length + 1), extensionAssemblies[i]));
                    }
                    catch (Exception)
                    {
                        Debug.LogWarning("Extension assembly \"" + extensionAssemblies[i] + "\" failed extraction and has been discarded.");
                    }
                }

                unityMeta = new UnityMeta(Utility.GetUnityVersion(unityInstallPath), meta.ToArray());
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
                throw;
            }

            DatabaseMeta.GetDatabase().Add(unityMeta);

            return(unityMeta);
        }