ScriptCodeManager.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System.IO;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. /// <summary>
  8. /// Handles various operations related to script code in the active project, like compilation and code editor syncing.
  9. /// </summary>
  10. public sealed class ScriptCodeManager
  11. {
  12. private bool isGameAssemblyDirty;
  13. private bool isEditorAssemblyDirty;
  14. private CompilerInstance compilerInstance;
  15. /// <summary>
  16. /// Constructs a new script code manager.
  17. /// </summary>
  18. internal ScriptCodeManager()
  19. {
  20. ProjectLibrary.OnEntryAdded += OnEntryAdded;
  21. ProjectLibrary.OnEntryRemoved += OnEntryRemoved;
  22. ProjectLibrary.OnEntryImported += OnEntryImported;
  23. }
  24. /// <summary>
  25. /// Triggers required compilation or code editor syncing if needed.
  26. /// </summary>
  27. internal void Update()
  28. {
  29. if (EditorApplication.HasFocus && CodeEditor.IsSolutionDirty)
  30. CodeEditor.SyncSolution();
  31. if (EditorApplication.IsStopped)
  32. {
  33. if (compilerInstance == null)
  34. {
  35. if (EditorApplication.HasFocus)
  36. {
  37. string outputDir = EditorApplication.ScriptAssemblyPath;
  38. if (isGameAssemblyDirty)
  39. {
  40. compilerInstance = ScriptCompiler.CompileAsync(
  41. ScriptAssemblyType.Game, BuildManager.ActivePlatform, true, outputDir);
  42. EditorApplication.SetStatusCompiling(true);
  43. isGameAssemblyDirty = false;
  44. }
  45. else if (isEditorAssemblyDirty)
  46. {
  47. compilerInstance = ScriptCompiler.CompileAsync(
  48. ScriptAssemblyType.Editor, BuildManager.ActivePlatform, true, outputDir);
  49. EditorApplication.SetStatusCompiling(true);
  50. isEditorAssemblyDirty = false;
  51. }
  52. }
  53. }
  54. else
  55. {
  56. if (compilerInstance.IsDone)
  57. {
  58. Debug.Clear(DebugMessageType.CompilerWarning);
  59. Debug.Clear(DebugMessageType.CompilerError);
  60. ConsoleWindow window = EditorWindow.GetWindow<ConsoleWindow>();
  61. if (window != null)
  62. window.Refresh();
  63. if (compilerInstance.HasErrors)
  64. {
  65. foreach (var msg in compilerInstance.WarningMessages)
  66. Debug.LogMessage(FormMessage(msg), DebugMessageType.CompilerWarning);
  67. foreach (var msg in compilerInstance.ErrorMessages)
  68. Debug.LogMessage(FormMessage(msg), DebugMessageType.CompilerError);
  69. }
  70. compilerInstance.Dispose();
  71. compilerInstance = null;
  72. EditorApplication.SetStatusCompiling(false);
  73. EditorApplication.ReloadAssemblies();
  74. }
  75. }
  76. }
  77. }
  78. /// <summary>
  79. /// Triggered when a new resource is added to the project library.
  80. /// </summary>
  81. /// <param name="path">Path of the added resource, relative to the project's resource folder.</param>
  82. private void OnEntryAdded(string path)
  83. {
  84. if (IsCodeEditorFile(path))
  85. CodeEditor.MarkSolutionDirty();
  86. }
  87. /// <summary>
  88. /// Triggered when a resource is removed from the project library.
  89. /// </summary>
  90. /// <param name="path">Path of the removed resource, relative to the project's resource folder.</param>
  91. private void OnEntryRemoved(string path)
  92. {
  93. if (IsCodeEditorFile(path))
  94. CodeEditor.MarkSolutionDirty();
  95. }
  96. /// <summary>
  97. /// Triggered when a resource is (re)imported in the project library.
  98. /// </summary>
  99. /// <param name="path">Path of the imported resource, relative to the project's resource folder.</param>
  100. private void OnEntryImported(string path)
  101. {
  102. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  103. if (entry == null || entry.Type != LibraryEntryType.File)
  104. return;
  105. FileEntry fileEntry = (FileEntry)entry;
  106. if (fileEntry.ResType != ResourceType.ScriptCode)
  107. return;
  108. ScriptCode codeFile = ProjectLibrary.Load<ScriptCode>(path);
  109. if(codeFile == null)
  110. return;
  111. if(codeFile.EditorScript)
  112. isEditorAssemblyDirty = true;
  113. else
  114. isGameAssemblyDirty = true;
  115. }
  116. /// <summary>
  117. /// Checks is the resource at the provided path a file relevant to the code editor.
  118. /// </summary>
  119. /// <param name="path">Path to the resource, absolute or relative to the project's resources folder.</param>
  120. /// <returns>True if the file is relevant to the code editor, false otherwise.</returns>
  121. private bool IsCodeEditorFile(string path)
  122. {
  123. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  124. if (entry != null && entry.Type == LibraryEntryType.File)
  125. {
  126. FileEntry fileEntry = (FileEntry)entry;
  127. foreach (var codeType in CodeEditor.CodeTypes)
  128. {
  129. if (fileEntry.ResType == codeType)
  130. return true;
  131. }
  132. }
  133. return false;
  134. }
  135. /// <summary>
  136. /// Converts data reported by the compiler into a readable string.
  137. /// </summary>
  138. /// <param name="msg">Message data as reported by the compiler.</param>
  139. /// <returns>Readable message string.</returns>
  140. private string FormMessage(CompilerMessage msg)
  141. {
  142. StringBuilder sb = new StringBuilder();
  143. if (msg.type == CompilerMessageType.Error)
  144. sb.AppendLine("Compiler error: " + msg.message);
  145. else
  146. sb.AppendLine("Compiler warning: " + msg.message);
  147. sb.AppendLine("\tin " + msg.file + "[" + msg.line + ":" + msg.column + "]");
  148. return sb.ToString();
  149. }
  150. /// <summary>
  151. /// Parses a log message and outputs a data object with a separate message and callstack entries. If the message
  152. /// is not a valid compiler message null is returned.
  153. /// </summary>
  154. /// <param name="message">Message to parse.</param>
  155. /// <returns>Parsed log message or null if not a valid compiler message.</returns>
  156. public static ParsedLogEntry ParseCompilerMessage(string message)
  157. {
  158. // Note: If modifying FormMessage method make sure to update this one as well to match the formattting
  159. // Check for error
  160. Regex regex = new Regex(@"Compiler error: (.*)\n\tin (.*)\[(.*):.*\]");
  161. var match = regex.Match(message);
  162. // Check for warning
  163. if (!match.Success)
  164. {
  165. regex = new Regex(@"Compiler warning: (.*)\n\tin (.*)\[(.*):.*\]");
  166. match = regex.Match(message);
  167. }
  168. // No match
  169. if (!match.Success)
  170. return null;
  171. ParsedLogEntry entry = new ParsedLogEntry();
  172. entry.callstack = new CallStackEntry[1];
  173. entry.message = match.Groups[1].Value;
  174. CallStackEntry callstackEntry = new CallStackEntry();
  175. callstackEntry.method = "";
  176. callstackEntry.file = match.Groups[2].Value;
  177. int.TryParse(match.Groups[3].Value, out callstackEntry.line);
  178. entry.callstack[0] = callstackEntry;
  179. return entry;
  180. }
  181. }
  182. }