LuaException.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. Copyright (c) 2015-2017 topameng(topameng@qq.com)
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. */
  19. using System;
  20. using System.Diagnostics;
  21. using System.Reflection;
  22. using System.Text;
  23. using UnityEngine;
  24. namespace LuaInterface
  25. {
  26. public class LuaException : Exception
  27. {
  28. public static Exception luaStack = null;
  29. public static string projectFolder = null;
  30. public static int InstantiateCount = 0;
  31. public static int SendMsgCount = 0;
  32. public static IntPtr L = IntPtr.Zero;
  33. public override string StackTrace
  34. {
  35. get
  36. {
  37. return _stack;
  38. }
  39. }
  40. protected string _stack = string.Empty;
  41. public LuaException(string msg, Exception e = null, int skip = 1)
  42. : base(msg)
  43. {
  44. if (e != null)
  45. {
  46. if (e is LuaException)
  47. {
  48. _stack = e.StackTrace;
  49. }
  50. else
  51. {
  52. StackTrace trace = new StackTrace(e, true);
  53. StringBuilder sb = new StringBuilder();
  54. ExtractFormattedStackTrace(trace, sb);
  55. StackTrace self = new StackTrace(skip, true);
  56. ExtractFormattedStackTrace(self, sb, trace);
  57. _stack = sb.ToString();
  58. }
  59. }
  60. else
  61. {
  62. StackTrace self = new StackTrace(skip, true);
  63. StringBuilder sb = new StringBuilder();
  64. ExtractFormattedStackTrace(self, sb);
  65. _stack = sb.ToString();
  66. }
  67. }
  68. public static Exception GetLastError()
  69. {
  70. Exception last = luaStack;
  71. luaStack = null;
  72. return last;
  73. }
  74. public static void ExtractFormattedStackTrace(StackTrace trace, StringBuilder sb, StackTrace skip = null)
  75. {
  76. int begin = 0;
  77. if (skip != null && skip.FrameCount > 0)
  78. {
  79. MethodBase m0 = skip.GetFrame(skip.FrameCount - 1).GetMethod();
  80. for (int i = 0; i < trace.FrameCount; i++)
  81. {
  82. StackFrame frame = trace.GetFrame(i);
  83. MethodBase method = frame.GetMethod();
  84. if (method == m0)
  85. {
  86. begin = i + 1;
  87. break;
  88. }
  89. }
  90. sb.AppendLineEx();
  91. }
  92. for (int i = begin; i < trace.FrameCount; i++)
  93. {
  94. StackFrame frame = trace.GetFrame(i);
  95. MethodBase method = frame.GetMethod();
  96. if (method == null || method.DeclaringType == null)
  97. {
  98. continue;
  99. }
  100. Type declaringType = method.DeclaringType;
  101. string str = declaringType.Namespace;
  102. if ( (InstantiateCount == 0 && declaringType == typeof(UnityEngine.Object) && method.Name == "Instantiate") //(method.Name == "Internal_CloneSingle"
  103. || (SendMsgCount == 0 && declaringType == typeof(GameObject) && method.Name == "SendMessage"))
  104. {
  105. break;
  106. }
  107. if ((str != null) && (str.Length != 0))
  108. {
  109. sb.Append(str);
  110. sb.Append(".");
  111. }
  112. sb.Append(declaringType.Name);
  113. sb.Append(":");
  114. sb.Append(method.Name);
  115. sb.Append("(");
  116. int index = 0;
  117. ParameterInfo[] parameters = method.GetParameters();
  118. bool flag = true;
  119. while (index < parameters.Length)
  120. {
  121. if (!flag)
  122. {
  123. sb.Append(", ");
  124. }
  125. else
  126. {
  127. flag = false;
  128. }
  129. sb.Append(parameters[index].ParameterType.Name);
  130. index++;
  131. }
  132. sb.Append(")");
  133. string fileName = frame.GetFileName();
  134. if (fileName != null)
  135. {
  136. fileName = fileName.Replace('\\', '/');
  137. sb.Append(" (at ");
  138. if (fileName.StartsWith(projectFolder))
  139. {
  140. fileName = fileName.Substring(projectFolder.Length, fileName.Length - projectFolder.Length);
  141. }
  142. sb.Append(fileName);
  143. sb.Append(":");
  144. sb.Append(frame.GetFileLineNumber().ToString());
  145. sb.Append(")");
  146. }
  147. if (i != trace.FrameCount - 1)
  148. {
  149. sb.Append("\n");
  150. }
  151. }
  152. }
  153. public static void Init(IntPtr L0)
  154. {
  155. L = L0;
  156. Type type = typeof(StackTraceUtility);
  157. FieldInfo field = type.GetField("projectFolder", BindingFlags.Static | BindingFlags.GetField | BindingFlags.NonPublic);
  158. LuaException.projectFolder = (string)field.GetValue(null);
  159. projectFolder = projectFolder.Replace('\\', '/');
  160. #if DEVELOPER
  161. Debugger.Log("projectFolder is {0}", projectFolder);
  162. #endif
  163. }
  164. }
  165. }