public void Audit(Action <ProjectIssue> onIssueFound, Action onComplete = null, IProgressBar progressBar = null)
        {
            if (progressBar != null)
            {
                progressBar.Initialize("Analyzing Settings", "Analyzing project settings", m_Analyzers.Count);
            }

            foreach (var analyzer in m_Analyzers)
            {
                if (progressBar != null)
                {
                    progressBar.AdvanceProgressBar();
                }

                foreach (var issue in analyzer.Analyze())
                {
                    onIssueFound(issue);
                }
            }

            if (progressBar != null)
            {
                progressBar.ClearProgressBar();
            }

            if (onComplete != null)
            {
                onComplete();
            }
        }
Example #2
0
        public void Audit(ProjectReport projectReport, IProgressBar progressBar = null)
        {
            var userAssemblies = GetPlayerAssemblies();

            if (userAssemblies.Count > 0)
            {
                m_AssemblyResolver = new DefaultAssemblyResolver();
#if UNITY_2019_1_OR_NEWER
                List <string> assemblyPaths = new List <string>();
                assemblyPaths.AddRange(CompilationPipeline.GetPrecompiledAssemblyPaths(CompilationPipeline.PrecompiledAssemblySources
                                                                                       .UserAssembly));
                assemblyPaths.AddRange(CompilationPipeline.GetPrecompiledAssemblyPaths(CompilationPipeline.PrecompiledAssemblySources
                                                                                       .UnityEngine));
                foreach (var dir in assemblyPaths.Select(path => Path.GetDirectoryName(path)).Distinct())
                {
                    m_AssemblyResolver.AddSearchDirectory(dir);
                }
#else
                m_AssemblyResolver.AddSearchDirectory(Path.Combine(EditorApplication.applicationContentsPath, "Managed", "UnityEngine"));
#endif
                m_AssemblyResolver.AddSearchDirectory(Path.Combine(EditorApplication.applicationContentsPath, "UnityExtensions", "Unity", "GUISystem"));

                foreach (var dir in userAssemblies.Select(path => Path.GetDirectoryName(path)).Distinct())
                {
                    m_AssemblyResolver.AddSearchDirectory(dir);
                }

                var callCrawler = new CallCrawler();

                if (progressBar != null)
                {
                    progressBar.Initialize("Analyzing Scripts", "Analyzing project scripts", m_PlayerAssemblies.Length);
                }

                // Analyse all Player assemblies, including Package assemblies.
                foreach (var assemblyPath in userAssemblies)
                {
                    if (progressBar != null)
                    {
                        progressBar.AdvanceProgressBar(string.Format("Analyzing {0} scripts", Path.GetFileName(assemblyPath)));
                    }

                    if (!File.Exists(assemblyPath))
                    {
                        Debug.LogError(assemblyPath + " not found.");
                        continue;
                    }

                    AnalyzeAssembly(assemblyPath, projectReport, callCrawler);
                }

                if (progressBar != null)
                {
                    progressBar.ClearProgressBar();
                }

                callCrawler.BuildCallHierarchies(projectReport, progressBar);
            }
        }
Example #3
0
        public void Audit(ProjectReport projectReport, IProgressBar progressBar = null)
        {
            if (!AssemblyHelper.CompileAssemblies())
            {
                return;
            }

            var callCrawler = new CallCrawler();

            using (var assemblyResolver = new DefaultAssemblyResolver())
            {
                var compiledAssemblyPaths = AssemblyHelper.GetCompiledAssemblyPaths();

                foreach (var dir in AssemblyHelper.GetPrecompiledAssemblyDirectories())
                {
                    assemblyResolver.AddSearchDirectory(dir);
                }

                foreach (var dir in AssemblyHelper.GetPrecompiledEngineAssemblyDirectories())
                {
                    assemblyResolver.AddSearchDirectory(dir);
                }

                foreach (var dir in AssemblyHelper.GetCompiledAssemblyDirectories())
                {
                    assemblyResolver.AddSearchDirectory(dir);
                }

                if (progressBar != null)
                {
                    progressBar.Initialize("Analyzing Scripts", "Analyzing project scripts", m_PlayerAssemblies.Length);
                }

                // Analyse all Player assemblies, including Package assemblies.
                foreach (var assemblyPath in compiledAssemblyPaths)
                {
                    if (progressBar != null)
                    {
                        progressBar.AdvanceProgressBar(string.Format("Analyzing {0} scripts", Path.GetFileName(assemblyPath)));
                    }

                    if (!File.Exists(assemblyPath))
                    {
                        Debug.LogError(assemblyPath + " not found.");
                        continue;
                    }

                    AnalyzeAssembly(assemblyPath, assemblyResolver, projectReport, callCrawler);
                }
            }

            if (progressBar != null)
            {
                progressBar.ClearProgressBar();
            }

            callCrawler.BuildCallHierarchies(projectReport, progressBar);
        }
