Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
*.meta
8 changes: 8 additions & 0 deletions Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

315 changes: 172 additions & 143 deletions FindReferencesInProject2.cs → Editor/FindReferencesInProject2.cs
Original file line number Diff line number Diff line change
@@ -1,143 +1,172 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using UnityEditor;
using UnityEngine;

public static class FindReferencesInProject2
{
private const string MenuItemName = "Assets/Find References In Project %#&f";
private const string MetaExtension = ".meta";

[MenuItem(MenuItemName, false, 25)]
public static void Find()
{
bool isMacOS = Application.platform == RuntimePlatform.OSXEditor;
int totalWaitMilliseconds = isMacOS ? 2 * 1000 : 300 * 1000;
int cpuCount = Environment.ProcessorCount;
string appDataPath = Application.dataPath;

var selectedObject = Selection.activeObject;
string selectedAssetPath = AssetDatabase.GetAssetPath(selectedObject);
string selectedAssetGUID = AssetDatabase.AssetPathToGUID(selectedAssetPath);
string selectedAssetMetaPath = selectedAssetPath + MetaExtension;

var references = new List<string>();
var output = new System.Text.StringBuilder();

var stopwatch = new Stopwatch();
stopwatch.Start();

var psi = new ProcessStartInfo();
psi.WindowStyle = ProcessWindowStyle.Minimized;

if (isMacOS)
{
psi.FileName = "/usr/bin/mdfind";
psi.Arguments = string.Format("-onlyin {0} {1}", appDataPath, selectedAssetGUID);
}
else
{
psi.FileName = Path.Combine(Environment.CurrentDirectory, @"Tools\FindReferencesInProject2\rg.exe");
psi.Arguments = string.Format("--case-sensitive --follow --files-with-matches --no-text --fixed-strings " +
"--ignore-file Assets/Editor/FindReferencesInProject2/ignore.txt " +
"--threads {0} --regexp {1} -- {2}",
cpuCount, selectedAssetGUID, appDataPath);
}

psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;

var process = new Process();
process.StartInfo = psi;

process.OutputDataReceived += (sender, e) =>
{
if (string.IsNullOrEmpty(e.Data))
return;

string relativePath = e.Data.Replace(appDataPath, "Assets").Replace("\\", "/");

// skip the meta file of whatever we have selected
if (relativePath == selectedAssetMetaPath)
return;

references.Add(relativePath);
};

process.ErrorDataReceived += (sender, e) =>
{
if (string.IsNullOrEmpty(e.Data))
return;

output.AppendLine("Error: " + e.Data);
};

process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();

while (!process.HasExited)
{
if (stopwatch.ElapsedMilliseconds < totalWaitMilliseconds)
{
float progress = (float)((double)stopwatch.ElapsedMilliseconds / totalWaitMilliseconds);
string info = string.Format("Finding {0}/{1}s {2:P2}", stopwatch.ElapsedMilliseconds / 1000,
totalWaitMilliseconds / 1000, progress);
bool canceled = EditorUtility.DisplayCancelableProgressBar("Find References in Project", info, progress);

if (canceled)
{
process.Kill();
break;
}

Thread.Sleep(100);
}
else
{
process.Kill();
break;
}
}

foreach (string file in references)
{
string guid = AssetDatabase.AssetPathToGUID(file);
output.AppendLine(string.Format("{0} {1}", guid, file));

string assetPath = file;
if (file.EndsWith(MetaExtension))
{
assetPath = file.Substring(0, file.Length - MetaExtension.Length);
}

UnityEngine.Debug.Log(string.Format("{0}\n{1}", file, guid), AssetDatabase.LoadMainAssetAtPath(assetPath));
}

EditorUtility.ClearProgressBar();
stopwatch.Stop();

string content = string.Format(
"{0} {1} found for object: \"{2}\" path: \"{3}\" guid: \"{4}\" total time: {5}s\n\n{6}",
references.Count, references.Count > 2 ? "references" : "reference", selectedObject.name, selectedAssetPath,
selectedAssetGUID, stopwatch.ElapsedMilliseconds / 1000d, output);
UnityEngine.Debug.LogWarning(content, selectedObject);
}

[MenuItem(MenuItemName, true)]
private static bool FindValidate()
{
var obj = Selection.activeObject;
if (obj != null && AssetDatabase.Contains(obj))
{
string path = AssetDatabase.GetAssetPath(obj);
return !AssetDatabase.IsValidFolder(path);
}

return false;
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using UnityEditor;
using UnityEngine;

public static class FindReferencesInProject2
{
private const string MenuItemName = "Assets/Find References In Project %#&f";
private const string MetaExtension = ".meta";

private static string InstallDirectory = "Assets/Editor/FindReferencesInProject2";

private static void UpdateInstallDirectory([CallerFilePath] string executingFilePath = "")
{
InstallDirectory = Path.GetDirectoryName(executingFilePath);
}

[MenuItem(MenuItemName, false, 25)]
public static void Find()
{
UpdateInstallDirectory();

bool isMacOS = Application.platform == RuntimePlatform.OSXEditor;
int totalWaitMilliseconds = isMacOS ? 2 * 1000 : 300 * 1000;
int cpuCount = Environment.ProcessorCount;
string appDataPath = Application.dataPath;

var selectedObject = Selection.activeObject;
string selectedAssetPath = AssetDatabase.GetAssetPath(selectedObject);
string selectedAssetGUID = AssetDatabase.AssetPathToGUID(selectedAssetPath);
string selectedAssetMetaPath = selectedAssetPath + MetaExtension;

var references = new List<string>();
var output = new System.Text.StringBuilder();

var stopwatch = new Stopwatch();
stopwatch.Start();

var psi = new ProcessStartInfo();
psi.WindowStyle = ProcessWindowStyle.Minimized;

if (isMacOS)
{
psi.FileName = "/usr/bin/mdfind";
psi.Arguments = string.Format("-onlyin {0} {1}", appDataPath, selectedAssetGUID);
}
else
{
var ignore_file = Path.Combine(InstallDirectory, "ignore.txt");
var filepath = Path.Combine(Environment.CurrentDirectory, @"Tools\FindReferencesInProject2\rg.exe");
if (!File.Exists(filepath))
{
// Assume it's in our path.
filepath = "rg.exe";
}
psi.FileName = filepath;
psi.Arguments = string.Format("--case-sensitive --follow --files-with-matches --no-text --fixed-strings " +
"--ignore-file {3} " +
"--threads {0} --regexp {1} -- {2}",
cpuCount, selectedAssetGUID, appDataPath, ignore_file);
}

psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;

var process = new Process();
process.StartInfo = psi;

process.OutputDataReceived += (sender, e) =>
{
if (string.IsNullOrEmpty(e.Data))
return;

string relativePath = e.Data.Replace(appDataPath, "Assets").Replace("\\", "/");

// skip the meta file of whatever we have selected
if (relativePath == selectedAssetMetaPath)
return;

references.Add(relativePath);
};

process.ErrorDataReceived += (sender, e) =>
{
if (string.IsNullOrEmpty(e.Data))
return;

output.AppendLine("Error: " + e.Data);
};

try
{
process.Start();
}
catch (SystemException)
{
if (!isMacOS)
{
var destination = Path.Combine(Environment.CurrentDirectory, @"Tools\FindReferencesInProject2");
UnityEngine.Debug.LogError($"Couldn't find ripgrep. Download ripgrep from https://github.com/BurntSushi/ripgrep/releases/latest and extract rg.exe to {destination} or add it to your PATH.");
}
throw;
}
process.BeginOutputReadLine();
process.BeginErrorReadLine();

while (!process.HasExited)
{
if (stopwatch.ElapsedMilliseconds < totalWaitMilliseconds)
{
float progress = (float)((double)stopwatch.ElapsedMilliseconds / totalWaitMilliseconds);
string info = string.Format("Finding {0}/{1}s {2:P2}", stopwatch.ElapsedMilliseconds / 1000,
totalWaitMilliseconds / 1000, progress);
bool canceled = EditorUtility.DisplayCancelableProgressBar("Find References in Project", info, progress);

if (canceled)
{
process.Kill();
break;
}

Thread.Sleep(100);
}
else
{
process.Kill();
break;
}
}

foreach (string file in references)
{
string guid = AssetDatabase.AssetPathToGUID(file);
output.AppendLine(string.Format("{0} {1}", guid, file));

string assetPath = file;
if (file.EndsWith(MetaExtension))
{
assetPath = file.Substring(0, file.Length - MetaExtension.Length);
}

UnityEngine.Debug.Log(string.Format("{0}\n{1}", file, guid), AssetDatabase.LoadMainAssetAtPath(assetPath));
}

EditorUtility.ClearProgressBar();
stopwatch.Stop();

string content = string.Format(
"{0} {1} found for object: \"{2}\" path: \"{3}\" guid: \"{4}\" total time: {5}s\n\n{6}",
references.Count, references.Count > 2 ? "references" : "reference", selectedObject.name, selectedAssetPath,
selectedAssetGUID, stopwatch.ElapsedMilliseconds / 1000d, output);
UnityEngine.Debug.LogWarning(content, selectedObject);
}

[MenuItem(MenuItemName, true)]
private static bool FindValidate()
{
var obj = Selection.activeObject;
if (obj != null && AssetDatabase.Contains(obj))
{
string path = AssetDatabase.GetAssetPath(obj);
return !AssetDatabase.IsValidFolder(path);
}

return false;
}
}
11 changes: 11 additions & 0 deletions Editor/FindReferencesInProject2.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Editor/findrefs.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "findrefs.Editor",
"references": [
],
"optionalUnityReferences": [
],
"includePlatforms": [
"Editor"
]
}
7 changes: 7 additions & 0 deletions Editor/findrefs.Editor.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading