BuildEventHelper.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. namespace XGame.Editor.Build
  5. {
  6. /// <summary>
  7. /// 准备开始构建安装包,仅在无参的静态方法上添加有效
  8. /// </summary>
  9. [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
  10. public class PrepareBuildCallBackAttribute : Attribute
  11. {
  12. }
  13. /// <summary>
  14. /// 安装包构建完成,仅在带有一个int类型参数的静态方法上添加有效
  15. /// </summary>
  16. [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
  17. public class BuildCompletedCallBackAttribute : Attribute
  18. {
  19. }
  20. internal static class BuildEventHelper
  21. {
  22. private static void AddMethods<T>(List<MethodInfo> methodInfos, Type type) where T : Attribute
  23. {
  24. var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
  25. foreach (var method in methods)
  26. {
  27. var attr = method.GetCustomAttribute<T>(false);
  28. if (null == attr) continue;
  29. methodInfos.Add(method);
  30. }
  31. }
  32. private static List<MethodInfo> CollectCallBackMethod<T>() where T : Attribute
  33. {
  34. List<MethodInfo> methodInfos = new List<MethodInfo>();
  35. try
  36. {
  37. var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  38. foreach (var assembly in assemblies)
  39. {
  40. var assemblyName = assembly.GetName().Name;
  41. //UnityEngine.Debug.Log($"CollectCallBackMethod assembly Name:{assemblyName}");
  42. if (assemblyName.StartsWith("Unity", StringComparison.Ordinal) || assemblyName.StartsWith("System", StringComparison.Ordinal))
  43. {
  44. continue;
  45. }
  46. var types = assembly.GetTypes();
  47. foreach (var type in types)
  48. {
  49. AddMethods<T>(methodInfos, type);
  50. }
  51. }
  52. }
  53. catch (Exception ex)
  54. {
  55. BuildLog.Exception(ex);
  56. }
  57. return methodInfos;
  58. }
  59. /// <summary>
  60. /// 通知即将开始构建
  61. /// </summary>
  62. internal static void InvokeBeforeBuild()
  63. {
  64. var methodInfos = CollectCallBackMethod<PrepareBuildCallBackAttribute>();
  65. object[] parameters = null;
  66. foreach (var method in methodInfos)
  67. {
  68. try
  69. {
  70. method.Invoke(null, parameters);
  71. }
  72. catch (Exception e)
  73. {
  74. BuildLog.Error("PrepareBuildCallBack failed: {0}.{1}", method.ReflectedType.FullName, method.Name);
  75. BuildLog.Exception(e);
  76. }
  77. }
  78. }
  79. /// <summary>
  80. /// 通知构建已结束
  81. /// </summary>
  82. internal static void InvokeAfterBuild(int code)
  83. {
  84. var methodInfos = CollectCallBackMethod<BuildCompletedCallBackAttribute>();
  85. object[] parameters = new object[1] { code };
  86. foreach (var method in methodInfos)
  87. {
  88. try
  89. {
  90. method.Invoke(null, parameters);
  91. }
  92. catch (Exception e)
  93. {
  94. BuildLog.Error("BuildCompletedCallBack failed: {0}.{1}", method.ReflectedType.FullName, method.Name);
  95. BuildLog.Exception(e);
  96. }
  97. }
  98. }
  99. }
  100. }