Example #4
0
        private void AnalyzeAssemblies(IEnumerable <AssemblyInfo> assemblyInfos, Action <CallInfo> onCallFound, Action <ProjectIssue> onIssueFound, Action <IProgressBar> onComplete, IProgressBar progressBar = null)
        {
            var compiledAssemblyDirectories = assemblyInfos.Select(info => Path.GetDirectoryName(info.path)).Distinct();

            using (var assemblyResolver = new DefaultAssemblyResolver())
            {
                foreach (var dir in AssemblyHelper.GetPrecompiledAssemblyDirectories())
                {
                    assemblyResolver.AddSearchDirectory(dir);
                }

                foreach (var dir in AssemblyHelper.GetPrecompiledEngineAssemblyDirectories())
                {
                    assemblyResolver.AddSearchDirectory(dir);
                }

                foreach (var dir in compiledAssemblyDirectories)
                {
                    assemblyResolver.AddSearchDirectory(dir);
                }

                if (progressBar != null)
                {
                    progressBar.Initialize("Analyzing Scripts", "Analyzing project scripts",
                                           assemblyInfos.Count());
                }

                // Analyse all Player assemblies
                foreach (var assemblyInfo in assemblyInfos)
                {
                    if (progressBar != null)
                    {
                        progressBar.AdvanceProgressBar(string.Format("Analyzing {0}", assemblyInfo.name));
                    }

                    if (!File.Exists(assemblyInfo.path))
                    {
                        Debug.LogError(assemblyInfo.path + " not found.");
                        continue;
                    }

                    AnalyzeAssembly(assemblyInfo, assemblyResolver, onCallFound, onIssueFound);
                }
            }

            if (progressBar != null)
            {
                progressBar.ClearProgressBar();
            }

            if (onComplete != null)
            {
                onComplete(progressBar);
            }
        }
