MailboxDispatcherComponentSystem.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. public static class MailboxDispatcherComponentHelper
  23. {
  24. public static void Awake(this MailboxDispatcherComponent self)
  25. {
  26. self.Load();
  27. }
  28. public static void Load(this MailboxDispatcherComponent self)
  29. {
  30. AppType appType = StartConfigComponent.Instance.StartConfig.AppType;
  31. self.MailboxHandlers.Clear();
  32. List<Type> types = Game.EventSystem.GetTypes(typeof(MailboxHandlerAttribute));
  33. foreach (Type type in types)
  34. {
  35. object[] attrs = type.GetCustomAttributes(typeof(MailboxHandlerAttribute), false);
  36. if (attrs.Length == 0)
  37. {
  38. continue;
  39. }
  40. MailboxHandlerAttribute mailboxHandlerAttribute = (MailboxHandlerAttribute) attrs[0];
  41. if (!mailboxHandlerAttribute.Type.Is(appType))
  42. {
  43. continue;
  44. }
  45. object obj = Activator.CreateInstance(type);
  46. IMailboxHandler iMailboxHandler = obj as IMailboxHandler;
  47. if (iMailboxHandler == null)
  48. {
  49. throw new Exception($"actor handler not inherit IEntityActorHandler: {obj.GetType().FullName}");
  50. }
  51. self.MailboxHandlers.Add(mailboxHandlerAttribute.MailboxType, iMailboxHandler);
  52. }
  53. }
  54. /// <summary>
  55. /// 根据mailbox类型做不同的处理
  56. /// </summary>
  57. public static async ETTask Handle(
  58. this MailboxDispatcherComponent self, MailBoxComponent mailBoxComponent, Session session, object message)
  59. {
  60. IMailboxHandler iMailboxHandler;
  61. if (self.MailboxHandlers.TryGetValue(mailBoxComponent.MailboxType, out iMailboxHandler))
  62. {
  63. await iMailboxHandler.Handle(session, mailBoxComponent.Entity, message);
  64. }
  65. }
  66. }
  67. }