Просмотр исходного кода

1.改了跨域调用机制,mono层到hotfix层只在最开始start使用appdomain调用了一次,之后的所有从mono层到hotfix层全改成了action回调。这样性能将大大提升
2.事件机制现在完全抹平了差异,而且使用action回调实现hotfix层订阅mono层事件,性能大大提升

tanghai 8 лет назад
Родитель
Сommit
9600a334fc
37 измененных файлов с 475 добавлено и 377 удалено
  1. 0 1
      Server/Hotfix/Module/Network/OuterMessageDispatcher.cs
  2. 14 14
      Server/Model/Base/Object/EventSystem.cs
  3. 3 3
      Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeAfterChangeNodeTypeEvent_SelectNode.cs
  4. 3 3
      Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeClickNodeEvent_SelectNode.cs
  5. 3 3
      Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeConnectStateEvent_HandleConnectLines.cs
  6. 3 3
      Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeCreateNodeEvent_SelectNode.cs
  7. 3 3
      Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeMouseInNodeEvent_HandleOperate.cs
  8. 3 3
      Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeNewCreateClickEvent_CreateNode.cs
  9. 3 3
      Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeOpenEditorEvent_SelectNode.cs
  10. 3 3
      Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeOpenEditorEvent_UpdatePropList.cs
  11. 3 3
      Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeReplaceClickEvent_ReplaceNode.cs
  12. 3 3
      Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeRightDesignerDragEvent_ModifyRightBorder.cs
  13. 4 5
      Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeRunTreeEvent_ShowDebugInfo.cs
  14. 23 22
      Unity/Assets/Scripts/Base/Event/EventIdType.cs
  15. 93 18
      Unity/Assets/Scripts/Base/Event/IEvent.cs
  16. 5 6
      Unity/Assets/Scripts/Base/Message/ClientDispatcher.cs
  17. 46 0
      Unity/Assets/Scripts/Base/Object/EventProxy.cs
  18. 2 2
      Unity/Assets/Scripts/Base/Object/EventProxy.cs.meta
  19. 31 46
      Unity/Assets/Scripts/Base/Object/EventSystem.cs
  20. 0 102
      Unity/Assets/Scripts/Base/Object/IEventMethod.cs
  21. 2 1
      Unity/Assets/Scripts/Component/NetOuterComponent.cs
  22. 1 1
      Unity/Assets/Scripts/Component/OpcodeTypeComponent.cs
  23. 2 2
      Unity/Assets/Scripts/Event/NumericChangeEvent_NotifyWatcher.cs
  24. 3 0
      Unity/Assets/Scripts/Helper/ILHelper.cs
  25. 9 13
      Unity/Assets/Scripts/Init.cs
  26. 3 3
      Unity/Assets/Scripts/UI/UILoading/Event/LoadingBeginEvent_CreateLoadingUI.cs
  27. 3 3
      Unity/Assets/Scripts/UI/UILoading/Event/LoadingFinishEvent_RemoveLoadingUI.cs
  28. 2 2
      Unity/Hotfix/Base/Event/EventIdType.cs
  29. 97 33
      Unity/Hotfix/Base/Event/IEvent.cs
  30. 64 39
      Unity/Hotfix/Base/Object/EventSystem.cs
  31. 14 0
      Unity/Hotfix/Event/TestHotfixSubscribMonoEvent_LogString.cs
  32. 14 2
      Unity/Hotfix/Init.cs
  33. 3 5
      Unity/Hotfix/Module/HotfixMessage/HotfixMessageDispatcher.cs
  34. 4 21
      Unity/Hotfix/Module/HotfixMessage/SessionHelper.cs
  35. 3 3
      Unity/Hotfix/UI/UILogin/Event/InitSceneStart_CreateLoginUI.cs
  36. 2 2
      Unity/Hotfix/Unity.Hotfix.csproj
  37. 1 1
      Unity/Unity.csproj

+ 0 - 1
Server/Hotfix/Module/Network/OuterMessageDispatcher.cs

@@ -8,7 +8,6 @@ namespace Hotfix
 		public async void Dispatch(Session session, PacketInfo packetInfo)
 		{
 			Type messageType = Game.Scene.GetComponent<OpcodeTypeComponent>().GetType(packetInfo.Opcode);
-			Log.Debug($"111111111111 {MongoHelper.ToJson(packetInfo)}");
 			IMessage message = (IMessage)session.Network.MessagePacker.DeserializeFrom(messageType, packetInfo.Bytes, packetInfo.Index, packetInfo.Length);
 
 			// gate session收到actor消息直接转发给actor自己去处理

+ 14 - 14
Server/Model/Base/Object/EventSystem.cs

@@ -11,13 +11,13 @@ namespace Model
 		Hotfix,
 	}
 
-	public interface IObjectEvent
+	public interface IObjectSystem
 	{
 		Type Type();
 		void Set(object value);
 	}
 
-	public abstract class ObjectSystem<T> : IObjectEvent
+	public abstract class ObjectSystem<T> : IObjectSystem
 	{
 		private T value;
 
@@ -43,7 +43,7 @@ namespace Model
 
 		private readonly Dictionary<EventIdType, List<object>> allEvents = new Dictionary<EventIdType, List<object>>();
 
-		private readonly Dictionary<Type, IObjectEvent> disposerEvents = new Dictionary<Type, IObjectEvent>();
+		private readonly Dictionary<Type, IObjectSystem> disposerEvents = new Dictionary<Type, IObjectSystem>();
 
 		private Queue<Disposer> updates = new Queue<Disposer>();
 		private Queue<Disposer> updates2 = new Queue<Disposer>();
@@ -72,13 +72,13 @@ namespace Model
 				}
 
 				object obj = Activator.CreateInstance(type);
-				IObjectEvent objectEvent = obj as IObjectEvent;
-				if (objectEvent == null)
+				IObjectSystem objectSystem = obj as IObjectSystem;
+				if (objectSystem == null)
 				{
 					Log.Error($"组件事件没有继承IObjectEvent: {type.Name}");
 					continue;
 				}
-				this.disposerEvents[objectEvent.Type()] = objectEvent;
+				this.disposerEvents[objectSystem.Type()] = objectSystem;
 			}
 
 
@@ -114,7 +114,7 @@ namespace Model
 
 		public void Add(Disposer disposer)
 		{
-			if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectEvent objectEvent))
+			if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
 			{
 				return;
 			}
@@ -139,7 +139,7 @@ namespace Model
 		{
 			this.Add(disposer);
 
-			if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectEvent objectEvent))
+			if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
 			{
 				return;
 			}
@@ -156,7 +156,7 @@ namespace Model
 		{
 			this.Add(disposer);
 
-			if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectEvent objectEvent))
+			if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
 			{
 				throw new Exception($"{disposer.GetType().Name} not found awake1");
 			}
@@ -173,7 +173,7 @@ namespace Model
 		{
 			this.Add(disposer);
 
-			if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectEvent objectEvent))
+			if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
 			{
 				throw new Exception($"{disposer.GetType().Name} not found awake2");
 			}
@@ -190,7 +190,7 @@ namespace Model
 		{
 			this.Add(disposer);
 
-			if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectEvent objectEvent))
+			if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
 			{
 				throw new Exception($"{disposer.GetType().Name} not found awake3");
 			}
