using System;
using System.Collections.Generic;
using System.Reflection;
namespace XGame.Editor.Build
{
///
/// 准备开始构建安装包,仅在无参的静态方法上添加有效
///
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class PrepareBuildCallBackAttribute : Attribute
{
}
///
/// 安装包构建完成,仅在带有一个int类型参数的静态方法上添加有效
///
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class BuildCompletedCallBackAttribute : Attribute
{
}
internal static class BuildEventHelper
{
private static void AddMethods(List methodInfos, Type type) where T : Attribute
{
var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
foreach (var method in methods)
{
var attr = method.GetCustomAttribute(false);
if (null == attr) continue;
methodInfos.Add(method);
}
}
private static List CollectCallBackMethod() where T : Attribute
{
List methodInfos = new List();
try
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assembly in assemblies)
{
var assemblyName = assembly.GetName().Name;
//UnityEngine.Debug.Log($"CollectCallBackMethod assembly Name:{assemblyName}");
if (assemblyName.StartsWith("Unity", StringComparison.Ordinal) || assemblyName.StartsWith("System", StringComparison.Ordinal))
{
continue;
}
var types = assembly.GetTypes();
foreach (var type in types)
{
AddMethods(methodInfos, type);
}
}
}
catch (Exception ex)
{
BuildLog.Exception(ex);
}
return methodInfos;
}
///
/// 通知即将开始构建
///
internal static void InvokeBeforeBuild()
{
var methodInfos = CollectCallBackMethod();
object[] parameters = null;
foreach (var method in methodInfos)
{
try
{
method.Invoke(null, parameters);
}
catch (Exception e)
{
BuildLog.Error("PrepareBuildCallBack failed: {0}.{1}", method.ReflectedType.FullName, method.Name);
BuildLog.Exception(e);
}
}
}
///
/// 通知构建已结束
///
internal static void InvokeAfterBuild(int code)
{
var methodInfos = CollectCallBackMethod();
object[] parameters = new object[1] { code };
foreach (var method in methodInfos)
{
try
{
method.Invoke(null, parameters);
}
catch (Exception e)
{
BuildLog.Error("BuildCompletedCallBack failed: {0}.{1}", method.ReflectedType.FullName, method.Name);
BuildLog.Exception(e);
}
}
}
}
}