ScriptCodeManager.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System.IO;
  2. using System.Text;
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /// <summary>
  7. /// Handles various operations related to script code in the active project, like compilation and code editor syncing.
  8. /// </summary>
  9. public sealed class ScriptCodeManager
  10. {
  11. private bool isGameAssemblyDirty;
  12. private bool isEditorAssemblyDirty;
  13. private CompilerInstance compilerInstance;
  14. /// <summary>
  15. /// Constructs a new script code manager.
  16. /// </summary>
  17. internal ScriptCodeManager()
  18. {
  19. ProjectLibrary.OnEntryAdded += OnEntryAdded;
  20. ProjectLibrary.OnEntryRemoved += OnEntryRemoved;
  21. ProjectLibrary.OnEntryImported += OnEntryImported;
  22. }
  23. /// <summary>
  24. /// Triggers required compilation or code editor syncing if needed.
  25. /// </summary>
  26. internal void Update()
  27. {
  28. if (CodeEditor.IsSolutionDirty)
  29. CodeEditor.SyncSolution();
  30. if (compilerInstance == null)
  31. {
  32. string outputDir = EditorApplication.ScriptAssemblyPath;
  33. if (isGameAssemblyDirty)
  34. {
  35. compilerInstance = ScriptCompiler.CompileAsync(
  36. ScriptAssemblyType.Game, BuildManager.ActivePlatform, true, outputDir);
  37. EditorApplication.SetStatusCompiling(true);
  38. isGameAssemblyDirty = false;
  39. }
  40. else if (isEditorAssemblyDirty)
  41. {
  42. compilerInstance = ScriptCompiler.CompileAsync(
  43. ScriptAssemblyType.Editor, BuildManager.ActivePlatform, true, outputDir);
  44. EditorApplication.SetStatusCompiling(true);
  45. isEditorAssemblyDirty = false;
  46. }
  47. }
  48. else
  49. {
  50. if (compilerInstance.IsDone)
  51. {
  52. if (compilerInstance.HasErrors)
  53. {
  54. foreach (var msg in compilerInstance.WarningMessages)
  55. Debug.LogError(FormMessage(msg));
  56. foreach (var msg in compilerInstance.ErrorMessages)
  57. Debug.LogError(FormMessage(msg));
  58. }
  59. compilerInstance.Dispose();
  60. compilerInstance = null;
  61. EditorApplication.SetStatusCompiling(false);
  62. EditorApplication.ReloadAssemblies();
  63. }
  64. }
  65. }
  66. /// <summary>
  67. /// Triggered when a new resource is added to the project library.
  68. /// </summary>
  69. /// <param name="path">Path of the added resource, relative to the project's resource folder.</param>
  70. private void OnEntryAdded(string path)
  71. {
  72. if (IsCodeEditorFile(path))
  73. CodeEditor.MarkSolutionDirty();
  74. }
  75. /// <summary>
  76. /// Triggered when a resource is removed from the project library.
  77. /// </summary>
  78. /// <param name="path">Path of the removed resource, relative to the project's resource folder.</param>
  79. private void OnEntryRemoved(string path)
  80. {
  81. if (IsCodeEditorFile(path))
  82. CodeEditor.MarkSolutionDirty();
  83. }
  84. /// <summary>
  85. /// Triggered when a resource is (re)imported in the project library.
  86. /// </summary>
  87. /// <param name="path">Path of the imported resource, relative to the project's resource folder.</param>
  88. private void OnEntryImported(string path)
  89. {
  90. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  91. if (entry == null || entry.Type != LibraryEntryType.File)
  92. return;
  93. FileEntry fileEntry = (FileEntry)entry;
  94. if (fileEntry.ResType != ResourceType.ScriptCode)
  95. return;
  96. ScriptCode codeFile = ProjectLibrary.Load<ScriptCode>(path);
  97. if(codeFile == null)
  98. return;
  99. if(codeFile.EditorScript)
  100. isEditorAssemblyDirty = true;
  101. else
  102. isGameAssemblyDirty = true;
  103. }
  104. /// <summary>
  105. /// Checks is the resource at the provided path a file relevant to the code editor.
  106. /// </summary>
  107. /// <param name="path">Path to the resource, absolute or relative to the project's resources folder.</param>
  108. /// <returns>True if the file is relevant to the code editor, false otherwise.</returns>
  109. private bool IsCodeEditorFile(string path)
  110. {
  111. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  112. if (entry != null && entry.Type == LibraryEntryType.File)
  113. {
  114. FileEntry fileEntry = (FileEntry)entry;
  115. foreach (var codeType in CodeEditor.CodeTypes)
  116. {
  117. if (fileEntry.ResType == codeType)
  118. return true;
  119. }
  120. }
  121. return false;
  122. }
  123. /// <summary>
  124. /// Converts data reported by the compiler into a readable string.
  125. /// </summary>
  126. /// <param name="msg">Message data as reported by the compiler.</param>
  127. /// <returns>Readable message string.</returns>
  128. private string FormMessage(CompilerMessage msg)
  129. {
  130. StringBuilder sb = new StringBuilder();
  131. if (msg.type == CompilerMessageType.Error)
  132. sb.AppendLine("Compiler error: " + msg.message);
  133. else
  134. sb.AppendLine("Compiler warning: " + msg.message);
  135. sb.AppendLine("\tin " + msg.file + "[" + msg.line + ":" + msg.column + "]");
  136. return sb.ToString();
  137. }
  138. }
  139. }