ScriptCodeManager.cs 7.2 KB

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