Esempio n. 1
0
        public void Start(string targetPath, bool isRecursive, ResultCallback callback)
        {
            if (!IsInitialized)
            {
                return;
            }
            if (string.IsNullOrEmpty(targetPath))
            {
                return;
            }
            if (targetPath.StartsWith("Packages/"))
            {
                return;
            }

            // var callbackTarget = callback.Target as UnityObject;
            // if (callbackTarget == null)
            // {
            //     throw new Exception("callback.Target is not UnityObject");
            // }
            lock (sync)
            {
                CancelImpl();
                EnqueueProcess(targetPath, isRecursive, callback);
                StartWorker();
            }
            AssetDependencyDatabase.SetScanPaused(true);
            AssetDependencyDatabase.EnqueueScanDependency(targetPath);
        }
Esempio n. 2
0
        // void OnStdout(object sender, System.Diagnostics.DataReceivedEventArgs e)
        // {
        //     if (string.IsNullOrEmpty(e.Data)) return;
        //     foreach (var line in e.Data.Split('\n'))
        //     {
        //         Debug.Log(line);
        //         OnStdoutLine(line.Trim());
        //     }
        // }

        void OnStdoutLine(string line)
        {
            // ReferenceSearchEngine.Log(line);
            var i = FindValue(line, 0, "type");

            if (!IsValue(line, i, "match"))
            {
                return;
            }

            i = FindValue(line, 0, "data");
            i = FindValue(line, i, "path");
            i = FindValue(line, i, "text");
            var path = GetValue(line, i);

            if (path == null)
            {
                return;
            }
            path = path.Replace("\\\\", "\\").Replace('\\', '/');
            if (path.StartsWith(ReferenceSearchEngine.Prefix))
            {
                path = path.Substring(ReferenceSearchEngine.Prefix.Length);
            }

            i = FindValue(line, 0, "submatches");
            i = FindValue(line, i, "match");
            i = FindValue(line, i, "text");
            var    m          = GetValue(line, i);
            string targetPath = null;

            for (var j = 0; j < TargetGUIDs.Count; ++j)
            {
                var guid = TargetGUIDs[j];
                if (guid == m)
                {
                    targetPath = TargetPaths[j];
                }
            }
            lock (Results)
            {
                if (Results.Contains(path))
                {
                    return;
                }
                Results.Add(path);
            }
            if (targetPath != null)
            {
                Reporter.FoundReference(this, path, targetPath);
                AssetDependencyDatabase.InsertDependencyPath(path, targetPath);
            }
        }
Esempio n. 3
0
 void ReadProcessOutput()
 {
     try
     {
         IsSearching = true;
         while (true)
         {
             if (Thread.CurrentThread.ManagedThreadId != workerId)
             {
                 return;
             }
             ReferenceSearchEngineProcess p;
             lock (sync)
             {
                 if (processQueue.Count == 0)
                 {
                     AssetDependencyDatabase.SetScanPaused(false);
                     return;
                 }
                 p = processQueue[0];
                 processQueue.RemoveAt(0);
                 currentProcess = p;
                 p.StartProcess();
             }
             if (Thread.CurrentThread.ManagedThreadId != workerId)
             {
                 return;
             }
             p.WaitForComplete();
             if (Thread.CurrentThread.ManagedThreadId != workerId)
             {
                 return;
             }
             lock (sync)
             {
                 currentProcess = null;
             }
         }
     }
     catch (ThreadAbortException)
     {
     }
     catch (System.Exception e)
     {
         Log(e.ToString());
     }
     finally
     {
         IsSearching = false;
     }
 }
Esempio n. 4
0
        void OnEnable()
        {
            titleContent.text = "References";

            objectList            = new ScrollableObjectList();
            objectList.OnPostDraw = OnPostDrawResultItem;
            beforeTarget          = null;
            if (engine != null)
            {
                engine.Cancel();
            }
            var root = Path.GetFullPath(Application.dataPath + "/..");

            engine = new ReferenceSearchEngine(root);

            ReferenceSearchEngine.Tracer  = s => Debug.Log(s);
            AssetDependencyDatabase.Trace = s => Debug.Log(s);

            AssetDependencyDatabase.StartFullScanWorker();
        }
Esempio n. 5
0
 public void FoundReference(ReferenceSearchEngineProcess process, string objectPath, string targetPath)
 {
     // var o = process.Callback.Target as UnityObject;
     // if (o == null)
     // {
     //     return;
     // }
     if (objectPath.StartsWith(Root))
     {
         objectPath = objectPath.Substring(objectPath.Length + 1);
     }
     process.Callback(objectPath);
     if (process.IsRecursive && !targetPath.StartsWith("ProjectSettings/"))
     {
         EnqueueProcess(objectPath, process.IsRecursive, process.Callback, process.Results);
     }
     if (process.Results.Count > 5)
     {
         AssetDependencyDatabase.SetScanPaused(false);
     }
     AssetDependencyDatabase.EnqueueScanDependency(objectPath);
 }
