ScriptCodeManager.cs 8.9 KB

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