BuildUtils.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using System.Text.RegularExpressions;
  5. namespace XGame.Editor.Build
  6. {
  7. internal static class BuildUtils
  8. {
  9. /// <summary>
  10. /// 回调函数必须是静态的
  11. /// </summary>
  12. /// <param name="callback"></param>
  13. /// <returns></returns>
  14. public static string CallbackToString(Action<int> callback)
  15. {
  16. return $"<callback type:{callback.Method.ReflectedType.AssemblyQualifiedName} method:{callback.Method.Name} /callback>";
  17. }
  18. public static string GetCommandLineArgs()
  19. {
  20. string[] args = Environment.GetCommandLineArgs();
  21. int index = args.ToList().IndexOf("--args");
  22. if (index >= args.Length) return string.Empty;
  23. string res = args[index + 1];
  24. if (res[0] == '-' || res.IndexOf("--") == 0) return string.Empty;
  25. return res;
  26. }
  27. public static void CmdCompleted(BuildErrorCode code, BuildContext context)
  28. {
  29. BuildLog.Log($"CmdCompleted. Code:{code}");
  30. if (code == BuildErrorCode.CmdCompleted)
  31. {
  32. var cmd = CmdFactory.Create(context);
  33. if (cmd != null)
  34. {
  35. // 还有Cmd接着执行
  36. cmd.Start();
  37. return;
  38. }
  39. else
  40. {
  41. //结束
  42. code = BuildErrorCode.Succeeded;
  43. }
  44. }
  45. else
  46. {
  47. BuildLog.Warn($"Build Completed. Code:{code}");
  48. }
  49. BuildCompleted(context.config.callback, code);
  50. BuildLog.Log($"context.config.editor.autoExit: {context.config.editor.autoExit}");
  51. if (context.config.editor.autoExit)
  52. {
  53. ExitApplication(code);
  54. }
  55. }
  56. public static void BuildCompleted(string callback, BuildErrorCode code)
  57. {
  58. BuildLog.Log($"OnBuildCompleted. callback:{callback} code:{code}");
  59. if (string.IsNullOrEmpty(callback) == false)
  60. {
  61. try
  62. {
  63. Regex regex = new Regex(@"<callback type:(.+?) method:(.+?) /callback>", RegexOptions.Multiline);
  64. var match = regex.Match(callback);
  65. if (match.Success)
  66. {
  67. var typeName = match.Groups[1].Value;
  68. var methodName = match.Groups[2].Value;
  69. Type type = Type.GetType(typeName);
  70. var methodInfo = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  71. methodInfo?.Invoke(null, new object[] { (int)code });
  72. }
  73. }
  74. catch (Exception ex)
  75. {
  76. BuildLog.Exception(ex);
  77. }
  78. }
  79. }
  80. public static void ExitApplication(BuildErrorCode code)
  81. {
  82. UnityEditor.EditorApplication.Exit(ErrorCodeToInt(code));
  83. }
  84. private static int ErrorCodeToInt(BuildErrorCode code)
  85. {
  86. switch (code)
  87. {
  88. case BuildErrorCode.Succeeded:
  89. return 0;
  90. case BuildErrorCode.Unknown:
  91. return 1;
  92. default:
  93. return (int)code;
  94. }
  95. }
  96. }
  97. }