ScriptCodeManager.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. if (fileEntry.ResType != ResourceType.ScriptCode)
  109. return;
  110. ScriptCode codeFile = ProjectLibrary.Load<ScriptCode>(path);
  111. if(codeFile == null)
  112. return;
  113. if(codeFile.EditorScript)
  114. isEditorAssemblyDirty = true;
  115. else
  116. isGameAssemblyDirty = true;
  117. }
  118. /// <summary>
  119. /// Checks is the resource at the provided path a file relevant to the code editor.
  120. /// </summary>
  121. /// <param name="path">Path to the resource, absolute or relative to the project's resources folder.</param>
  122. /// <returns>True if the file is relevant to the code editor, false otherwise.</returns>
  123. private bool IsCodeEditorFile(string path)
  124. {
  125. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  126. if (entry != null && entry.Type == LibraryEntryType.File)
  127. {
  128. FileEntry fileEntry = (FileEntry)entry;
  129. foreach (var codeType in CodeEditor.CodeTypes)
  130. {
  131. if (fileEntry.ResType == codeType)
  132. return true;
  133. }
  134. }
  135. return false;
  136. }
  137. /// <summary>
  138. /// Converts data reported by the compiler into a readable string.
  139. /// </summary>
  140. /// <param name="msg">Message data as reported by the compiler.</param>
  141. /// <returns>Readable message string.</returns>
  142. private string FormMessage(CompilerMessage msg)
  143. {
  144. StringBuilder sb = new StringBuilder();
  145. if (msg.type == CompilerMessageType.Error)
  146. sb.AppendLine("Compiler error: " + msg.message);
  147. else
  148. sb.AppendLine("Compiler warning: " + msg.message);
  149. sb.AppendLine("\tin " + msg.file + "[" + msg.line + ":" + msg.column + "]");
  150. return sb.ToString();
  151. }
  152. /// <summary>
  153. /// Parses a log message and outputs a data object with a separate message and callstack entries. If the message
  154. /// is not a valid compiler message null is returned.
  155. /// </summary>
  156. /// <param name="message">Message to parse.</param>
  157. /// <returns>Parsed log message or null if not a valid compiler message.</returns>
  158. public static ParsedLogEntry ParseCompilerMessage(string message)
  159. {
  160. // Note: If modifying FormMessage method make sure to update this one as well to match the formattting
  161. // Check for error
  162. Regex regex = new Regex(@"Compiler error: (.*)\n\tin (.*)\[(.*):.*\]");
  163. var match = regex.Match(message);
  164. // Check for warning
  165. if (!match.Success)
  166. {
  167. regex = new Regex(@"Compiler warning: (.*)\n\tin (.*)\[(.*):.*\]");
  168. match = regex.Match(message);
  169. }
  170. // No match
  171. if (!match.Success)
  172. return null;
  173. ParsedLogEntry entry = new ParsedLogEntry();
  174. entry.callstack = new CallStackEntry[1];
  175. entry.message = match.Groups[1].Value;
  176. CallStackEntry callstackEntry = new CallStackEntry();
  177. callstackEntry.method = "";
  178. callstackEntry.file = match.Groups[2].Value;
  179. int.TryParse(match.Groups[3].Value, out callstackEntry.line);
  180. entry.callstack[0] = callstackEntry;
  181. return entry;
  182. }
  183. }
  184. }