ScriptCodeManager.cs 8.8 KB

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