Browse Source

客户端事件与服务端一模一样,服务端共用大部分客户端代码

tanghai 8 năm trước cách đây
mục cha
commit
8e0fa7ebd0
80 tập tin đã thay đổi với 1048 bổ sung1490 xóa
  1. 2 2
      Server/App/Program.cs
  2. 1 1
      Server/Hotfix/Handler/C2G_LoginGateHandler.cs
  3. 1 1
      Server/Hotfix/Handler/G2M_CreateUnitHandler.cs
  4. 1 1
      Server/Hotfix/Handler/M2A_ReloadHandler.cs
  5. 1 1
      Server/Model/Component/ActorProxyComponent.cs
  6. 1 1
      Server/Model/Component/ConfigComponent.cs
  7. 0 232
      Server/Model/Component/EventComponent.cs
  8. 0 110
      Server/Model/Component/TimerComponent.cs
  9. 0 247
      Server/Model/Entity/Session.cs
  10. 12 12
      Server/Model/Event/EventIdType.cs
  11. 1 1
      Server/Model/Helper/DllHelper.cs
  12. 0 20
      Server/Model/Message/AMRpcHandler.cs
  13. 0 43
      Server/Model/Message/AppType.cs
  14. 0 7
      Server/Model/Message/IMessageDispatcher.cs
  15. 0 18
      Server/Model/Message/IMessagePacker.cs
  16. 0 23
      Server/Model/Message/RpcException.cs
  17. 0 51
      Server/Model/Object/Component.cs
  18. 0 30
      Server/Model/Object/Disposer.cs
  19. 0 215
      Server/Model/Object/Entity.cs
  20. 0 22
      Server/Model/Object/IAwake.cs
  21. 0 7
      Server/Model/Object/ILoad.cs
  22. 0 7
      Server/Model/Object/IStart.cs
  23. 0 7
      Server/Model/Object/IUpdate.cs
  24. 0 42
      Server/Model/Object/Object.cs
  25. 0 9
      Server/Model/Object/ObjectEventAttribute.cs
  26. 3 3
      Server/Model/Object/ObjectEvents.cs
  27. 58 18
      Server/Model/Server.Model.csproj
  28. 6 0
      Server/Model/Server.Model.csproj.user
  29. 1 5
      Unity/Assets/Editor/ServerCommandLineEditor/ServerCommandLineEditor.cs
  30. 3 3
      Unity/Assets/Scripts/Base/Message/AMHandler.cs
  31. 1 1
      Unity/Assets/Scripts/Base/Message/AMessage.cs
  32. 2 2
      Unity/Assets/Scripts/Base/Message/ClientDispatcher.cs
  33. 1 1
      Unity/Assets/Scripts/Base/Message/IMHandler.cs
  34. 1 1
      Unity/Assets/Scripts/Base/Message/IMessageDispatcher.cs
  35. 6 0
      Unity/Assets/Scripts/Base/Object/ComponentDB.cs
  36. 3 0
      Unity/Assets/Scripts/Base/Object/ComponentDB.cs.meta
  37. 39 0
      Unity/Assets/Scripts/Base/Object/ComponentFactory.cs
  38. 3 0
      Unity/Assets/Scripts/Base/Object/ComponentFactory.cs.meta
  39. 11 3
      Unity/Assets/Scripts/Base/Object/Disposer.cs
  40. 60 37
      Unity/Assets/Scripts/Base/Object/Entity.cs
  41. 0 31
      Unity/Assets/Scripts/Base/Object/EntityEventId.cs
  42. 0 12
      Unity/Assets/Scripts/Base/Object/EntityEventId.cs.meta
  43. 1 1
      Unity/Assets/Scripts/Base/Object/EntityFactory.cs
  44. 3 0
      Unity/Assets/Scripts/Base/Object/EntityFactory.cs.meta
  45. 1 1
      Unity/Assets/Scripts/Base/Object/Object.cs
  46. 0 6
      Unity/Assets/Scripts/Base/Object/ObjectEventAttribute.cs
  47. 215 38
      Unity/Assets/Scripts/Base/Object/ObjectEvents.cs
  48. 14 1
      Unity/Assets/Scripts/Component/BehaviorTreeComponent.cs
  49. 20 65
      Unity/Assets/Scripts/Component/CrossComponent.cs
  50. 15 2
      Unity/Assets/Scripts/Component/EventComponent.cs
  51. 17 4
      Unity/Assets/Scripts/Component/MessageDispatherComponent.cs
  52. 15 1
      Unity/Assets/Scripts/Component/NetOuterComponent.cs
  53. 10 1
      Unity/Assets/Scripts/Component/OpcodeTypeComponent.cs
  54. 0 1
      Unity/Assets/Scripts/Component/ResourcesComponent.cs
  55. 9 1
      Unity/Assets/Scripts/Component/TimerComponent.cs
  56. 1 1
      Unity/Assets/Scripts/Entity/Session.cs
  57. 9 1
      Unity/Assets/Scripts/Entity/WWWAsync.cs
  58. 1 1
      Unity/Assets/Scripts/Handler/Actor_TestHandler.cs
  59. 4 15
      Unity/Assets/Scripts/Helper/ILHelper.cs
  60. 2 6
      Unity/Assets/Scripts/Init.cs
  61. 6 0
      Unity/Hotfix/Base/Object/ComponentDB.cs
  62. 39 0
      Unity/Hotfix/Base/Object/ComponentFactory.cs
  63. 46 43
      Unity/Hotfix/Base/Object/Entity.cs
  64. 67 0
      Unity/Hotfix/Base/Object/EntityFactory.cs
  65. 1 1
      Unity/Hotfix/Base/Object/Object.cs
  66. 232 21
      Unity/Hotfix/Base/Object/ObjectEvents.cs
  67. 15 2
      Unity/Hotfix/Component/EventComponent.cs
  68. 10 2
      Unity/Hotfix/Component/GameObjectComponent.cs
  69. 14 1
      Unity/Hotfix/Component/MessageDispatherComponent.cs
  70. 0 18
      Unity/Hotfix/Component/RobotComponent.cs
  71. 10 2
      Unity/Hotfix/Component/TimerComponent.cs
  72. 15 3
      Unity/Hotfix/Component/UIComponent.cs
  73. 2 4
      Unity/Hotfix/Entity/Scene.cs
  74. 1 1
      Unity/Hotfix/Entity/UI.cs
  75. 1 1
      Unity/Hotfix/Entity/Unit.cs
  76. 10 2
      Unity/Hotfix/UI/UILogin/Component/UILoginComponent.cs
  77. 17 8
      Unity/Hotfix/UI/UILogin/Factory/UILoginFactory.cs
  78. 3 1
      Unity/Hotfix/Unity.Hotfix.csproj
  79. 3 1
      Unity/Unity.csproj
  80. 1 6
      Unity/Unity.sln

+ 2 - 2
Server/App/Program.cs

@@ -15,8 +15,8 @@ namespace App
 
 			try
 			{
-				ObjectEvents.Instance.Register("Model", typeof(Game).Assembly);
-				ObjectEvents.Instance.Register("Hotfix", DllHelper.GetHotfixAssembly());
+				ObjectEvents.Instance.Add("Model", typeof(Game).Assembly);
+				ObjectEvents.Instance.Add("Hotfix", DllHelper.GetHotfixAssembly());
 
 				Options options = Game.Scene.AddComponent<OptionComponent, string[]>(args).Options;
 				StartConfig startConfig = Game.Scene.AddComponent<StartConfigComponent, string, int>(options.Config, options.AppId).StartConfig;

+ 1 - 1
Server/Hotfix/Handler/C2G_LoginGateHandler.cs

@@ -19,7 +19,7 @@ namespace Hotfix
 					reply(response);
 					return;
 				}
-				Player player = ObjectFactory.Create<Player, string>(account);
+				Player player = EntityFactory.Create<Player, string>(account);
 				Game.Scene.GetComponent<PlayerComponent>().Add(player);
 				session.AddComponent<SessionPlayerComponent>().Player = player;
 				session.AddComponent<ActorComponent, IEntityActorHandler>(new GateSessionEntityActorHandler());

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

@@ -11,7 +11,7 @@ namespace Hotfix
 			M2G_CreateUnit response = new M2G_CreateUnit();
 			try
 			{
-				Unit unit = ObjectFactory.Create<Unit, UnitType>(UnitType.Hero);
+				Unit unit = EntityFactory.Create<Unit, UnitType>(UnitType.Hero);
 				unit.AddComponent<ActorComponent, IEntityActorHandler>(new CommonEntityActorHandler());
 				unit.AddComponent<UnitGateComponent, long>(message.GateSessionId);
 				Game.Scene.GetComponent<UnitComponent>().Add(unit);

+ 1 - 1
Server/Hotfix/Handler/M2A_ReloadHandler.cs

@@ -11,7 +11,7 @@ namespace Hotfix
 			A2M_Reload response = new A2M_Reload();
 			try
 			{
-				ObjectEvents.Instance.Register("Hotfix", DllHelper.GetHotfixAssembly());
+				ObjectEvents.Instance.Add("Hotfix", DllHelper.GetHotfixAssembly());
 				reply(response);
 			}
 			catch (Exception e)

+ 1 - 1
Server/Model/Component/ActorProxyComponent.cs

@@ -13,7 +13,7 @@ namespace Model
 				return actorProxy;
 			}
 			
-			actorProxy = ObjectFactory.CreateWithId<ActorProxy>(id);
+			actorProxy = EntityFactory.CreateWithId<ActorProxy>(id);
 			this.dictionary[id] = actorProxy;
 			return actorProxy;
 		}

+ 1 - 1
Server/Model/Component/ConfigComponent.cs

