ScriptCodeManager.cs 7.5 KB

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