ScriptCompiler.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace BansheeEditor
  11. {
  12. //MonoCompiler::StartCompile
  13. // - Finds all code files for the specified assembly using ProjectLibrary
  14. // - Starts a Process with specified parameters:
  15. // - default: -target:library -out: [name]
  16. // - references: -r: [filename]
  17. // - library folder: -lib: [path], [path2]
  18. // - defines: -d: [define]
  19. // - optimization: -o [+/-]
  20. // - debug: -debug [+/-]
  21. // - followed by a list of files (space separated I guess)
  22. public enum ScriptAssemblyType
  23. {
  24. Game, Editor
  25. }
  26. public struct CompileData
  27. {
  28. public string[] files;
  29. public string defines;
  30. }
  31. public class CompilerInstance
  32. {
  33. private Process process;
  34. private Thread readErrorsThread;
  35. private List<CompilerMessage> errors = new List<CompilerMessage>();
  36. private List<CompilerMessage> warnings = new List<CompilerMessage>();
  37. private Regex messageParseRegex = new Regex(@"\s*(?<file>.*)\(\s*(?<line>\d+)\s*,\s*(?<column>\d+)\s*\)\s*:\s*(?<type>warning|error)\s+(.*):\s*(?<message>.*)");
  38. internal CompilerInstance(string[] files, string defines, string[] assemblyFolders, string[] assemblies,
  39. bool debugBuild, string outputFile)
  40. {
  41. ProcessStartInfo procStartInfo = new ProcessStartInfo();
  42. String executable;
  43. switch (EditorApplication.EditorPlatform)
  44. {
  45. case EditorPlatformType.Windows:
  46. executable = Path.Combine(EditorApplication.CompilerPath, "mcs.exe");
  47. break;
  48. default:
  49. throw new NotImplementedException("Mono compilation not supported on this platform.");
  50. }
  51. StringBuilder argumentsBuilder = new StringBuilder();
  52. argumentsBuilder.Append(executable);
  53. if (!string.IsNullOrEmpty(defines))
  54. argumentsBuilder.Append(" -d:" + defines);
  55. if (assemblyFolders != null && assemblyFolders.Length > 0)
  56. {
  57. argumentsBuilder.Append(" -lib:");
  58. for (int i = 0; i < assemblyFolders.Length - 1; i++)
  59. argumentsBuilder.Append(assemblyFolders[i] + ",");
  60. argumentsBuilder.Append(assemblyFolders[assemblyFolders.Length - 1]);
  61. }
  62. if (assemblies != null && assemblies.Length > 0)
  63. {
  64. argumentsBuilder.Append(" -r:");
  65. for (int i = 0; i < assemblies.Length - 1; i++)
  66. argumentsBuilder.Append(assemblies[i] + ",");
  67. argumentsBuilder.Append(assemblies[assemblies.Length - 1]);
  68. }
  69. if (debugBuild)
  70. argumentsBuilder.Append(" -debug+ -o-");
  71. else
  72. argumentsBuilder.Append(" -debug- -o+");
  73. argumentsBuilder.Append(" -target:library -out:" + outputFile);
  74. for (int i = 0; i < files.Length; i++)
  75. argumentsBuilder.Append(" \"" + files[i] + "\"");
  76. procStartInfo.Arguments = argumentsBuilder.ToString();
  77. procStartInfo.CreateNoWindow = true;
  78. procStartInfo.FileName = executable;
  79. procStartInfo.RedirectStandardError = true;
  80. procStartInfo.RedirectStandardOutput = false;
  81. procStartInfo.UseShellExecute = false;
  82. procStartInfo.WorkingDirectory = EditorApplication.ProjectPath;
  83. process = new Process();
  84. process.StartInfo = procStartInfo;
  85. process.Start();
  86. readErrorsThread = new Thread(ReadErrorStream);
  87. }
  88. private void ReadErrorStream()
  89. {
  90. while (true)
  91. {
  92. if (process == null || process.HasExited)
  93. return;
  94. string line = process.StandardOutput.ReadLine();
  95. if (line == null)
  96. continue;
  97. CompilerMessage message;
  98. if (TryParseCompilerMessage(line, out message))
  99. {
  100. if (message.type == CompilerMessageType.Warning)
  101. {
  102. lock (warnings)
  103. warnings.Add(message);
  104. }
  105. else if (message.type == CompilerMessageType.Error)
  106. {
  107. lock (errors)
  108. errors.Add(message);
  109. }
  110. }
  111. }
  112. }
  113. private bool TryParseCompilerMessage(string messageText, out CompilerMessage message)
  114. {
  115. message = new CompilerMessage();
  116. Match match = messageParseRegex.Match(messageText);
  117. if (!match.Success)
  118. return false;
  119. message.file = match.Groups["file"].Value;
  120. message.line = Int32.Parse(match.Groups["line"].Value);
  121. message.column = Int32.Parse(match.Groups["column"].Value);
  122. message.type = match.Groups["type"].Value == "error" ? CompilerMessageType.Error : CompilerMessageType.Warning;
  123. message.message = match.Groups["message"].Value;
  124. return true;
  125. }
  126. public bool IsDone
  127. {
  128. get { return process.HasExited; }
  129. }
  130. public bool HasErrors
  131. {
  132. get { return IsDone && process.ExitCode != 0; }
  133. }
  134. public CompilerMessage[] WarningMessages
  135. {
  136. get
  137. {
  138. lock (warnings)
  139. {
  140. return warnings.ToArray();
  141. }
  142. }
  143. }
  144. public CompilerMessage[] ErrorMessages
  145. {
  146. get
  147. {
  148. lock (errors)
  149. {
  150. return errors.ToArray();
  151. }
  152. }
  153. }
  154. public void Dispose()
  155. {
  156. if (process == null)
  157. return;
  158. if (!process.HasExited)
  159. {
  160. process.Kill();
  161. process.WaitForExit();
  162. }
  163. process.Dispose();
  164. }
  165. }
  166. public static class ScriptCompiler
  167. {
  168. public static CompilerInstance CompileAsync(ScriptAssemblyType type, PlatformType platform, bool debug, string outputDir)
  169. {
  170. LibraryEntry[] scriptEntries = ProjectLibrary.Search("*", new ResourceType[] { ResourceType.ScriptCode });
  171. List<string> scriptFiles = new List<string>();
  172. for (int i = 0; i < scriptEntries.Length; i++)
  173. {
  174. if(scriptEntries[i].Type != LibraryEntryType.File)
  175. continue;
  176. FileEntry fileEntry = (FileEntry)scriptEntries[i];
  177. ScriptCodeImportOptions io = (ScriptCodeImportOptions) fileEntry.Options;
  178. if (io.EditorScript && type == ScriptAssemblyType.Editor ||
  179. !io.EditorScript && type == ScriptAssemblyType.Game)
  180. {
  181. scriptFiles.Add(scriptEntries[i].Path);
  182. }
  183. }
  184. string[] assemblyFolders;
  185. string[] assemblies;
  186. string outputFile;
  187. string[] frameworkAssemblies = BuildManager.GetFrameworkAssemblies(platform);
  188. if (type == ScriptAssemblyType.Game)
  189. {
  190. assemblyFolders = new string[]
  191. {
  192. EditorApplication.BuiltinAssemblyPath,
  193. EditorApplication.FrameworkAssemblyPath
  194. };
  195. assemblies = new string[frameworkAssemblies.Length + 1];
  196. assemblies[assemblies.Length - 1] = EditorApplication.EngineAssembly;
  197. outputFile = Path.Combine(outputDir, EditorApplication.ScriptGameAssembly);
  198. }
  199. else
  200. {
  201. assemblyFolders = new string[]
  202. {
  203. EditorApplication.BuiltinAssemblyPath,
  204. EditorApplication.FrameworkAssemblyPath,
  205. EditorApplication.ScriptAssemblyPath
  206. };
  207. assemblies = new string[frameworkAssemblies.Length + 3];
  208. assemblies[assemblies.Length - 1] = EditorApplication.EngineAssembly;
  209. assemblies[assemblies.Length - 2] = EditorApplication.EditorAssembly;
  210. assemblies[assemblies.Length - 3] = EditorApplication.ScriptGameAssembly;
  211. outputFile = Path.Combine(outputDir, EditorApplication.ScriptEditorAssembly);
  212. }
  213. Array.Copy(frameworkAssemblies, assemblies, frameworkAssemblies.Length);
  214. string defines = BuildManager.GetDefines(platform);
  215. return new CompilerInstance(scriptFiles.ToArray(), defines, assemblyFolders, assemblies, debug, outputFile);
  216. }
  217. }
  218. public enum CompilerMessageType
  219. {
  220. Warning, Error
  221. }
  222. public struct CompilerMessage
  223. {
  224. public CompilerMessageType type;
  225. public string message;
  226. public string file;
  227. public int line;
  228. public int column;
  229. }
  230. }