Kaynağa Gözat

hotfix层订阅mono层消息也改成action回调方式

tanghai 8 yıl önce
ebeveyn
işleme
f78852f0ef
33 değiştirilmiş dosya ile 173 ekleme ve 184 silme
  1. 0 1
      Server/Hotfix/Handler/C2G_EnterMapHandler.cs
  2. 2 0
      Server/Hotfix/Handler/C2G_LoginGateHandler.cs
  3. 0 1
      Server/Hotfix/Handler/C2R_LoginHandler.cs
  4. 0 1
      Server/Hotfix/Handler/G2M_CreateUnitHandler.cs
  5. 1 1
      Unity/Assets/Scripts/Base/Message/AMHandler.cs
  6. 1 1
      Unity/Assets/Scripts/Base/Message/IMHandler.cs
  7. 0 6
      Unity/Assets/Scripts/Base/Message/MessageHandlerAttribute.cs
  8. 26 0
      Unity/Assets/Scripts/Base/Message/MessageProxy.cs
  9. 2 2
      Unity/Assets/Scripts/Base/Message/MessageProxy.cs.meta
  10. 1 1
      Unity/Assets/Scripts/Base/Message/OpcodeHelper.cs
  11. 0 3
      Unity/Assets/Scripts/Base/UI.meta
  12. 26 80
      Unity/Assets/Scripts/Component/MessageDispatherComponent.cs
  13. 1 1
      Unity/Assets/Scripts/Entity/Session.cs
  14. 1 1
      Unity/Assets/Scripts/Event/NumericChangeEvent_NotifyWatcher.cs
  15. 1 1
      Unity/Assets/Scripts/Handler/Actor_CreateUnitsHandler.cs
  16. 1 1
      Unity/Assets/Scripts/Handler/Actor_TestHandler.cs
  17. 1 1
      Unity/Assets/Scripts/Handler/Frame_ClickMapHandler.cs
  18. 3 0
      Unity/Assets/Scripts/Helper/ILHelper.cs
  19. 3 3
      Unity/Assets/Scripts/Init.cs
  20. 9 0
      Unity/Assets/Scripts/Module.meta
  21. 0 0
      Unity/Assets/Scripts/UI/LayerNames.cs
  22. 0 0
      Unity/Assets/Scripts/UI/LayerNames.cs.meta
  23. 0 1
      Unity/Assets/ThirdParty/ILRuntime/Generated/CLRBindings.cs
  24. 0 53
      Unity/Assets/ThirdParty/ILRuntime/Generated/Model_MessageHandlerAttribute_Binding.cs
  25. 9 1
      Unity/Hotfix/Base/Object/Component.cs
  26. 13 0
      Unity/Hotfix/Handler/G2C_TestHotfixHandler.cs
  27. 1 1
      Unity/Hotfix/Module/HotfixMessage/AMHandler.cs
  28. 8 0
      Unity/Hotfix/Module/HotfixMessage/HotfixMessage.cs
  29. 6 4
      Unity/Hotfix/Module/HotfixMessage/HotfixOpcode.cs
  30. 1 1
      Unity/Hotfix/Module/HotfixMessage/IMHandler.cs
  31. 45 9
      Unity/Hotfix/Module/HotfixMessage/MessageDispatherComponent.cs
  32. 2 3
      Unity/Hotfix/Unity.Hotfix.csproj
  33. 9 6
      Unity/Unity.csproj

+ 0 - 1
Server/Hotfix/Handler/C2G_EnterMapHandler.cs

