StackTraceHelper.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Diagnostics;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Security;
  6. using System.Text;
  7. using UnityEngine;
  8. namespace LitMotion.Editor
  9. {
  10. internal static class StackTraceHelper
  11. {
  12. static readonly StringBuilder sb = new();
  13. public static string AddHyperLink(this StackTrace stackTrace)
  14. {
  15. if (stackTrace == null) return "";
  16. sb.Clear();
  17. for (int i = 0; i < stackTrace.FrameCount; i++)
  18. {
  19. var sf = stackTrace.GetFrame(i);
  20. if (sf.GetILOffset() != -1)
  21. {
  22. string fileName = null;
  23. try
  24. {
  25. fileName = sf.GetFileName();
  26. }
  27. catch (NotSupportedException) { }
  28. catch (SecurityException) { }
  29. if (fileName != null)
  30. {
  31. sb.Append(' ');
  32. sb.AppendFormat(CultureInfo.InvariantCulture, "(at {0})", AppendHyperLink(fileName, sf.GetFileLineNumber().ToString()));
  33. sb.AppendLine();
  34. }
  35. }
  36. }
  37. return sb.ToString();
  38. }
  39. static string AppendHyperLink(string path, string line)
  40. {
  41. var fi = new FileInfo(path);
  42. if (fi.Directory == null)
  43. {
  44. return fi.Name;
  45. }
  46. else
  47. {
  48. var fname = fi.FullName.Replace(Path.DirectorySeparatorChar, '/').Replace(Application.dataPath, "");
  49. var withAssetsPath = "Assets/" + fname;
  50. return "<a href=\"" + withAssetsPath + "\" line=\"" + line + "\">" + withAssetsPath + ":" + line + "</a>";
  51. }
  52. }
  53. }
  54. }