ScriptCodeManager.cs 8.0 KB

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