Esempio n. 6
0
        public void AddItemsToMenu(GenericMenu menu)
        {
            menu.AddItem(new GUIContent("Lock #q"), isLocked, () =>
            {
                isLocked     = !isLocked;
                beforeTarget = null;
            });
            menu.AddItem(new GUIContent("Recursive #r"), isRecursive, () =>
            {
                isRecursive = !isRecursive;
            });
            menu.AddSeparator("");

            menu.AddItem(new GUIContent("Copy Result"), false, () =>
            {
                var sb = new StringBuilder();
                foreach (var file in objectList)
                {
                    sb.Append(file);
                    sb.Append('\n');
                }
                EditorGUIUtility.systemCopyBuffer = sb.ToString();
            });
            menu.AddItem(new GUIContent("Sort Result"), false, () =>
            {
                objectList.Sort();
            });
            menu.AddItem(new GUIContent("Clear Cache"), false, () =>
            {
                AssetDependencyDatabase.Truncate();
            });
            menu.AddItem(new GUIContent("Start Full Scan"), false, () =>
            {
                AssetDependencyDatabase.StartFullScanWorker();
            });
        }
Esempio n. 7
0
        void OnGUI()
        {
            OnKeyEvent();
            var e = Event.current;

            EditorGUIUtility.labelWidth = 100f;

            if (Target == null && !string.IsNullOrEmpty(guid) && guid.Length == 32)
            {
                var assetPath = AssetDatabase.GUIDToAssetPath(guid);
                if (!string.IsNullOrEmpty(assetPath))
                {
                    Target     = AssetDatabase.LoadAssetAtPath(assetPath, typeof(UnityObject));
                    targetPath = assetPath;
                }
            }
            Target = EditorGUILayout.ObjectField(Target, typeof(UnityObject), false);
            if (Target != null && AssetMenu.IsMouseDownInLastRect())
            {
                AssetMenu.PopupObjectHelperMenu(Target);
            }
            if (Target == null)
            {
                targetPath = null;
            }
            else
            {
                targetPath = AssetDatabase.GetAssetPath(Target);
                guid       = AssetDatabase.AssetPathToGUID(targetPath);
            }
            var newGUID = EditorGUILayout.TextField("GUID", guid ?? "");

            if (newGUID != guid)
            {
                guid       = newGUID;
                Target     = null;
                targetPath = null;
            }
            EditorGUILayout.TextField("Path", targetPath ?? "");

            if (Target != null && Target != beforeTarget)
            {
                beforeTarget = Target;
                objectList.Clear();
                engine.Start(targetPath, isRecursive, AddObjectList);
            }

            EditorGUILayout.BeginHorizontal();
            if (!engine.IsInitialized)
            {
                EditorGUILayout.LabelField("initialize ...");
            }
            else
            {
                EditorGUILayout.LabelField(string.Format("references: {0} {1}", objectList.Count, engine.IsSearching ? "..." : ""));
            }
            if (AssetDependencyDatabase.IsFullScanRunning())
            {
                EditorGUILayout.LabelField(string.Format("scan: {0}", AssetDependencyDatabase.GetScanQueueCount()));
            }
            else
            {
                EditorGUILayout.LabelField("");
            }
            EditorGUILayout.EndHorizontal();

            objectList.Draw();

            AssetMenu.DequeueContextAction();
        }
Esempio n. 8
0
 void OnDisable()
 {
     AssetDependencyDatabase.Close();
 }