Example #5
0
        public void BuildCallHierarchies(List <ProjectIssue> issues, IProgressBar progressBar = null)
        {
            foreach (var entry in m_CallPairs)
            {
                if (!m_BucketedCallPairs.ContainsKey(entry.Value.callee.FullName))
                {
                    m_BucketedCallPairs.Add(entry.Value.callee.FullName, new List <CallInfo>());
                }
                m_BucketedCallPairs[entry.Value.callee.FullName].Add(entry.Value);
            }

            if (issues.Count > 0)
            {
                Profiler.BeginSample("CallCrawler.BuildCallHierarchies");

                if (progressBar != null)
                {
                    progressBar.Initialize("Analyzing Scripts", "Analyzing call trees", issues.Count);
                }

                foreach (var issue in issues)
                {
                    if (progressBar != null)
                    {
                        progressBar.AdvanceProgressBar();
                    }

                    var depth    = 0;
                    var callTree = issue.callTree;
                    BuildHierarchy(callTree.GetChild(), depth);

                    // temp fix for null location (ScriptAuditor was unable to get sequence point)
                    if (issue.location == null && callTree.HasChildren())
                    {
                        issue.location = callTree.GetChild().location;
                    }
                }
                if (progressBar != null)
                {
                    progressBar.ClearProgressBar();
                }

                Profiler.EndSample();
            }
        }
        IEnumerable <string> CompilePlayerAssemblies(Assembly[] assemblies, IProgressBar progressBar = null)
        {
            if (progressBar != null)
            {
                var numAssemblies = assemblies.Length;
                progressBar.Initialize("Assembly Compilation", "Compiling project scripts",
                                       numAssemblies);
                m_OnAssemblyCompilationStarted = s =>
                {
                    // The compilation pipeline might compile Editor-specific assemblies
                    // let's advance the progress bar only for Player ones.
                    var assemblyName = Path.GetFileNameWithoutExtension(s);
                    if (assemblies.FirstOrDefault(asm => asm.name.Equals(assemblyName)) != null)
                    {
                        progressBar.AdvanceProgressBar(assemblyName);
                    }
                };
                CompilationPipeline.assemblyCompilationStarted += m_OnAssemblyCompilationStarted;
            }
            CompilationPipeline.assemblyCompilationFinished += OnAssemblyCompilationFinished;

            m_OutputFolder = FileUtil.GetUniqueTempPathInProject();

            var input = new ScriptCompilationSettings
            {
                target = EditorUserBuildSettings.activeBuildTarget,
                group  = EditorUserBuildSettings.selectedBuildTargetGroup
            };

            var compilationResult = PlayerBuildInterface.CompilePlayerScripts(input, m_OutputFolder);

            if (progressBar != null)
            {
                progressBar.ClearProgressBar();
            }

            if (!m_Success)
            {
                Dispose();
                throw new AssemblyCompilationException();
            }

            return(compilationResult.assemblies.Select(assembly => Path.Combine(m_OutputFolder, assembly)));
        }
        public void Audit(Action <ProjectIssue> onIssueFound, Action onComplete, IProgressBar progressBar = null)
        {
            if (m_ProblemDescriptors == null)
            {
                throw new Exception("Issue Database not initialized.");
            }

            if (progressBar != null)
            {
                progressBar.Initialize("Analyzing Settings", "Analyzing project settings", m_ProblemDescriptors.Count);
            }

            foreach (var descriptor in m_ProblemDescriptors)
            {
                if (progressBar != null)
                {
                    progressBar.AdvanceProgressBar();
                }

                if (m_SettingsAnalyzers.ContainsKey(descriptor.id))
                {
                    var analyzer     = m_SettingsAnalyzers[descriptor.id];
                    var projectIssue = analyzer.Analyze();
                    if (projectIssue != null)
                    {
                        onIssueFound(projectIssue);
                    }
                }
                else
                {
                    SearchAndEval(descriptor, onIssueFound);
                }
            }

            if (progressBar != null)
            {
                progressBar.ClearProgressBar();
            }

            onComplete();
        }
Example #8
0
        public void Audit(ProjectReport projectReport, IProgressBar progressBar = null)
        {
            if (progressBar != null)
            {
                progressBar.Initialize("Analyzing Settings", "Analyzing project settings", m_ProblemDescriptors.Count);
            }

            foreach (var descriptor in m_ProblemDescriptors)
            {
                if (progressBar != null)
                {
                    progressBar.AdvanceProgressBar();
                }

                SearchAndEval(descriptor, projectReport);
            }
            if (progressBar != null)
            {
                progressBar.ClearProgressBar();
            }
        }
        IEnumerable <string> CompileAssemblies(Assembly[] assemblies, IProgressBar progressBar = null)
        {
            if (progressBar != null)
            {
                var numAssemblies = assemblies.Length;
                progressBar.Initialize("Assembly Compilation", "Compiling project scripts",
                                       numAssemblies);
            }

            m_OutputFolder = FileUtil.GetUniqueTempPathInProject();

            if (!Directory.Exists(m_OutputFolder))
            {
                Directory.CreateDirectory(m_OutputFolder);
            }

            PrepareAssemblyBuilders(assemblies, (assemblyPath, messages) =>
            {
                var assemblyName = Path.GetFileNameWithoutExtension(assemblyPath);
                m_AssemblyCompilationUnits[assemblyName].messages = messages;

                if (progressBar != null)
                {
                    progressBar.AdvanceProgressBar(assemblyName);
                }

                if (AssemblyCompilationFinished != null)
                {
                    AssemblyCompilationFinished(assemblyName, messages);
                }
            });
            UpdateAssemblyBuilders();

            if (progressBar != null)
            {
                progressBar.ClearProgressBar();
            }

            return(m_AssemblyCompilationUnits.Where(pair => pair.Value.Success()).Select(unit => unit.Value.assemblyPath));
        }
