11using GameFrameX . Apps . Common . Event ;
2- using GameFrameX . Core . Abstractions ;
32using GameFrameX . Core . Abstractions . Agent ;
43using GameFrameX . Core . Abstractions . Events ;
54using GameFrameX . Hotfix . Logic . Server . Server ;
65using GameFrameX . Utility . Extensions ;
6+ using GameFrameX . Utility . Log ;
77using GameFrameX . Utility . Setting ;
88
9- namespace GameFrameX . Hotfix . Common . Events
9+ namespace GameFrameX . Hotfix . Common . Events ;
10+
11+ public static class EventDispatcherExtensions
1012{
11- public static class EventDispatcherExtensions
13+ public static void Dispatch ( this IComponentAgent agent , int evtId , Param args = null )
1214 {
13- public static void Dispatch ( this IComponentAgent agent , int evtId , Param args = null )
15+ var evt = new Event
1416 {
15- var evt = new Event
16- {
17- EventId = evtId ,
18- Data = args
19- } ;
17+ EventId = evtId ,
18+ Data = args ,
19+ } ;
2020
21- // 自己处理
22- SelfHandle ( agent , evtId , evt ) ;
21+ // 自己处理
22+ SelfHandle ( agent , evtId , evt ) ;
23+
24+ if ( ( EventId ) evtId > EventId . RoleSeparator && agent . OwnerType > GlobalConst . ActorTypeSeparator )
25+ {
26+ // 全局非玩家事件,抛给所有玩家
27+ agent . Tell ( ( )
28+ =>
29+ {
30+ return ServerComponentAgent . OnlineRoleForeach ( role
31+ =>
32+ {
33+ role . Dispatch ( evtId , args ) ;
34+ } ) ;
35+ } ) ;
36+ }
37+ }
2338
24- if ( ( EventId ) evtId > EventId . RoleSeparator && agent . OwnerType > GlobalConst . ActorTypeSeparator )
39+ private static void SelfHandle ( IComponentAgent agent , int evtId , Event evt )
40+ {
41+ agent . Tell ( async ( ) =>
42+ {
43+ // 事件需要在本actor内执行,不可多线程执行,所以不能使用Task.WhenAll来处理
44+ var listeners = HotfixManager . FindListeners ( agent . OwnerType , evtId ) ;
45+ if ( listeners . IsNullOrEmpty ( ) )
2546 {
26- // 全局非玩家事件,抛给所有玩家
27- agent . Tell ( ( )
28- => ServerComponentAgent . OnlineRoleForeach ( role
29- => role . Dispatch ( evtId , args ) ) ) ;
47+ // Log.Warn($"事件:{(EventID)evtId} 没有找到任何监听者");
48+ return ;
3049 }
3150
32- static void SelfHandle ( IComponentAgent agent , int evtId , Event evt )
51+ foreach ( var listener in listeners )
3352 {
34- agent . Tell ( async ( ) =>
53+ var comp = await agent . GetComponentAgent ( listener . AgentType ) ;
54+ try
3555 {
36- // 事件需要在本actor内执行,不可多线程执行,所以不能使用Task.WhenAll来处理
37- var listeners = HotfixManager . FindListeners ( agent . OwnerType , evtId ) ;
38- if ( listeners . IsNullOrEmpty ( ) )
39- {
40- // Log.Warn($"事件:{(EventID)evtId} 没有找到任何监听者");
41- return ;
42- }
43-
44- foreach ( var listener in listeners )
45- {
46- var comp = await agent . GetComponentAgent ( listener . AgentType ) ;
47- await listener . HandleEvent ( comp , evt ) ;
48- }
49- } ) ;
56+ await listener . HandleEvent ( comp , evt ) ;
57+ }
58+ catch ( Exception exception )
59+ {
60+ LogHelper . Error ( exception ) ;
61+ }
5062 }
51- }
63+ } ) ;
64+ }
5265
53- public static void Dispatch ( this IComponentAgent agent , EventId evtId , Param args = null )
54- {
55- Dispatch ( agent , ( int ) evtId , args ) ;
56- }
66+ public static void Dispatch ( this IComponentAgent agent , EventId evtId , Param args = null )
67+ {
68+ Dispatch ( agent , ( int ) evtId , args ) ;
5769 }
5870}
0 commit comments