@@ -19,7 +19,7 @@ namespace Model
 
 		public void Load()
 		{
-			Assembly assembly = ObjectEvents.Instance.GetAssembly("Model");
+			Assembly assembly = ObjectEvents.Instance.Get("Model");
 
 			this.allConfig = new Dictionary<Type, ICategory>();
 			Type[] types = assembly.GetTypes();

+ 0 - 232
Server/Model/Component/EventComponent.cs

@@ -1,232 +0,0 @@
-using System;
-using System.Collections.Generic;
-
-namespace Model
-{
-	[ObjectEvent]
-	public class EventComponentEvent : ObjectEvent<EventComponent>, IAwake, ILoad
-	{
-		public void Awake()
-		{
-			this.Get().Awake();
-		}
-
-		public void Load()
-		{
-			this.Get().Load();
-		}
-	}
-
-	/// <summary>
-	/// 事件分发
-	/// </summary>
-	public class EventComponent: Component
-	{
-		private Dictionary<int, List<object>> allEvents;
-
-		public void Awake()
-		{
-			this.Load();
-		}
-
-		public void Load()
-		{
-			this.allEvents = new Dictionary<int, List<object>>();
-
-			Type[] types = DllHelper.GetMonoTypes();
-			foreach (Type type in types)
-			{
-				object[] attrs = type.GetCustomAttributes(typeof (EventAttribute), false);
-
-				foreach (object attr in attrs)
-				{
-					EventAttribute aEventAttribute = (EventAttribute) attr;
-
-					object obj = Activator.CreateInstance(type);
-					if (!this.allEvents.ContainsKey(aEventAttribute.Type))
-					{
-						this.allEvents.Add(aEventAttribute.Type, new List<object>());
-					}
-					this.allEvents[aEventAttribute.Type].Add(obj);
-				}
-			}
-		}
-
-		public void Run(int type)
-		{
-			if (!this.allEvents.TryGetValue(type, out List<object> iEvents))
-			{
-				return;
-			}
-
-			foreach (object obj in iEvents)
-			{
-				try
-				{
-					IEvent iEvent = obj as IEvent;
-					if (iEvent == null)
-					{
-						throw new Exception($"event type: {type} is not IEvent");
-					}
-					iEvent.Run();
-				}
-				catch (Exception err)
-				{
-					Log.Error(err.ToString());
-				}
-			}
-		}
-
-		public void Run<A>(int type, A a)
-		{
-			if (!this.allEvents.TryGetValue(type, out List<object> iEvents))
-			{
-				return;
-			}
-
-			foreach (object obj in iEvents)
-			{
-				try
-				{
-					var iEvent = obj as IEvent<A>;
-					if (iEvent == null)
-					{
-						throw new Exception($"event type: {type} is not IEvent<{typeof (A).Name}>");
-					}
-					iEvent.Run(a);
-				}
-				catch (Exception err)
-				{
-					Log.Error(err.ToString());
-				}
-			}
-		}
-
-		public void Run<A, B>(int type, A a, B b)
-		{
-			if (!this.allEvents.TryGetValue(type, out List<object> iEvents))
-			{
-				return;
-			}
-
-			foreach (object obj in iEvents)
-			{
-				try
-				{
-					var iEvent = obj as IEvent<A, B>;
-					if (iEvent == null)
-					{
-						throw new Exception($"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}>");
-					}
-					iEvent.Run(a, b);
-				}
-				catch (Exception err)
-				{
-					Log.Error(err.ToString());
-				}
-			}
-		}
-
-		public void Run<A, B, C>(int type, A a, B b, C c)
-		{
-			if (!this.allEvents.TryGetValue(type, out List<object> iEvents))
-			{
-				return;
-			}
-
-			foreach (object obj in iEvents)
-			{
-				try
-				{
-					var iEvent = obj as IEvent<A, B, C>;
-					if (iEvent == null)
-					{
-						throw new Exception($"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}, {typeof (C).Name}>");
-					}
-					iEvent.Run(a, b, c);
-				}
-				catch (Exception err)
-				{
-					Log.Error(err.ToString());
-				}
-			}
-		}
-
-		public void Run<A, B, C, D>(int type, A a, B b, C c, D d)
-		{
-			if (!this.allEvents.TryGetValue(type, out List<object> iEvents))
-			{
-				return;
-			}
-
-			foreach (object obj in iEvents)
-			{
-				try
-				{
-					var iEvent = obj as IEvent<A, B, C, D>;
-					if (iEvent == null)
-					{
-						throw new Exception($"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}, {typeof (C).Name}, {typeof (D).Name}>");
-					}
-					iEvent.Run(a, b, c, d);
-				}
-				catch (Exception err)
-				{
-					Log.Error(err.ToString());
-				}
-			}
-		}
-
-		public void Run<A, B, C, D, E>(int type, A a, B b, C c, D d, E e)
-		{
-			if (!this.allEvents.TryGetValue(type, out List<object> iEvents))
-			{
-				return;
-			}
-
-			foreach (object obj in iEvents)
-			{
-				try
-				{
-					var iEvent = obj as IEvent<A, B, C, D, E>;
-					if (iEvent == null)
-					{
-						throw new Exception(
-								$"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}, {typeof (C).Name}, {typeof (D).Name}, {typeof (E).Name}>");
-					}
-					iEvent.Run(a, b, c, d, e);
-				}
-				catch (Exception err)
-				{
-					Log.Error(err.ToString());
-				}
-			}
-		}
-
-		public void Run<A, B, C, D, E, F>(int type, A a, B b, C c, D d, E e, F f)
-		{
-			if (!this.allEvents.TryGetValue(type, out List<object> iEvents))
-			{
-				return;
-			}
-
-			foreach (object obj in iEvents)
-			{
-				try
-				{
-					var iEvent = obj as IEvent<A, B, C, D, E, F>;
-					if (iEvent == null)
-					{
-						throw new Exception(
-								$"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}, {typeof (C).Name}, {typeof (D).Name}, {typeof (E).Name}>");
-					}
-					iEvent.Run(a, b, c, d, e, f);
-				}
-				catch (Exception err)
-				{
-					Log.Error(err.ToString());
-				}
-			}
-		}
-	}
-}

+ 0 - 110
Server/Model/Component/TimerComponent.cs

@@ -1,110 +0,0 @@
-using System.Collections.Generic;
-using System.Threading;
-using System.Threading.Tasks;
-
-namespace Model
-{
-	[ObjectEvent]
-	public class TimerComponentEvent: ObjectEvent<TimerComponent>, IUpdate
-	{
-		public void Update()
-		{
-			this.Get().Update();
-		}
-	}
-
-	public class Timer
-	{
-		public long Id { get; set; }
-		public long Time { get; set; }
-		public TaskCompletionSource<bool> tcs;
-	}
-
-	public class TimerComponent: Component
-	{
-		private readonly Dictionary<long, Timer> timers = new Dictionary<long, Timer>();
-
-		/// <summary>
-		/// key: time, value: timer id
-		/// </summary>
-		private readonly MultiMap<long, long> timeId = new MultiMap<long, long>();
-
-		private readonly Queue<long> timeoutTimer = new Queue<long>();
-
-		public void Update()
-		{
-			long timeNow = TimeHelper.Now();
-			foreach (long time in this.timeId.Keys)
-			{
-				if (time > timeNow)
-				{
-					break;
-				}
-				this.timeoutTimer.Enqueue(time);
-			}
-
-			while (this.timeoutTimer.Count > 0)
-			{
-				long key = this.timeoutTimer.Dequeue();
-				long[] timeOutId = this.timeId.GetAll(key);
-				foreach (long id in timeOutId)
-				{
-					if (!this.timers.TryGetValue(id, out Timer timer))
-					{
-						continue;
-					}
-					this.Remove(id);
-					timer.tcs.SetResult(true);
-				}
-			}
-		}
-
-		private void Remove(long id)
-		{
-			if (!this.timers.TryGetValue(id, out Timer timer))
-			{
-				return;
-			}
-			this.timers.Remove(id);
-			this.timeId.Remove(timer.Time, timer.Id);
-		}
-
-		public Task WaitTillAsync(long tillTime, CancellationToken cancellationToken)
-		{
-			TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
-			Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs };
-			this.timers[timer.Id] = timer;
-			this.timeId.Add(timer.Time, timer.Id);
-			cancellationToken.Register(() => { this.Remove(timer.Id); });
-			return tcs.Task;
-		}
-
-		public Task WaitTillAsync(long tillTime)
-		{
-			TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
-			Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs };
-			this.timers[timer.Id] = timer;
-			this.timeId.Add(timer.Time, timer.Id);
-			return tcs.Task;
-		}
-
-		public Task WaitAsync(long time, CancellationToken cancellationToken)
-		{
-			TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
-			Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = TimeHelper.Now() + time, tcs = tcs };
-			this.timers[timer.Id] = timer;
-			this.timeId.Add(timer.Time, timer.Id);
-			cancellationToken.Register(() => { this.Remove(timer.Id); });
-			return tcs.Task;
-		}
-
-		public Task WaitAsync(long time)
-		{
-			TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
-			Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = TimeHelper.Now() + time, tcs = tcs };
-			this.timers[timer.Id] = timer;
-			this.timeId.Add(timer.Time, timer.Id);
-			return tcs.Task;
-		}
-	}
-}

+ 0 - 247
Server/Model/Entity/Session.cs

@@ -1,247 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Threading;
-using System.Threading.Tasks;
-
-namespace Model
-{
-	public sealed class Session : Entity
-	{
-		private static uint RpcId { get; set; }
-		private readonly NetworkComponent network;
-		private readonly Dictionary<uint, Action<object>> requestCallback = new Dictionary<uint, Action<object>>();
-		private readonly AChannel channel;
-
-		public Session(NetworkComponent network, AChannel channel)
-		{
-			this.network = network;
-			this.channel = channel;
-			this.StartRecv();
-		}
-
-		public string RemoteAddress
-		{
-			get
-			{
-				return this.channel.RemoteAddress;
-			}
-		}
-
-		public ChannelType ChannelType
-		{
-			get
-			{
-				return this.channel.ChannelType;
-			}
-		}
-
-		private async void StartRecv()
-		{
-			while (true)
-			{
-				if (this.Id == 0)
-				{
-					return;
-				}
-
-				byte[] messageBytes;
-				try
-				{
-					messageBytes = await channel.Recv();
-					if (this.Id == 0)
-					{
-						return;
-					}
-				}
-				catch (Exception e)
-				{
-					Log.Error(e.ToString());
-					continue;
-				}
-
-				if (messageBytes.Length < 3)
-				{
-					continue;
-				}
-
-				ushort opcode = BitConverter.ToUInt16(messageBytes, 0);
-				opcode = NetworkHelper.NetworkToHostOrder(opcode);
-				try
-				{
-					this.Run(opcode, messageBytes);
-				}
-				catch (Exception e)
-				{
-					Log.Error(e.ToString());
-				}
-			}
-		}
-
-		private void Run(ushort opcode, byte[] messageBytes)
-		{
-			int offset = 0;
-			byte flag = messageBytes[2];
-
-			bool isCompressed = (flag & 0x80) > 0;
-			const int opcodeAndFlagLength = 3;
-			if (isCompressed) // 最高位为1,表示有压缩,需要解压缩
-			{
-				messageBytes = ZipHelper.Decompress(messageBytes, opcodeAndFlagLength, messageBytes.Length - opcodeAndFlagLength);
-				offset = 0;
-			}
-			else
-			{
-				offset = opcodeAndFlagLength;
-			}
-
-			this.RunDecompressedBytes(opcode, messageBytes, offset);
-		}
-
-		private void RunDecompressedBytes(ushort opcode, byte[] messageBytes, int offset)
-		{
-			Type messageType = this.network.Owner.GetComponent<OpcodeTypeComponent>().GetType(opcode);
-			object message = this.network.MessagePacker.DeserializeFrom(messageType, messageBytes, offset, messageBytes.Length - offset);
-
-			AResponse response = message as AResponse;
-			if (response != null)
-			{
-				// rpcFlag>0 表示这是一个rpc响应消息
-				// Rpc回调有找不着的可能,因为client可能取消Rpc调用
-				Action<object> action;
-				if (!this.requestCallback.TryGetValue(response.RpcId, out action))
-				{
-					return;
-				}
-				this.requestCallback.Remove(response.RpcId);
-				action(message);
-				return;
-			}
-
-			this.network.MessageDispatcher.Dispatch(this, opcode, offset, messageBytes, (AMessage)message);
-		}
-
-		/// <summary>
-		/// Rpc调用
-		/// </summary>
-		public Task<Response> Call<Response>(ARequest request, CancellationToken cancellationToken)
-			where Response : AResponse
-		{
-			request.RpcId = ++RpcId;
-			//Log.Debug($"{request.GetType().FullName} {request.RpcId}");
-			this.SendMessage(request);
-
-			var tcs = new TaskCompletionSource<Response>();
-
-			this.requestCallback[RpcId] = (message) =>
-			{
-				try
-				{
-					Response response = (Response)message;
-					if (response.Error > 100)
-					{
-						tcs.SetException(new RpcException(response.Error, response.Message));
-						return;
-					}
-					//Log.Debug($"recv: {MongoHelper.ToJson(response)}");
-					tcs.SetResult(response);
-				}
-				catch (Exception e)
-				{
-					tcs.SetException(new Exception($"Rpc Error: {typeof(Response).FullName}", e));
-				}
-			};
-
-			cancellationToken.Register(() => { this.requestCallback.Remove(RpcId); });
-
-			return tcs.Task;
-		}
-
-		/// <summary>
-		/// Rpc调用,发送一个消息,等待返回一个消息
-		/// </summary>
-		public Task<Response> Call<Response>(ARequest request) where Response : AResponse
-		{
-			request.RpcId = ++RpcId;
-			this.SendMessage(request);
-
-			var tcs = new TaskCompletionSource<Response>();
-			this.requestCallback[RpcId] = (message) =>
-			{
-				try
-				{
-					Response response = (Response)message;
-					if (response.Error > 100)
-					{
-						tcs.SetException(new RpcException(response.Error, response.Message));
-						return;
-					}
-					//Log.Debug($"recv: {response.ToJson()}");
-					tcs.SetResult(response);
-				}
-				catch (Exception e)
-				{
-					tcs.SetException(new Exception($"Rpc Error: {typeof(Response).FullName}", e));
-				}
-			};
-
-			return tcs.Task;
-		}
-
-		public void Send<Message>(Message message) where Message : AMessage
-		{
-			if (this.Id == 0)
-			{
-				throw new Exception("session已经被Dispose了");
-			}
-			this.SendMessage(message);
-		}
-
-		public void Reply<Response>(Response message) where Response : AResponse
-		{
-			if (this.Id == 0)
-			{
-				throw new Exception("session已经被Dispose了");
-			}
-			this.SendMessage(message);
-		}
-
-		private void SendMessage(object message)
-		{
-			//Log.Debug($"send: {message.ToJson()}");
-			ushort opcode = this.network.Owner.GetComponent<OpcodeTypeComponent>().GetOpcode(message.GetType());
-			opcode = NetworkHelper.HostToNetworkOrder(opcode);
-			byte[] opcodeBytes = BitConverter.GetBytes(opcode);
-
-			byte[] messageBytes = this.network.MessagePacker.SerializeToByteArray(message);
-			byte flag = 0;
-			if (messageBytes.Length > 100)
-			{
-				byte[] newMessageBytes = ZipHelper.Compress(messageBytes);
-				if (newMessageBytes.Length < messageBytes.Length)
-				{
-					messageBytes = newMessageBytes;
-					flag |= 0x80;
-				}
-			}
-
-			byte[] flagBytes = { flag };
-
-			channel.Send(new List<byte[]> { opcodeBytes, flagBytes, messageBytes });
-		}
-
-		public override void Dispose()
-		{
-			if (this.Id == 0)
-			{
-				return;
-			}
-
-			long id = this.Id;
-
-			base.Dispose();
-
-			this.channel.Dispose();
-			this.network.Remove(id);
-		}
-	}
-}

+ 12 - 12
Server/Model/Event/EventIdType.cs

@@ -1,18 +1,18 @@
 namespace Model
 {
-	public static class EventIdType
+	public enum EventIdType
 	{
-		public const int InitSceneStart = 1;
+		InitSceneStart,
 
-		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,
 	}
 }

+ 1 - 1
Server/Model/Helper/DllHelper.cs