Example #10
0
        public void BuildCallHierarchies(ProjectReport projectReport, IProgressBar progressBar = null)
        {
            foreach (var entry in m_CallPairs)
            {
                if (!m_BucketedCallPairs.ContainsKey(entry.Value.callee.FullName))
                {
                    m_BucketedCallPairs.Add(entry.Value.callee.FullName, new List <CallPair>());
                }
                m_BucketedCallPairs[entry.Value.callee.FullName].Add(entry.Value);
            }

            var numIssues = projectReport.GetNumIssues(IssueCategory.ApiCalls);

            if (numIssues > 0)
            {
                var issues = projectReport.GetIssues(IssueCategory.ApiCalls);
                if (progressBar != null)
                {
                    progressBar.Initialize("Analyzing Scripts", "Analyzing call trees", numIssues);
                }

                foreach (var issue in issues)
                {
                    if (progressBar != null)
                    {
                        progressBar.AdvanceProgressBar();
                    }

                    int depth = 0;
                    BuildHierarchy(issue.callTree.GetChild(), depth);
                }
                if (progressBar != null)
                {
                    progressBar.ClearProgressBar();
                }
            }
        }
        public IEnumerable <AssemblyInfo> Compile(IProgressBar progressBar = null)
        {
#if UNITY_2018_1_OR_NEWER
            var assemblies = CompilationPipeline.GetAssemblies(AssembliesType.Player);
#else
            var assemblies = CompilationPipeline.GetAssemblies();
#endif

#if UNITY_2018_2_OR_NEWER
            if (progressBar != null)
            {
                var numAssemblies = assemblies.Length;
                progressBar.Initialize("Assembly Compilation", "Compiling project scripts",
                                       numAssemblies);
                m_OnAssemblyCompilationStarted = (s) =>
                {
                    progressBar.AdvanceProgressBar(Path.GetFileName(s));
                };
                CompilationPipeline.assemblyCompilationStarted += m_OnAssemblyCompilationStarted;
            }
            CompilationPipeline.assemblyCompilationFinished += OnAssemblyCompilationFinished;

            m_OutputFolder = FileUtil.GetUniqueTempPathInProject();

            var input = new ScriptCompilationSettings
            {
                target = EditorUserBuildSettings.activeBuildTarget,
                group  = EditorUserBuildSettings.selectedBuildTargetGroup
            };

            var compilationResult = PlayerBuildInterface.CompilePlayerScripts(input, m_OutputFolder);

            if (progressBar != null)
            {
                progressBar.ClearProgressBar();
            }

            if (!m_Success)
            {
                Dispose();
                throw new AssemblyCompilationException();
            }

            var compiledAssemblyPaths = compilationResult.assemblies.Select(assembly => Path.Combine(m_OutputFolder, assembly));
#else
            // fallback to CompilationPipeline assemblies
            var compiledAssemblyPaths = CompilationPipeline.GetAssemblies()
                                        .Where(a => a.flags != AssemblyFlags.EditorAssembly).Select(assembly => assembly.outputPath);
#endif

            var assemblyInfos = new List <AssemblyInfo>();
            foreach (var compiledAssemblyPath in compiledAssemblyPaths)
            {
                var assemblyInfo = AssemblyHelper.GetAssemblyInfoFromAssemblyPath(compiledAssemblyPath);
                var assembly     = assemblies.First(a => a.name.Equals(assemblyInfo.name));
                var sourcePaths  = assembly.sourceFiles.Select(file => file.Remove(0, assemblyInfo.relativePath.Length + 1));

                assemblyInfo.sourcePaths = sourcePaths.ToArray();
                assemblyInfos.Add(assemblyInfo);
            }

            return(assemblyInfos);
        }