@@ -12,7 +12,6 @@ namespace Hotfix
 			G2C_EnterMap response = new G2C_EnterMap();
 			try
 			{
-				Log.Debug(MongoHelper.ToJson(message));
 				Player player = session.GetComponent<SessionPlayerComponent>().Player;
 				// 在map服务器上创建战斗Unit
 				IPEndPoint mapAddress = Game.Scene.GetComponent<StartConfigComponent>().MapConfigs[0].GetComponent<InnerConfig>().IPEndPoint;

+ 2 - 0
Server/Hotfix/Handler/C2G_LoginGateHandler.cs

@@ -26,6 +26,8 @@ namespace Hotfix
 
 				response.PlayerId = player.Id;
 				reply(response);
+
+				session.Send(new G2C_TestHotfixMessage() { Info = "recv hotfix message success" });
 			}
 			catch (Exception e)
 			{

+ 0 - 1
Server/Hotfix/Handler/C2R_LoginHandler.cs

@@ -10,7 +10,6 @@ namespace Hotfix
 	{
 		protected override async void Run(Session session, C2R_Login message, Action<R2C_Login> reply)
 		{
-			Log.Debug(message.ToJson());
 			R2C_Login response = new R2C_Login();
 			try
 			{

+ 0 - 1
Server/Hotfix/Handler/G2M_CreateUnitHandler.cs

@@ -28,7 +28,6 @@ namespace Hotfix
 					{
 						actorCreateUnits.Units.Add(new UnitInfo() {UnitId = u.Id, X = (int)(u.Position.X * 1000), Z = (int)(u.Position.Z * 1000) });
 					}
-					Log.Debug($"{MongoHelper.ToJson(actorCreateUnits)}");
 					MessageHelper.Broadcast(actorCreateUnits);
 				}
 			}

+ 1 - 1
Unity/Assets/Scripts/Base/Message/AMHandler.cs

@@ -6,7 +6,7 @@ namespace Model
 	{
 		protected abstract void Run(Session session, Message message);
 
-		public void Handle(Session session, IMessage msg)
+		public void Handle(Session session, object msg)
 		{
 			Message message = msg as Message;
 			if (message == null)

+ 1 - 1
Unity/Assets/Scripts/Base/Message/IMHandler.cs

@@ -4,7 +4,7 @@ namespace Model
 {
 	public interface IMHandler
 	{
-		void Handle(Session session, IMessage message);
+		void Handle(Session session, object message);
 		Type GetMessageType();
 	}
 }

+ 0 - 6
Unity/Assets/Scripts/Base/Message/MessageHandlerAttribute.cs

@@ -7,11 +7,5 @@ namespace Model
 	/// </summary>
 	public class MessageHandlerAttribute: Attribute
 	{
-		public ushort Opcode { get; }
-
-		public MessageHandlerAttribute(ushort opcode)
-		{
-			this.Opcode = opcode;
-		}
 	}
 }

+ 26 - 0
Unity/Assets/Scripts/Base/Message/MessageProxy.cs

@@ -0,0 +1,26 @@
+using System;
+
+namespace Model
+{
+	public class MessageProxy: IMHandler
+	{
+		private readonly Type type;
+		private readonly Action<Session, object> action;
+
+		public MessageProxy(Type type, Action<Session, object> action)
+		{
+			this.type = type;
+			this.action = action;
+		}
+
+		public void Handle(Session session, object message)
+		{
+			this.action.Invoke(session, message);
+		}
+
+		public Type GetMessageType()
+		{
+			return this.type;
+		}
+	}
+}

+ 2 - 2
Unity/Assets/ThirdParty/ILRuntime/Generated/Model_MessageHandlerAttribute_Binding.cs.meta → Unity/Assets/Scripts/Base/Message/MessageProxy.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
-guid: 23504b1d8aefb6441846b0cd8cf75f10
-timeCreated: 1517835576
+guid: 56412570d68dd7247ad5cf67dc3703f6
+timeCreated: 1518229433
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/Scripts/Base/Message/OpcodeHelper.cs

@@ -24,7 +24,7 @@ namespace Model
 
 		public static bool IsClientHotfixMessage(ushort opcode)
 		{
-			return opcode < 1000;
+			return opcode > 10000;
 		}
 	}
 }

+ 0 - 3
Unity/Assets/Scripts/Base/UI.meta

@@ -1,3 +0,0 @@
-fileFormatVersion: 2
-guid: e64b29d0fee5409889d0f320e76d9920
-timeCreated: 1504163155

+ 26 - 80
Unity/Assets/Scripts/Component/MessageDispatherComponent.cs

@@ -1,58 +1,8 @@
 using System;
 using System.Collections.Generic;
-using System.Reflection;
-using ILRuntime.CLR.Method;
-using ILRuntime.Runtime.Intepreter;
 
 namespace Model
 {
-	/// <summary>
-	/// 用来抹平ILRuntime跟mono层差异
-	/// </summary>
-	public interface IMessageMethod
-	{
-		void Run(Session session, IMessage a);
-	}
-
-	public class IMessageMonoMethod : IMessageMethod
-	{
-		private readonly IMHandler iMHandler;
-
-		public IMessageMonoMethod(IMHandler iMHandler)
-		{
-			this.iMHandler = iMHandler;
-		}
-
-		public void Run(Session session, IMessage a)
-		{
-			this.iMHandler.Handle(session, a);
-		}
-	}
-
-	public class IMessageILMethod : IMessageMethod
-	{
-		private readonly ILRuntime.Runtime.Enviorment.AppDomain appDomain;
-		private readonly ILTypeInstance instance;
-		private readonly IMethod method;
-		private readonly object[] param;
-
-		public IMessageILMethod(Type type, string methodName)
-		{
-			appDomain = Init.Instance.AppDomain;
-			this.instance = this.appDomain.Instantiate(type.FullName);
-			this.method = this.instance.Type.GetMethod(methodName, 2);
-			int n = this.method.ParameterCount;
-			this.param = new object[n];
-		}
-
-		public void Run(Session session, IMessage a)
-		{
-			this.param[0] = a;
-			this.appDomain.Invoke(this.method, this.instance, param);
-		}
-	}
-
-
 	[ObjectSystem]
 	public class MessageDispatherComponentSystem : ObjectSystem<MessageDispatherComponent>, IAwake, ILoad
 	{
@@ -72,7 +22,7 @@ namespace Model
 	/// </summary>
 	public class MessageDispatherComponent : Component
 	{
-		private Dictionary<ushort, List<IMessageMethod>> handlers;
+		private readonly Dictionary<ushort, List<IMHandler>> handlers = new Dictionary<ushort, List<IMHandler>>();
 
 
 		public void Awake()
@@ -82,7 +32,7 @@ namespace Model
 
 		public void Load()
 		{
-			handlers = new Dictionary<ushort, List<IMessageMethod>>();
+			this.handlers.Clear();
 
 			Type[] types = DllHelper.GetMonoTypes();
 
@@ -93,53 +43,49 @@ namespace Model
 				{
 					continue;
 				}
-				MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
-				IMHandler iMHandler = (IMHandler)Activator.CreateInstance(type);
-				if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode))
-				{
-					this.handlers.Add(messageHandlerAttribute.Opcode, new List<IMessageMethod>());
-				}
-				this.handlers[messageHandlerAttribute.Opcode].Add(new IMessageMonoMethod(iMHandler));
-			}
 
-			// hotfix dll
-			Type[] hotfixTypes = DllHelper.GetHotfixTypes();
-			foreach (Type type in hotfixTypes)
-			{
-				object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
-				if (attrs.Length == 0)
+				IMHandler iMHandler = Activator.CreateInstance(type) as IMHandler;
+				if (iMHandler == null)
 				{
+					Log.Error($"message handle {type.Name} 需要继承 IMHandler");
 					continue;
 				}
-				MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
-#if ILRuntime
-				IMessageMethod iMessageMethod = new IMessageILMethod(type, "Handle");
-#else
-				IMHandler iMHandler = (IMHandler)Activator.CreateInstance(type);
-				IMessageMethod iMessageMethod = new IMessageMonoMethod(iMHandler);
-#endif
-				if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode))
+
+				Type messageType = iMHandler.GetMessageType();
+				ushort opcode = this.Entity.GetComponent<OpcodeTypeComponent>().GetOpcode(messageType);
+				if (opcode == 0)
 				{
-					this.handlers.Add(messageHandlerAttribute.Opcode, new List<IMessageMethod>());
+					Log.Error($"消息opcode为0: {messageType.Name}");
+					continue;
 				}
-				this.handlers[messageHandlerAttribute.Opcode].Add(iMessageMethod);
+				this.RegisterHandler(opcode, iMHandler);
 			}
 		}
 
