1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using XGame;
- using XGame.Framework.Data;
- using XGame.Framework.Network;
- namespace FL.Data
- {
- public class GmToolsService : DataSingleton<GmToolsService>, IDisposable
- {
- private Dictionary<int, Type> _msgRequestMap;
- /// <summary>
- /// 查询消息
- /// </summary>
- /// <param name="protoId"></param>
- /// <returns></returns>
- public IMsgRequest QueryMessage(int protoId)
- {
- if (_msgRequestMap == null)
- {
- _msgRequestMap = new Dictionary<int, Type>();
- var msgAssembly = "Business.Domain";
- var msgRequestType = typeof(IMsgRequest);
- var types = AppDomain.CurrentDomain.GetAssemblies()
- .First(assembly => assembly.GetName().Name.Equals(msgAssembly))
- .GetTypes();
- foreach (var type in types)
- {
- if (! msgRequestType.IsAssignableFrom(type))
- continue;
- var instance = Activator.CreateInstance(type) as IMessage;
- _msgRequestMap.Add(instance.ProtocolID, type);
- //Log.Info($"MsgRequest Id:{instance.ProtocolID} Type:{type}");
- }
- }
- if (_msgRequestMap.TryGetValue(protoId, out var protoType))
- {
- return Activator.CreateInstance(protoType) as IMsgRequest;
- }
- Log.Info($"找不到 MsgRequest. ProtoId:{protoId}");
- return null;
- }
- /// <summary>
- /// 发送修改后的消息
- /// </summary>
- /// <param name="msg"></param>
- public void SendMessage(IMsgRequest msg)
- {
- NetModule.Request(msg);
- }
- void IDisposable.Dispose()
- {
- _msgRequestMap?.Clear();
- _msgRequestMap = null;
- }
- }
- }
|