MailboxDispatcherComponentSystem.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. using ETModel;
  4. namespace ETHotfix
  5. {
  6. [ObjectSystem]
  7. public class MailboxDispatcherComponentAwakeSystem: AwakeSystem<MailboxDispatcherComponent>
  8. {
  9. public override void Awake(MailboxDispatcherComponent self)
  10. {
  11. self.Awake();
  12. }
  13. }
  14. [ObjectSystem]
  15. public class MailboxDispatcherComponentLoadSystem: LoadSystem<MailboxDispatcherComponent>
  16. {
  17. public override void Load(MailboxDispatcherComponent self)
  18. {
  19. self.Load();
  20. }
  21. }
  22. [ObjectSystem]
  23. public class MailboxDispatcherComponentDestroySystem: DestroySystem<MailboxDispatcherComponent>
  24. {
  25. public override void Destroy(MailboxDispatcherComponent self)
  26. {
  27. self.MailboxHandlers.Clear();
  28. }
  29. }
  30. public static class MailboxDispatcherComponentHelper
  31. {
  32. public static void Awake(this MailboxDispatcherComponent self)
  33. {
  34. MailboxDispatcherComponent.Instance = self;
  35. self.Load();
  36. }
  37. public static void Load(this MailboxDispatcherComponent self)
  38. {
  39. self.MailboxHandlers.Clear();
  40. HashSet<Type> types = Game.EventSystem.GetTypes(typeof(MailboxHandlerAttribute));
  41. foreach (Type type in types)
  42. {
  43. object[] attrs = type.GetCustomAttributes(typeof(MailboxHandlerAttribute), false);
  44. if (attrs.Length == 0)
  45. {
  46. continue;
  47. }
  48. MailboxHandlerAttribute mailboxHandlerAttribute = (MailboxHandlerAttribute) attrs[0];
  49. object obj = Activator.CreateInstance(type);
  50. if (!(obj is IMailboxHandler iMailboxHandler))
  51. {
  52. throw new Exception($"actor handler not inherit IEntityActorHandler: {obj.GetType().FullName}");
  53. }
  54. self.MailboxHandlers.Add((int)mailboxHandlerAttribute.MailboxType, iMailboxHandler);
  55. }
  56. }
  57. /// <summary>
  58. /// 根据mailbox类型做不同的处理
  59. /// </summary>
  60. public static async ETTask Handle(
  61. this MailboxDispatcherComponent self, Entity entity, MailboxType mailboxType, Session session, object message)
  62. {
  63. if (self.MailboxHandlers.TryGetValue((int) mailboxType, out IMailboxHandler iMailboxHandler))
  64. {
  65. await iMailboxHandler.Handle(session, entity, message);
  66. }
  67. }
  68. }
  69. }