@@ -219,7 +219,7 @@ namespace Model
 					continue;
 				}
 
-				if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectEvent objectEvent))
+				if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
 				{
 					continue;
 				}
@@ -257,7 +257,7 @@ namespace Model
 					continue;
 				}
 
-				if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectEvent objectEvent))
+				if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
 				{
 					continue;
 				}
@@ -289,7 +289,7 @@ namespace Model
 					continue;
 				}
 
-				if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectEvent objectEvent))
+				if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
 				{
 					continue;
 				}

+ 3 - 3
Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeAfterChangeNodeTypeEvent_SelectNode.cs

@@ -2,10 +2,10 @@
 
 namespace MyEditor
 {
-	[Event((int)EventIdType.BehaviorTreeAfterChangeNodeType)]
-	public class BehaviorTreeAfterChangeNodeTypeEvent_SelectNode: IEvent
+	[Event(EventIdType.BehaviorTreeAfterChangeNodeType)]
+	public class BehaviorTreeAfterChangeNodeTypeEvent_SelectNode: AEvent
 	{
-		public void Run()
+		public override void Run()
 		{
 			NodeDesigner dstNode = BTEditorWindow.Instance.GraphDesigner.RootNode;
 			BTEditorWindow.Instance.OnSelectNode(dstNode.NodeData, dstNode);

+ 3 - 3
Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeClickNodeEvent_SelectNode.cs

@@ -2,10 +2,10 @@
 
 namespace MyEditor
 {
-	[Event((int)EventIdType.BehaviorTreeClickNode)]
-	public class BehaviorTreeClickNodeEvent_SelectNode: IEvent<NodeDesigner>
+	[Event(EventIdType.BehaviorTreeClickNode)]
+	public class BehaviorTreeClickNodeEvent_SelectNode: AEvent<NodeDesigner>
 	{
-		public void Run(NodeDesigner dstNode)
+		public override void Run(NodeDesigner dstNode)
 		{
 			BTEditorWindow.Instance.OnSelectNode(dstNode.NodeData, dstNode);
 		}

+ 3 - 3
Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeConnectStateEvent_HandleConnectLines.cs

@@ -2,10 +2,10 @@
 
 namespace MyEditor
 {
-	[Event((int)EventIdType.BehaviorTreeConnectState)]
-	public class BehaviorTreeConnectStateEvent_HandleConnectLines: IEvent<NodeDesigner, State>
+	[Event(EventIdType.BehaviorTreeConnectState)]
+	public class BehaviorTreeConnectStateEvent_HandleConnectLines: AEvent<NodeDesigner, State>
 	{
-		public void Run(NodeDesigner nodeDesigner, State state)
+		public override void Run(NodeDesigner nodeDesigner, State state)
 		{
 			BTEditorWindow.Instance.onStartConnect(nodeDesigner, state);
 		}

+ 3 - 3
Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeCreateNodeEvent_SelectNode.cs

@@ -2,10 +2,10 @@
 
 namespace MyEditor
 {
-	[Event((int)EventIdType.BehaviorTreeCreateNode)]
-	public class BehaviorTreeCreateNodeEvent_SelectNode: IEvent<NodeDesigner>
+	[Event(EventIdType.BehaviorTreeCreateNode)]
+	public class BehaviorTreeCreateNodeEvent_SelectNode: AEvent<NodeDesigner>
 	{
-		public void Run(NodeDesigner dstNode)
+		public override void Run(NodeDesigner dstNode)
 		{
 			BTEditorWindow.Instance.OnSelectNode(dstNode.NodeData, dstNode);
 		}

+ 3 - 3
Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeMouseInNodeEvent_HandleOperate.cs

@@ -2,10 +2,10 @@
 
 namespace MyEditor
 {
-	[Event((int)EventIdType.BehaviorTreeMouseInNode)]
-	public class BehaviorTreeMouseInNodeEvent_HandleOperate: IEvent<BehaviorNodeData, NodeDesigner>
+	[Event(EventIdType.BehaviorTreeMouseInNode)]
+	public class BehaviorTreeMouseInNodeEvent_HandleOperate: AEvent<BehaviorNodeData, NodeDesigner>
 	{
-		public void Run(BehaviorNodeData nodeData, NodeDesigner nodeDesigner)
+		public override void Run(BehaviorNodeData nodeData, NodeDesigner nodeDesigner)
 		{
 			BTEditorWindow.Instance.onMouseInNode(nodeData, nodeDesigner);
 		}

+ 3 - 3
Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeNewCreateClickEvent_CreateNode.cs

@@ -3,10 +3,10 @@ using UnityEngine;
 
 namespace MyEditor
 {
-	[Event((int)EventIdType.BehaviorTreePropertyDesignerNewCreateClick)]
-	public class BehaviorTreeNewCreateClickEvent_CreateNode: IEvent<string, Vector2>
+	[Event(EventIdType.BehaviorTreePropertyDesignerNewCreateClick)]
+	public class BehaviorTreeNewCreateClickEvent_CreateNode: AEvent<string, Vector2>
 	{
-		public void Run(string name, Vector2 pos)
+		public override void Run(string name, Vector2 pos)
 		{
 			BTEditorWindow.Instance.onCreateNode(name, pos);
 		}

+ 3 - 3
Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeOpenEditorEvent_SelectNode.cs

@@ -3,10 +3,10 @@ using UnityEngine;
 
 namespace MyEditor
 {
-	[Event((int)EventIdType.BehaviorTreeOpenEditor)]
-	public class BehaviorTreeOpenEditorEvent_SelectNode: IEvent
+	[Event(EventIdType.BehaviorTreeOpenEditor)]
+	public class BehaviorTreeOpenEditorEvent_SelectNode: AEvent
 	{
-		public void Run()
+		public override void Run()
 		{
 			NodeDesigner dstNode = BTEditorWindow.Instance.onCreateTree();
 			if (dstNode == null)

+ 3 - 3
Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeOpenEditorEvent_UpdatePropList.cs

@@ -2,10 +2,10 @@
 
 namespace MyEditor
 {
-	[Event((int)EventIdType.BehaviorTreeOpenEditor)]
-	public class BehaviorTreeOpenEditorEvent_UpdatePropList: IEvent
+	[Event(EventIdType.BehaviorTreeOpenEditor)]
+	public class BehaviorTreeOpenEditorEvent_UpdatePropList: AEvent
 	{
-		public void Run()
+		public override void Run()
 		{
 			BTEditorWindow.Instance.onUpdatePropList();
 		}

+ 3 - 3
Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeReplaceClickEvent_ReplaceNode.cs

@@ -3,10 +3,10 @@ using UnityEngine;
 
 namespace MyEditor
 {
-	[Event((int)EventIdType.BehaviorTreeReplaceClick)]
-	public class BehaviorTreeReplaceClickEvent_ReplaceNode: IEvent<string, Vector2>
+	[Event(EventIdType.BehaviorTreeReplaceClick)]
+	public class BehaviorTreeReplaceClickEvent_ReplaceNode: AEvent<string, Vector2>
 	{
-		public void Run(string name, Vector2 pos)
+		public override void Run(string name, Vector2 pos)
 		{
 			BTEditorWindow.Instance.onChangeNodeType(name, pos);
 		}

+ 3 - 3
Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeRightDesignerDragEvent_ModifyRightBorder.cs

@@ -2,10 +2,10 @@
 
 namespace MyEditor
 {
-	[Event((int)EventIdType.BehaviorTreeRightDesignerDrag)]
-	public class BehaviorTreeRightDesignerDragEvent_ModifyRightBorder: IEvent<float>
+	[Event(EventIdType.BehaviorTreeRightDesignerDrag)]
+	public class BehaviorTreeRightDesignerDragEvent_ModifyRightBorder: AEvent<float>
 	{
-		public void Run(float deltaX)
+		public override void Run(float deltaX)
 		{
 			BTEditorWindow.Instance.onDraggingRightDesigner(deltaX);
 		}

+ 4 - 5
Unity/Assets/Editor/BehaviorTreeEditor/Event/BehaviorTreeRunTreeEvent_ShowDebugInfo.cs

@@ -1,12 +1,11 @@
-using System.Collections.Generic;
-using Model;
+using Model;
 
 namespace MyEditor
 {
-	[Event((int)EventIdType.BehaviorTreeRunTreeEvent)]
-	public class BehaviorTreeRunTreeEvent_ShowDebugInfo: IEvent<BehaviorTree>
+	[Event(EventIdType.BehaviorTreeRunTreeEvent)]
+	public class BehaviorTreeRunTreeEvent_ShowDebugInfo: AEvent<BehaviorTree>
 	{
-		public void Run(BehaviorTree tree)
+		public override void Run(BehaviorTree tree)
 		{
 			if (BTEditor.Instance.CurTreeGO == null)
 			{

+ 23 - 22
Unity/Assets/Scripts/Base/Event/EventIdType.cs

@@ -1,32 +1,33 @@
 namespace Model
 {
-	public enum EventIdType
+	public static class EventIdType
 	{
-		InitSceneStart = 0,
+		public const int RecvHotfixMessage = 1;
 
-		RecvHotfixMessage,
+		public const int BehaviorTreeRunTreeEvent = 2;
+		public const int BehaviorTreeOpenEditor = 3;
+		public const int BehaviorTreeClickNode = 4;
+		public const int BehaviorTreeAfterChangeNodeType = 5;
+		public const int BehaviorTreeCreateNode = 6;
+		public const int BehaviorTreePropertyDesignerNewCreateClick = 7;
+		public const int BehaviorTreeMouseInNode = 8;
+		public const int BehaviorTreeConnectState = 9;
+		public const int BehaviorTreeReplaceClick = 10;
+		public const int BehaviorTreeRightDesignerDrag = 11;
 
-		BehaviorTreeRunTreeEvent,
-		BehaviorTreeOpenEditor,
-		BehaviorTreeClickNode,
-		BehaviorTreeAfterChangeNodeType,
-		BehaviorTreeCreateNode,
-		BehaviorTreePropertyDesignerNewCreateClick,
-		BehaviorTreeMouseInNode,
-		BehaviorTreeConnectState,
-		BehaviorTreeReplaceClick,
-		BehaviorTreeRightDesignerDrag,
+		public const int SessionRecvMessage = 12;
+		public const int NumbericChange = 13;
 
-		SessionRecvMessage,
-		NumbericChange,
+		public const int MessageDeserializeFinish = 14;
+		public const int SceneChange = 15;
+		public const int FrameUpdate = 16;
 
-		MessageDeserializeFinish,
-		SceneChange,
-		FrameUpdate,
-		
-		LoadingBegin,
-		LoadingFinish,
 
-		MaxModelEvent = 10000,
+		public const int LoadingBegin = 17;
+		public const int LoadingFinish = 18;
+
+		public const int TestHotfixSubscribMonoEvent = 19;
+
+		public const int MaxModelEvent = 10000;
 	}
 }

+ 93 - 18
Unity/Assets/Scripts/Base/Event/IEvent.cs

@@ -1,37 +1,112 @@
-namespace Model
+using System;
+
+namespace Model
 {
 	public interface IEvent
 	{
-		void Run();
+		void Handle();
+		void Handle(object a);
+		void Handle(object a, object b);
+		void Handle(object a, object b, object c);
 	}
 
-	public interface IEvent<in A>
+	public abstract class AEvent : IEvent
 	{
-		void Run(A uid);
-	}
+		public void Handle()
+		{
+			this.Run();
+		}
 
-	public interface IEvent<in A, in B>
-	{
-		void Run(A a, B b);
-	}
+		public void Handle(object a)
+		{
+			throw new NotImplementedException();
+		}
 
-	public interface IEvent<in A, in B, in C>
-	{
-		void Run(A a, B b, C c);
+		public void Handle(object a, object b)
+		{
+			throw new NotImplementedException();
+		}
+
+		public void Handle(object a, object b, object c)
+		{
+			throw new NotImplementedException();
+		}
+
+		public abstract void Run();
 	}
 
-	public interface IEvent<in A, in B, in C, in D>
+	public abstract class AEvent<A>: IEvent
 	{
-		void Run(A a, B b, C c, D d);
+		public void Handle()
+		{
+			throw new NotImplementedException();
+		}
+
+		public void Handle(object a)
+		{
+			this.Run((A)a);
+		}
+
+		public void Handle(object a, object b)
+		{
+			throw new NotImplementedException();
+		}
+
+		public void Handle(object a, object b, object c)
+		{
+			throw new NotImplementedException();
+		}
+
+		public abstract void Run(A a);
 	}
 
-	public interface IEvent<in A, in B, in C, in D, in E>
+	public abstract class AEvent<A, B>: IEvent
 	{
-		void Run(A a, B b, C c, D d, E e);
+		public void Handle()
+		{
+			throw new NotImplementedException();
+		}
+
+		public void Handle(object a)
+		{
+			throw new NotImplementedException();
+		}
+
+		public void Handle(object a, object b)
+		{
+			this.Run((A)a, (B)b);
+		}
+
+		public void Handle(object a, object b, object c)
+		{
+			throw new NotImplementedException();
+		}
+
+		public abstract void Run(A a, B b);
 	}
 
-	public interface IEvent<in A, in B, in C, in D, in E, in F>
+	public abstract class AEvent<A, B, C>: IEvent
 	{
-		void Run(A a, B b, C c, D d, E e, F f);
+		public void Handle()
+		{
+			throw new NotImplementedException();
+		}
+
+		public void Handle(object a)
+		{
+			throw new NotImplementedException();
+		}
+
+		public void Handle(object a, object b)
+		{
+			throw new NotImplementedException();
+		}
+
+		public void Handle(object a, object b, object c)
+		{
+			this.Run((A)a, (B)b, (C)c);
+		}
+
+		public abstract void Run(A a, B b, C c);
 	}
 }

+ 5 - 6
Unity/Assets/Scripts/Base/Message/ClientDispatcher.cs

@@ -4,17 +4,16 @@ namespace Model
 {
 	public class ClientDispatcher: IMessageDispatcher
 	{
+		// 热更层消息回调
+		public Action<Session, PacketInfo> HotfixCallback;
+
 		public void Dispatch(Session session, PacketInfo packetInfo)
 		{
-#if ILRuntime
-// 热更消息抛到hotfix层
-			if (OpcodeHelper.IsClientHotfixMessage(packetInfo.Header.Opcode))
+			if (OpcodeHelper.IsClientHotfixMessage(packetInfo.Opcode))
 			{
-				Game.EventSystem.Run(EventIdType.RecvHotfixMessage, packetInfo);
+				HotfixCallback.Invoke(session, packetInfo);
 				return;
 			}
-#endif
-
 
 			Type messageType = Game.Scene.GetComponent<OpcodeTypeComponent>().GetType(packetInfo.Opcode);
 			IMessage message = (IMessage)session.Network.MessagePacker.DeserializeFrom(messageType, packetInfo.Bytes, packetInfo.Index, packetInfo.Length);

+ 46 - 0
Unity/Assets/Scripts/Base/Object/EventProxy.cs

@@ -0,0 +1,46 @@
+using System;
+using System.Collections.Generic;
+
+namespace Model
+{
+	public class EventProxy: IEvent
+	{
+		public Action<List<object>> action;
+		public List<object> param = new List<object>();
+
+		public EventProxy(Action<List<object>> action)
+		{
+			this.action = action;
+		}
+
+		public void Handle()
+		{
+			this.param.Clear();
+			this.action.Invoke(this.param);
+		}
+
+		public void Handle(object a)
+		{
+			this.param.Clear();
+			this.param.Add(a);
+			this.action.Invoke(this.param);
+		}
+
+		public void Handle(object a, object b)
+		{
+			this.param.Clear();
+			this.param.Add(a);
+			this.param.Add(b);
+			this.action.Invoke(this.param);
+		}
+
+		public void Handle(object a, object b, object c)
+		{
+			this.param.Clear();
+			this.param.Add(a);
+			this.param.Add(b);
+			this.param.Add(c);
+			this.action.Invoke(this.param);
+		}
+	}
+}

+ 2 - 2
Unity/Assets/Scripts/Base/Object/IEventMethod.cs.meta → Unity/Assets/Scripts/Base/Object/EventProxy.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
-guid: 56f77b0dfb72d8e488d7ef044ede8dba
-timeCreated: 1516100437
+guid: 22310cc42f777f5439dea1ea1ca183e1
+timeCreated: 1518145019
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 31 - 46
Unity/Assets/Scripts/Base/Object/EventSystem.cs

@@ -56,7 +56,7 @@ namespace Model
 
 		private readonly Dictionary<DLLType, Assembly> assemblies = new Dictionary<DLLType, Assembly>();
 
-		private readonly Dictionary<EventIdType, List<IEventMethod>> allEvents = new Dictionary<EventIdType, List<IEventMethod>>();
+		private readonly Dictionary<int, List<IEvent>> allEvents = new Dictionary<int, List<IEvent>>();
 
 		private readonly Dictionary<Type, IObjectSystem> disposerEvents = new Dictionary<Type, IObjectSystem>();
 
@@ -73,8 +73,6 @@ namespace Model
 
 		private readonly HashSet<Disposer> unique = new HashSet<Disposer>();
 
-
-
 		public void LoadHotfixDll()
 		{
 #if ILRuntime
@@ -114,7 +112,6 @@ namespace Model
 				this.disposerEvents[objectSystem.Type()] = objectSystem;
 			}
 
-
 			this.allEvents.Clear();
 			foreach (Type type in types)
 			{
@@ -124,35 +121,23 @@ namespace Model
 				{
 					EventAttribute aEventAttribute = (EventAttribute)attr;
 					object obj = Activator.CreateInstance(type);
-					if (!this.allEvents.ContainsKey((EventIdType)aEventAttribute.Type))
+					IEvent iEvent = obj as IEvent;
+					if (iEvent == null)
 					{
-						this.allEvents.Add((EventIdType)aEventAttribute.Type, new List<IEventMethod>());
+						Log.Error($"{obj.GetType().Name} 没有继承IEvent");
 					}
-					this.allEvents[(EventIdType)aEventAttribute.Type].Add(new IEventMonoMethod(obj));
+					this.RegisterEvent(aEventAttribute.Type, iEvent);
 				}
 			}
+		}
 
-			// hotfix dll
-			Type[] hotfixTypes = DllHelper.GetHotfixTypes();
-			foreach (Type type in hotfixTypes)
+		public void RegisterEvent(int eventId, IEvent e)
+		{
+			if (!this.allEvents.ContainsKey(eventId))
 			{
-				object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
-				foreach (object attr in attrs)
-				{
-					EventAttribute aEventAttribute = (EventAttribute)attr;
-#if ILRuntime
-					IEventMethod method = new IEventILMethod(type, "Run");
-#else
-					object obj = Activator.CreateInstance(type);
-					IEventMethod method = new IEventMonoMethod(obj);
-#endif
-					if (!allEvents.ContainsKey((EventIdType)aEventAttribute.Type))
-					{
-						allEvents.Add((EventIdType)aEventAttribute.Type, new List<IEventMethod>());
-					}
-					allEvents[(EventIdType)aEventAttribute.Type].Add(method);
-				}
+				this.allEvents.Add(eventId, new List<IEvent>());
 			}
+			this.allEvents[eventId].Add(e);
 		}
 
 		public Assembly Get(DLLType dllType)
@@ -424,18 +409,18 @@ namespace Model
 			ObjectHelper.Swap(ref this.lateUpdates, ref this.lateUpdates2);
 		}
 
-		public void Run(EventIdType type)
+		public void Run(int type)
 		{
-			List<IEventMethod> iEvents;
-			if (!this.allEvents.TryGetValue(type, out iEvents))
+			List<IEvent> iEvents;
+			if (!this.allEvents.TryGetValue((int)type, out iEvents))
 			{
 				return;
 			}
-			foreach (IEventMethod iEvent in iEvents)
+			foreach (IEvent iEvent in iEvents)
 			{
 				try
 				{
-					iEvent.Run();
+					iEvent?.Handle();
 				}
 				catch (Exception e)
 				{
@@ -444,18 +429,18 @@ namespace Model
 			}
 		}
 
-		public void Run<A>(EventIdType type, A a)
+		public void Run<A>(int type, A a)
 		{
-			List<IEventMethod> iEvents;
-			if (!this.allEvents.TryGetValue(type, out iEvents))
+			List<IEvent> iEvents;
+			if (!this.allEvents.TryGetValue((int)type, out iEvents))
 			{
 				return;
 			}
-			foreach (IEventMethod iEvent in iEvents)
+			foreach (IEvent iEvent in iEvents)
 			{
 				try
 				{
-					iEvent.Run(a);
+					iEvent?.Handle(a);
 				}
 				catch (Exception e)
 				{
@@ -464,18 +449,18 @@ namespace Model
 			}
 		}
 
-		public void Run<A, B>(EventIdType type, A a, B b)
+		public void Run<A, B>(int type, A a, B b)
 		{
-			List<IEventMethod> iEvents;
-			if (!this.allEvents.TryGetValue(type, out iEvents))
+			List<IEvent> iEvents;
+			if (!this.allEvents.TryGetValue((int)type, out iEvents))
 			{
 				return;
 			}
-			foreach (IEventMethod iEvent in iEvents)
+			foreach (IEvent iEvent in iEvents)
 			{
 				try
 				{
-					iEvent.Run(a, b);
+					iEvent?.Handle(a, b);
 				}
 				catch (Exception e)
 				{
@@ -484,18 +469,18 @@ namespace Model
 			}
 		}
 
-		public void Run<A, B, C>(EventIdType type, A a, B b, C c)
+		public void Run<A, B, C>(int type, A a, B b, C c)
 		{
-			List<IEventMethod> iEvents;
-			if (!this.allEvents.TryGetValue(type, out iEvents))
+			List<IEvent> iEvents;
+			if (!this.allEvents.TryGetValue((int)type, out iEvents))
 			{
 				return;
 			}
-			foreach (IEventMethod iEvent in iEvents)
+			foreach (IEvent iEvent in iEvents)
 			{
 				try
 				{
-					iEvent.Run(a, b, c);
+					iEvent?.Handle(a, b, c);
 				}
 				catch (Exception e)
 				{

+ 0 - 102
Unity/Assets/Scripts/Base/Object/IEventMethod.cs

@@ -1,102 +0,0 @@
-using System;
-using ILRuntime.CLR.Method;
-using ILRuntime.Runtime.Intepreter;
-
-namespace Model
-{
-	public interface IEventMethod
-	{
-		void Run();
-		void Run<A>(A a);
-		void Run<A, B>(A a, B b);
-		void Run<A, B, C>(A a, B b, C c);
-		void Run<A, B, C, D>(A a, B b, C c, D d);
-	}
-
-	public class IEventMonoMethod : IEventMethod
-	{
-		private readonly object obj;
-
-		public IEventMonoMethod(object obj)
-		{
-			this.obj = obj;
-		}
-
-		public void Run()
-		{
-			((IEvent)obj).Run();
-		}
-
-		public void Run<A>(A a)
-		{
-			((IEvent<A>)obj).Run(a);
-		}
-
-		public void Run<A, B>(A a, B b)
-		{
-			((IEvent<A, B>)obj).Run(a, b);
-		}
-
-		public void Run<A, B, C>(A a, B b, C c)
-		{
-			((IEvent<A, B, C>)obj).Run(a, b, c);
-		}
-
-		public void Run<A, B, C, D>(A a, B b, C c, D d)
-		{
-			((IEvent<A, B, C, D>)obj).Run(a, b, c, d);
-		}
-	}
-
-	public class IEventILMethod : IEventMethod
-	{
-		private readonly ILRuntime.Runtime.Enviorment.AppDomain appDomain;
-		private readonly ILTypeInstance instance;
-		private readonly IMethod method;
-		private readonly object[] param;
-
-		public IEventILMethod(Type type, string methodName)
-		{
-			appDomain = Init.Instance.AppDomain;
-			this.instance = this.appDomain.Instantiate(type.FullName);
-			this.method = this.instance.Type.GetMethod(methodName);
-			int n = this.method.ParameterCount;
-			this.param = new object[n];
-		}
-
-		public void Run()
-		{
-			this.appDomain.Invoke(this.method, this.instance, param);
-		}
-
-		public void Run<A>(A a)
-		{
-			this.param[0] = a;
-			this.appDomain.Invoke(this.method, this.instance, param);
-		}
-
-		public void Run<A, B>(A a, B b)
-		{
-			this.param[0] = a;
-			this.param[1] = b;
-			this.appDomain.Invoke(this.method, this.instance, param);
-		}
-
-		public void Run<A, B, C>(A a, B b, C c)
-		{
-			this.param[0] = a;
-			this.param[1] = b;
-			this.param[2] = c;
-			this.appDomain.Invoke(this.method, this.instance, param);
-		}
-
-		public void Run<A, B, C, D>(A a, B b, C c, D d)
-		{
-			this.param[0] = a;
-			this.param[1] = b;
-			this.param[2] = c;
-			this.param[3] = d;
-			this.appDomain.Invoke(this.method, this.instance, param);
-		}
-	}
-}

+ 2 - 1
Unity/Assets/Scripts/Component/NetOuterComponent.cs

@@ -20,7 +20,8 @@
 		{
 			this.Awake(NetworkProtocol.TCP);
 			this.MessagePacker = new ProtobufPacker();
-			this.MessageDispatcher = new ClientDispatcher();
+			// 由hotfix中设置
+			//this.MessageDispatcher = new ClientDispatcher();
 		}
 
 		public new void Update()

+ 1 - 1
Unity/Assets/Scripts/Component/OpcodeTypeComponent.cs

@@ -17,7 +17,7 @@ namespace Model
 
 		public void Awake()
 		{
-			Type[] types = DllHelper.GetAllTypes();
+			Type[] types = DllHelper.GetMonoTypes();
 			foreach (Type type in types)
 			{
 				object[] attrs = type.GetCustomAttributes(typeof(MessageAttribute), false);

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

@@ -2,9 +2,9 @@
 {
 	// 分发数值监听
 	[Event((int)EventIdType.NumbericChange)]
-	public class NumericChangeEvent_NotifyWatcher: IEvent<NumericType, long, int>
+	public class NumericChangeEvent_NotifyWatcher: AEvent<NumericType, long, int>
 	{
-		public void Run(NumericType numericType, long id, int value)
+		public override void Run(NumericType numericType, long id, int value)
 		{
 			Game.Scene.GetComponent<NumericWatcherComponent>().Run(numericType, id, value);
 		}

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

@@ -1,4 +1,5 @@
 using System;
+using System.Collections.Generic;
 using System.Reflection;
 using ILRuntime.CLR.Method;
 using ILRuntime.Runtime.Enviorment;
@@ -22,9 +23,11 @@ namespace Model
 			Init.Instance.AppDomain.RegisterCLRMethodRedirection(mi3, ILRedirection.LogError);
 
 			// 注册委托
+			Init.Instance.AppDomain.DelegateManager.RegisterMethodDelegate<List<object>>();
 			Init.Instance.AppDomain.DelegateManager.RegisterMethodDelegate<AChannel, System.Net.Sockets.SocketError>();
 			Init.Instance.AppDomain.DelegateManager.RegisterMethodDelegate<byte[], int, int>();
 			Init.Instance.AppDomain.DelegateManager.RegisterMethodDelegate<IResponse>();
+			Init.Instance.AppDomain.DelegateManager.RegisterMethodDelegate<Session, PacketInfo>();
 
 
 			// 注册适配器

+ 9 - 13
Unity/Assets/Scripts/Init.cs

@@ -6,7 +6,6 @@ using System.Threading;
 using ILRuntime.CLR.Method;
 using ILRuntime.CLR.TypeSystem;
 using ILRuntime.Runtime.Enviorment;
-using MongoDB.Bson.IO;
 using UnityEngine;
 
 namespace Model
@@ -18,9 +17,10 @@ namespace Model
 		public ILRuntime.Runtime.Enviorment.AppDomain AppDomain;
 
 		private IStaticMethod start;
-		private IStaticMethod update;
-		private IStaticMethod lateUpdate;
-		private IStaticMethod onApplicationQuit;
+
+		public Action HotfixUpdate;
+		public Action HotfixLateUpdate;
+		public Action HotfixOnApplicationQuit;
 
 		private async void Start()
 		{
@@ -66,9 +66,6 @@ namespace Model
 				ILHelper.InitILRuntime();
 				
 				this.start = new ILStaticMethod("Hotfix.Init", "Start", 0);
-				this.update = new ILStaticMethod("Hotfix.Init", "Update", 0);
-				this.lateUpdate = new ILStaticMethod("Hotfix.Init", "LateUpdate", 0);
-				this.onApplicationQuit = new ILStaticMethod("Hotfix.Init", "OnApplicationQuit", 0);
 #else
 				Log.Debug("run in mono mode");
 				Game.Scene.GetComponent<ResourcesComponent>().LoadBundle($"code.unity3d");
@@ -76,15 +73,14 @@ namespace Model
 				Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle($"code.unity3d");
 				Type hotfixInit = Game.EventSystem.HotfixAssembly.GetType("Hotfix.Init");
 				this.start = new MonoStaticMethod(hotfixInit, "Start");
-				this.update = new MonoStaticMethod(hotfixInit, "Update");
-				this.lateUpdate = new MonoStaticMethod(hotfixInit, "LateUpdate");
-				this.onApplicationQuit = new MonoStaticMethod(hotfixInit, "OnApplicationQuit");
 #endif
 
 				Game.Scene.AddComponent<OpcodeTypeComponent>();
 
 				// 进入热更新层
 				this.start.Run();
+
+				Game.EventSystem.Run(EventIdType.TestHotfixSubscribMonoEvent, "TestHotfixSubscribMonoEvent");
 			}
 			catch (Exception e)
 			{
@@ -94,13 +90,13 @@ namespace Model
 
 		private void Update()
 		{
-			this.update?.Run();
+			this.HotfixUpdate.Invoke();
 			Game.EventSystem.Update();
 		}
 
 		private void LateUpdate()
 		{
-			this.lateUpdate?.Run();
+			this.HotfixLateUpdate.Invoke();
 			Game.EventSystem.LateUpdate();
 		}
 
@@ -108,7 +104,7 @@ namespace Model
 		{
 			Instance = null;
 			Game.Close();
-			this.onApplicationQuit?.Run();
+			this.HotfixOnApplicationQuit.Invoke();
 		}
 	}
 }

+ 3 - 3
Unity/Assets/Scripts/UI/UILoading/Event/LoadingBeginEvent_CreateLoadingUI.cs

@@ -1,9 +1,9 @@
 namespace Model
 {
-    [Event((int)EventIdType.LoadingBegin)]
-    public class LoadingBeginEvent_CreateLoadingUI : IEvent
+    [Event(EventIdType.LoadingBegin)]
+    public class LoadingBeginEvent_CreateLoadingUI : AEvent
     {
-        public void Run()
+        public override void Run()
         {
 			Game.Scene.GetComponent<UIComponent>().Create(UIType.UILoading);
         }

+ 3 - 3
Unity/Assets/Scripts/UI/UILoading/Event/LoadingFinishEvent_RemoveLoadingUI.cs

@@ -1,9 +1,9 @@
 namespace Model
 {
-    [Event((int)EventIdType.LoadingFinish)]
-    public class LoadingFinishEvent_RemoveLoadingUI : IEvent
+    [Event(EventIdType.LoadingFinish)]
+    public class LoadingFinishEvent_RemoveLoadingUI : AEvent
     {
-        public void Run()
+        public override void Run()
         {
 			Game.Scene.GetComponent<UIComponent>().Remove(UIType.UILoading);
         }

+ 2 - 2
Unity/Hotfix/Base/Event/EventIdType.cs

@@ -1,7 +1,7 @@
 namespace Hotfix
 {
-	public enum EventIdType
+	public static class EventIdType
 	{
-		InitSceneStart = 10001,
+		public const int InitSceneStart = 10001;
 	}
 }

+ 97 - 33
Unity/Hotfix/Base/Event/IEvent.cs

@@ -1,66 +1,130 @@
-namespace Hotfix
+using System;
+using System.Collections.Generic;
+
+namespace Hotfix
 {
 #if ILRuntime
 	public interface IEvent
 	{
-		void Run();
+		void Handle();
+		void Handle(object a);
+		void Handle(object a, object b);
+		void Handle(object a, object b, object c);
 	}
 
-	public interface IEvent<in A>
+	public abstract class AEvent : IEvent
 	{
-		void Run(A uid);
-	}
+		public void Handle()
+		{
+			this.Run();
+		}
 
-	public interface IEvent<in A, in B>
-	{
-		void Run(A a, B b);
-	}
+		public void Handle(object a)
+		{
+			throw new NotImplementedException();
+		}
 
-	public interface IEvent<in A, in B, in C>
-	{
-		void Run(A a, B b, C c);
-	}
+		public void Handle(object a, object b)
+		{
+			throw new NotImplementedException();
+		}
 
-	public interface IEvent<in A, in B, in C, in D>
-	{
-		void Run(A a, B b, C c, D d);
-	}
+		public void Handle(object a, object b, object c)
+		{
+			throw new NotImplementedException();
+		}
 
-	public interface IEvent<in A, in B, in C, in D, in E>
-	{
-		void Run(A a, B b, C c, D d, E e);
+		public abstract void Run();
 	}
 
-	public interface IEvent<in A, in B, in C, in D, in E, in F>
-	{
-		void Run(A a, B b, C c, D d, E e, F f);
-	}
-#else
-	public interface IEvent : Model.IEvent
+	public abstract class AEvent<A>: IEvent
 	{
+		public void Handle()
+		{
+			throw new NotImplementedException();
+		}
+
+		public void Handle(object a)
+		{
+			this.Run((A)a);
+		}
+
+		public void Handle(object a, object b)
+		{
+			throw new NotImplementedException();
+		}
+
+		public void Handle(object a, object b, object c)
+		{
+			throw new NotImplementedException();
+		}
+
+		public abstract void Run(A a);
 	}
 
-	public interface IEvent<in A> : Model.IEvent<A>
+	public abstract class AEvent<A, B>: IEvent
 	{
+		public void Handle()
+		{
+			throw new NotImplementedException();
+		}
+
+		public void Handle(object a)
+		{
+			throw new NotImplementedException();
+		}
+
+		public void Handle(object a, object b)
+		{
+			this.Run((A)a, (B)b);
+		}
+
+		public void Handle(object a, object b, object c)
+		{
+			throw new NotImplementedException();
+		}
+
+		public abstract void Run(A a, B b);
 	}
 
-	public interface IEvent<in A, in B> : Model.IEvent<A, B>
+	public abstract class AEvent<A, B, C>: IEvent
 	{
-	}
+		public void Handle()
+		{
+			throw new NotImplementedException();
+		}
+
+		public void Handle(object a)
+		{
+			throw new NotImplementedException();
+		}
+
+		public void Handle(object a, object b)
+		{
+			throw new NotImplementedException();
+		}
 
-	public interface IEvent<in A, in B, in C> : Model.IEvent<A, B, C>
+		public void Handle(object a, object b, object c)
+		{
+			this.Run((A)a, (B)b, (C)c);
+		}
+
+		public abstract void Run(A a, B b, C c);
+	}
+#else
+	public abstract class AEvent : Model.AEvent
 	{
 	}
 
-	public interface IEvent<in A, in B, in C, in D> : Model.IEvent<A, B, C, D>
+	public abstract class AEvent<A> : Model.AEvent<A>
 	{
 	}
 
-	public interface IEvent<in A, in B, in C, in D, in E> : Model.IEvent<A, B, C, D, E>
+	public abstract class AEvent<A, B> : Model.AEvent<A, B>
 	{
 	}
 
-	public interface IEvent<in A, in B, in C, in D, in E, in F> : Model.IEvent<A, B, C, D, E, F>
+	public abstract class AEvent<A, B, C>: Model.AEvent<A, B, C>
 	{
 	}
 #endif

+ 64 - 39
Unity/Hotfix/Base/Object/EventSystem.cs

@@ -34,7 +34,7 @@ namespace Hotfix
 	{
 		private readonly Dictionary<Type, IObjectSystem> disposerEvents = new Dictionary<Type, IObjectSystem>();
 
-		private readonly Dictionary<EventIdType, List<object>> allEvents = new Dictionary<EventIdType, List<object>>();
+		private readonly Dictionary<int, List<IEvent>> allEvents = new Dictionary<int, List<IEvent>>();
 
 		private Queue<Disposer> updates = new Queue<Disposer>();
 		private Queue<Disposer> updates2 = new Queue<Disposer>();
@@ -73,7 +73,7 @@ namespace Hotfix
 				this.disposerEvents[objectSystem.Type()] = objectSystem;
 			}
 
-			allEvents.Clear();
+			this.allEvents.Clear();
 			foreach (Type type in types)
 			{
 				object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
@@ -82,17 +82,50 @@ namespace Hotfix
 				{
 					EventAttribute aEventAttribute = (EventAttribute)attr;
 					object obj = Activator.CreateInstance(type);
-					if (!this.allEvents.ContainsKey((EventIdType)aEventAttribute.Type))
+					IEvent iEvent = obj as IEvent;
+					if (iEvent == null)
 					{
-						this.allEvents.Add((EventIdType)aEventAttribute.Type, new List<object>());
+						Log.Error($"{obj.GetType().Name} 没有继承IEvent");
 					}
-					this.allEvents[(EventIdType)aEventAttribute.Type].Add(obj);
+					this.RegisterEvent(aEventAttribute.Type, iEvent);
+
+					// hotfix的事件也要注册到mono层,hotfix可以订阅mono层的事件
+					Action<List<object>> action = list => { Handle(aEventAttribute.Type, list); };
+					Game.EventSystem.RegisterEvent(aEventAttribute.Type, new EventProxy(action));
 				}
 			}
 
 			this.Load();
 		}
 
+		public void RegisterEvent(int eventId, IEvent e)
+		{
+			if (!this.allEvents.ContainsKey(eventId))
+			{
+				this.allEvents.Add(eventId, new List<IEvent>());
+			}
+			this.allEvents[eventId].Add(e);
+		}
+
+		public static void Handle(int type, List<object> param)
+		{
+			switch (param.Count)
+			{
+				case 0:
+					Hotfix.EventSystem.Run(type);
+					break;
+				case 1:
+					Hotfix.EventSystem.Run(type, param[0]);
+					break;
+				case 2:
+					Hotfix.EventSystem.Run(type, param[0], param[1]);
+					break;
+				case 3:
+					Hotfix.EventSystem.Run(type, param[0], param[1], param[2]);
+					break;
+			}
+		}
+
 		public void Add(Disposer disposer)
 		{
 			if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectSystem))
@@ -344,20 +377,18 @@ namespace Hotfix
 			ObjectHelper.Swap(ref this.lateUpdates, ref this.lateUpdates2);
 		}
 
-
-		public void Run(EventIdType type)
+		public void Run(int type)
 		{
-			List<object> iEvents;
-			if (!this.allEvents.TryGetValue(type, out iEvents))
+			List<IEvent> iEvents;
+			if (!this.allEvents.TryGetValue((int)type, out iEvents))
 			{
 				return;
 			}
-			foreach (object obj in iEvents)
+			foreach (IEvent iEvent in iEvents)
 			{
 				try
 				{
-					IEvent iEvent = (IEvent)obj;
-					iEvent.Run();
+					iEvent?.Handle();
 				}
 				catch (Exception e)
 				{
@@ -366,68 +397,62 @@ namespace Hotfix
 			}
 		}
 
-		public void Run<A>(EventIdType type, A a)
+		public void Run<A>(int type, A a)
 		{
-			List<object> iEvents;
-			if (!this.allEvents.TryGetValue(type, out iEvents))
+			List<IEvent> iEvents;
+			if (!this.allEvents.TryGetValue((int)type, out iEvents))
 			{
 				return;
 			}
-
-			foreach (object obj in iEvents)
+			foreach (IEvent iEvent in iEvents)
 			{
 				try
 				{
-					IEvent<A> iEvent = (IEvent<A>)obj;
-					iEvent.Run(a);
+					iEvent?.Handle(a);
 				}
-				catch (Exception err)
+				catch (Exception e)
 				{
-					Log.Error(err.ToString());
+					Log.Error(e.ToString());
 				}
 			}
 		}
 
-		public void Run<A, B>(EventIdType type, A a, B b)
+		public void Run<A, B>(int type, A a, B b)
 		{
-			List<object> iEvents;
-			if (!this.allEvents.TryGetValue(type, out iEvents))
+			List<IEvent> iEvents;
+			if (!this.allEvents.TryGetValue((int)type, out iEvents))
 			{
 				return;
 			}
-
-			foreach (object obj in iEvents)
+			foreach (IEvent iEvent in iEvents)
 			{
 				try
 				{
-					IEvent<A, B> iEvent = (IEvent<A, B>)obj;
-					iEvent.Run(a, b);
+					iEvent?.Handle(a, b);
 				}
-				catch (Exception err)
+				catch (Exception e)
 				{
-					Log.Error(err.ToString());
+					Log.Error(e.ToString());
 				}
 			}
 		}
 
-		public void Run<A, B, C>(EventIdType type, A a, B b, C c)
+		public void Run<A, B, C>(int type, A a, B b, C c)
 		{
-			List<object> iEvents;
-			if (!this.allEvents.TryGetValue(type, out iEvents))
+			List<IEvent> iEvents;
+			if (!this.allEvents.TryGetValue((int)type, out iEvents))
 			{
 				return;
 			}
-
-			foreach (object obj in iEvents)
+			foreach (IEvent iEvent in iEvents)
 			{
 				try
 				{
-					IEvent<A, B, C> iEvent = (IEvent<A, B, C>)obj;
-					iEvent.Run(a, b, c);
+					iEvent?.Handle(a, b, c);
 				}
-				catch (Exception err)
+				catch (Exception e)
 				{
-					Log.Error(err.ToString());
+					Log.Error(e.ToString());
 				}
 			}
 		}

+ 14 - 0
Unity/Hotfix/Event/TestHotfixSubscribMonoEvent_LogString.cs

@@ -0,0 +1,14 @@
+using Model;
+
+namespace Hotfix
+{
+	// 分发数值监听
+	[Event(Model.EventIdType.TestHotfixSubscribMonoEvent)]
+	public class TestHotfixSubscribMonoEvent_LogString : AEvent<string>
+	{
+		public override void Run(string info)
+		{
+			Log.Info(info);
+		}
+	}
+}

+ 14 - 2
Unity/Hotfix/Init.cs

@@ -10,11 +10,23 @@ namespace Hotfix
 			try
 			{
 				Hotfix.Scene.ModelScene = Game.Scene;
+
+				// 注册热更层回调
+				Model.Init.Instance.HotfixUpdate = () => { Update(); };
+				Model.Init.Instance.HotfixLateUpdate = () => { LateUpdate(); };
+				Model.Init.Instance.HotfixOnApplicationQuit = () => { OnApplicationQuit(); };
+
+				// 注册热更层消息回调
+				ClientDispatcher clientDispatcher = new ClientDispatcher
+				{
+					HotfixCallback = (s, p) => { HotfixMessageDispatcher.Run(s, p); }
+				};
+				Game.Scene.GetComponent<NetOuterComponent>().MessageDispatcher = clientDispatcher;
+
 				Hotfix.Scene.AddComponent<UIComponent>();
-#if ILRuntime
 				Hotfix.Scene.AddComponent<OpcodeTypeComponent>();
 				Hotfix.Scene.AddComponent<MessageDispatherComponent>();
-#endif
+
 				Hotfix.EventSystem.Run(EventIdType.InitSceneStart);
 			}
 			catch (Exception e)

+ 3 - 5
Unity/Hotfix/Module/HotfixMessage/RecvHotfixMessageEvent_HandleHotfixMessage.cs → Unity/Hotfix/Module/HotfixMessage/HotfixMessageDispatcher.cs

@@ -3,14 +3,12 @@ using Model;
 
 namespace Hotfix
 {
-	// 分发数值监听
-	[Event((int)Model.EventIdType.RecvHotfixMessage)]
-	public class RecvHotfixMessageEvent_HandleHotfixMessage: IEvent<Session, PacketInfo>
+	public static class HotfixMessageDispatcher
 	{
-		public void Run(Session session, PacketInfo packetInfo)
+		public static void Run(Session session, PacketInfo packetInfo)
 		{
 			ushort opcode = packetInfo.Opcode;
-			Type t = SessionHelper.GetMessageType(opcode);
+			Type t = Hotfix.Scene.GetComponent<OpcodeTypeComponent>().GetType(opcode);
 			object aa = ProtobufHelper.FromBytes(t, packetInfo.Bytes, packetInfo.Index, packetInfo.Length);
 			IMessage message = (IMessage)aa;
 			Hotfix.Scene.GetComponent<MessageDispatherComponent>().Handle(session, opcode, message);

+ 4 - 21
Unity/Hotfix/Module/HotfixMessage/SessionHelper.cs

@@ -8,39 +8,22 @@ namespace Hotfix
 	{
 		public static void Send(this Session session, IMessage message)
 		{
-			ushort opcode = GetOpcode(message.GetType());
+			ushort opcode = Hotfix.Scene.GetComponent<OpcodeTypeComponent>().GetOpcode(message.GetType());
 			byte[] bytes = ProtobufHelper.ToBytes(message);
 			session.Send(opcode, bytes);
 		}
 
 		public static async Task<IResponse> Call(this Session session, IRequest request)
 		{
+			OpcodeTypeComponent opcodeTypeComponent = Hotfix.Scene.GetComponent<OpcodeTypeComponent>();
 			byte[] bytes = ProtobufHelper.ToBytes(request);
-			ushort opcode = GetOpcode(request.GetType());
+			ushort opcode = opcodeTypeComponent.GetOpcode(request.GetType());
 			PacketInfo packetInfo = await session.Call(opcode, bytes);
 			ushort responseOpcode = packetInfo.Opcode;
-			Type t = GetMessageType(responseOpcode);
+			Type t = opcodeTypeComponent.GetType(responseOpcode);
 			object aa = ProtobufHelper.FromBytes(t, packetInfo.Bytes, packetInfo.Index, packetInfo.Length);
 			IResponse response = (IResponse)aa;
 			return response;
 		}
-
-		public static ushort GetOpcode(Type type)
-		{
-#if ILRuntime
-			return Hotfix.Scene.GetComponent<OpcodeTypeComponent>().GetOpcode(type);
-#else
-			return Game.Scene.GetComponent<Model.OpcodeTypeComponent>().GetOpcode(type);
-#endif
-		}
-
-		public static Type GetMessageType(ushort opcode)
-		{
-#if ILRuntime
-			return Hotfix.Scene.GetComponent<OpcodeTypeComponent>().GetType(opcode);
-#else
-			return Game.Scene.GetComponent<Model.OpcodeTypeComponent>().GetType(opcode);
-#endif
-		}
 	}
 }

+ 3 - 3
Unity/Hotfix/UI/UILogin/Event/InitSceneStart_CreateLoginUI.cs

@@ -2,10 +2,10 @@
 
 namespace Hotfix
 {
-	[Event((int)EventIdType.InitSceneStart)]
-	public class InitSceneStart_CreateLoginUI: IEvent
+	[Event(EventIdType.InitSceneStart)]
+	public class InitSceneStart_CreateLoginUI: AEvent
 	{
-		public void Run()
+		public override void Run()
 		{
 			UI ui = Hotfix.Scene.GetComponent<UIComponent>().Create(UIType.UILogin);
 		}

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

@@ -54,7 +54,8 @@
     <Compile Include="Base\Helper\ArrayHelper.cs" />
     <Compile Include="Base\Helper\AssetBundleHelper.cs" />
     <Compile Include="Base\Helper\ExceptionHelper.cs" />
-    <Compile Include="Module\HotfixMessage\RecvHotfixMessageEvent_HandleHotfixMessage.cs" />
+    <Compile Include="Event\TestHotfixSubscribMonoEvent_LogString.cs" />
+    <Compile Include="Module\HotfixMessage\HotfixMessageDispatcher.cs" />
     <Compile Include="Module\HotfixMessage\IMessage.cs" />
     <Compile Include="Module\HotfixMessage\AMHandler.cs" />
     <Compile Include="Module\HotfixMessage\IMHandler.cs" />
@@ -91,7 +92,6 @@
     <Compile Include="UI\UILogin\Factory\UILoginFactory.cs" />
   </ItemGroup>
   <ItemGroup>
-    <Folder Include="Event\" />
     <Folder Include="Handler\" />
   </ItemGroup>
   <ItemGroup>

+ 1 - 1
Unity/Unity.csproj

@@ -246,9 +246,9 @@
     <Compile Include="Assets\Scripts\Base\Object\EntityEventAttribute.cs" />
     <Compile Include="Assets\Scripts\Base\Object\EntityFactory.cs" />
     <Compile Include="Assets\Scripts\Base\Object\EntityType.cs" />
+    <Compile Include="Assets\Scripts\Base\Object\EventProxy.cs" />
     <Compile Include="Assets\Scripts\Base\Object\EventSystem.cs" />
     <Compile Include="Assets\Scripts\Base\Object\IAwake.cs" />
-    <Compile Include="Assets\Scripts\Base\Object\IEventMethod.cs" />
     <Compile Include="Assets\Scripts\Base\Object\ILateUpdate.cs" />
     <Compile Include="Assets\Scripts\Base\Object\ILoad.cs" />
     <Compile Include="Assets\Scripts\Base\Object\ISerializeToEntity.cs" />