@@ -22,7 +22,7 @@ namespace Model
 		public static Type[] GetMonoTypes()
 		{
 			List<Type> types = new List<Type>();
-			foreach (Assembly assembly in ObjectEvents.Instance.GetAssemblies())
+			foreach (Assembly assembly in ObjectEvents.Instance.GetAll())
 			{
 				types.AddRange(assembly.GetTypes());
 			}

+ 0 - 20
Server/Model/Message/AMHandler.cs → Server/Model/Message/AMRpcHandler.cs

@@ -2,26 +2,6 @@
 
 namespace Model
 {
-	public abstract class AMHandler<Message>: IMHandler where Message : AMessage
-	{
-		protected abstract void Run(Session session, Message message);
-
-		public void Handle(Session session, AMessage msg)
-		{
-			Message message = msg as Message;
-			if (message == null)
-			{
-				Log.Error($"消息类型转换错误: {msg.GetType().Name} to {typeof (Message).Name}");
-			}
-			this.Run(session, message);
-		}
-
-		public Type GetMessageType()
-		{
-			return typeof (Message);
-		}
-	}
-
 	public abstract class AMRpcHandler<Request, Response>: IMHandler where Request : ARequest where Response : AResponse
 	{
 		protected static void ReplyError(Response response, Exception e, Action<Response> reply)

+ 0 - 43
Server/Model/Message/AppType.cs

@@ -1,43 +0,0 @@
-using System;
-using System.Collections.Generic;
-
-namespace Model
-{
-	[Flags]
-	public enum AppType
-	{
-		None = 0,
-		Manager = 1,
-		Realm = 1 << 1,
-		Gate = 1 << 2,
-		Http = 1 << 3,
-		DB = 1 << 4,
-		Location = 1 << 5,
-		Map = 1 << 6,
-
-		Robot = 1 << 29,
-		Benchmark = 1 << 30,
-		Client = 1 << 31,
-
-		// 7
-		AllServer = Manager | Realm | Gate | Http | DB | Location | Map
-	}
-
-	public static class AppTypeHelper
-	{
-		public static List<AppType> GetServerTypes()
-		{
-			List<AppType> appTypes = new List<AppType> { AppType.Manager, AppType.Realm, AppType.Gate };
-			return appTypes;
-		}
-
-		public static bool Is(this AppType a, AppType b)
-		{
-			if ((a & b) != 0)
-			{
-				return true;
-			}
-			return false;
-		}
-	}
-}

+ 0 - 7
Server/Model/Message/IMessageDispatcher.cs

@@ -1,7 +0,0 @@
-namespace Model
-{
-	public interface IMessageDispatcher
-	{
-		void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, AMessage message);
-	}
-}

+ 0 - 18
Server/Model/Message/IMessagePacker.cs

@@ -1,18 +0,0 @@
-using System;
-
-namespace Model
-{
-	public interface IMessagePacker
-	{
-		byte[] SerializeToByteArray(object obj);
-		string SerializeToText(object obj);
-
-		object DeserializeFrom(Type type, byte[] bytes);
-		object DeserializeFrom(Type type, byte[] bytes, int index, int count);
-		T DeserializeFrom<T>(byte[] bytes);
-		T DeserializeFrom<T>(byte[] bytes, int index, int count);
-
-		T DeserializeFrom<T>(string str);
-		object DeserializeFrom(Type type, string str);
-	}
-}

+ 0 - 23
Server/Model/Message/RpcException.cs

@@ -1,23 +0,0 @@
-using System;
-
-namespace Model
-{
-	/// <summary>
-	/// RPC异常,带ErrorCode
-	/// </summary>
-	[Serializable]
-	public class RpcException: Exception
-	{
-		public int Error { get; private set; }
-
-		public RpcException(int error, string message): base($"Error: {error} Message: {message}")
-		{
-			this.Error = error;
-		}
-
-		public RpcException(int error, string message, Exception e): base($"Error: {error} Message: {message}", e)
-		{
-			this.Error = error;
-		}
-	}
-}

+ 0 - 51
Server/Model/Object/Component.cs

@@ -1,51 +0,0 @@
-using MongoDB.Bson.Serialization.Attributes;
-
-namespace Model
-{
-	[BsonKnownTypes(typeof(ComponentDB))]
-	public abstract class Component: Disposer
-	{
-		[BsonIgnore]
-		public Entity Owner { get; set; }
-
-		public T GetOwner<T>() where T : Entity
-		{
-			return this.Owner as T;
-		}
-
-		protected Component()
-		{
-		}
-
-		protected Component(long id): base(id)
-		{
-		}
-
-		public T GetComponent<T>() where T : Component
-		{
-			return this.Owner.GetComponent<T>();
-		}
-
-		public override void Dispose()
-		{
-			if (this.Id == 0)
-			{
-				return;
-			}
-
-			base.Dispose();
-
-			if (this.Owner.Id != 0)
-			{
-				this.Owner?.RemoveComponent(this.GetType());
-			}
-		}
-
-		public override void EndInit()
-		{
-			base.EndInit();
-
-			ObjectEvents.Instance.Add(this);
-		}
-	}
-}

+ 0 - 30
Server/Model/Object/Disposer.cs

@@ -1,30 +0,0 @@
-using System;
-
-namespace Model
-{
-	public abstract class Disposer: Object, IDisposable
-	{
-		protected Disposer(): base(IdGenerater.GenerateId())
-		{
-			ObjectEvents.Instance.Add(this);
-		}
-
-		protected Disposer(long id): base(id)
-		{
-			ObjectEvents.Instance.Add(this);
-		}
-
-		public virtual void Dispose()
-		{
-			this.Id = 0;
-		}
-
-		public override void BeginInit()
-		{
-		}
-
-		public override void EndInit()
-		{
-		}
-	}
-}

+ 0 - 215
Server/Model/Object/Entity.cs

@@ -1,215 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using MongoDB.Bson.Serialization.Attributes;
-
-namespace Model
-{
-	public class Entity: Disposer
-	{
-		[BsonIgnore]
-		public Entity Parent { get; set; }
-
-		public EntityType Type { get; set; }
-
-		[BsonElement]
-		[BsonIgnoreIfNull]
-		private HashSet<Component> components = new HashSet<Component>();
-
-		[BsonIgnore]
-		private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
-
-		protected Entity()
-		{
-		}
-
-		protected Entity(long id): base(id)
-		{
-		}
-
-		public override void Dispose()
-		{
-			if (this.Id == 0)
-			{
-				return;
-			}
-
-			base.Dispose();
-
-			foreach (Component component in this.GetComponents())
-			{
-				try
-				{
-					component.Dispose();
-				}
-				catch (Exception e)
-				{
-					Log.Error(e.ToString());
-				}
-			}
-		}
-
-		public K AddComponent<K>() where K : Component, new()
-		{
-			K component = ObjectFactory.Create<K>();
-			component.Owner = this;
-
-			if (this.componentDict.ContainsKey(component.GetType()))
-			{
-				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
-			}
-
-			if (this.components == null)
-			{
-				this.components = new HashSet<Component>();
-			}
-
-			if (component is ComponentDB)
-			{
-				this.components.Add(component);
-			}
-			this.componentDict.Add(component.GetType(), component);
-			return component;
-		}
-
-		public K AddComponent<K, P1>(P1 p1) where K : Component, new()
-		{
-			K component = ObjectFactory.Create<K, P1>(p1);
-			component.Owner = this;
-
-			if (this.componentDict.ContainsKey(component.GetType()))
-			{
-				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
-			}
-
-			if (this.components == null)
-			{
-				this.components = new HashSet<Component>();
-			}
-
-			if (component is ComponentDB)
-			{
-				this.components.Add(component);
-			}
-			this.componentDict.Add(component.GetType(), component);
-			return component;
-		}
-
-		public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
-		{
-			K component = ObjectFactory.Create<K, P1, P2>(p1, p2);
-			component.Owner = this;
-
-			if (this.componentDict.ContainsKey(component.GetType()))
-			{
-				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
-			}
-
-			if (this.components == null)
-			{
-				this.components = new HashSet<Component>();
-			}
-
-			if (component is ComponentDB)
-			{
-				this.components.Add(component);
-			}
-			this.componentDict.Add(component.GetType(), component);
-			return component;
-		}
-
-		public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
-		{
-			K component = ObjectFactory.Create<K, P1, P2, P3>(p1, p2, p3);
-			component.Owner = this;
-
-			if (this.componentDict.ContainsKey(component.GetType()))
-			{
-				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
-			}
-
-			if (this.components == null)
-			{
-				this.components = new HashSet<Component>();
-			}
-
-			if (component is ComponentDB)
-			{
-				this.components.Add(component);
-			}
-			this.componentDict.Add(component.GetType(), component);
-			return component;
-		}
-
-		public void RemoveComponent<K>() where K : Component
-		{
-			if (!this.componentDict.TryGetValue(typeof(K), out Component component))
-			{
-				return;
-			}
-
-			this.components.Remove(component);
-			this.componentDict.Remove(typeof (K));
-			if (this.components.Count == 0)
-			{
-				this.components = null;
-			}
-			component.Dispose();
-		}
-
-		public void RemoveComponent(Type type)
-		{
-			if (!this.componentDict.TryGetValue(type, out Component component))
-			{
-				return;
-			}
-
-			this.components.Remove(component);
-			this.componentDict.Remove(type);
-			if (this.components.Count == 0)
-			{
-				this.components = null;
-			}
-			component.Dispose();
-		}
-
-		public K GetComponent<K>() where K : Component
-		{
-			if (!this.componentDict.TryGetValue(typeof(K), out Component component))
-			{
-				return default(K);
-			}
-			return (K) component;
-		}
-
-		public Component[] GetComponents()
-		{
-			return this.componentDict.Values.ToArray();
-		}
-
-		public override void BeginInit()
-		{
-			base.BeginInit();
-			this.components = new HashSet<Component>();
-			this.componentDict = new Dictionary<Type, Component>();
-		}
-
-		public override void EndInit()
-		{
-			base.EndInit();
-
-			ObjectEvents.Instance.Add(this);
-
-			if (this.components.Count == 0)
-			{
-				this.components = null;
-				return;
-			}
-			foreach (Component component in this.components)
-			{
-				component.Owner = this;
-				this.componentDict.Add(component.GetType(), component);
-			}
-		}
-	}
-}

+ 0 - 22
Server/Model/Object/IAwake.cs

@@ -1,22 +0,0 @@
-namespace Model
-{
-	public interface IAwake
-	{
-		void Awake();
-	}
-
-	public interface IAwake<A>
-	{
-		void Awake(A a);
-	}
-
-	public interface IAwake<A, B>
-	{
-		void Awake(A a, B b);
-	}
-
-	public interface IAwake<A, B, C>
-	{
-		void Awake(A a, B b, C c);
-	}
-}

+ 0 - 7
Server/Model/Object/ILoad.cs

@@ -1,7 +0,0 @@
-namespace Model
-{
-	public interface ILoad
-	{
-		void Load();
-	}
-}

+ 0 - 7
Server/Model/Object/IStart.cs

@@ -1,7 +0,0 @@
-namespace Model
-{
-	public interface IStart
-	{
-		void Start();
-	}
-}

+ 0 - 7
Server/Model/Object/IUpdate.cs

@@ -1,7 +0,0 @@
-namespace Model
-{
-	public interface IUpdate
-	{
-		void Update();
-	}
-}

+ 0 - 42
Server/Model/Object/Object.cs

@@ -1,42 +0,0 @@
-using System;
-using System.ComponentModel;
-using MongoDB.Bson;
-using MongoDB.Bson.Serialization.Attributes;
-
-namespace Model
-{
-	public abstract class Object: ISupportInitialize, ICloneable
-	{
-		[BsonId]
-		[BsonIgnoreIfDefault]
-		public long Id { get; set; }
-
-		protected Object()
-		{
-			Id = IdGenerater.GenerateId();
-		}
-
-		protected Object(long id)
-		{
-			this.Id = id;
-		}
-
-		public virtual void BeginInit()
-		{
-		}
-
-		public virtual void EndInit()
-		{
-		}
-
-		public override string ToString()
-		{
-			return this.ToJson();
-		}
-
-		public object Clone()
-		{
-			return MongoHelper.FromBson(this.GetType(), this.ToBson());
-		}
-	}
-}

+ 0 - 9
Server/Model/Object/ObjectEventAttribute.cs

@@ -1,9 +0,0 @@
-using System;
-
-namespace Model
-{
-	[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
-	public class ObjectEventAttribute: Attribute
-	{
-	}
-}

+ 3 - 3
Server/Model/Object/ObjectEvents.cs

@@ -55,7 +55,7 @@ namespace Model
 		private Queue<Disposer> loaders = new Queue<Disposer>();
 		private Queue<Disposer> loaders2 = new Queue<Disposer>();
 		
-		public void Register(string name, Assembly assembly)
+		public void Add(string name, Assembly assembly)
 		{
 			this.assemblies[name] = assembly;
 
@@ -86,12 +86,12 @@ namespace Model
 			this.Load();
 		}
 
-		public Assembly GetAssembly(string name)
+		public Assembly Get(string name)
 		{
 			return this.assemblies[name];
 		}
 
-		public Assembly[] GetAssemblies()
+		public Assembly[] GetAll()
 		{
 			return this.assemblies.Values.ToArray();
 		}

+ 58 - 18
Server/Model/Server.Model.csproj

@@ -52,6 +52,18 @@
     <Reference Include="System.Numerics" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Message\AMHandler.cs">
+      <Link>Message\AMHandler.cs</Link>
+    </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Message\AppType.cs">
+      <Link>Message\AppType.cs</Link>
+    </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Message\IMessageDispatcher.cs">
+      <Link>Message\IMessageDispatcher.cs</Link>
+    </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Message\IMessagePacker.cs">
+      <Link>Message\IMessagePacker.cs</Link>
+    </Compile>
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Message\JsondotnetPacker.cs">
       <Link>Message\JsondotnetPacker.cs</Link>
     </Compile>
@@ -61,15 +73,60 @@
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Message\ProtobufPacker.cs">
       <Link>Message\ProtobufPacker.cs</Link>
     </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Message\RpcException.cs">
+      <Link>Message\RpcException.cs</Link>
+    </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\Component.cs">
+      <Link>Object\Component.cs</Link>
+    </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\ComponentFactory.cs">
+      <Link>Object\ComponentFactory.cs</Link>
+    </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\Disposer.cs">
+      <Link>Object\Disposer.cs</Link>
+    </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\Entity.cs">
+      <Link>Object\Entity.cs</Link>
+    </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\EntityFactory.cs">
+      <Link>Object\EntityFactory.cs</Link>
+    </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\IAwake.cs">
+      <Link>Object\IAwake.cs</Link>
+    </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\ILoad.cs">
+      <Link>Object\ILoad.cs</Link>
+    </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\IStart.cs">
+      <Link>Object\IStart.cs</Link>
+    </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\IUpdate.cs">
+      <Link>Object\IUpdate.cs</Link>
+    </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\Object.cs">
+      <Link>Object\Object.cs</Link>
+    </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\ObjectEventAttribute.cs">
+      <Link>Object\ObjectEventAttribute.cs</Link>
+    </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Component\EventComponent.cs">
+      <Link>Component\EventComponent.cs</Link>
+    </Compile>
     <Compile Include="..\..\Unity\Assets\Scripts\Component\NetworkComponent.cs">
       <Link>Component\NetworkComponent.cs</Link>
     </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Component\TimerComponent.cs">
+      <Link>Component\TimerComponent.cs</Link>
+    </Compile>
     <Compile Include="..\..\Unity\Assets\Scripts\Entity\Message\Opcode.cs">
       <Link>Entity\Message\Opcode.cs</Link>
     </Compile>
     <Compile Include="..\..\Unity\Assets\Scripts\Entity\Message\OuterMessage.cs">
       <Link>Entity\Message\OuterMessage.cs</Link>
     </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Entity\Session.cs">
+      <Link>Entity\Session.cs</Link>
+    </Compile>
     <Compile Include="..\..\Unity\Assets\Scripts\Helper\MongoHelper.cs">
       <Link>Helper\MongoHelper.cs</Link>
     </Compile>
@@ -78,7 +135,7 @@
     <Compile Include="Component\Unit\UnitGateComponent.cs" />
     <Compile Include="Entity\Player.cs" />
     <Compile Include="Entity\Location.cs" />
-    <Compile Include="Helper\ObjectFactory.cs" />
+    <Compile Include="Message\AMRpcHandler.cs" />
     <Compile Include="Message\IEntityActorHandler.cs" />
     <Compile Include="Component\OpcodeTypeComponent.cs" />
     <Compile Include="Component\ActorManagerComponent.cs" />
@@ -92,7 +149,6 @@
     <Compile Include="Component\DBCacheComponent.cs" />
     <Compile Include="Component\DBComponent.cs" />
     <Compile Include="Component\DBProxyComponent.cs" />
-    <Compile Include="Component\EventComponent.cs" />
     <Compile Include="Component\KVComponent.cs" />
     <Compile Include="Component\LocationProxyComponent.cs" />
     <Compile Include="Component\ActorMessageDispatherComponent.cs" />
@@ -101,7 +157,6 @@
     <Compile Include="Component\NetOuterComponent.cs" />
     <Compile Include="Component\RobotComponent.cs" />
     <Compile Include="Component\Session\SessionPlayerComponent.cs" />
-    <Compile Include="Component\TimerComponent.cs" />
     <Compile Include="Component\LocationComponent.cs" />
     <Compile Include="Component\ActorComponent.cs" />
     <Compile Include="Component\UnitComponent.cs" />
@@ -131,7 +186,6 @@
     <Compile Include="Entity\Message\InnerMessage.cs" />
     <Compile Include="Entity\Message\InnerOpcode.cs" />
     <Compile Include="Entity\Scene.cs" />
-    <Compile Include="Entity\Session.cs" />
     <Compile Include="Entity\Unit.cs" />
     <Compile Include="Event\AEventAttribute.cs" />
     <Compile Include="Event\Env.cs" />
@@ -143,33 +197,19 @@
     <Compile Include="Message\AActorMessage.cs" />
     <Compile Include="Message\AMessage.cs" />
     <Compile Include="Message\AMActorHandler.cs" />
-    <Compile Include="Message\AMHandler.cs" />
-    <Compile Include="Message\AppType.cs" />
     <Compile Include="Message\ErrorCode.cs" />
     <Compile Include="Message\IMActorHandler.cs" />
     <Compile Include="Message\OuterMessageDispatcher.cs" />
     <Compile Include="Message\InnerMessageDispatcher.cs" />
-    <Compile Include="Message\IMessageDispatcher.cs" />
     <Compile Include="Message\IMHandler.cs" />
     <Compile Include="Message\ActorMessageAttribute.cs" />
     <Compile Include="Message\MessageAttribute.cs" />
     <Compile Include="Message\ActorMessageHandlerAttribute.cs" />
     <Compile Include="Message\MessageHandlerAttribute.cs" />
     <Compile Include="Message\OpcodeHelper.cs" />
-    <Compile Include="Message\RpcException.cs" />
     <Compile Include="Object\ComponentDB.cs" />
-    <Compile Include="Object\Component.cs" />
-    <Compile Include="Object\Disposer.cs" />
-    <Compile Include="Object\Entity.cs" />
-    <Compile Include="Object\IAwake.cs" />
-    <Compile Include="Object\ILoad.cs" />
-    <Compile Include="Object\IStart.cs" />
-    <Compile Include="Object\IUpdate.cs" />
-    <Compile Include="Object\ObjectEventAttribute.cs" />
     <Compile Include="Object\EntityType.cs" />
-    <Compile Include="Object\Object.cs" />
     <Compile Include="Object\ObjectEvents.cs" />
-    <Compile Include="Message\IMessagePacker.cs" />
     <Compile Include="Other\OneThreadSynchronizationContext.cs" />
     <Compile Include="Other\Options.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />

+ 6 - 0
Server/Model/Server.Model.csproj.user

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <ProjectView>ProjectFiles</ProjectView>
+  </PropertyGroup>
+</Project>

+ 1 - 5
Unity/Assets/Editor/ServerCommandLineEditor/ServerCommandLineEditor.cs

@@ -359,11 +359,7 @@ namespace MyEditor
 			}
 			GUILayout.EndHorizontal();
 		}
-
-		private void OnDisable()
-		{
-		}
-
+		
 		private void OnDestroy()
 		{
 			this.ClearConfig();

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

@@ -4,16 +4,16 @@ namespace Model
 {
 	public abstract class AMHandler<Message> : IMHandler where Message : AMessage
 	{
-		protected abstract void Run(Message message);
+		protected abstract void Run(Session session, Message message);
 
-		public void Handle(AMessage msg)
+		public void Handle(Session session, AMessage msg)
 		{
 			Message message = msg as Message;
 			if (message == null)
 			{
 				Log.Error($"消息类型转换错误: {msg.GetType().Name} to {typeof(Message).Name}");
 			}
-			this.Run(message);
+			this.Run(session, message);
 		}
 
 		public Type GetMessageType()

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

@@ -12,7 +12,7 @@
 	/// <summary>
 	/// 服务端回的RPC消息需要继承这个抽象类
 	/// </summary>
-	public abstract class AResponse
+	public abstract class AResponse: AMessage
 	{
 		public uint RpcId;
 

+ 2 - 2
Unity/Assets/Scripts/Base/Message/ClientDispatcher.cs

@@ -4,7 +4,7 @@ namespace Model
 {
 	public class ClientDispatcher: IMessageDispatcher
 	{
-		public void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, object message)
+		public void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, AMessage message)
 		{
 			// 普通消息或者是Rpc请求消息
 			if (message is AMessage || message is ARequest)
@@ -16,7 +16,7 @@ namespace Model
 				}
 				else
 				{
-					Game.Scene.GetComponent<MessageDispatherComponent>().Handle(messageInfo);
+					Game.Scene.GetComponent<MessageDispatherComponent>().Handle(session, messageInfo);
 				}
 				return;
 			}

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

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

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

@@ -2,6 +2,6 @@
 {
 	public interface IMessageDispatcher
 	{
-		void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, object message);
+		void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, AMessage message);
 	}
 }

+ 6 - 0
Unity/Assets/Scripts/Base/Object/ComponentDB.cs

@@ -0,0 +1,6 @@
+namespace Model
+{
+	public abstract class ComponentDB : Component
+	{
+	}
+}

+ 3 - 0
Unity/Assets/Scripts/Base/Object/ComponentDB.cs.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: acd5fac31dfb47f4a815f61ec4b22fb3
+timeCreated: 1504236405

+ 39 - 0
Unity/Assets/Scripts/Base/Object/ComponentFactory.cs

@@ -0,0 +1,39 @@
+using System;
+
+namespace Model
+{
+	public static class ComponentFactory
+	{
+		public static T Create<T>(Entity entity) where T : Component
+		{
+			T disposer = (T)Activator.CreateInstance(typeof(T));
+			disposer.Owner = entity;
+			ObjectEvents.Instance.Awake(disposer);
+			return disposer;
+		}
+
+		public static T Create<T, A>(Entity entity, A a) where T : Component
+		{
+			T disposer = (T)Activator.CreateInstance(typeof(T));
+			disposer.Owner = entity;
+			ObjectEvents.Instance.Awake(disposer, a);
+			return disposer;
+		}
+
+		public static T Create<T, A, B>(Entity entity, A a, B b) where T : Component
+		{
+			T disposer = (T)Activator.CreateInstance(typeof(T));
+			disposer.Owner = entity;
+			ObjectEvents.Instance.Awake(disposer, a, b);
+			return disposer;
+		}
+
+		public static T Create<T, A, B, C>(Entity entity, A a, B b, C c) where T : Component
+		{
+			T disposer = (T)Activator.CreateInstance(typeof(T));
+			disposer.Owner = entity;
+			ObjectEvents.Instance.Awake(disposer, a, b, c);
+			return disposer;
+		}
+	}
+}

+ 3 - 0
Unity/Assets/Scripts/Base/Object/ComponentFactory.cs.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: a68d8b697bd643b0ad363104caa72b8e
+timeCreated: 1504238374

+ 11 - 3
Unity/Assets/Scripts/Base/Object/Disposer.cs

@@ -2,14 +2,14 @@
 
 namespace Model
 {
-	public abstract class Disposer: Object, IDisposable
+	public abstract class Disposer : Object, IDisposable
 	{
-		protected Disposer(): base(IdGenerater.GenerateId())
+		protected Disposer() : base(IdGenerater.GenerateId())
 		{
 			ObjectEvents.Instance.Add(this);
 		}
 
-		protected Disposer(long id): base(id)
+		protected Disposer(long id) : base(id)
 		{
 			ObjectEvents.Instance.Add(this);
 		}
@@ -18,5 +18,13 @@ namespace Model
 		{
 			this.Id = 0;
 		}
+
+		public override void BeginInit()
+		{
+		}
+
+		public override void EndInit()
+		{
+		}
 	}
 }

+ 60 - 37
Unity/Assets/Scripts/Base/Object/Entity.cs

@@ -5,19 +5,25 @@ using MongoDB.Bson.Serialization.Attributes;
 
 namespace Model
 {
-	public class Entity: Disposer
+	public class Entity : Disposer
 	{
+		[BsonIgnore]
+		public Entity Parent { get; set; }
+
+		public EntityType Type { get; set; }
+
 		[BsonElement]
+		[BsonIgnoreIfNull]
 		private HashSet<Component> components = new HashSet<Component>();
-		
+
 		[BsonIgnore]
-		private readonly Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
+		private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
 
 		protected Entity()
 		{
 		}
 
-		protected Entity(long id): base(id)
+		protected Entity(long id) : base(id)
 		{
 		}
 
@@ -45,31 +51,33 @@ namespace Model
 
 		public K AddComponent<K>() where K : Component, new()
 		{
-			K component = (K) Activator.CreateInstance(typeof (K));
-			component.Owner = this;
+			K component = ComponentFactory.Create<K>(this);
+
 			if (this.componentDict.ContainsKey(component.GetType()))
 			{
-				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
+				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
+
 			if (this.components == null)
 			{
 				this.components = new HashSet<Component>();
 			}
-			this.components.Add(component);
+
+			if (component is ComponentDB)
+			{
+				this.components.Add(component);
+			}
 			this.componentDict.Add(component.GetType(), component);
-			IAwake awake = component as IAwake;
-			awake?.Awake();
 			return component;
 		}
 
 		public K AddComponent<K, P1>(P1 p1) where K : Component, new()
 		{
-			K component = (K) Activator.CreateInstance(typeof (K));
-			component.Owner = this;
+			K component = ComponentFactory.Create<K, P1>(this, p1);
 
 			if (this.componentDict.ContainsKey(component.GetType()))
 			{
-				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
+				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
 			if (this.components == null)
@@ -77,21 +85,21 @@ namespace Model
 				this.components = new HashSet<Component>();
 			}
 
-			this.components.Add(component);
+			if (component is ComponentDB)
+			{
+				this.components.Add(component);
+			}
 			this.componentDict.Add(component.GetType(), component);
-			IAwake<P1> awake = component as IAwake<P1>;
-			awake?.Awake(p1);
 			return component;
 		}
 
 		public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
 		{
-			K component = (K) Activator.CreateInstance(typeof (K));
-			component.Owner = this;
+			K component = ComponentFactory.Create<K, P1, P2>(this, p1, p2);
 
 			if (this.componentDict.ContainsKey(component.GetType()))
 			{
-				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
+				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
 			if (this.components == null)
@@ -99,21 +107,21 @@ namespace Model
 				this.components = new HashSet<Component>();
 			}
 
-			this.components.Add(component);
+			if (component is ComponentDB)
+			{
+				this.components.Add(component);
+			}
 			this.componentDict.Add(component.GetType(), component);
-			IAwake<P1, P2> awake = component as IAwake<P1, P2>;
-			awake?.Awake(p1, p2);
 			return component;
 		}
 
 		public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
 		{
-			K component = (K) Activator.CreateInstance(typeof (K));
-			component.Owner = this;
+			K component = ComponentFactory.Create<K, P1, P2, P3>(this, p1, p2, p3);
 
 			if (this.componentDict.ContainsKey(component.GetType()))
 			{
-				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
+				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
 			if (this.components == null)
@@ -121,24 +129,25 @@ namespace Model
 				this.components = new HashSet<Component>();
 			}
 
-			this.components.Add(component);
+			if (component is ComponentDB)
+			{
+				this.components.Add(component);
+			}
 			this.componentDict.Add(component.GetType(), component);
-			IAwake<P1, P2, P3> awake = component as IAwake<P1, P2, P3>;
-			awake?.Awake(p1, p2, p3);
 			return component;
 		}
 
 		public void RemoveComponent<K>() where K : Component
 		{
 			Component component;
-			if (!this.componentDict.TryGetValue(typeof (K), out component))
+			if (!this.componentDict.TryGetValue(typeof(K), out component))
 			{
 				return;
 			}
 
-			this.components.Remove(component);
-			this.componentDict.Remove(typeof (K));
-			if (this.components.Count == 0)
+			this.components?.Remove(component);
+			this.componentDict.Remove(typeof(K));
+			if (this.components != null && this.components.Count == 0)
 			{
 				this.components = null;
 			}
@@ -153,9 +162,9 @@ namespace Model
 				return;
 			}
 
-			this.components.Remove(component);
+			this.components?.Remove(component);
 			this.componentDict.Remove(type);
-			if (this.components.Count == 0)
+			if (this.components != null && this.components.Count == 0)
 			{
 				this.components = null;
 			}
@@ -165,24 +174,38 @@ namespace Model
 		public K GetComponent<K>() where K : Component
 		{
 			Component component;
-			if (!this.componentDict.TryGetValue(typeof (K), out component))
+			if (!this.componentDict.TryGetValue(typeof(K), out component))
 			{
 				return default(K);
 			}
-			return (K) component;
+			return (K)component;
 		}
 
 		public Component[] GetComponents()
 		{
-			return components.ToArray();
+			return this.componentDict.Values.ToArray();
+		}
+
+		public override void BeginInit()
+		{
+			base.BeginInit();
+			this.components = new HashSet<Component>();
+			this.componentDict = new Dictionary<Type, Component>();
 		}
 
 		public override void EndInit()
 		{
 			base.EndInit();
 
+			ObjectEvents.Instance.Add(this);
+
+			if (this.components != null && this.components.Count == 0)
+			{
+				this.components = null;
+			}
 			foreach (Component component in this.components)
 			{
+				component.Owner = this;
 				this.componentDict.Add(component.GetType(), component);
 			}
 		}

+ 0 - 31
Unity/Assets/Scripts/Base/Object/EntityEventId.cs

@@ -1,31 +0,0 @@
-namespace Model
-{
-	public enum EntityEventId
-	{
-		TimerComponent,
-		ClientConfigComponent,
-		EventComponent,
-		GameObjectComponent,
-		ILEventComponent,
-		MessageDispatherComponent,
-		NetInnerComponent,
-		NetOuterComponent,
-		RobotComponent,
-		UIComponent,
-		WWWAsync,
-		ILRuntimeComponent,
-		ResourcesComponent,
-		UnitComponent,
-
-		AppManagerComponent,
-		GateSessionKeyComponent,
-		OptionComponent,
-		StartConfigComponent,
-		LockComponent,
-		MasterComponent,
-		RealmGateAddressComponent,
-		UILoginComponent,
-		BehaviorTreeComponent,
-		CrossComponent,
-	}
-}

+ 0 - 12
Unity/Assets/Scripts/Base/Object/EntityEventId.cs.meta

@@ -1,12 +0,0 @@
-fileFormatVersion: 2
-guid: 5899ef72e43a3354dbda95717ae3d73a
-timeCreated: 1498117617
-licenseType: Free
-MonoImporter:
-  serializedVersion: 2
-  defaultReferences: []
-  executionOrder: 0
-  icon: {instanceID: 0}
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 1 - 1
Server/Model/Helper/ObjectFactory.cs → Unity/Assets/Scripts/Base/Object/EntityFactory.cs

@@ -2,7 +2,7 @@
 
 namespace Model
 {
-	public static class ObjectFactory
+	public static class EntityFactory
 	{
 		public static T Create<T>() where T : Disposer
 		{

+ 3 - 0
Unity/Assets/Scripts/Base/Object/EntityFactory.cs.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 3a5156e0921549728e8ffeceeeee34a3
+timeCreated: 1504236309

+ 1 - 1
Unity/Assets/Scripts/Base/Object/Object.cs

@@ -4,7 +4,7 @@ namespace Model
 {
 	public abstract class Object: ISupportInitialize
 	{
-		public long Id { get; protected set; }
+		public long Id { get; set; }
 
 		protected Object()
 		{

+ 0 - 6
Unity/Assets/Scripts/Base/Object/ObjectEventAttribute.cs

@@ -5,11 +5,5 @@ namespace Model
 	[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
 	public class ObjectEventAttribute: Attribute
 	{
-		public int ClassType;
-
-		public ObjectEventAttribute(int classType)
-		{
-			this.ClassType = classType;
-		}
 	}
 }

+ 215 - 38
Unity/Assets/Scripts/Base/Object/ObjectEvents.cs

@@ -1,25 +1,40 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using System.Linq;
 using System.Reflection;
 
 namespace Model
 {
-	public sealed class ObjectEvents
+	public interface IObjectEvent
 	{
-		private static ObjectEvents instance;
+		Type Type();
+		void Set(object value);
+	}
 
+	public abstract class ObjectEvent<T> : IObjectEvent
+	{
+		private T value;
 
-		private Queue<Disposer> loads = new Queue<Disposer>();
-		private Queue<Disposer> loads2 = new Queue<Disposer>();
+		protected T Get()
+		{
+			return value;
+		}
 
-		private Queue<Disposer> starts = new Queue<Disposer>();
+		public void Set(object v)
+		{
+			this.value = (T)v;
+		}
 
-		private Queue<Disposer> updates = new Queue<Disposer>();
-		private Queue<Disposer> updates2 = new Queue<Disposer>();
+		public Type Type()
+		{
+			return typeof(T);
+		}
+	}
+
+	public sealed class ObjectEvents
+	{
+		private static ObjectEvents instance;
 
-		private Queue<Disposer> lateUpdates = new Queue<Disposer>();
-		private Queue<Disposer> lateUpdates2 = new Queue<Disposer>();
-		
 		public static ObjectEvents Instance
 		{
 			get
@@ -28,73 +43,190 @@ namespace Model
 			}
 		}
 
+		private readonly Dictionary<string, Assembly> assemblies = new Dictionary<string, Assembly>();
+
+		private readonly Dictionary<Type, IObjectEvent> disposerEvents = new Dictionary<Type, IObjectEvent>();
+
+		private Queue<Disposer> updates = new Queue<Disposer>();
+		private Queue<Disposer> updates2 = new Queue<Disposer>();
+
+		private readonly Queue<Disposer> starts = new Queue<Disposer>();
+
+		private Queue<Disposer> loaders = new Queue<Disposer>();
+		private Queue<Disposer> loaders2 = new Queue<Disposer>();
+
+		private Queue<Disposer> lateUpdates = new Queue<Disposer>();
+		private Queue<Disposer> lateUpdates2 = new Queue<Disposer>();
+
 		public static void Close()
 		{
 			instance = null;
 		}
 
-		private readonly Dictionary<string, Assembly> dictionary = new Dictionary<string, Assembly>();
-
 		public void Add(string name, Assembly assembly)
 		{
-			this.dictionary[name] = assembly;
+			this.assemblies[name] = assembly;
+
+			this.disposerEvents.Clear();
+			foreach (Assembly ass in this.assemblies.Values)
+			{
+				Type[] types = ass.GetTypes();
+				foreach (Type type in types)
+				{
+					object[] attrs = type.GetCustomAttributes(typeof(ObjectEventAttribute), false);
+
+					if (attrs.Length == 0)
+					{
+						continue;
+					}
+
+					object obj = Activator.CreateInstance(type);
+					IObjectEvent objectEvent = obj as IObjectEvent;
+					if (objectEvent == null)
+					{
+						Log.Error($"组件事件没有继承IObjectEvent: {type.Name}");
+						continue;
+					}
+					this.disposerEvents[objectEvent.Type()] = objectEvent;
+				}
+			}
+
 			this.Load();
 		}
 
-		public void Remove(string name)
+		public Assembly Get(string name)
 		{
-			this.dictionary.Remove(name);
+			return this.assemblies[name];
 		}
 
 		public Assembly[] GetAll()
 		{
-			return this.dictionary.Values.ToArray();
+			return this.assemblies.Values.ToArray();
 		}
 
-		public Assembly Get(string name)
+		public void Add(Disposer disposer)
 		{
-			return this.dictionary[name];
+			IObjectEvent objectEvent;
+			if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectEvent))
+			{
+				return;
+			}
+
+			if (objectEvent is ILoad)
+			{
+				this.loaders.Enqueue(disposer);
+			}
+
+			if (objectEvent is IUpdate)
+			{
+				this.updates.Enqueue(disposer);
+			}
+
+			if (objectEvent is IStart)
+			{
+				this.starts.Enqueue(disposer);
+			}
 		}
 
-		public void Add(Disposer disposer)
+		public void Awake(Disposer disposer)
 		{
-			if (disposer is ILoad)
+			IObjectEvent objectEvent;
+			if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectEvent))
 			{
-				this.loads.Enqueue(disposer);
+				return;
 			}
+			IAwake iAwake = objectEvent as IAwake;
+			if (iAwake == null)
+			{
+				return;
+			}
+			objectEvent.Set(disposer);
+			iAwake.Awake();
+		}
 
-			if (disposer is IStart)
+		public void Awake<P1>(Disposer disposer, P1 p1)
+		{
+			IObjectEvent objectEvent;
+			if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectEvent))
 			{
-				this.starts.Enqueue(disposer);
+				return;
+			}
+			IAwake<P1> iAwake = objectEvent as IAwake<P1>;
+			if (iAwake == null)
+			{
+				return;
 			}
+			objectEvent.Set(disposer);
+			iAwake.Awake(p1);
+		}
 
-			if (disposer is IUpdate)
+		public void Awake<P1, P2>(Disposer disposer, P1 p1, P2 p2)
+		{
+			IObjectEvent objectEvent;
+			if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectEvent))
 			{
-				this.updates.Enqueue(disposer);
+				return;
 			}
+			IAwake<P1, P2> iAwake = objectEvent as IAwake<P1, P2>;
+			if (iAwake == null)
+			{
+				return;
+			}
+			objectEvent.Set(disposer);
+			iAwake.Awake(p1, p2);
+		}
 
-			if (disposer is ILateUpdate)
+		public void Awake<P1, P2, P3>(Disposer disposer, P1 p1, P2 p2, P3 p3)
+		{
+			IObjectEvent objectEvent;
+			if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectEvent))
 			{
-				this.lateUpdates.Enqueue(disposer);
+				return;
 			}
+			IAwake<P1, P2, P3> iAwake = objectEvent as IAwake<P1, P2, P3>;
+			if (iAwake == null)
+			{
+				return;
+			}
+			objectEvent.Set(disposer);
+			iAwake.Awake(p1, p2, p3);
 		}
 
 		public void Load()
 		{
-			while (this.loads.Count > 0)
+			while (this.loaders.Count > 0)
 			{
-				Disposer disposer = this.loads.Dequeue();
+				Disposer disposer = this.loaders.Dequeue();
 				if (disposer.Id == 0)
 				{
 					continue;
 				}
 
-				this.loads2.Enqueue(disposer);
+				IObjectEvent objectEvent;
+				if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectEvent))
+				{
+					continue;
+				}
+
+				this.loaders2.Enqueue(disposer);
 
-				((ILoad)disposer).Load();
+				ILoad iLoad = objectEvent as ILoad;
+				if (iLoad == null)
+				{
+					continue;
+				}
+				objectEvent.Set(disposer);
+				try
+				{
+					iLoad.Load();
+				}
+				catch (Exception e)
+				{
+					Log.Error(e.ToString());
+				}
 			}
 
-			ObjectHelper.Swap(ref this.loads, ref this.loads2);
+			ObjectHelper.Swap(ref this.loaders, ref this.loaders2);
 		}
 
 		private void Start()
