static async Task triggerTimer(long actorId, string actorType, string handlerType, Param param) { try { var handler = HotfixMgr.GetInstance <ITimerHandler>(handlerType); var agentType = handler.GetType().BaseType.GenericTypeArguments[0]; if (agentType.GetInterface(typeof(IComponentActorAgent).FullName) != null) { //actor var agent = await ActorManager.GetOrNew(agentType, actorId); var actor = (ComponentActor)agent.Owner; _ = actor.SendAsync(() => handler.InternalHandleTimer(agent, param), false); } else if (agentType.GetInterface(typeof(IComponentAgent).FullName) != null) { //component var actorAgentType = HotfixMgr.GetType(actorType, agentType); var compType = agentType.BaseType.GenericTypeArguments[0]; var agent = await ActorManager.GetOrNew(actorAgentType, actorId); var actor = (ComponentActor)agent.Owner; var comp = await actor.GetComponent(compType); _ = actor.SendAsync(() => handler.InternalHandleTimer(comp.GetAgent(agentType), param), false); } } catch (Exception e) { LOGGER.Error(e.ToString()); } }
public void DispatchEvent(int evtType, Func <int, TActor, Type, Task <bool> > checkDispatchFunc, Param param = null) { lineActor.SendAsync(async() => { if (!eventHandlers.ContainsKey(evtType)) { return; } Event evt = new Event(); evt.EventId = evtType; evt.Data = param; var list = eventHandlers[evtType]; foreach (var evtInfo in list) { var actor = await ActorManager.Get <TActor>(evtInfo.ActorId); if (actor == null) { continue; } var info = evtInfo; //需要存个临时变量 _ = actor.SendAsync(async() => { try { var handler = HotfixMgr.GetInstance <IEventListener>(info.AgentHandler); var agentType = handler.GetType().BaseType.GenericTypeArguments[0]; if (agentType.GetInterface(typeof(IComponentActorAgent).FullName) != null) { //actor if (checkDispatchFunc != null && !(await checkDispatchFunc(evtType, actor, null))) { return; } await handler.InternalHandleEvent(actor.GetAgent(agentType), evt); } else if (agentType.GetInterface(typeof(IComponentAgent).FullName) != null) { //component var compType = agentType.BaseType.GenericTypeArguments[0]; if (checkDispatchFunc != null && !(await checkDispatchFunc(evtType, actor, compType))) { return; } var comp = await actor.GetComponent(compType); await handler.InternalHandleEvent(actor.GetAgent(agentType), evt); } } catch (Exception e) { LOGGER.Error(e.ToString()); } }, false); } }, false); }
/// <summary> /// 分发事件 /// </summary> public void DispatchEvent(int evtType, Param param = null) { if (HotfixMgr.IsFromHotfix(param)) { LOGGER.Fatal($"不能添加hotfix工程的类型作为参数 DispatchEvent {param.GetType()}"); return; } Event evt = new Event(); evt.EventId = evtType; evt.Data = param; if (eventHandlers.ContainsKey(evtType)) { var list = eventHandlers[evtType]; foreach (var handlerType in list) { try { var handler = HotfixMgr.GetInstance <IEventListener>(handlerType); var agentType = handler.GetType().BaseType.GenericTypeArguments[1]; if (agentType.GetInterface(typeof(IComponentActorAgent).FullName) != null) { //actor ownerActor.SendAsync(() => handler.InternalHandleEvent(ownerActor.GetAgent(agentType), evt)); } else if (agentType.GetInterface(typeof(IComponentAgent).FullName) != null) { //component var compType = agentType.BaseType.GenericTypeArguments[0]; ownerActor.SendAsync(async() => { var comp = await ownerActor.GetComponent(compType); await handler.InternalHandleEvent(comp.GetAgent(agentType), evt); }, false); } } catch (Exception e) { LOGGER.Error(e.ToString()); } } } }