Esempio n. 9
0
        public static void PopupObjectHelperMenu(UnityObject o)
        {
            var path = AssetDatabase.GetAssetPath(o);
            var guid = AssetDatabase.AssetPathToGUID(path);
            var iid  = o.GetInstanceID();

            if (getLocalIdentifierInFile == null)
            {
                var method = typeof(Unsupported).GetMethod(
                    "GetLocalIdentifierInFile",
                    BindingFlags.Static | BindingFlags.Public);
                if (method != null)
                {
                    getLocalIdentifierInFile = (Func <int, int>)Delegate.CreateDelegate(typeof(Func <int, int>), method);
                }
                if (getLocalIdentifierInFile == null)
                {
                    getLocalIdentifierInFile = _ => 0;
                }
            }
            var localId = getLocalIdentifierInFile(iid);

            var info     = new FileInfo(path);
            var name     = Path.GetFileName(path);
            var itemPath = ToMenuItemPath(path);

            var menu = new GenericMenu();

            menu.AddItem(new GUIContent(name + "   " + info.Length.ToString("N0") + " B   " + info.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")), false, null);
            menu.AddSeparator("");
            var deps = AssetDatabase.GetDependencies(path, false);

            System.Array.Sort(deps);
            var limit = 30;

            foreach (var depPath in deps)
            {
                var target = AssetDatabase.LoadAssetAtPath(depPath, typeof(UnityObject));
                menu.AddItem(new GUIContent("Dependencies/" + ToMenuItemPath(depPath)), false, t => PopupObjectHelperMenu(t as UnityObject), target);
                if (--limit < 0)
                {
                    break;
                }
            }
            menu.AddItem(new GUIContent("Dependencies/..."), false, DependencyListWindow.Open, o);

            var refs = AssetDependencyDatabase.GetReferences(path);

            refs.Sort();
            limit = 30;
            foreach (var refPath in refs)
            {
                var target = AssetDatabase.LoadAssetAtPath(refPath, typeof(UnityObject));
                if (target == null)
                {
                    continue;
                }
                menu.AddItem(new GUIContent("References/" + ToMenuItemPath(refPath)), false, t => PopupObjectHelperMenu(t as UnityObject), target);
                if (--limit < 0)
                {
                    break;
                }
            }
            menu.AddItem(new GUIContent("References/..."), false, ReferenceGrepWindow.Open, o);

            menu.AddSeparator("");
            menu.AddItem(new GUIContent("Copy " + itemPath), false, CopyToClipboard, path);
            menu.AddItem(new GUIContent("Copy GUID " + guid), false, CopyToClipboard, guid);
            menu.AddItem(new GUIContent("Copy InstanceID " + iid), false, CopyToClipboard, iid);
            if (localId != 0)
            {
                menu.AddItem(new GUIContent("Copy LocalID " + localId), false, CopyToClipboard, localId);
            }
            menu.AddItem(new GUIContent("Open In Finder"), false, OpenInFinder, path);
            menu.AddItem(new GUIContent("Open In Editor"), false, OpenInEditor, path);
            menu.AddItem(new GUIContent("Find References In Scene"), false, FindReferencesInScean, o);
            if (Event.current == null)
            {
                EditorGUIUtility.PingObject(o);
                Selection.activeObject = o;
                actionQueue.Enqueue(() =>
                {
                    menu.ShowAsContext();
                });
            }
            else
            {
                menu.ShowAsContext();
            }
        }
Esempio n. 10
0
        void EnqueueProcess(string path, bool isRecursive, ResultCallback callback, HashSet <string> results = null)
        {
            results = results ?? new HashSet <string>();
            var guid = AssetDependencyDatabase.PathToGUID(path);

            var crumbs = Path.GetDirectoryName(path).Split(new[] { '/', '\\' });
            var sb     = new StringBuilder();
            var dirs   = new List <string>();

            for (var n = crumbs.Length; n > 0; --n)
            {
                sb.Length = 0;
                for (var j = 0; j < n; ++j)
                {
                    if (j > 0)
                    {
                        sb.Append('/');
                    }
                    sb.Append(crumbs[j]);
                }
                dirs.Add(sb.ToString());
            }
            dirs.Add("ProjectSettings");

            ReferenceSearchEngineProcess process = null;

            for (var i = 0; i < dirs.Count; ++i)
            {
                var dir    = dirs[i];
                var ignore = i > 0 ? dirs[i - 1] + "/" : null;
                if (dir == "ProjectSettings")
                {
                    ignore = null;
                }
                var merged = false;
                foreach (var q in processQueue)
                {
                    if (q.SearchDirectory == dir && q.IgnoreDirectory == ignore)
                    {
                        q.TargetPaths.Add(path);
                        q.TargetGUIDs.Add(guid);
                        merged  = true;
                        process = q;
                        break;
                    }
                }
                if (!merged)
                {
                    process = new ReferenceSearchEngineProcess()
                    {
                        Argument        = string.Format("--json {0} ", commonIgnorePatterns),
                        SearchDirectory = Prefix + dir,
                        IgnoreDirectory = ignore == null ? null : Prefix + ignore,
                        TargetPaths     = new List <string>()
                        {
                            path
                        },
                        TargetGUIDs = new List <string>()
                        {
                            guid
                        },
                        Callback    = callback,
                        IsRecursive = isRecursive,
                        Results     = results,
                        Reporter    = this,
                    };
                    processQueue.Add(process);
                }
            }
            var refPaths = AssetDependencyDatabase.GetReferences(path);

            if (refPaths == null)
            {
                return;
            }
            foreach (var refPath in refPaths)
            {
                lock (results)
                {
                    if (results.Contains(refPath))
                    {
                        continue;
                    }
                    results.Add(refPath);
                }
                FoundReference(process, refPath, path);
            }
        }