+		public void RegisterHandler(ushort opcode, IMHandler handler)
+		{
+			Log.Debug($"11111111111111111111111 {opcode}");
+			if (!this.handlers.ContainsKey(opcode))
+			{
+				this.handlers.Add(opcode, new List<IMHandler>());
+			}
+			this.handlers[opcode].Add(handler);
+		}
+
 		public void Handle(Session session, MessageInfo messageInfo)
 		{
-			List<IMessageMethod> actions;
+			List<IMHandler> actions;
 			if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions))
 			{
 				Log.Error($"消息 {messageInfo.Opcode} 没有处理");
 				return;
 			}
-
-			foreach (IMessageMethod ev in actions)
+			
+			foreach (IMHandler ev in actions)
 			{
 				try
 				{
-					ev.Run(session, messageInfo.Message);
+					ev.Handle(session, messageInfo.Message);
 				}
 				catch (Exception e)
 				{

+ 1 - 1
Unity/Assets/Scripts/Entity/Session.cs

@@ -139,7 +139,7 @@ namespace Model
 				Opcode = opcode,
 				Bytes = packet.Bytes
 			};
-
+			
 			if ((flag & 0xC0) > 0)
 			{
 				uint rpcId = packet.RpcId();

+ 1 - 1
Unity/Assets/Scripts/Event/NumericChangeEvent_NotifyWatcher.cs

@@ -1,7 +1,7 @@
 namespace Model
 {
 	// 分发数值监听
-	[Event((int)EventIdType.NumbericChange)]
+	[Event(EventIdType.NumbericChange)]
 	public class NumericChangeEvent_NotifyWatcher: AEvent<NumericType, long, int>
 	{
 		public override void Run(NumericType numericType, long id, int value)

+ 1 - 1
Unity/Assets/Scripts/Handler/Actor_CreateUnitsHandler.cs

@@ -2,7 +2,7 @@
 
 namespace Model
 {
-	[MessageHandler(Opcode.Actor_CreateUnits)]
+	[MessageHandler]
 	public class Actor_CreateUnitsHandler : AMHandler<Actor_CreateUnits>
 	{
 		protected override void Run(Session session, Actor_CreateUnits message)

+ 1 - 1
Unity/Assets/Scripts/Handler/Actor_TestHandler.cs

@@ -1,6 +1,6 @@
 namespace Model
 {
-	[MessageHandler(Opcode.Actor_Test)]
+	[MessageHandler]
 	public class Actor_TestHandler : AMHandler<Actor_Test>
 	{
 		protected override void Run(Session session, Actor_Test message)

+ 1 - 1
Unity/Assets/Scripts/Handler/Frame_ClickMapHandler.cs

@@ -2,7 +2,7 @@
 
 namespace Model
 {
-	[MessageHandler(Opcode.Frame_ClickMap)]
+	[MessageHandler]
 	public class Frame_ClickMapHandler : AMHandler<Frame_ClickMap>
 	{
 		protected override void Run(Session session, Frame_ClickMap message)

+ 3 - 0
Unity/Assets/Scripts/Helper/ILHelper.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Reflection;
 using ILRuntime.CLR.Method;
 using ILRuntime.Runtime.Enviorment;
+using ILRuntime.Runtime.Generated;
 using ILRuntime.Runtime.Intepreter;
 using UnityEngine;
 
@@ -28,7 +29,9 @@ namespace Model
 			Init.Instance.AppDomain.DelegateManager.RegisterMethodDelegate<byte[], int, int>();
 			Init.Instance.AppDomain.DelegateManager.RegisterMethodDelegate<IResponse>();
 			Init.Instance.AppDomain.DelegateManager.RegisterMethodDelegate<Session, PacketInfo>();
+			Init.Instance.AppDomain.DelegateManager.RegisterMethodDelegate<Session, object>();
 
+			 CLRBindings.Initialize(Init.Instance.AppDomain);
 
 			// 注册适配器
 			Assembly assembly = typeof(Init).Assembly;

+ 3 - 3
Unity/Assets/Scripts/Init.cs

@@ -54,8 +54,10 @@ namespace Model
 				Game.Scene.GetComponent<ResourcesComponent>().LoadBundle("config.unity3d");
 				Game.Scene.AddComponent<ConfigComponent>();
 				Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle("config.unity3d");
-				
+
+				Game.Scene.AddComponent<OpcodeTypeComponent>();
 				Game.Scene.AddComponent<MessageDispatherComponent>();
+
 #if ILRuntime
 				Log.Debug("run in ilruntime mode");
 
@@ -75,8 +77,6 @@ namespace Model
 				this.start = new MonoStaticMethod(hotfixInit, "Start");
 #endif
 
-				Game.Scene.AddComponent<OpcodeTypeComponent>();
-
 				// 进入热更新层
 				this.start.Run();
 

+ 9 - 0
Unity/Assets/Scripts/Module.meta

@@ -0,0 +1,9 @@
+fileFormatVersion: 2
+guid: 603e07e4a68080946b7b1a5c7869e59b
+folderAsset: yes
+timeCreated: 1518228693
+licenseType: Free
+DefaultImporter:
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 0 - 0
Unity/Assets/Scripts/Base/UI/LayerNames.cs → Unity/Assets/Scripts/UI/LayerNames.cs


+ 0 - 0
Unity/Assets/Scripts/Base/UI/LayerNames.cs.meta → Unity/Assets/Scripts/UI/LayerNames.cs.meta


+ 0 - 1
Unity/Assets/ThirdParty/ILRuntime/Generated/CLRBindings.cs

@@ -34,7 +34,6 @@ namespace ILRuntime.Runtime.Generated
             Model_AEventAttribute_Binding.Register(app);
             System_Collections_Generic_Dictionary_2_Type_Queue_1_ILTypeInstance_Binding.Register(app);
             System_Collections_Generic_Dictionary_2_UInt16_List_1_ILTypeInstance_Binding.Register(app);
-            Model_MessageHandlerAttribute_Binding.Register(app);
             System_Collections_Generic_List_1_ILTypeInstance_Binding.Register(app);
             System_Collections_Generic_List_1_ILTypeInstance_Binding_Enumerator_Binding.Register(app);
             Model_MessageAttribute_Binding.Register(app);

+ 0 - 53
Unity/Assets/ThirdParty/ILRuntime/Generated/Model_MessageHandlerAttribute_Binding.cs

@@ -1,53 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Reflection;
-using System.Runtime.InteropServices;
-
-using ILRuntime.CLR.TypeSystem;
-using ILRuntime.CLR.Method;
-using ILRuntime.Runtime.Enviorment;
-using ILRuntime.Runtime.Intepreter;
-using ILRuntime.Runtime.Stack;
-using ILRuntime.Reflection;
-using ILRuntime.CLR.Utils;
-
-namespace ILRuntime.Runtime.Generated
-{
-    unsafe class Model_MessageHandlerAttribute_Binding
-    {
-        public static void Register(ILRuntime.Runtime.Enviorment.AppDomain app)
-        {
-            BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
-            MethodBase method;
-            FieldInfo field;
-            Type[] args;
-            Type type = typeof(Model.MessageHandlerAttribute);
-            args = new Type[]{};
-            method = type.GetMethod("get_Opcode", flag, null, args, null);
-            app.RegisterCLRMethodRedirection(method, get_Opcode_0);
-
-
-        }
-
-
-        static StackObject* get_Opcode_0(ILIntepreter __intp, StackObject* __esp, IList<object> __mStack, CLRMethod __method, bool isNewObj)
-        {
-            ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;
-            StackObject* ptr_of_this_method;
-            StackObject* __ret = ILIntepreter.Minus(__esp, 1);
-            ptr_of_this_method = ILIntepreter.Minus(__esp, 1);
-            Model.MessageHandlerAttribute instance_of_this_method;
-            instance_of_this_method = (Model.MessageHandlerAttribute)typeof(Model.MessageHandlerAttribute).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
-            __intp.Free(ptr_of_this_method);
-
-            var result_of_this_method = instance_of_this_method.Opcode;
-
-            __ret->ObjectType = ObjectTypes.Integer;
-            __ret->Value = result_of_this_method;
-            return __ret + 1;
-        }
-
-
-
-    }
-}

+ 9 - 1
Unity/Hotfix/Base/Object/Component.cs

@@ -5,13 +5,21 @@ namespace Hotfix
 	public abstract class Component : Disposer
 	{
 		[BsonIgnore]
-		public Entity Parent { get; set; }
+		public Disposer Parent { get; set; }
 
 		public T GetParent<T>() where T : Entity
 		{
 			return this.Parent as T;
 		}
 
+		public Entity Entity
+		{
+			get
+			{
+				return this.Parent as Entity;
+			}
+		}
+
 		protected Component()
 		{
 			this.Id = 1;

+ 13 - 0
Unity/Hotfix/Handler/G2C_TestHotfixHandler.cs

@@ -0,0 +1,13 @@
+using Model;
+
+namespace Hotfix
+{
+	[MessageHandler]
+	public class G2C_TestHotfixMessageHandler : AMHandler<G2C_TestHotfixMessage>
+	{
+		protected override void Run(Session session, G2C_TestHotfixMessage message)
+		{
+			Log.Debug(message.Info);
+		}
+	}
+}

+ 1 - 1
Unity/Hotfix/Module/HotfixMessage/AMHandler.cs

@@ -7,7 +7,7 @@ namespace Hotfix
 	{
 		protected abstract void Run(Session session, Message message);
 
-		public void Handle(Session session, IMessage msg)
+		public void Handle(Session session, object msg)
 		{
 			Message message = msg as Message;
 			if (message == null)

+ 8 - 0
Unity/Hotfix/Module/HotfixMessage/HotfixMessage.cs

@@ -53,4 +53,12 @@ namespace Hotfix
 		[ProtoMember(91, IsRequired = true)]
 		public string Message { get; set; }
 	}
+
+	[Message(HotfixOpcode.G2C_TestHotfixMessage)]
+	[ProtoContract]
+	public class G2C_TestHotfixMessage : MessageObject, IMessage
+	{
+		[ProtoMember(1, IsRequired = true)]
+		public string Info;
+	}
 }

+ 6 - 4
Unity/Hotfix/Module/HotfixMessage/HotfixOpcode.cs

@@ -2,9 +2,11 @@ namespace Hotfix
 {
 	public static class HotfixOpcode
 	{
-		 public const ushort C2R_Login = 101;
-		 public const ushort R2C_Login = 102;
-		 public const ushort C2G_LoginGate = 103;
-		 public const ushort G2C_LoginGate = 104;
+		
+		public const ushort C2R_Login = 10001;
+		public const ushort R2C_Login = 10002;
+		public const ushort C2G_LoginGate = 10003;
+		public const ushort G2C_LoginGate = 10004;
+		public const ushort G2C_TestHotfixMessage = 10005;
 	}
 }

+ 1 - 1
Unity/Hotfix/Module/HotfixMessage/IMHandler.cs

@@ -6,7 +6,7 @@ namespace Hotfix
 #if ILRuntime
 	public interface IMHandler
 	{
-		void Handle(Session session, IMessage message);
+		void Handle(Session session, object message);
 		Type GetMessageType();
 	}
 #else

+ 45 - 9
Unity/Hotfix/Module/HotfixMessage/MessageDispatherComponent.cs

@@ -23,7 +23,7 @@ namespace Hotfix
 	/// </summary>
 	public class MessageDispatherComponent : Component
 	{
-		private Dictionary<ushort, List<IMHandler>> handlers;
+		private readonly Dictionary<ushort, List<IMHandler>> handlers = new Dictionary<ushort, List<IMHandler>>();
 
 		public void Awake()
 		{
@@ -32,9 +32,13 @@ namespace Hotfix
 
 		public void Load()
 		{
-			this.handlers = new Dictionary<ushort, List<IMHandler>>();
-			
+			this.handlers.Clear();
+
+			Model.MessageDispatherComponent messageDispatherComponent = Game.Scene.GetComponent<Model.MessageDispatherComponent>();
+			Model.OpcodeTypeComponent opcodeTypeComponent = Game.Scene.GetComponent<Model.OpcodeTypeComponent>();
+
 			Type[] types = DllHelper.GetHotfixTypes();
+
 			foreach (Type type in types)
 			{
 				object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
@@ -42,17 +46,49 @@ namespace Hotfix
 				{
 					continue;
 				}
-				MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
-				IMHandler iMHandler = (IMHandler)Activator.CreateInstance(type);
-				if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode))
+
+				IMHandler iMHandler = Activator.CreateInstance(type) as IMHandler;
+				if (iMHandler == null)
+				{
+					Log.Error($"message handle {type.Name} 需要继承 IMHandler");
+					continue;
+				}
+				
+				Type messageType = iMHandler.GetMessageType();
+
+				ushort opcode = this.Entity.GetComponent<OpcodeTypeComponent>().GetOpcode(messageType);
+				if (opcode == 0)
 				{
-					this.handlers.Add(messageHandlerAttribute.Opcode, new List<IMHandler>());
+					Log.Error($"消息opcode为0: {messageType.Name}");
+					continue;
 				}
-				this.handlers[messageHandlerAttribute.Opcode].Add(iMHandler);
+				this.RegisterHandler(opcode, iMHandler);
+
+				// 尝试注册到mono层
+				if (messageDispatherComponent != null && opcodeTypeComponent != null)
+				{
+					ushort monoOpcode = opcodeTypeComponent.GetOpcode(messageType);
+					if (monoOpcode == 0)
+					{
+						continue;
+					}
+
+					MessageProxy messageProxy = new MessageProxy(messageType, (session, o) => { iMHandler.Handle(session, o); });
+					messageDispatherComponent.RegisterHandler(monoOpcode, messageProxy);
+				}
+			}
+		}
+
+		public void RegisterHandler(ushort opcode, IMHandler handler)
+		{
+			if (!this.handlers.ContainsKey(opcode))
+			{
+				this.handlers.Add(opcode, new List<IMHandler>());
 			}
+			this.handlers[opcode].Add(handler);
 		}
 
-		public void Handle(Session session, ushort opcode, IMessage message)
+		public void Handle(Session session, ushort opcode, object message)
 		{
 			if (!this.handlers.TryGetValue(opcode, out List<IMHandler> actions))
 			{

+ 2 - 3
Unity/Hotfix/Unity.Hotfix.csproj

@@ -55,6 +55,7 @@
     <Compile Include="Base\Helper\AssetBundleHelper.cs" />
     <Compile Include="Base\Helper\ExceptionHelper.cs" />
     <Compile Include="Event\TestHotfixSubscribMonoEvent_LogString.cs" />
+    <Compile Include="Handler\G2C_TestHotfixHandler.cs" />
     <Compile Include="Module\HotfixMessage\HotfixMessageDispatcher.cs" />
     <Compile Include="Module\HotfixMessage\IMessage.cs" />
     <Compile Include="Module\HotfixMessage\AMHandler.cs" />
@@ -91,9 +92,7 @@
     <Compile Include="UI\UILogin\Event\InitSceneStart_CreateLoginUI.cs" />
     <Compile Include="UI\UILogin\Factory\UILoginFactory.cs" />
   </ItemGroup>
-  <ItemGroup>
-    <Folder Include="Handler\" />
-  </ItemGroup>
+  <ItemGroup />
   <ItemGroup>
     <ProjectReference Include="..\Unity.csproj">
       <Project>{cf118143-7e37-744f-be45-3f55345fec40}</Project>

+ 9 - 6
Unity/Unity.csproj

@@ -12,12 +12,15 @@
     <ProjectTypeGuids>{E097FAD1-6243-4DAD-9C02-E9B9EFC3FFC1};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
     <TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier>
     <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
-    <TargetFrameworkProfile></TargetFrameworkProfile>
-    <CompilerResponseFile></CompilerResponseFile>
+    <TargetFrameworkProfile>
+    </TargetFrameworkProfile>
+    <CompilerResponseFile>
+    </CompilerResponseFile>
     <UnityProjectType>Game:1</UnityProjectType>
     <UnityBuildTarget>StandaloneWindows64:19</UnityBuildTarget>
     <UnityVersion>2017.1.1p4</UnityVersion>
-    <RootNamespace></RootNamespace>
+    <RootNamespace>
+    </RootNamespace>
     <LangVersion>6</LangVersion>
   </PropertyGroup>
   <PropertyGroup>
@@ -213,6 +216,7 @@
     <Compile Include="Assets\Scripts\Base\Message\AppType.cs" />
     <Compile Include="Assets\Scripts\Base\Message\ClientDispatcher.cs" />
     <Compile Include="Assets\Scripts\Base\Message\ErrorCode.cs" />
+    <Compile Include="Assets\Scripts\Base\Message\MessageProxy.cs" />
     <Compile Include="Assets\Scripts\Base\Message\IMessage.cs" />
     <Compile Include="Assets\Scripts\Base\Message\IMessageDispatcher.cs" />
     <Compile Include="Assets\Scripts\Base\Message\IMessagePacker.cs" />
@@ -259,7 +263,6 @@
     <Compile Include="Assets\Scripts\Base\Object\ObjectSystemAttribute.cs" />
     <Compile Include="Assets\Scripts\Base\QueueDictionary.cs" />
     <Compile Include="Assets\Scripts\Base\TryLocker.cs" />
-    <Compile Include="Assets\Scripts\Base\UI\LayerNames.cs" />
     <Compile Include="Assets\Scripts\BehaviorTreeNode\CreateUIEffect.cs" />
     <Compile Include="Assets\Scripts\BehaviorTreeNode\Root\Root.cs" />
     <Compile Include="Assets\Scripts\BehaviorTreeNode\Root\UICreateRoot.cs" />
@@ -343,6 +346,7 @@
     <Compile Include="Assets\Scripts\Other\ReferenceCollector.cs" />
     <Compile Include="Assets\Scripts\Other\UIFactoryAttribute.cs" />
     <Compile Include="Assets\Scripts\Other\UIType.cs" />
+    <Compile Include="Assets\Scripts\UI\LayerNames.cs" />
     <Compile Include="Assets\Scripts\UI\UILoading\Component\UILoadingComponent.cs" />
     <Compile Include="Assets\Scripts\UI\UILoading\Event\LoadingBeginEvent_CreateLoadingUI.cs" />
     <Compile Include="Assets\Scripts\UI\UILoading\Event\LoadingFinishEvent_RemoveLoadingUI.cs" />
@@ -369,7 +373,6 @@
     <Compile Include="Assets\ThirdParty\ILRuntime\Generated\Model_IStart_Binding.cs" />
     <Compile Include="Assets\ThirdParty\ILRuntime\Generated\Model_Log_Binding.cs" />
     <Compile Include="Assets\ThirdParty\ILRuntime\Generated\Model_MessageAttribute_Binding.cs" />
-    <Compile Include="Assets\ThirdParty\ILRuntime\Generated\Model_MessageHandlerAttribute_Binding.cs" />
     <Compile Include="Assets\ThirdParty\ILRuntime\Generated\Model_MongoHelper_Binding.cs" />
     <Compile Include="Assets\ThirdParty\ILRuntime\Generated\Model_NetworkComponent_Binding.cs" />
     <Compile Include="Assets\ThirdParty\ILRuntime\Generated\Model_NetworkHelper_Binding.cs" />
@@ -753,4 +756,4 @@
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Target Name="GenerateTargetFrameworkMonikerAttribute" />
-</Project>
+</Project>