Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Ink / Editor / Tools / Build Validation / InkPreBuildValidationCheck.cs
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEditor.Build;
  4. using System.Text;
  5. using Ink.UnityIntegration;
  6. using System.Linq;
  7. #if UNITY_2018_1_OR_NEWER
  8. using UnityEditor.Build.Reporting;
  9. #endif
  10. class InkPreBuildValidationCheck :
  11. #if UNITY_2018_1_OR_NEWER
  12. IPreprocessBuildWithReport
  13. #else
  14. IPreprocessBuild
  15. #endif
  16. {
  17. public int callbackOrder { get { return 0; } }
  18. #if UNITY_2018_1_OR_NEWER
  19. public void OnPreprocessBuild(BuildReport report) {
  20. PreprocessValidationStep();
  21. }
  22. #else
  23. public void OnPreprocessBuild(BuildTarget target, string path) {
  24. PreprocessValidationStep();
  25. }
  26. #endif
  27. static void PreprocessValidationStep () {
  28. // If we're compiling, we've throw an error to cancel the build. Exit out immediately.
  29. if(!AssertNotCompiling()) return;
  30. EnsureInkIsCompiled();
  31. }
  32. // Prevent building if ink is currently compiling.
  33. // Ideally we'd force it to complete instantly.
  34. // It seems you can do this with WaitHandle.WaitAll but I'm out of my depth!
  35. // Info here - https://stackoverflow.com/questions/540078/wait-for-pooled-threads-to-complete
  36. static bool AssertNotCompiling () {
  37. if(InkCompiler.executingCompilationStack) {
  38. StringBuilder sb = new StringBuilder("Ink is currently compiling!");
  39. var errorString = sb.ToString();
  40. InkCompiler.SetBuildBlocked();
  41. if(UnityEditor.EditorUtility.DisplayDialog("Ink Build Error!", errorString, "Ok")) {
  42. Debug.LogError(errorString);
  43. }
  44. return false;
  45. }
  46. return true;
  47. }
  48. // Immediately compile any files that aren't compiled and should be.
  49. static void EnsureInkIsCompiled () {
  50. var filesToRecompile = InkLibrary.GetFilesRequiringRecompile();
  51. if(filesToRecompile.Any()) {
  52. if(InkSettings.instance.compileAllFilesAutomatically) {
  53. InkCompiler.CompileInk(filesToRecompile.ToArray(), true, null);
  54. }
  55. }
  56. }
  57. }