Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

namespace QFramework
Expand Down Expand Up @@ -237,6 +238,11 @@ public static void UnRegisterEvent<T>(this IOnEvent<T> self) where T : struct
}
}

public interface IOnEventInArchitecture<T>
{
void OnEventInArchitecture(T e);
}

#endregion

#region Controller
Expand Down Expand Up @@ -467,6 +473,8 @@ public interface ICanRegisterEvent : IBelongToArchitecture

public static class CanRegisterEventExtension
{
private static readonly string mHandlerInArchitectureMethodName = "OnEventInArchitecture";

public static IUnRegister RegisterEvent<T>(this ICanRegisterEvent self, Action<T> onEvent)
{
return self.GetArchitecture().RegisterEvent<T>(onEvent);
Expand All @@ -476,6 +484,71 @@ public static void UnRegisterEvent<T>(this ICanRegisterEvent self, Action<T> onE
{
self.GetArchitecture().UnRegisterEvent<T>(onEvent);
}

private static void ForEachSpecificMethod(object obj, string methodName, Action<Type, Delegate> callback)
{
if (obj == null) return;

Type type = obj.GetType();
MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);
foreach (MethodInfo method in methods)
{
if (method.Name == methodName)
{
Type paramType = method.GetParameters()[0].ParameterType;
Type delegateType = typeof(Action<>).MakeGenericType(paramType);
var handler = method.CreateDelegate(delegateType, obj);

callback?.Invoke(paramType, handler);
}
}
}

/// <summary>
/// 注册所有通过 IOnEventInArchitecture<T> 接口实现的事件处理器
/// </summary>
/// <param name="self"></param>
/// <param name="obj"></param>
/// <param name="isAutoUnregister">是否在 gameObject 销毁后自动注销</param>
public static void RegisterAllEventInArchitecture(this ICanRegisterEvent self, object obj, bool isAutoUnregister = true)
{
if (obj == null) return;

GameObject gameObject = obj is Component ? (obj as Component).gameObject : null;
Type architectureType = self.GetArchitecture().GetType();

ForEachSpecificMethod(obj, mHandlerInArchitectureMethodName, (paramType, handler) =>
{
MethodInfo registerMethod = architectureType
.GetMethod("RegisterEvent", BindingFlags.Public | BindingFlags.Instance)
.MakeGenericMethod(paramType);
var unregister = registerMethod.Invoke(self.GetArchitecture(), new object[] { handler }) as IUnRegister;
if (isAutoUnregister && gameObject is not null)
{
unregister.UnRegisterWhenGameObjectDestroyed(gameObject);
}
});
}

/// <summary>
/// 注销所有通过 IOnEventInArchitecture<T> 接口实现的事件处理器
/// </summary>
/// <param name="self"></param>
/// <param name="obj"></param>
public static void UnRegisterAllEventInArchitecture(this ICanRegisterEvent self, object obj)
{
if (obj == null) return;

Type architectureType = self.GetArchitecture().GetType();

ForEachSpecificMethod(obj, mHandlerInArchitectureMethodName, (paramType, handler) =>
{
MethodInfo unregisterMethod = architectureType
.GetMethod("UnRegisterEvent", BindingFlags.Public | BindingFlags.Instance)
.MakeGenericMethod(paramType);
unregisterMethod.Invoke(self.GetArchitecture(), new object[] { handler });
});
}
}

public interface ICanSendCommand : IBelongToArchitecture
Expand Down Expand Up @@ -732,7 +805,7 @@ public BindableProperty(T defaultValue = default)
{
mValue = defaultValue;
}

protected T mValue;

public T Value
Expand Down