|
@@ -72,7 +72,7 @@ namespace ET
|
|
|
|
|
|
|
|
private readonly Dictionary<Type, List<EventInfo>> allEvents = new();
|
|
private readonly Dictionary<Type, List<EventInfo>> allEvents = new();
|
|
|
|
|
|
|
|
- private Dictionary<Type, Dictionary<int, object>> allCallbacks = new Dictionary<Type, Dictionary<int, object>>();
|
|
|
|
|
|
|
+ private Dictionary<Type, Dictionary<int, object>> allInvokes = new();
|
|
|
|
|
|
|
|
private TypeSystems typeSystems = new();
|
|
private TypeSystems typeSystems = new();
|
|
|
|
|
|
|
@@ -169,7 +169,7 @@ namespace ET
|
|
|
{
|
|
{
|
|
|
EventAttribute eventAttribute = attr as EventAttribute;
|
|
EventAttribute eventAttribute = attr as EventAttribute;
|
|
|
|
|
|
|
|
- Type eventType = obj.GetEventType();
|
|
|
|
|
|
|
+ Type eventType = obj.Type;
|
|
|
|
|
|
|
|
EventInfo eventInfo = new(obj, eventAttribute.SceneType);
|
|
EventInfo eventInfo = new(obj, eventAttribute.SceneType);
|
|
|
|
|
|
|
@@ -181,34 +181,34 @@ namespace ET
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- this.allCallbacks = new Dictionary<Type, Dictionary<int, object>>();
|
|
|
|
|
- foreach (Type type in types[typeof (CallbackAttribute)])
|
|
|
|
|
|
|
+ this.allInvokes = new Dictionary<Type, Dictionary<int, object>>();
|
|
|
|
|
+ foreach (Type type in types[typeof (InvokeAttribute)])
|
|
|
{
|
|
{
|
|
|
object obj = Activator.CreateInstance(type);
|
|
object obj = Activator.CreateInstance(type);
|
|
|
- ICallbackType iCallbackType = obj as ICallbackType;
|
|
|
|
|
- if (iCallbackType == null)
|
|
|
|
|
|
|
+ IInvoke iInvoke = obj as IInvoke;
|
|
|
|
|
+ if (iInvoke == null)
|
|
|
{
|
|
{
|
|
|
throw new Exception($"type not is callback: {type.Name}");
|
|
throw new Exception($"type not is callback: {type.Name}");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- object[] attrs = type.GetCustomAttributes(typeof(CallbackAttribute), false);
|
|
|
|
|
|
|
+ object[] attrs = type.GetCustomAttributes(typeof(InvokeAttribute), false);
|
|
|
foreach (object attr in attrs)
|
|
foreach (object attr in attrs)
|
|
|
{
|
|
{
|
|
|
- if (!this.allCallbacks.TryGetValue(iCallbackType.Type, out var dict))
|
|
|
|
|
|
|
+ if (!this.allInvokes.TryGetValue(iInvoke.Type, out var dict))
|
|
|
{
|
|
{
|
|
|
dict = new Dictionary<int, object>();
|
|
dict = new Dictionary<int, object>();
|
|
|
- this.allCallbacks.Add(iCallbackType.Type, dict);
|
|
|
|
|
|
|
+ this.allInvokes.Add(iInvoke.Type, dict);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- CallbackAttribute callbackAttribute = attr as CallbackAttribute;
|
|
|
|
|
|
|
+ InvokeAttribute invokeAttribute = attr as InvokeAttribute;
|
|
|
|
|
|
|
|
try
|
|
try
|
|
|
{
|
|
{
|
|
|
- dict.Add(callbackAttribute.Id, obj);
|
|
|
|
|
|
|
+ dict.Add(invokeAttribute.Type, obj);
|
|
|
}
|
|
}
|
|
|
catch (Exception e)
|
|
catch (Exception e)
|
|
|
{
|
|
{
|
|
|
- throw new Exception($"action type duplicate: {iCallbackType.Type.Name} {callbackAttribute.Id}", e);
|
|
|
|
|
|
|
+ throw new Exception($"action type duplicate: {iInvoke.Type.Name} {invokeAttribute.Type}", e);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
@@ -712,44 +712,49 @@ namespace ET
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void Callback<A>(A args) where A: struct, ICallback
|
|
|
|
|
|
|
+ // Invoke跟Publish的区别(特别注意)
|
|
|
|
|
+ // Invoke类似函数,必须有被调用方,否则异常,调用者跟被调用者属于同一模块,比如MoveComponent中的Timer计时器,调用跟被调用的代码均属于移动模块
|
|
|
|
|
+ // 既然Invoke跟函数一样,那么为什么不使用函数呢? 因为有时候不方便直接调用,比如Config加载,在客户端跟服务端加载方式不一样。比如TimerComponent需要根据Id分发
|
|
|
|
|
+ // 注意,不要把Invoke当函数使用,这样会造成代码可读性降低,能用函数不要用Invoke
|
|
|
|
|
+ // publish是事件,抛出去可以没人订阅,调用者跟被调用者属于两个模块,比如任务系统需要知道道具使用的信息,则订阅道具使用事件
|
|
|
|
|
+ public void Invoke<A>(int type, A args) where A: struct
|
|
|
{
|
|
{
|
|
|
- if (!this.allCallbacks.TryGetValue(typeof(A), out var callbackHandlers))
|
|
|
|
|
|
|
+ if (!this.allInvokes.TryGetValue(typeof(A), out var invokeHandlers))
|
|
|
{
|
|
{
|
|
|
- throw new Exception($"Callback error: {typeof(A).Name}");
|
|
|
|
|
|
|
+ throw new Exception($"Invoke error: {typeof(A).Name}");
|
|
|
}
|
|
}
|
|
|
- if (!callbackHandlers.TryGetValue(args.Id, out var callbackHandler))
|
|
|
|
|
|
|
+ if (!invokeHandlers.TryGetValue(type, out var invokeHandler))
|
|
|
{
|
|
{
|
|
|
- throw new Exception($"Callback error: {typeof(A).Name} {args.Id}");
|
|
|
|
|
|
|
+ throw new Exception($"Invoke error: {typeof(A).Name} {type}");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- var aCallbackHandler = callbackHandler as ACallbackHandler<A>;
|
|
|
|
|
- if (aCallbackHandler == null)
|
|
|
|
|
|
|
+ var aInvokeHandler = invokeHandler as AInvokeHandler<A>;
|
|
|
|
|
+ if (aInvokeHandler == null)
|
|
|
{
|
|
{
|
|
|
- throw new Exception($"Callback error, not ACallbackHandler: {typeof(A).Name} {args.Id}");
|
|
|
|
|
|
|
+ throw new Exception($"Invoke error, not AInvokeHandler: {typeof(A).Name} {type}");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- aCallbackHandler.Handle(args);
|
|
|
|
|
|
|
+ aInvokeHandler.Handle(args);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public T Callback<A, T>(A args) where A: struct, ICallback
|
|
|
|
|
|
|
+ public T Invoke<A, T>(int type, A args) where A: struct
|
|
|
{
|
|
{
|
|
|
- if (!this.allCallbacks.TryGetValue(typeof(A), out var callbackHandlers))
|
|
|
|
|
|
|
+ if (!this.allInvokes.TryGetValue(typeof(A), out var invokeHandlers))
|
|
|
{
|
|
{
|
|
|
- throw new Exception($"ResultCallback error: {typeof(A).Name}");
|
|
|
|
|
|
|
+ throw new Exception($"Invoke error: {typeof(A).Name}");
|
|
|
}
|
|
}
|
|
|
- if (!callbackHandlers.TryGetValue(args.Id, out var callbackHandler))
|
|
|
|
|
|
|
+ if (!invokeHandlers.TryGetValue(type, out var invokeHandler))
|
|
|
{
|
|
{
|
|
|
- throw new Exception($"ResultCallback error: {typeof(A).Name} {args.Id}");
|
|
|
|
|
|
|
+ throw new Exception($"Invoke error: {typeof(A).Name} {type}");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- var aCallbackHandler = callbackHandler as ACallbackHandler<A, T>;
|
|
|
|
|
- if (aCallbackHandler == null)
|
|
|
|
|
|
|
+ var aInvokeHandler = invokeHandler as AInvokeHandler<A, T>;
|
|
|
|
|
+ if (aInvokeHandler == null)
|
|
|
{
|
|
{
|
|
|
- throw new Exception($"ResultCallback error, not AResultCallbackHandler: {typeof(T).Name} {args.Id}");
|
|
|
|
|
|
|
+ throw new Exception($"Invoke error, not AInvokeHandler: {typeof(T).Name} {type}");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return aCallbackHandler.Handle(args);
|
|
|
|
|
|
|
+ return aInvokeHandler.Handle(args);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public override string ToString()
|
|
public override string ToString()
|