Exemple #1
0
        private static void ExcludeSubFoldersOfEmptyFolders(ref List <string> emptyFolders)
        {
            var emptyFoldersFiltered = new List <string>(emptyFolders.Count);

            for (var i = emptyFolders.Count - 1; i >= 0; i--)
            {
                var folder = emptyFolders[i];

                if (CSFilterTools.HasEnabledFilters(ProjectSettings.Cleaner.pathIncludesFilters))
                {
                    var emptyFolder = CSPathTools.GetProjectRelativePath(folder);
                    if (!CSFilterTools.IsValueMatchesAnyFilter(emptyFolder, ProjectSettings.Cleaner.pathIncludesFilters))
                    {
                        continue;
                    }
                }

                if (!CSArrayTools.IsItemContainsAnyStringFromArray(folder, emptyFoldersFiltered))
                {
                    emptyFoldersFiltered.Add(folder);
                }
            }
            emptyFolders = emptyFoldersFiltered;
        }
Exemple #2
0
        /////////////////////////////////////////////////////////////////////////
        // Game Object's Components Processing
        /////////////////////////////////////////////////////////////////////////

        public static void ProcessComponent(Component component, int orderIndex)
        {
#if !UNITY_2019_1_OR_NEWER
            if (missingComponentDetector.CheckAndRecordNullComponent(component))
            {
                return;
            }
#else
            if (component == null)
            {
                return;
            }
#endif

            if ((component.hideFlags & HideFlags.HideInInspector) != 0)
            {
                return;
            }

            if (!ProjectSettings.Issues.touchDisabledComponents)
            {
                if (EditorUtility.GetObjectEnabled(component) == 0)
                {
                    return;
                }
            }

            var componentType = component.GetType();
            var componentName = componentType.Name;

            if (CSFilterTools.HasEnabledFilters(ProjectSettings.Issues.componentIgnoresFilters))
            {
                if (CSFilterTools.IsValueMatchesAnyFilterOfKind(componentName, ProjectSettings.Issues.componentIgnoresFilters, FilterKind.Type))
                {
                    return;
                }
            }

            duplicateComponentDetector.ProcessComponent(component, componentType);

            var shouldCheckPropertiesForDuplicate = duplicateComponentDetector.IsPropertiesHashCalculationRequired();
            if (shouldCheckPropertiesForDuplicate)
            {
                // skipping duplicate search for non-standard components with invisible properties
                var baseType = componentType.BaseType;
                if (baseType != null)
                {
                    if (baseType.Name == "MegaModifier")
                    {
                        shouldCheckPropertiesForDuplicate = false;
                        duplicateComponentDetector.SkipComponent();
                    }
                }
            }

            var shouldTraverseProperties = missingReferenceDetector.Enabled || shouldCheckPropertiesForDuplicate;
            if (shouldTraverseProperties)
            {
                var initialInfo = new SerializedObjectTraverseInfo(component);

                CSTraverseTools.TraverseObjectProperties(initialInfo, (info, property) =>
                {
                    if (property.type == "UnityEvent")
                    {
                        missingReferenceDetector.TryDetectUnityEventIssues(currentLocation, currentAsset.Path,
                                                                           currentGameObject, componentType, componentName, orderIndex, property);

                        info.skipCurrentTree = true;
                        return;
                    }

                    missingReferenceDetector.TryDetectIssue(currentLocation, currentAsset.Path, currentGameObject, componentType, componentName, orderIndex, property);

                    if (shouldCheckPropertiesForDuplicate)
                    {
                        duplicateComponentDetector.ProcessProperty(property);
                    }
                });
            }

            if (shouldCheckPropertiesForDuplicate)
            {
                duplicateComponentDetector.TryDetectIssue(currentLocation, currentAsset.Path, currentGameObject, componentType, componentName, orderIndex);
            }

            if (component is Terrain)
            {
                inconsistentTerrainDataDetector.ProcessTerrainComponent((Terrain)component, componentType, componentName, orderIndex);
            }
            else if (component is TerrainCollider)
            {
                inconsistentTerrainDataDetector.ProcessTerrainColliderComponent((TerrainCollider)component);
            }
            //Debug.Log("ProcessComponent: " + target.name + ":" + component);
        }
