AsyncDebugger.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #if (!UNITY_5) || UNITY_STANDALONE || UNITY_EDITOR
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading;
  9. using MoonSharp.Interpreter;
  10. using MoonSharp.Interpreter.Debugging;
  11. using MoonSharp.VsCodeDebugger;
  12. using MoonSharp.VsCodeDebugger.SDK;
  13. namespace MoonSharp.VsCodeDebugger.DebuggerLogic
  14. {
  15. internal class AsyncDebugger : IDebugger
  16. {
  17. private static object s_AsyncDebuggerIdLock = new object();
  18. private static int s_AsyncDebuggerIdCounter = 0;
  19. object m_Lock = new object();
  20. private IAsyncDebuggerClient m_Client__;
  21. DebuggerAction m_PendingAction = null;
  22. List<WatchItem>[] m_WatchItems;
  23. Dictionary<int, SourceCode> m_SourcesMap = new Dictionary<int, SourceCode>();
  24. Dictionary<int, string> m_SourcesOverride = new Dictionary<int, string>();
  25. Func<SourceCode, string> m_SourceFinder;
  26. public DebugService DebugService { get; private set; }
  27. public Regex ErrorRegex { get; set; }
  28. public Script Script { get; private set; }
  29. public bool PauseRequested { get; set; }
  30. public string Name { get; set; }
  31. public int Id { get; private set; }
  32. public AsyncDebugger(Script script, Func<SourceCode, string> sourceFinder, string name)
  33. {
  34. lock (s_AsyncDebuggerIdLock)
  35. Id = s_AsyncDebuggerIdCounter++;
  36. m_SourceFinder = sourceFinder;
  37. ErrorRegex = new Regex(@"\A.*\Z");
  38. Script = script;
  39. m_WatchItems = new List<WatchItem>[(int)WatchType.MaxValue];
  40. Name = name;
  41. for (int i = 0; i < m_WatchItems.Length; i++)
  42. m_WatchItems[i] = new List<WatchItem>(64);
  43. }
  44. public IAsyncDebuggerClient Client
  45. {
  46. get { return m_Client__; }
  47. set
  48. {
  49. lock (m_Lock)
  50. {
  51. if (m_Client__ != null && m_Client__ != value)
  52. {
  53. m_Client__.Unbind();
  54. }
  55. if (value != null)
  56. {
  57. for (int i = 0; i < Script.SourceCodeCount; i++)
  58. if (m_SourcesMap.ContainsKey(i))
  59. value.OnSourceCodeChanged(i);
  60. }
  61. m_Client__ = value;
  62. }
  63. }
  64. }
  65. DebuggerAction IDebugger.GetAction(int ip, SourceRef sourceref)
  66. {
  67. PauseRequested = false;
  68. lock (m_Lock)
  69. if (Client != null)
  70. {
  71. Client.SendStopEvent();
  72. }
  73. while (true)
  74. {
  75. lock (m_Lock)
  76. {
  77. if (Client == null)
  78. {
  79. return new DebuggerAction() { Action = DebuggerAction.ActionType.Run };
  80. }
  81. if (m_PendingAction != null)
  82. {
  83. var action = m_PendingAction;
  84. m_PendingAction = null;
  85. return action;
  86. }
  87. }
  88. System.Threading.Thread.Sleep(10);
  89. }
  90. }
  91. public void QueueAction(DebuggerAction action)
  92. {
  93. while (true)
  94. {
  95. lock (m_Lock)
  96. if (m_PendingAction == null)
  97. {
  98. m_PendingAction = action;
  99. break;
  100. }
  101. System.Threading.Thread.Sleep(10);
  102. }
  103. }
  104. private DynamicExpression CreateDynExpr(string code)
  105. {
  106. try
  107. {
  108. return Script.CreateDynamicExpression(code);
  109. }
  110. catch (Exception ex)
  111. {
  112. return Script.CreateConstantDynamicExpression(code, DynValue.NewString(ex.Message));
  113. }
  114. }
  115. List<DynamicExpression> IDebugger.GetWatchItems()
  116. {
  117. return new List<DynamicExpression>();
  118. }
  119. bool IDebugger.IsPauseRequested()
  120. {
  121. return PauseRequested;
  122. }
  123. void IDebugger.RefreshBreakpoints(IEnumerable<SourceRef> refs)
  124. {
  125. }
  126. void IDebugger.SetByteCode(string[] byteCode)
  127. {
  128. }
  129. void IDebugger.SetSourceCode(SourceCode sourceCode)
  130. {
  131. m_SourcesMap[sourceCode.SourceID] = sourceCode;
  132. bool invalidFile = false;
  133. string file = m_SourceFinder(sourceCode);
  134. if (!string.IsNullOrEmpty(file))
  135. {
  136. try
  137. {
  138. if (!File.Exists(file))
  139. invalidFile = true;
  140. }
  141. catch
  142. {
  143. invalidFile = true;
  144. }
  145. }
  146. else
  147. {
  148. invalidFile = true;
  149. }
  150. if (invalidFile)
  151. {
  152. file = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N") + ".lua");
  153. File.WriteAllText(file, sourceCode.Code + GetFooterForTempFile());
  154. m_SourcesOverride[sourceCode.SourceID] = file;
  155. }
  156. else if (file != sourceCode.Name)
  157. {
  158. m_SourcesOverride[sourceCode.SourceID] = file;
  159. }
  160. lock (m_Lock)
  161. if (Client != null)
  162. Client.OnSourceCodeChanged(sourceCode.SourceID);
  163. }
  164. private string GetFooterForTempFile()
  165. {
  166. return "\n\n" +
  167. "----------------------------------------------------------------------------------------------------------\n" +
  168. "-- This file has been generated by the debugger as a placeholder for a script snippet stored in memory. --\n" +
  169. "-- If you restart the host process, the contents of this file are not valid anymore. --\n" +
  170. "----------------------------------------------------------------------------------------------------------\n";
  171. }
  172. public string GetSourceFile(int sourceId)
  173. {
  174. if (m_SourcesOverride.ContainsKey(sourceId))
  175. return m_SourcesOverride[sourceId];
  176. else if (m_SourcesMap.ContainsKey(sourceId))
  177. return m_SourcesMap[sourceId].Name;
  178. return null;
  179. }
  180. public bool IsSourceOverride(int sourceId)
  181. {
  182. return (m_SourcesOverride.ContainsKey(sourceId));
  183. }
  184. void IDebugger.SignalExecutionEnded()
  185. {
  186. lock (m_Lock)
  187. if (Client != null)
  188. Client.OnExecutionEnded();
  189. }
  190. bool IDebugger.SignalRuntimeException(ScriptRuntimeException ex)
  191. {
  192. lock (m_Lock)
  193. if (Client == null)
  194. return false;
  195. Client.OnException(ex);
  196. PauseRequested = ErrorRegex.IsMatch(ex.Message);
  197. return PauseRequested;
  198. }
  199. void IDebugger.Update(WatchType watchType, IEnumerable<WatchItem> items)
  200. {
  201. var list = m_WatchItems[(int)watchType];
  202. list.Clear();
  203. list.AddRange(items);
  204. lock (m_Lock)
  205. if (Client != null)
  206. Client.OnWatchesUpdated(watchType);
  207. }
  208. public List<WatchItem> GetWatches(WatchType watchType)
  209. {
  210. return m_WatchItems[(int)watchType];
  211. }
  212. public SourceCode GetSource(int id)
  213. {
  214. if (m_SourcesMap.ContainsKey(id))
  215. return m_SourcesMap[id];
  216. return null;
  217. }
  218. public SourceCode FindSourceByName(string path)
  219. {
  220. // we use case insensitive match - be damned if you have files which differ only by
  221. // case in the same directory on Unix.
  222. path = path.Replace('\\', '/').ToUpperInvariant();
  223. foreach (var kvp in m_SourcesOverride)
  224. {
  225. if (kvp.Value.Replace('\\', '/').ToUpperInvariant() == path)
  226. return m_SourcesMap[kvp.Key];
  227. }
  228. return m_SourcesMap.Values.FirstOrDefault(s => s.Name.Replace('\\', '/').ToUpperInvariant() == path);
  229. }
  230. void IDebugger.SetDebugService(DebugService debugService)
  231. {
  232. DebugService = debugService;
  233. }
  234. public DynValue Evaluate(string expression)
  235. {
  236. DynamicExpression expr = CreateDynExpr(expression);
  237. return expr.Evaluate();
  238. }
  239. DebuggerCaps IDebugger.GetDebuggerCaps()
  240. {
  241. return DebuggerCaps.CanDebugSourceCode | DebuggerCaps.HasLineBasedBreakpoints;
  242. }
  243. }
  244. }
  245. #endif