GmToolsService.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using XGame;
  5. using XGame.Framework.Data;
  6. using XGame.Framework.Network;
  7. namespace FL.Data
  8. {
  9. public class GmToolsService : DataSingleton<GmToolsService>, IDisposable
  10. {
  11. private Dictionary<int, Type> _msgRequestMap;
  12. /// <summary>
  13. /// 查询消息
  14. /// </summary>
  15. /// <param name="protoId"></param>
  16. /// <returns></returns>
  17. public IMsgRequest QueryMessage(int protoId)
  18. {
  19. if (_msgRequestMap == null)
  20. {
  21. _msgRequestMap = new Dictionary<int, Type>();
  22. var msgAssembly = "Business.Domain";
  23. var msgRequestType = typeof(IMsgRequest);
  24. var types = AppDomain.CurrentDomain.GetAssemblies()
  25. .First(assembly => assembly.GetName().Name.Equals(msgAssembly))
  26. .GetTypes();
  27. foreach (var type in types)
  28. {
  29. if (! msgRequestType.IsAssignableFrom(type))
  30. continue;
  31. var instance = Activator.CreateInstance(type) as IMessage;
  32. _msgRequestMap.Add(instance.ProtocolID, type);
  33. //Log.Info($"MsgRequest Id:{instance.ProtocolID} Type:{type}");
  34. }
  35. }
  36. if (_msgRequestMap.TryGetValue(protoId, out var protoType))
  37. {
  38. return Activator.CreateInstance(protoType) as IMsgRequest;
  39. }
  40. Log.Info($"找不到 MsgRequest. ProtoId:{protoId}");
  41. return null;
  42. }
  43. /// <summary>
  44. /// 发送修改后的消息
  45. /// </summary>
  46. /// <param name="msg"></param>
  47. public void SendMessage(IMsgRequest msg)
  48. {
  49. NetModule.Request(msg);
  50. }
  51. void IDisposable.Dispose()
  52. {
  53. _msgRequestMap?.Clear();
  54. _msgRequestMap = null;
  55. }
  56. }
  57. }