示例#1
0
 private static void     RestorePendingFiles()
 {
     foreach (string file in AssetFinderCache.pendingFiles)
     {
         AssetFinderCache.ExtractReferences(file, true);
     }
     AssetFinderCache.pendingFiles.Clear();
 }
示例#2
0
        public static void              UpdateFile(string file)
        {
            if (AssetFinderCache.pendingFiles.Contains(file) == false)
            {
                AssetFinderCache.ExtractReferences(file, false);
                AssetFinderCache.pendingFiles.Add(file);
            }

            EditorApplication.delayCall += AssetFinderCache.RestorePendingFiles;
        }
示例#3
0
        private static string[]         OnWillSaveAssets(string[] paths)
        {
            if (AssetFinderCache.usages == null)
            {
                return(paths);
            }

            foreach (string file in paths)
            {
                AssetFinderCache.ExtractReferences(file, false);

                if (AssetFinderCache.pendingFiles.Contains(file) == false)
                {
                    AssetFinderCache.pendingFiles.Add(file);
                }
            }

            EditorApplication.delayCall += AssetFinderCache.RestorePendingFiles;

            return(paths);
        }
示例#4
0
        public void     ReplaceReferencesInScene(SearchResult result, NGAssetFinderWindow window)
        {
            string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(window.targetAsset));
            string id   = Utility.GetLocalIdentifierFromObject(window.targetAsset) + ", guid: " + guid;

            if (string.IsNullOrEmpty(id) == true)
            {
                return;
            }

            string newGuid = string.Empty;
            string newID   = null;

            if (window.replaceAsset != null)
            {
                newGuid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(window.replaceAsset));
                newID   = Utility.GetLocalIdentifierFromObject(window.replaceAsset) + ", guid: " + newGuid;
            }

            if (this.prefabCount > 0 && newID == null)
            {
                if (EditorUtility.DisplayDialog(NGAssetFinderWindow.Title, "You are replacing prefabs with nothing.\nPrefabs will be destroyed and they will be replace with \"Missing Prefab\" in the scene.", "Continue", "Cancel") == false)
                {
                    return;
                }
            }

            string[] lines = File.ReadAllLines(this.scenePath);
            bool     searchingComponentType = (window.replaceAsset is Component || window.replaceAsset is MonoScript);

            for (int i = 0; i < lines.Length; i++)
            {
                string line = lines[i];

                if (line.Length < 11 + 8 + 32 + 1)                 // {fileID: , guid: }
                {
                    continue;
                }

                int position = line.IndexOf(" {fileID: ");
                if (position == -1)
                {
                    continue;
                }

                // References of scripts.
                if (line.StartsWith("  m_Script: {fileID: ") == true)
                {
                    if (newID != null && searchingComponentType == true && line.IndexOf(id, "  m_Script: {fileID: ".Length) != -1)
                    {
                        lines[i] = line.Replace(id, newID);
                        --this.count;
                        ++result.updatedReferencesCount;
                        continue;
                    }
                }
                // References of prefabs.
                else if (line.StartsWith("  m_ParentPrefab: {fileID: ") == true)
                {
                    if (line.IndexOf(guid) != -1)
                    {
                        if (newID == null)
                        {
                            lines[i] = line.Substring(0, line.IndexOf("{fileID: ")) + "{fileID: 0}";
                        }
                        else
                        {
                            lines[i] = line.Replace(guid, newGuid);
                        }
                        --this.prefabCount;
                        ++result.updatedReferencesCount;
                        continue;
                    }
                }
                // Modifications of prefabs.
                else if (line.StartsWith("    - target: {fileID: ") == false)
                {
                    if (newID == null)
                    {
                        lines[i] = line.Substring(0, line.IndexOf("{fileID: ")) + "{fileID: 0}";
                    }
                    else
                    {
                        lines[i] = line.Replace(guid, newGuid);
                    }
                    --this.prefabModificationCount;
                    ++result.updatedReferencesCount;
                    continue;
                }
                // References in script.
                else if (line.IndexOf(id, 11) != -1)
                {
                    if (newID == null)
                    {
                        lines[i] = line.Substring(0, line.IndexOf("{fileID: ")) + "{fileID: 0}";
                    }
                    else
                    {
                        lines[i] = line.Replace(id, newID);
                    }
                    --this.count;
                    ++result.updatedReferencesCount;
                    continue;
                }
            }

            this.PrepareResults();

            AssetFinderCache.UpdateFile(this.scenePath);
            File.WriteAllLines(this.scenePath, lines);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
        }