@@ -102,12 +234,19 @@ namespace Model
 			while (this.starts.Count > 0)
 			{
 				Disposer disposer = this.starts.Dequeue();
-				if (disposer.Id == 0)
+
+				IObjectEvent objectEvent;
+				if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectEvent))
 				{
 					continue;
 				}
-
-				((IStart)disposer).Start();
+				IStart iStart = objectEvent as IStart;
+				if (iStart == null)
+				{
+					continue;
+				}
+				objectEvent.Set(disposer);
+				iStart.Start();
 			}
 		}
 
@@ -123,9 +262,28 @@ namespace Model
 					continue;
 				}
 
+				IObjectEvent objectEvent;
+				if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectEvent))
+				{
+					continue;
+				}
+
 				this.updates2.Enqueue(disposer);
 
-				((IUpdate)disposer).Update();
+				IUpdate iUpdate = objectEvent as IUpdate;
+				if (iUpdate == null)
+				{
+					continue;
+				}
+				objectEvent.Set(disposer);
+				try
+				{
+					iUpdate.Update();
+				}
+				catch (Exception e)
+				{
+					Log.Error(e.ToString());
+				}
 			}
 
 			ObjectHelper.Swap(ref this.updates, ref this.updates2);
@@ -141,9 +299,28 @@ namespace Model
 					continue;
 				}
 