Exemple #3
0
        private static bool ScanProjectFiles(ICollection <CleanerRecord> results, bool showProgress = true)
        {
            currentPhase++;

            var ignoredScenes = new List <string>();

            if (ProjectSettings.Cleaner.ignoreScenesInBuild)
            {
                ignoredScenes.AddRange(CSSceneTools.GetScenesInBuild(!ProjectSettings.Cleaner.ignoreOnlyEnabledScenesInBuild));
            }

            foreach (var sceneFilter in ProjectSettings.Cleaner.sceneIgnoresFilters)
            {
                if (!sceneFilter.enabled)
                {
                    continue;
                }

                if (ignoredScenes.IndexOf(sceneFilter.value) == -1)
                {
                    ignoredScenes.Add(sceneFilter.value);
                }
            }

            CheckScenesForExistence(results, ignoredScenes);

            if (ignoredScenes.Count == 0)
            {
                if (!UserSettings.Cleaner.muteNoIgnoredScenesWarning)
                {
                    var dialogResult = EditorUtility.DisplayDialogComplex(
                        "No ignored scenes!",
                        "No scenes were added to the build settings or to the Filters > Scenes Ignores.\n" +
                        "All not ignored scenes are treated as unused if not referenced somewhere in other ignored assets.",
                        "Proceed anyway",
                        "Proceed and never show again", "Cancel");

                    if (dialogResult == 1)
                    {
                        UserSettings.Cleaner.muteNoIgnoredScenesWarning = true;
                        UserSettings.Save();
                    }
                    else if (dialogResult == 2)
                    {
                        return(false);
                    }
                }

                results.Add(CleanerWarningRecord.Create("No scenes added to the build settings or Filters > Scenes Ignores tab.\n" +
                                                        "Search may include all assets used in your game scenes!"));
            }

            var map = AssetsMap.GetUpdated();

            if (map == null)
            {
                results.Add(CleanerErrorRecord.Create("Can't get assets map!"));
                return(false);
            }

            EditorUtility.DisplayCancelableProgressBar(string.Format(ProgressCaption, currentPhase, phasesCount, 0, 0), "Analyzing Assets Map for references...", 0);

#if UNITY_2019_3_OR_NEWER
            BuildReportAnalyzer.Init();
#endif

            var allAssetsInProject = map.assets;
            var count = allAssetsInProject.Count;

#if !UNITY_2020_1_OR_NEWER
            var updateStep = Math.Max(count / ProjectSettings.UpdateProgressStep, 1);
#endif
            var referencedAssets = new HashSet <AssetInfo>();

            for (var i = 0; i < count; i++)
            {
                if (showProgress
#if !UNITY_2020_1_OR_NEWER
                    && i % updateStep == 0
#endif
                    && i != 0 && EditorUtility.DisplayCancelableProgressBar(
                        string.Format(ProgressCaption, currentPhase, phasesCount, i + 1, count), "Analyzing Assets Map for references...",
                        (float)(i + 1) / count))
                {
                    return(true);
                }

                var asset = allAssetsInProject[i];

                /*if (asset.Path.EndsWith("1.pdf"))
                 * {
                 *      Debug.Log(asset.Type);
                 *      if (asset.Type == CSReflectionTools.defaultAssetType)
                 *      {
                 *              var importer = AssetImporter.GetAtPath(asset.Path);
                 *              Debug.Log(importer);
                 *      }
                 * }*/

                if (asset.Kind != AssetKind.Regular)
                {
                    continue;
                }

                if (AssetInIgnores(asset, ignoredScenes))
                {
                    referencedAssets.Add(asset);
                    var references = asset.GetReferencesRecursive();
                    foreach (var reference in references)
                    {
                        referencedAssets.Add(reference);
                    }
                }

#if UNITY_2019_3_OR_NEWER
                if (BuildReportAnalyzer.IsFileInBuildReport(asset.GUID))
                {
                    referencedAssets.Add(asset);
                    var references = asset.GetReferencesRecursive();
                    foreach (var reference in references)
                    {
                        if (!referencedAssets.Contains(reference))
                        {
                            referencedAssets.Add(reference);
                        }
                    }
                }
#endif

                if (AssetInIgnoresSecondPass(asset, referencedAssets))
                {
                    referencedAssets.Add(asset);
                    var references = asset.GetReferencesRecursive();
                    foreach (var reference in references)
                    {
                        if (!referencedAssets.Contains(reference))
                        {
                            referencedAssets.Add(reference);
                        }
                    }
                }
            }

            var unreferencedAssets = new List <AssetInfo>(count);
            for (var i = 0; i < count; i++)
            {
                if (showProgress
#if !UNITY_2020_1_OR_NEWER
                    && i % updateStep == 0
#endif
                    && i != 0 && EditorUtility.DisplayCancelableProgressBar(
                        string.Format(ProgressCaption, currentPhase, phasesCount, i + 1, count), "Filtering out unreferenced assets...",
                        (float)(i + 1) / count))
                {
                    return(true);
                }

                var asset = allAssetsInProject[i];

                if (asset.Kind != AssetKind.Regular)
                {
                    continue;
                }

                if (CSFilterTools.HasEnabledFilters(ProjectSettings.Cleaner.pathIncludesFilters))
                {
                    if (!CSFilterTools.IsValueMatchesAnyFilter(asset.Path, ProjectSettings.Cleaner.pathIncludesFilters))
                    {
                        continue;
                    }
                }

                if (!referencedAssets.Contains(asset))
                {
                    if (unreferencedAssets.IndexOf(asset) == -1)
                    {
                        unreferencedAssets.Add(asset);
                    }
                }
            }

            count = unreferencedAssets.Count;

#if !UNITY_2020_1_OR_NEWER
            updateStep = Math.Max(count / ProjectSettings.UpdateProgressStep, 1);
#endif

            for (var i = count - 1; i > -1; i--)
            {
                if (showProgress
#if !UNITY_2020_1_OR_NEWER
                    && i % updateStep == 0
#endif
                    && i != 0)
                {
                    var index = count - i;
                    if (EditorUtility.DisplayCancelableProgressBar(
                            string.Format(ProgressCaption, currentPhase, phasesCount, index, count), "Populating results...",
                            (float)index / count))
                    {
                        return(true);
                    }
                }

                var unreferencedAsset = unreferencedAssets[i];
                results.Add(AssetRecord.Create(RecordType.UnreferencedAsset, unreferencedAsset));
            }

            return(false);
        }
        private static bool LookForAssetsReferences(FilterItem[] selectedAssets, List <ProjectReferenceItem> results)
        {
            var canceled = !CSSceneTools.SaveCurrentModifiedScenes(false);

            if (!canceled)
            {
                var map = AssetsMap.GetUpdated();
                if (map == null)
                {
                    return(true);
                }

                var count = map.assets.Count;

#if !UNITY_2020_1_OR_NEWER
                var updateStep = Math.Max(count / ProjectSettings.UpdateProgressStep, 1);
#endif
                var root = new ProjectReferenceItem
                {
                    id    = results.Count,
                    name  = "root",
                    depth = -1
                };
                results.Add(root);

                for (var i = 0; i < count; i++)
                {
                    if (
#if !UNITY_2020_1_OR_NEWER
                        i % updateStep == 0 &&
#endif
                        EditorUtility.DisplayCancelableProgressBar(
                            string.Format(ReferencesFinder.ProgressCaption, 1, ReferencesFinder.PhasesCount),
                            string.Format(ReferencesFinder.ProgressText, "Building references tree", i + 1, count),
                            (float)i / count))
                    {
                        canceled = true;
                        break;
                    }

                    var assetInfo = map.assets[i];

                    // excludes settings assets from the list depth 0 items
                    if (assetInfo.Kind == AssetKind.Settings)
                    {
                        continue;
                    }

                    // excludes all assets except selected ones from the list depth 0 items, if any was selected
                    if (selectedAssets != null)
                    {
                        if (!CSFilterTools.IsValueMatchesAnyFilter(assetInfo.Path, selectedAssets))
                        {
                            continue;
                        }
                    }

                    if (CSFilterTools.HasEnabledFilters(ProjectSettings.References.pathIncludesFilters))
                    {
                        // excludes all root assets except included ones
                        if (!CSFilterTools.IsValueMatchesAnyFilter(assetInfo.Path, ProjectSettings.References.pathIncludesFilters))
                        {
                            continue;
                        }
                    }

                    // excludes ignored root asset
                    if (CSFilterTools.IsValueMatchesAnyFilter(assetInfo.Path, ProjectSettings.References.pathIgnoresFilters))
                    {
                        continue;
                    }

                    var branchElements = new List <ProjectReferenceItem>();
                    ProjectScopeReferencesTreeBuilder.BuildTreeBranch(assetInfo, 0, results.Count, ConjunctionInfoList, branchElements);
                    results.AddRange(branchElements);
                }
            }

            if (!canceled)
            {
                canceled = ProjectEntryFinder.FillProjectScopeReferenceEntries(ConjunctionInfoList, TryAddEntryToMatchedConjunctions);
            }

            // TODO: remove this work-around when this bug will be fixed:
            // https://issuetracker.unity3d.com/issues/assets-used-in-components-of-a-nested-prefab-are-counted-as-direct-dependencies-of-all-higher-level-nested-prefabs
            for (var i = results.Count - 1; i >= 0; i--)
            {
                var result        = results[i];
                var resultEntries = result.referencingEntries;
                if (resultEntries == null || resultEntries.Length == 0)
                {
                    continue;
                }

                foreach (var referencingEntry in resultEntries)
                {
                    if (referencingEntry.Location == Location.NotFound && result.assetTypeName == "GameObject")
                    {
                        results.Remove(result);
                        break;
                    }
                }
            }

            if (!canceled)
            {
                AssetsMap.Save();
            }

            if (canceled)
            {
                ProjectReferencesTab.AutoSelectPath = null;
            }

            return(canceled);
        }