示例#5
0
        public static void      CacheProjectReferences(bool showProgress = true)
        {
            AssetDatabase.SaveAssets();
            AssetFinderCache.RestorePendingFiles();

            if (AssetFinderCache.usages != null)
            {
                return;
            }

            try
            {
                if (showProgress == true)
                {
                    EditorUtility.DisplayProgressBar(NGAssetFinderWindow.Title + " - Project", "Loading cache from disk...", 0F);
                }

                if (AssetFinderCache.LoadCache(AssetFinderCache.GetCachePath()) == false)
                {
                    AssetFinderCache.usagesFiles = new Dictionary <string, string[]>();
                }

                AssetFinderCache.usages = new Dictionary <string, List <string> >((int)(AssetFinderCache.usagesFiles.Count * .375F));              // Approximation of an average used assets.

                EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();

                if (showProgress == true)
                {
                    EditorUtility.DisplayProgressBar(NGAssetFinderWindow.Title + " - Project", "Fetching all asset paths...", 0F);
                }

                double   time      = EditorApplication.timeSinceStartup;
                string[] assets    = AssetDatabase.GetAllAssetPaths();
                int      cacheHits = 0;

                for (int i = 0; i < assets.Length; i++)
                {
                    if ((assets[i].StartsWith("Assets/") == false &&
                         assets[i].StartsWith("ProjectSettings/") == false &&
                         assets[i].StartsWith("Library/") == false) ||
                        Directory.Exists(assets[i]) == true)
                    {
                        continue;
                    }

                    if (showProgress == true && (i % 18) == 0)
                    {
                        string progressBarTitle   = NGAssetFinderWindow.Title + " - Project (" + (i + 1) + " / " + assets.Length + ")";
                        string progressBarContent = "Caching...";
                        float  progressBarRate    = (float)(i + 1) / (float)assets.Length;

                        if (EditorUtility.DisplayCancelableProgressBar(progressBarTitle, progressBarContent, progressBarRate) == true)
                        {
                            throw new BreakException();
                        }
                    }

                    string[] IDs;
                    long     ticks = -1;

                    if (AssetFinderCache.usagesFiles.TryGetValue(assets[i], out IDs) == true)
                    {
                        ticks = File.GetLastWriteTime(assets[i]).Ticks;
                        if (ticks == long.Parse(IDs[0]))
                        {
                            ++cacheHits;

                            for (int j = 1; j < IDs.Length; j++)
                            {
                                List <string> cache;

                                if (AssetFinderCache.usages.TryGetValue(IDs[j], out cache) == false)
                                {
                                    cache = new List <string>();
                                    AssetFinderCache.usages.Add(IDs[j], cache);
                                }

                                if (cache.Contains(assets[i]) == false)
                                {
                                    cache.Add(assets[i]);
                                }
                            }

                            continue;
                        }
                        else
                        {
                            AssetFinderCache.usagesFiles.Remove(assets[i]);
                        }
                    }

                    AssetFinderCache.ExtractReferences(assets[i], true, ticks);
                }

                InternalNGDebug.VerboseLogFormat("[AssetFinderCache] Constructed cache in {0} seconds. ({1} cache hits, {2} elements)", EditorApplication.timeSinceStartup - time, cacheHits, AssetFinderCache.usages.Count);
            }
            finally
            {
                if (showProgress == true)
                {
                    EditorUtility.ClearProgressBar();
                }
            }
        }
示例#6
0
 public static void      ClearCache()
 {
     AssetFinderCache.usages      = null;
     AssetFinderCache.usagesFiles = null;
     File.Delete(AssetFinderCache.GetCachePath());
 }