+				IObjectEvent objectEvent;
+				if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectEvent))
+				{
+					continue;
+				}
+
 				this.lateUpdates2.Enqueue(disposer);
 
-				((ILateUpdate)disposer).LateUpdate();
+				ILateUpdate iLateUpdate = objectEvent as ILateUpdate;
+				if (iLateUpdate == null)
+				{
+					continue;
+				}
+				objectEvent.Set(disposer);
+				try
+				{
+					iLateUpdate.LateUpdate();
+				}
+				catch (Exception e)
+				{
+					Log.Error(e.ToString());
+				}
 			}
 
 			ObjectHelper.Swap(ref this.lateUpdates, ref this.lateUpdates2);

+ 14 - 1
Unity/Assets/Scripts/Component/BehaviorTreeComponent.cs

@@ -5,7 +5,20 @@ using UnityEngine;
 
 namespace Model
 {
-	[EntityEvent((int)EntityEventId.BehaviorTreeComponent)]
+	[ObjectEvent]
+	public class BehaviorTreeComponentEvent : ObjectEvent<BehaviorTreeComponent>, IAwake, ILoad
+	{
+		public void Awake()
+		{
+			this.Get().Awake();
+		}
+
+		public void Load()
+		{
+			this.Get().Load();
+		}
+	}
+	
 	public class BehaviorTreeComponent : Component
 	{
 		private Dictionary<string, Func<NodeProto, Node>> dictionary;

+ 20 - 65
Unity/Assets/Scripts/Component/CrossComponent.cs

@@ -3,18 +3,25 @@ using System.Collections.Generic;
 
 namespace Model
 {
+	[ObjectEvent]
+	public class CrossComponentEvent : ObjectEvent<CrossComponent>, IAwake, ILoad
+	{
+		public void Awake()
+		{
+			this.Get().Awake();
+		}
+
+		public void Load()
+		{
+			this.Get().Load();
+		}
+	}
+
 	/// <summary>
 	/// 事件分发,可以将事件分发到IL层
 	/// </summary>
-	[EntityEvent((int)EntityEventId.CrossComponent)]
-	public class CrossComponent : Component, IAwake
+	public class CrossComponent : Component
 	{
-		private static object[] args0 = new object[0];
-		private static object[] args1 = new object[1];
-		private static object[] args2 = new object[2];
-		private static object[] args3 = new object[3];
-		private static object[] args4 = new object[4];
-
 		private Dictionary<int, List<IInstanceMethod>> allEvents;
 
 		public void Awake()
@@ -22,7 +29,7 @@ namespace Model
 			this.Load();
 		}
 
-		private void Load()
+		public void Load()
 		{
 			this.allEvents = new Dictionary<int, List<IInstanceMethod>>();
 
@@ -56,7 +63,7 @@ namespace Model
 			{
 				try
 				{
-					obj.Run(args0);
+					obj.Run();
 				}
 				catch (Exception err)
 				{
@@ -77,8 +84,7 @@ namespace Model
 			{
 				try
 				{
-					args1[0] = a;
-					obj.Run(args1);
+					obj.Run(a);
 				}
 				catch (Exception err)
 				{
@@ -99,9 +105,7 @@ namespace Model
 			{
 				try
 				{
-					args2[0] = a;
-					args2[1] = b;
-					obj.Run(args2);
+					obj.Run(a, b);
 				}
 				catch (Exception err)
 				{
@@ -122,56 +126,7 @@ namespace Model
 			{
 				try
 				{
-					args3[0] = a;
-					args3[1] = b;
-					args3[2] = c;
-					obj.Run(args3);
-				}
-				catch (Exception err)
-				{
-					Log.Error(err.ToString());
-				}
-			}
-		}
-
-		public void Run<A, B, C, D>(int type, A a, B b, C c, D d)
-		{
-			List<IInstanceMethod> iEvents = null;
-			if (!this.allEvents.TryGetValue(type, out iEvents))
-			{
-				return;
-			}
-
-			foreach (IInstanceMethod obj in iEvents)
-			{
-				try
-				{
-					args4[0] = a;
-					args4[1] = b;
-					args4[2] = c;
-					args4[3] = d;
-					obj.Run(args4);
-				}
-				catch (Exception err)
-				{
-					Log.Error(err.ToString());
-				}
-			}
-		}
-
-		public void Run(int type, params object[] param)
-		{
-			List<IInstanceMethod> iEvents = null;
-			if (!this.allEvents.TryGetValue(type, out iEvents))
-			{
-				return;
-			}
-
-			foreach (IInstanceMethod obj in iEvents)
-			{
-				try
-				{
-					obj.Run(param);
+					obj.Run(a, b, c);
 				}
 				catch (Exception err)
 				{

+ 15 - 2
Unity/Assets/Scripts/Component/EventComponent.cs

@@ -3,8 +3,21 @@ using System.Collections.Generic;
 
 namespace Model
 {
-	[ObjectEvent((int)EntityEventId.EventComponent)]
-	public class EventComponent : Component, IAwake, ILoad
+	[ObjectEvent]
+	public class EventComponentEvent : ObjectEvent<EventComponent>, IAwake, ILoad
+	{
+		public void Awake()
+		{
+			this.Get().Awake();
+		}
+
+		public void Load()
+		{
+			this.Get().Load();
+		}
+	}
+	
+	public class EventComponent : Component
 	{
 		public static EventComponent Instance;
 

+ 17 - 4
Unity/Assets/Scripts/Component/MessageDispatherComponent.cs

@@ -3,11 +3,24 @@ using System.Collections.Generic;
 
 namespace Model
 {
+	[ObjectEvent]
+	public class MessageDispatherComponentEvent : ObjectEvent<MessageDispatherComponent>, IAwake, ILoad
+	{
+		public void Awake()
+		{
+			this.Get().Awake();
+		}
+
+		public void Load()
+		{
+			this.Get().Load();
+		}
+	}
+
 	/// <summary>
 	/// 消息分发组件
 	/// </summary>
-	[ObjectEvent((int)EntityEventId.MessageDispatherComponent)]
-	public class MessageDispatherComponent : Component, IAwake, ILoad
+	public class MessageDispatherComponent : Component
 	{
 		private Dictionary<ushort, List<IMHandler>> handlers;
 
@@ -39,7 +52,7 @@ namespace Model
 			}
 		}
 
-		public void Handle(MessageInfo messageInfo)
+		public void Handle(Session session, MessageInfo messageInfo)
 		{
 			List<IMHandler> actions;
 			if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions))
@@ -52,7 +65,7 @@ namespace Model
 			{
 				try
 				{
-					ev.Handle(messageInfo.Message);
+					ev.Handle(session, messageInfo.Message);
 				}
 				catch (Exception e)
 				{

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

@@ -1,6 +1,20 @@
 namespace Model
 {
-	public class NetOuterComponent : NetworkComponent, IAwake, IAwake<string, int>
+	[ObjectEvent]
+	public class NetOuterComponentEvent : ObjectEvent<NetOuterComponent>, IAwake, IAwake<string, int>
+	{
+		public void Awake()
+		{
+			this.Get().Awake();
+		}
+
+		public void Awake(string host, int port)
+		{
+			this.Get().Awake();
+		}
+	}
+
+	public class NetOuterComponent : NetworkComponent
 	{
 		public void Awake()
 		{

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

@@ -2,7 +2,16 @@
 
 namespace Model
 {
-	public class OpcodeTypeComponent : Component, IAwake
+	[ObjectEvent]
+	public class OpcodeTypeComponentEvent : ObjectEvent<OpcodeTypeComponent>, IAwake
+	{
+		public void Awake()
+		{
+			this.Get().Awake();
+		}
+	}
+
+	public class OpcodeTypeComponent : Component
 	{
 		private readonly DoubleMap<ushort, Type> opcodeTypes = new DoubleMap<ushort, Type>();
 

+ 0 - 1
Unity/Assets/Scripts/Component/ResourcesComponent.cs

@@ -5,7 +5,6 @@ using UnityEngine;
 
 namespace Model
 {
-	[ObjectEvent((int)EntityEventId.ResourcesComponent)]
 	public class ResourcesComponent: Component
 	{
 		public static AssetBundleManifest AssetBundleManifestObject { get; set; }

+ 9 - 1
Unity/Assets/Scripts/Component/TimerComponent.cs

@@ -12,7 +12,15 @@ namespace Model
 		public TaskCompletionSource<bool> tcs;
 	}
 
-	[EntityEvent((int)EntityEventId.TimerComponent)]
+	[ObjectEvent]
+	public class TimerComponentEvent : ObjectEvent<TimerComponent>, IUpdate
+	{
+		public void Update()
+		{
+			this.Get().Update();
+		}
+	}
+
 	public class TimerComponent: Component, IUpdate
 	{
 		private readonly Dictionary<long, Timer> timers = new Dictionary<long, Timer>();

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

@@ -119,7 +119,7 @@ namespace Model
 				return;
 			}
 
-			this.network.MessageDispatcher.Dispatch(this, opcode, offset, messageBytes, message);
+			this.network.MessageDispatcher.Dispatch(this, opcode, offset, messageBytes, (AMessage)message);
 		}
 
 		/// <summary>

+ 9 - 1
Unity/Assets/Scripts/Entity/WWWAsync.cs

@@ -5,7 +5,15 @@ using UnityEngine;
 
 namespace Model
 {
-	[EntityEvent((int)EntityEventId.WWWAsync)]
+	[ObjectEvent]
+	public class WWWAsyncEvent : ObjectEvent<WWWAsync>, IUpdate
+	{
+		public void Update()
+		{
+			this.Get().Update();
+		}
+	}
+	
 	public class WWWAsync: Entity
 	{
 		public WWW www;

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

@@ -3,7 +3,7 @@
 	[MessageHandler(Opcode.Actor_Test)]
 	public class Actor_TestHandler : AMHandler<Actor_Test>
 	{
-		protected override void Run(Actor_Test message)
+		protected override void Run(Session session, Actor_Test message)
 		{
 			Log.Debug(message.Info);
 		}

+ 4 - 15
Unity/Assets/Scripts/Helper/ILHelper.cs

@@ -1,20 +1,14 @@
 using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
 using System.Reflection;
-using ILRuntime.CLR.Method;
-using ILRuntime.CLR.TypeSystem;
 using ILRuntime.Runtime.Enviorment;
-using UnityEngine;
-using AppDomain = System.AppDomain;
 
 namespace Model
 {
 	public static class ILHelper
 	{
-		public static unsafe void RegisterRedirection()
+		public static unsafe void InitILRuntime()
 		{
+			// 注册重定向函数
 			MethodInfo mi = typeof(Log).GetMethod("Debug", new Type[] { typeof(string) });
 			Init.Instance.AppDomain.RegisterCLRMethodRedirection(mi, ILRedirection.LogDebug);
 
@@ -23,19 +17,14 @@ namespace Model
 
 			MethodInfo mi3 = typeof(Log).GetMethod("Error", new Type[] { typeof(string) });
 			Init.Instance.AppDomain.RegisterCLRMethodRedirection(mi3, ILRedirection.LogError);
-		}
 
-		public static unsafe void RegisterDelegate()
-		{
+			// 注册委托
 			Init.Instance.AppDomain.DelegateManager.RegisterMethodDelegate<AChannel, System.Net.Sockets.SocketError>();
 			Init.Instance.AppDomain.DelegateManager.RegisterMethodDelegate<byte[], int, int>();
 
-		}
 
-		public static unsafe void RegisterILAdapter()
-		{
+			// 注册适配器
 			Assembly assembly = typeof(Init).Assembly;
-
 			foreach (Type type in assembly.GetTypes())
 			{
 				object[] attrs = type.GetCustomAttributes(typeof(ILAdapterAttribute), false);

+ 2 - 6
Unity/Assets/Scripts/Init.cs

@@ -40,17 +40,13 @@ namespace Model
 				Log.Debug("run in ilruntime mode");
 
 				this.AppDomain = new ILRuntime.Runtime.Enviorment.AppDomain();
-				
-				DllHelper.LoadHotfixAssembly();			
-				ILHelper.RegisterILAdapter();
-				ILHelper.RegisterDelegate();
-				ILHelper.RegisterRedirection();
+				DllHelper.LoadHotfixAssembly();
+				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");
 				ObjectEvents.Instance.Add("Model", typeof(Init).Assembly);

+ 6 - 0
Unity/Hotfix/Base/Object/ComponentDB.cs

@@ -0,0 +1,6 @@
+namespace Hotfix
+{
+	public abstract class ComponentDB : Component
+	{
+	}
+}

+ 39 - 0
Unity/Hotfix/Base/Object/ComponentFactory.cs

@@ -0,0 +1,39 @@
+using System;
+
+namespace Hotfix
+{
+	public static class ComponentFactory
+	{
+		public static T Create<T>(Entity entity) where T : Component
+		{
+			T disposer = (T)Activator.CreateInstance(typeof(T));
+			disposer.Owner = entity;
+			ObjectEvents.Instance.Awake(disposer);
+			return disposer;
+		}
+
+		public static T Create<T, A>(Entity entity, A a) where T : Component
+		{
+			T disposer = (T)Activator.CreateInstance(typeof(T));
+			disposer.Owner = entity;
+			ObjectEvents.Instance.Awake(disposer, a);
+			return disposer;
+		}
+
+		public static T Create<T, A, B>(Entity entity, A a, B b) where T : Component
+		{
+			T disposer = (T)Activator.CreateInstance(typeof(T));
+			disposer.Owner = entity;
+			ObjectEvents.Instance.Awake(disposer, a, b);
+			return disposer;
+		}
+
+		public static T Create<T, A, B, C>(Entity entity, A a, B b, C c) where T : Component
+		{
+			T disposer = (T)Activator.CreateInstance(typeof(T));
+			disposer.Owner = entity;
+			ObjectEvents.Instance.Awake(disposer, a, b, c);
+			return disposer;
+		}
+	}
+}

+ 46 - 43
Unity/Hotfix/Base/Object/Entity.cs

@@ -2,30 +2,30 @@
 using System.Collections.Generic;
 using System.Linq;
 using Model;
+using MongoDB.Bson.Serialization.Attributes;
 
 namespace Hotfix
 {
-	public class Entity: Disposer
+	public class Entity : Disposer
 	{
+		[BsonIgnore]
+		public Entity Parent { get; set; }
+
 		public EntityType Type { get; set; }
-		
+
+		[BsonElement]
+		[BsonIgnoreIfNull]
 		private HashSet<Component> components = new HashSet<Component>();
-		
-		private readonly Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
 
-		protected Entity()
-		{
-			this.Type = EntityType.None;
-		}
+		[BsonIgnore]
+		private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
 
-		protected Entity(EntityType entityType)
+		protected Entity()
 		{
-			this.Type = entityType;
 		}
 
-		protected Entity(long id, EntityType entityType): base(id)
+		protected Entity(long id) : base(id)
 		{
-			this.Type = entityType;
 		}
 
 		public override void Dispose()
@@ -52,31 +52,33 @@ namespace Hotfix
 
 		public K AddComponent<K>() where K : Component, new()
 		{
-			K component = (K) Activator.CreateInstance(typeof (K));
-			component.Owner = this;
+			K component = ComponentFactory.Create<K>(this);
+
 			if (this.componentDict.ContainsKey(component.GetType()))
 			{
-				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
+				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
+
 			if (this.components == null)
 			{
 				this.components = new HashSet<Component>();
 			}
-			this.components.Add(component);
+
+			if (component is ComponentDB)
+			{
+				this.components.Add(component);
+			}
 			this.componentDict.Add(component.GetType(), component);
-			IAwake awake = component as IAwake;
-			awake?.Awake();
 			return component;
 		}
 
 		public K AddComponent<K, P1>(P1 p1) where K : Component, new()
 		{
-			K component = (K) Activator.CreateInstance(typeof (K));
-			component.Owner = this;
+			K component = ComponentFactory.Create<K, P1>(this, p1);
 
 			if (this.componentDict.ContainsKey(component.GetType()))
 			{
-				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
+				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
 			if (this.components == null)
@@ -84,21 +86,21 @@ namespace Hotfix
 				this.components = new HashSet<Component>();
 			}
 
-			this.components.Add(component);
+			if (component is ComponentDB)
+			{
+				this.components.Add(component);
+			}
 			this.componentDict.Add(component.GetType(), component);
-			IAwake<P1> awake = component as IAwake<P1>;
-			awake?.Awake(p1);
 			return component;
 		}
 
 		public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
 		{
-			K component = (K) Activator.CreateInstance(typeof (K));
-			component.Owner = this;
+			K component = ComponentFactory.Create<K, P1, P2>(this, p1, p2);
 
 			if (this.componentDict.ContainsKey(component.GetType()))
 			{
-				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
+				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
 			if (this.components == null)
@@ -106,21 +108,21 @@ namespace Hotfix
 				this.components = new HashSet<Component>();
 			}
 
-			this.components.Add(component);
+			if (component is ComponentDB)
+			{
+				this.components.Add(component);
+			}
 			this.componentDict.Add(component.GetType(), component);
-			IAwake<P1, P2> awake = component as IAwake<P1, P2>;
-			awake?.Awake(p1, p2);
 			return component;
 		}
 
 		public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
 		{
-			K component = (K) Activator.CreateInstance(typeof (K));
-			component.Owner = this;
+			K component = ComponentFactory.Create<K, P1, P2, P3>(this, p1, p2, p3);
 
 			if (this.componentDict.ContainsKey(component.GetType()))
 			{
-				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
+				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
 			if (this.components == null)
@@ -128,10 +130,11 @@ namespace Hotfix
 				this.components = new HashSet<Component>();
 			}
 
-			this.components.Add(component);
+			if (component is ComponentDB)
+			{
+				this.components.Add(component);
+			}
 			this.componentDict.Add(component.GetType(), component);
-			IAwake<P1, P2, P3> awake = component as IAwake<P1, P2, P3>;
-			awake?.Awake(p1, p2, p3);
 			return component;
 		}
 
@@ -143,9 +146,9 @@ namespace Hotfix
 				return;
 			}
 
-			this.components.Remove(component);
-			this.componentDict.Remove(typeof (K));
-			if (this.components.Count == 0)
+			this.components?.Remove(component);
+			this.componentDict.Remove(typeof(K));
+			if (this.components != null && this.components.Count == 0)
 			{
 				this.components = null;
 			}
@@ -160,9 +163,9 @@ namespace Hotfix
 				return;
 			}
 
-			this.components.Remove(component);
+			this.components?.Remove(component);
 			this.componentDict.Remove(type);
-			if (this.components.Count == 0)
+			if (this.components != null && this.components.Count == 0)
 			{
 				this.components = null;
 			}
@@ -176,12 +179,12 @@ namespace Hotfix
 			{
 				return default(K);
 			}
-			return (K) component;
+			return (K)component;
 		}
 
 		public Component[] GetComponents()
 		{
-			return components.ToArray();
+			return this.componentDict.Values.ToArray();
 		}
 	}
 }

+ 67 - 0
Unity/Hotfix/Base/Object/EntityFactory.cs

@@ -0,0 +1,67 @@
+using System;
+
+namespace Hotfix
+{
+	public static class EntityFactory
+	{
+		public static T Create<T>() where T : Disposer
+		{
+			T disposer = (T)Activator.CreateInstance(typeof(T));
+			ObjectEvents.Instance.Awake(disposer);
+			return disposer;
+		}
+
+		public static T Create<T, A>(A a) where T : Disposer
+		{
+			T disposer = (T)Activator.CreateInstance(typeof(T));
+			ObjectEvents.Instance.Awake(disposer, a);
+			return disposer;
+		}
+
+		public static T Create<T, A, B>(A a, B b) where T : Disposer
+		{
+			T disposer = (T)Activator.CreateInstance(typeof(T));
+			ObjectEvents.Instance.Awake(disposer, a, b);
+			return disposer;
+		}
+
+		public static T Create<T, A, B, C>(A a, B b, C c) where T : Disposer
+		{
+			T disposer = (T)Activator.CreateInstance(typeof(T));
+			ObjectEvents.Instance.Awake(disposer, a, b, c);
+			return disposer;
+		}
+
+		public static T CreateWithId<T>(long id) where T : Disposer
+		{
+			T disposer = (T)Activator.CreateInstance(typeof(T));
+			disposer.Id = id;
+			ObjectEvents.Instance.Awake(disposer);
+			return disposer;
+		}
+
+		public static T CreateWithId<T, A>(long id, A a) where T : Disposer
+		{
+			T disposer = (T)Activator.CreateInstance(typeof(T));
+			disposer.Id = id;
+			ObjectEvents.Instance.Awake(disposer, a);
+			return disposer;
+		}
+
+		public static T CreateWithId<T, A, B>(long id, A a, B b) where T : Disposer
+		{
+			T disposer = (T)Activator.CreateInstance(typeof(T));
+			disposer.Id = id;
+			ObjectEvents.Instance.Awake(disposer, a, b);
+			return disposer;
+		}
+
+		public static T CreateWithId<T, A, B, C>(long id, A a, B b, C c) where T : Disposer
+		{
+			T disposer = (T)Activator.CreateInstance(typeof(T));
+			disposer.Id = id;
+			ObjectEvents.Instance.Awake(disposer, a, b, c);
+			return disposer;
+		}
+	}
+}

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

@@ -9,7 +9,7 @@ namespace Hotfix
 
 	public abstract class Object: IDisposable2
 	{
-		public long Id { get; protected set; }
+		public long Id { get; set; }
 
 		protected Object()
 		{

+ 232 - 21
Unity/Hotfix/Base/Object/ObjectEvents.cs

@@ -1,69 +1,242 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using Model;
 
 namespace Hotfix
 {
+	public interface IObjectEvent
+	{
+		Type Type();
+		void Set(object value);
+	}
+
+	public abstract class ObjectEvent<T> : IObjectEvent
+	{
+		private T value;
+
+		protected T Get()
+		{
+			return value;
+		}
+
+		public void Set(object v)
+		{
+			this.value = (T)v;
+		}
+
+		public Type Type()
+		{
+			return typeof(T);
+		}
+	}
+
 	public sealed class ObjectEvents
 	{
 		private static ObjectEvents instance;
 
-		private Queue<Disposer> loads = new Queue<Disposer>();
-		private Queue<Disposer> loads2 = new Queue<Disposer>();
+		public static ObjectEvents Instance
+		{
+			get
+			{
+				return instance ?? (instance = new ObjectEvents());
+			}
+		}
+		
+		private readonly Dictionary<Type, IObjectEvent> disposerEvents = new Dictionary<Type, IObjectEvent>();
 
 		private Queue<Disposer> updates = new Queue<Disposer>();
 		private Queue<Disposer> updates2 = new Queue<Disposer>();
 
+		private readonly Queue<Disposer> starts = new Queue<Disposer>();
+
+		private Queue<Disposer> loaders = new Queue<Disposer>();
+		private Queue<Disposer> loaders2 = new Queue<Disposer>();
+
 		private Queue<Disposer> lateUpdates = new Queue<Disposer>();
 		private Queue<Disposer> lateUpdates2 = new Queue<Disposer>();
 
-		private Queue<Disposer> frameUpdates = new Queue<Disposer>();
-		private Queue<Disposer> frameUpdates2 = new Queue<Disposer>();
+		public static void Close()
+		{
+			instance = null;
+		}
 
-		public static ObjectEvents Instance
+
+
+		public ObjectEvents()
 		{
-			get
+			Type[] types = DllHelper.GetHotfixTypes();
+			foreach (Type type in types)
 			{
-				return instance ?? (instance = new ObjectEvents());
+				object[] attrs = type.GetCustomAttributes(typeof(ObjectEventAttribute), false);
+
+				if (attrs.Length == 0)
+				{
+					continue;
+				}
+
+				object obj = Activator.CreateInstance(type);
+				IObjectEvent objectEvent = obj as IObjectEvent;
+				if (objectEvent == null)
+				{
+					Log.Error($"组件事件没有继承IObjectEvent: {type.Name}");
+					continue;
+				}
+				this.disposerEvents[objectEvent.Type()] = objectEvent;
 			}
 		}
 
 		public void Add(Disposer disposer)
 		{
-			if (disposer is ILoad)
+			IObjectEvent objectEvent;
+			if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectEvent))
+			{
+				return;
+			}
+
+			if (objectEvent is ILoad)
+			{
+				this.loaders.Enqueue(disposer);
+			}
+
+			if (objectEvent is IUpdate)
+			{
+				this.updates.Enqueue(disposer);
+			}
+
+			if (objectEvent is IStart)
+			{
+				this.starts.Enqueue(disposer);
+			}
+		}
+
+		public void Awake(Disposer disposer)
+		{
+			IObjectEvent objectEvent;
+			if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectEvent))
+			{
+				return;
+			}
+			IAwake iAwake = objectEvent as IAwake;
+			if (iAwake == null)
+			{
+				return;
+			}
+			objectEvent.Set(disposer);
+			iAwake.Awake();
+		}
+
+		public void Awake<P1>(Disposer disposer, P1 p1)
+		{
+			IObjectEvent objectEvent;
+			if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectEvent))
+			{
+				return;
+			}
+			IAwake<P1> iAwake = objectEvent as IAwake<P1>;
+			if (iAwake == null)
 			{
-				loads.Enqueue(disposer);
+				return;
 			}
+			objectEvent.Set(disposer);
+			iAwake.Awake(p1);
+		}
 
-			if (disposer is IUpdate)
+		public void Awake<P1, P2>(Disposer disposer, P1 p1, P2 p2)
+		{
+			IObjectEvent objectEvent;
+			if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectEvent))
+			{
+				return;
+			}
+			IAwake<P1, P2> iAwake = objectEvent as IAwake<P1, P2>;
+			if (iAwake == null)
 			{
-				updates.Enqueue(disposer);
+				return;
 			}
+			objectEvent.Set(disposer);
+			iAwake.Awake(p1, p2);
+		}
 
-			if (disposer is ILateUpdate)
+		public void Awake<P1, P2, P3>(Disposer disposer, P1 p1, P2 p2, P3 p3)
+		{
+			IObjectEvent objectEvent;
+			if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectEvent))
+			{
+				return;
+			}
+			IAwake<P1, P2, P3> iAwake = objectEvent as IAwake<P1, P2, P3>;
+			if (iAwake == null)
 			{
-				lateUpdates.Enqueue(disposer);
+				return;
 			}
+			objectEvent.Set(disposer);
+			iAwake.Awake(p1, p2, p3);
 		}
 
 		public void Load()
 		{
-			while (this.loads.Count > 0)
+			while (this.loaders.Count > 0)
 			{
-				Disposer disposer = this.loads.Dequeue();
+				Disposer disposer = this.loaders.Dequeue();
 				if (disposer.Id == 0)
 				{
 					continue;
 				}
 
-				this.loads2.Enqueue(disposer);
+				IObjectEvent objectEvent;
+				if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectEvent))
+				{
+					continue;
+				}
 
-				((ILoad)disposer).Load();
+				this.loaders2.Enqueue(disposer);
+
+				ILoad iLoad = objectEvent as ILoad;
+				if (iLoad == null)
+				{
+					continue;
+				}
+				objectEvent.Set(disposer);
+				try
+				{
+					iLoad.Load();
+				}
+				catch (Exception e)
+				{
+					Log.Error(e.ToString());
+				}
 			}
 
-			ObjectHelper.Swap(ref this.loads, ref this.loads2);
+			ObjectHelper.Swap(ref this.loaders, ref this.loaders2);
+		}
+
+		private void Start()
+		{
+			while (this.starts.Count > 0)
+			{
+				Disposer disposer = this.starts.Dequeue();
+
+				IObjectEvent objectEvent;
+				if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectEvent))
+				{
+					continue;
+				}
+				IStart iStart = objectEvent as IStart;
+				if (iStart == null)
+				{
+					continue;
+				}
+				objectEvent.Set(disposer);
+				iStart.Start();
+			}
 		}
 
 		public void Update()
 		{
+			this.Start();
+
 			while (this.updates.Count > 0)
 			{
 				Disposer disposer = this.updates.Dequeue();
@@ -72,9 +245,28 @@ namespace Hotfix
 					continue;
 				}
 
+				IObjectEvent objectEvent;
+				if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectEvent))
+				{
+					continue;
+				}
+
 				this.updates2.Enqueue(disposer);
 
-				((IUpdate)disposer).Update();
+				IUpdate iUpdate = objectEvent as IUpdate;
+				if (iUpdate == null)
+				{
+					continue;
+				}
+				objectEvent.Set(disposer);
+				try
+				{
+					iUpdate.Update();
+				}
+				catch (Exception e)
+				{
+					Log.Error(e.ToString());
+				}
 			}
 
 			ObjectHelper.Swap(ref this.updates, ref this.updates2);
@@ -90,9 +282,28 @@ namespace Hotfix
 					continue;
 				}
 
+				IObjectEvent objectEvent;
+				if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectEvent))
+				{
+					continue;
+				}
+
 				this.lateUpdates2.Enqueue(disposer);
 
-				((ILateUpdate)disposer).LateUpdate();
+				ILateUpdate iLateUpdate = objectEvent as ILateUpdate;
+				if (iLateUpdate == null)
+				{
+					continue;
+				}
+				objectEvent.Set(disposer);
+				try
+				{
+					iLateUpdate.LateUpdate();
+				}
+				catch (Exception e)
+				{
+					Log.Error(e.ToString());
+				}
 			}
 
 			ObjectHelper.Swap(ref this.lateUpdates, ref this.lateUpdates2);

+ 15 - 2
Unity/Hotfix/Component/EventComponent.cs

@@ -4,8 +4,21 @@ using Model;
 
 namespace Hotfix
 {
-	[ObjectEvent((int)EntityEventId.EventComponent)]
-	public class EventComponent : Component, IAwake
+	[ObjectEvent]
+	public class EventComponentEvent : ObjectEvent<EventComponent>, IAwake, ILoad
+	{
+		public void Awake()
+		{
+			this.Get().Awake();
+		}
+
+		public void Load()
+		{
+			this.Get().Load();
+		}
+	}
+	
+	public class EventComponent : Component
 	{
 		private Dictionary<EventIdType, List<object>> allEvents;
 

+ 10 - 2
Unity/Hotfix/Component/GameObjectComponent.cs

@@ -3,12 +3,20 @@ using UnityEngine;
 
 namespace Hotfix
 {
-	[ObjectEvent((int)EntityEventId.GameObjectComponent)]
+	[ObjectEvent]
+	public class GameObjectComponentEvent : ObjectEvent<GameObjectComponent>, IAwake<GameObject>
+	{
+		public void Awake(GameObject gameObject)
+		{
+			this.Get().Awake(gameObject);
+		}
+	}
+	
 	public class GameObjectComponent: Component
 	{
 		public GameObject GameObject { get; private set; }
 
-		private void Awake(GameObject gameObject)
+		public void Awake(GameObject gameObject)
 		{
 			this.GameObject = gameObject;
 		}

+ 14 - 1
Unity/Hotfix/Component/MessageDispatherComponent.cs

@@ -4,10 +4,23 @@ using Model;
 
 namespace Hotfix
 {
+	[ObjectEvent]
+	public class MessageDispatherComponentEvent : ObjectEvent<MessageDispatherComponent>, IAwake, ILoad
+	{
+		public void Awake()
+		{
+			this.Get().Awake();
+		}
+
+		public void Load()
+		{
+			this.Get().Load();
+		}
+	}
+
 	/// <summary>
 	/// 消息分发组件
 	/// </summary>
-	[ObjectEvent((int)EntityEventId.MessageDispatherComponent)]
 	public class MessageDispatherComponent: Component, IAwake, ILoad
 	{
 		private Dictionary<ushort, List<IMHandler>> handlers;

+ 0 - 18
Unity/Hotfix/Component/RobotComponent.cs

@@ -1,18 +0,0 @@
-using Model;
-
-namespace Hotfix
-{
-	[ObjectEvent((int)EntityEventId.RobotComponent)]
-	public class RobotComponent: Component
-	{
-		public override void Dispose()
-		{
-			if (this.Id == 0)
-			{
-				return;
-			}
-
-			base.Dispose();
-		}
-	}
-}

+ 10 - 2
Unity/Hotfix/Component/TimerComponent.cs

@@ -12,8 +12,16 @@ namespace Hotfix
 		public TaskCompletionSource<bool> tcs;
 	}
 
-	[ObjectEvent((int)EntityEventId.TimerComponent)]
-	public class TimerComponent: Component, IUpdate
+	[ObjectEvent]
+	public class TimerComponentEvent : ObjectEvent<TimerComponent>, IUpdate
+	{
+		public void Update()
+		{
+			this.Get().Update();
+		}
+	}
+	
+	public class TimerComponent: Component
 	{
 		private readonly Dictionary<long, Timer> timers = new Dictionary<long, Timer>();
 

+ 15 - 3
Unity/Hotfix/Component/UIComponent.cs

@@ -6,11 +6,24 @@ using UnityEngine;
 
 namespace Hotfix
 {
+	[ObjectEvent]
+	public class UIComponentEvent : ObjectEvent<UIComponent>, IAwake, ILoad
+	{
+		public void Awake()
+		{
+			this.Get().Awake();
+		}
+
+		public void Load()
+		{
+			this.Get().Load();
+		}
+	}
+
 	/// <summary>
 	/// 管理所有UI
 	/// </summary>
-	[ObjectEvent((int)EntityEventId.UIComponent)]
-	public class UIComponent: Component, IAwake, ILoad
+	public class UIComponent: Component
 	{
 		private GameObject Root;
 		private Dictionary<UIType, IUIFactory> UiTypes;
@@ -70,7 +83,6 @@ namespace Hotfix
 					Log.Error($"{o.GetType().FullName} 没有继承 IUIFactory");
 					continue;
 				}
-
 				this.UiTypes.Add((UIType)attribute.Type, factory);
 			}
 		}

+ 2 - 4
Unity/Hotfix/Entity/Scene.cs

@@ -17,17 +17,15 @@
 	
 	public sealed class Scene: Entity
 	{
-		public Scene Parent { get; set; }
-
 		public Model.Scene ModelScene { get; set; } = new Model.Scene();
 
 		public string Name { get; set; }
 
-		public Scene(): base(EntityType.Scene)
+		public Scene()
 		{
 		}
 
-		public Scene(long id): base(id, EntityType.Scene)
+		public Scene(long id): base(id)
 		{
 		}
 

+ 1 - 1
Unity/Hotfix/Entity/UI.cs

@@ -37,7 +37,7 @@ namespace Hotfix
 			this.GameObject.transform.SetAsFirstSibling();
 		}
 
-		public UI(Scene scene, UIType uiType, UI parent, GameObject gameObject): base(EntityType.UI)
+		public UI(Scene scene, UIType uiType, UI parent, GameObject gameObject)
 		{
 			this.Scene = scene;
 			this.UIType = uiType;

+ 1 - 1
Unity/Hotfix/Entity/Unit.cs

@@ -20,7 +20,7 @@
 			base.Dispose();
 		}
 
-		public Unit(UnitType unitType): base(EntityType.UI)
+		public Unit(UnitType unitType)
 		{
 			this.UnitType = unitType;
 		}

+ 10 - 2
Unity/Hotfix/UI/UILogin/Component/UILoginComponent.cs

@@ -5,8 +5,16 @@ using UnityEngine.UI;
 
 namespace Hotfix
 {
-	[ObjectEvent((int)EntityEventId.UILoginComponent)]
-	public class UILoginComponent: Component, IAwake
+	[ObjectEvent]
+	public class UILoginComponentEvent : ObjectEvent<UILoginComponent>, IAwake
+	{
+		public void Awake()
+		{
+			this.Get().Awake();
+		}
+	}
+	
+	public class UILoginComponent: Component
 	{
 		private GameObject account;
 		private GameObject loginBtn;

+ 17 - 8
Unity/Hotfix/UI/UILogin/Factory/UILoginFactory.cs

@@ -1,4 +1,5 @@
-using Model;
+using System;
+using Model;
 using UnityEngine;
 
 namespace Hotfix
@@ -8,13 +9,21 @@ namespace Hotfix
     {
         public UI Create(Scene scene, UIType type, GameObject gameObject)
         {
-			GameObject bundleGameObject = scene.ModelScene.GetComponent<ResourcesComponent>().GetAsset<GameObject>("uilogin", "UILogin");
-			GameObject lobby = UnityEngine.Object.Instantiate(bundleGameObject);
-			lobby.layer = LayerMask.NameToLayer(LayerNames.UI);
-			UI ui = new UI(scene, type, null, lobby);
+	        try
+	        {
+				GameObject bundleGameObject = scene.ModelScene.GetComponent<ResourcesComponent>().GetAsset<GameObject>("uilogin", "UILogin");
+				GameObject lobby = UnityEngine.Object.Instantiate(bundleGameObject);
+				lobby.layer = LayerMask.NameToLayer(LayerNames.UI);
+				UI ui = new UI(scene, type, null, lobby);
 
-	        ui.AddComponent<UILoginComponent>();
-			return ui;
-        }
+				ui.AddComponent<UILoginComponent>();
+				return ui;
+	        }
+	        catch (Exception e)
+	        {
+				Log.Error(e.ToStr());
+		        return null;
+	        }
+		}
     }
 }

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

@@ -62,8 +62,11 @@
     <Compile Include="Base\Message\AMHandler.cs" />
     <Compile Include="Base\Message\IMHandler.cs" />
     <Compile Include="Base\Object\Component.cs" />
+    <Compile Include="Base\Object\ComponentDB.cs" />
+    <Compile Include="Base\Object\ComponentFactory.cs" />
     <Compile Include="Base\Object\Disposer.cs" />
     <Compile Include="Base\Object\Entity.cs" />
+    <Compile Include="Base\Object\EntityFactory.cs" />
     <Compile Include="Base\Object\EntityType.cs" />
     <Compile Include="Base\Object\IAwake.cs" />
     <Compile Include="Base\Object\ILateUpdate.cs" />
@@ -76,7 +79,6 @@
     <Compile Include="Component\GameObjectComponent.cs" />
     <Compile Include="Component\KVComponent.cs" />
     <Compile Include="Component\MessageDispatherComponent.cs" />
-    <Compile Include="Component\RobotComponent.cs" />
     <Compile Include="Component\TimeComponent.cs" />
     <Compile Include="Component\TimerComponent.cs" />
     <Compile Include="Component\UIComponent.cs" />

+ 3 - 1
Unity/Unity.csproj

@@ -414,10 +414,10 @@
     <Compile Include="Assets\Scripts\Base\Network\UNet\USocketManager.cs" />
     <Compile Include="Assets\Scripts\Base\Object\AssemblyManager.cs" />
     <Compile Include="Assets\Scripts\Base\Object\Component.cs" />
+    <Compile Include="Assets\Scripts\Base\Object\ComponentDB.cs" />
     <Compile Include="Assets\Scripts\Base\Object\Disposer.cs" />
     <Compile Include="Assets\Scripts\Base\Object\Entity.cs" />
     <Compile Include="Assets\Scripts\Base\Object\EntityEventAttribute.cs" />
-    <Compile Include="Assets\Scripts\Base\Object\EntityEventId.cs" />
     <Compile Include="Assets\Scripts\Base\Object\EntityType.cs" />
     <Compile Include="Assets\Scripts\Base\Object\IAwake.cs" />
     <Compile Include="Assets\Scripts\Base\Object\ILateUpdate.cs" />
@@ -427,6 +427,8 @@
     <Compile Include="Assets\Scripts\Base\Object\Object.cs" />
     <Compile Include="Assets\Scripts\Base\Object\ObjectEventAttribute.cs" />
     <Compile Include="Assets\Scripts\Base\Object\ObjectEvents.cs" />
+    <Compile Include="Assets\Scripts\Base\Object\ComponentFactory.cs" />
+    <Compile Include="Assets\Scripts\Base\Object\EntityFactory.cs" />
     <Compile Include="Assets\Scripts\Base\QueueDictionary.cs" />
     <Compile Include="Assets\Scripts\Base\TryLocker.cs" />
     <Compile Include="Assets\Scripts\Base\UI\LayerNames.cs" />

+ 1 - 6
Unity/Unity.sln

@@ -1,8 +1,6 @@
 
 Microsoft Visual Studio Solution File, Format Version 12.00
-# Visual Studio 15
-VisualStudioVersion = 15.0.26730.10
-MinimumVisualStudioVersion = 10.0.40219.1
+# Visual Studio 2017
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Plugins", "Unity.Plugins.csproj", "{D1FDB199-0FB7-099D-3771-C6A942E4E326}"
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity", "Unity.csproj", "{CF118143-7E37-744F-BE45-3F55345FEC40}"
@@ -37,7 +35,4 @@ Global
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
 	EndGlobalSection
-	GlobalSection(ExtensibilityGlobals) = postSolution
-		SolutionGuid = {0AB8BABF-C9E8-4CFB-B2C5-0A6CD8350FC4}
-	EndGlobalSection
 EndGlobal