Browse Source

Component增加了一个IComponentSerialize接口,该接口增加的原因是,对象在反序列化之后
(比如对象迁移,从数据库中读取出来)需要做一些操作,比如Add到EventSystem从而注册Update等事件,
但是,如果在EndInit方法中注册到EventSystem的话,对象传到DBServer就也会注册,因此这种需求不能
自动化,必须让开发者手动调用。

tanghai 7 years ago
parent
commit
bed1e8cb4b

+ 7 - 0
Client-Server.sln.DotSettings.user

@@ -11,10 +11,17 @@
 	<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=CF118143_002D7E37_002D744F_002DBE45_002D3F55345FEC40_002Fd_003AAssets_002Fd_003AScripts_002Fd_003ABase_002Fd_003AObject_002Ff_003AComponent_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
 	<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=CF118143_002D7E37_002D744F_002DBE45_002D3F55345FEC40_002Fd_003AAssets_002Fd_003AScripts_002Fd_003ABase_002Fd_003AObject_002Ff_003AComponentWithId_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
 	<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=CF118143_002D7E37_002D744F_002DBE45_002D3F55345FEC40_002Fd_003AAssets_002Fd_003AScripts_002Fd_003ABase_002Fd_003AObject_002Ff_003AEntity_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
+	<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=CF118143_002D7E37_002D744F_002DBE45_002D3F55345FEC40_002Fd_003AAssets_002Fd_003AScripts_002Fd_003ABase_002Fd_003AObject_002Ff_003AEventSystem_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
+	<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=CF118143_002D7E37_002D744F_002DBE45_002D3F55345FEC40_002Fd_003AAssets_002Fd_003AScripts_002Fd_003ABase_002Fd_003AObject_002Ff_003AObject_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
+	<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=CF118143_002D7E37_002D744F_002DBE45_002D3F55345FEC40_002Fd_003AAssets_002Fd_003AScripts_002Fd_003ABase_002Fd_003AObject_002Ff_003AObjectPool_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
 	<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=CF118143_002D7E37_002D744F_002DBE45_002D3F55345FEC40_002Fd_003AAssets_002Fd_003AScripts_002Fd_003AComponent_002Fd_003AConfig_002Ff_003AClientConfig_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
 	<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=CF118143_002D7E37_002D744F_002DBE45_002D3F55345FEC40_002Fd_003AAssets_002Fd_003AScripts_002Fd_003AComponent_002Fd_003AConfig_002Ff_003AInnerConfig_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
+	<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=CF118143_002D7E37_002D744F_002DBE45_002D3F55345FEC40_002Fd_003AAssets_002Fd_003AScripts_002Fd_003AModule_002Fd_003AMessage_002Fd_003ANetwork_002Fd_003ATCP_002Ff_003ATChannel_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
+	<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=CF118143_002D7E37_002D744F_002DBE45_002D3F55345FEC40_002Fd_003AAssets_002Fd_003AScripts_002Fd_003AModule_002Fd_003AMessage_002Fd_003ANetwork_002Fd_003ATCP_002Ff_003ATService_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
+	<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=CF118143_002D7E37_002D744F_002DBE45_002D3F55345FEC40_002Fd_003AAssets_002Fd_003AScripts_002Fd_003AModule_002Fd_003AMessage_002Fd_003ANetwork_002Ff_003AAChannel_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
 	<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=EE38E3D2_002D3D2A_002D4061_002DBD50_002DAE8568126C7D_002Fd_003ABase_002Fd_003AObject_002Ff_003AEntityType_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
 	<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=EE38E3D2_002D3D2A_002D4061_002DBD50_002DAE8568126C7D_002Fd_003AEntity_002Fd_003AConfig_002Ff_003AStartConfig_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
+	<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=EE38E3D2_002D3D2A_002D4061_002DBD50_002DAE8568126C7D_002Fd_003AModule_002Fd_003AMessage_002Ff_003AAMRpcHandler_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
 	<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=EE38E3D2_002D3D2A_002D4061_002DBD50_002DAE8568126C7D_002Fd_003AModule_002Fd_003AMessage_002Ff_003ANetInnerComponent_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
 	<s:String x:Key="/Default/CodeInspection/Highlighting/AnalysisEnabled/@EntryValue">VISIBLE_FILES</s:String>
 	<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=EnforceForeachStatementBraces/@EntryIndexedValue">HINT</s:String>

+ 0 - 12
Server/Model/Base/Object/EntityType.cs

@@ -1,12 +0,0 @@
-namespace ETModel
-{
-	public enum EntityType
-	{
-		None,
-		Scene,
-		Session,
-		UI,
-		Config,
-		Unit
-	}
-}

+ 1 - 8
Server/Model/Module/Config/AConfigComponent.cs

@@ -1,15 +1,8 @@
-using MongoDB.Bson.Serialization.Attributes;
-
-namespace ETModel
+namespace ETModel
 {
 	/// <summary>
 	/// 每个Config的基类
 	/// </summary>
-	[BsonKnownTypes(typeof (ClientConfig))]
-	[BsonKnownTypes(typeof (InnerConfig))]
-	[BsonKnownTypes(typeof (OuterConfig))]
-	[BsonKnownTypes(typeof (HttpConfig))]
-	[BsonKnownTypes(typeof (DBConfig))]
 	public abstract class AConfigComponent: Component, ISerializeToEntity
 	{
 	}

+ 1 - 0
Server/Model/Server.Model.csproj

@@ -42,6 +42,7 @@
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\EventSystem.cs" Link="Base\Object\EventSystem.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\IAwakeSystem.cs" Link="Base\Object\IAwakeSystem.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\IChangeSystem.cs" Link="Base\Object\IChangeSystem.cs" />
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\IComponentSerialize.cs" Link="Base\Object\IComponentSerialize.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\IDestroySystem.cs" Link="Base\Object\IDestroySystem.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\ILateUpdateSystem.cs" Link="Base\Object\ILateUpdateSystem.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\ILoadSystem.cs" Link="Base\Object\ILoadSystem.cs" />

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

@@ -4,7 +4,7 @@ using MongoDB.Bson.Serialization.Attributes;
 namespace ETModel
 {
 	[BsonIgnoreExtraElements]
-	public abstract class Component : Object, IDisposable
+	public abstract class Component : Object, IDisposable, IComponentSerialize
 	{
 		[BsonIgnore]
 		public long InstanceId { get; protected set; }
@@ -87,5 +87,13 @@ namespace ETModel
 			// 触发Destroy事件
 			Game.EventSystem.Destroy(this);
 		}
+
+		public virtual void BeginSerialize()
+		{
+		}
+
+		public virtual void EndDeSerialize()
+		{
+		}
 	}
 }

+ 20 - 0
Unity/Assets/Scripts/Base/Object/Entity.cs

@@ -233,5 +233,25 @@ namespace ETModel
 				Log.Error(e);
 			}
 		}
+
+		public override void BeginSerialize()
+		{
+			base.BeginSerialize();
+
+			foreach (Component component in this.components)
+			{
+				component.BeginSerialize();
+			}
+		}
+
+		public override void EndDeSerialize()
+		{
+			base.EndDeSerialize();
+			
+			foreach (Component component in this.components)
+			{
+				component.EndDeSerialize();
+			}
+		}
 	}
 }

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

@@ -1,12 +0,0 @@
-namespace ETModel
-{
-	public enum EntityType
-	{
-		None,
-		Scene,
-		Session,
-		UI,
-		Config,
-		Unit
-	}
-}

+ 13 - 0
Unity/Assets/Scripts/Base/Object/IComponentSerialize.cs

@@ -0,0 +1,13 @@
+namespace ETModel
+{
+	// 在序列化前或者反序列化之后需要做一些操作,可以实现该接口,该接口的方法需要手动调用
+	// 相比ISupportInitialize接口,BeginSerialize在BeginInit之前调用,EndDeSerialize在EndInit之后调用
+	// 并且需要手动调用,可以在反序列化之后,在次方法中将注册组件到EventSystem之中等等
+	public interface IComponentSerialize
+	{
+		// 序列化之前调用
+		void BeginSerialize();
+		// 反序列化之后调用
+		void EndDeSerialize();
+	}
+}

+ 3 - 3
Unity/Assets/Scripts/Base/Object/EntityType.cs.meta → Unity/Assets/Scripts/Base/Object/IComponentSerialize.cs.meta

@@ -1,7 +1,7 @@
 fileFormatVersion: 2
-guid: a8f6aec364235394fa87b26ff5b476cf
-timeCreated: 1498116853
-licenseType: Free
+guid: 1d4dd4af4f735fc43a9893df26ab0bce
+timeCreated: 1526888904
+licenseType: Pro
 MonoImporter:
   serializedVersion: 2
   defaultReferences: []

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

@@ -4,7 +4,7 @@ using MongoDB.Bson.Serialization.Attributes;
 namespace ETHotfix
 {
 	[BsonIgnoreExtraElements]
-	public abstract class Component : Object, IDisposable
+	public abstract class Component : Object, IDisposable, IComponentSerialize
 	{
 		[BsonIgnore]
 		public long InstanceId { get; protected set; }
@@ -87,5 +87,13 @@ namespace ETHotfix
 			// 触发Destroy事件
 			Game.EventSystem.Destroy(this);
 		}
+
+		public virtual void BeginSerialize()
+		{
+		}
+
+		public virtual void EndDeSerialize()
+		{
+		}
 	}
 }

+ 64 - 24
Unity/Hotfix/Base/Object/Entity.cs

@@ -1,13 +1,12 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
-using ETModel;
 using MongoDB.Bson.Serialization.Attributes;
 
 namespace ETHotfix
 {
 	[BsonIgnoreExtraElements]
-	public partial class Entity : ComponentWithId
+	public class Entity : ComponentWithId
 	{
 		[BsonElement]
 		[BsonIgnoreIfNull]
@@ -16,13 +15,13 @@ namespace ETHotfix
 		[BsonIgnore]
 		private Dictionary<Type, Component> componentDict;
 
-		protected Entity()
+		public Entity()
 		{
 			this.components = new HashSet<Component>();
 			this.componentDict = new Dictionary<Type, Component>();
 		}
 
-		protected Entity(long id) : base(id)
+		protected Entity(long id): base(id)
 		{
 			this.components = new HashSet<Component>();
 			this.componentDict = new Dictionary<Type, Component>();
@@ -52,12 +51,28 @@ namespace ETHotfix
 			this.components.Clear();
 			this.componentDict.Clear();
 		}
+		
+		public Component AddComponent(Component component)
+		{
+			Type type = component.GetType();
+			if (this.componentDict.ContainsKey(type))
+			{
+				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {type.Name}");
+			}
+
+			if (component is ISerializeToEntity)
+			{
+				this.components.Add(component);
+			}
+			this.componentDict.Add(type, component);
+			return component;
+		}
 
 		public Component AddComponent(Type type)
 		{
 			if (this.componentDict.ContainsKey(type))
 			{
-				throw new Exception($"AddComponent, component already exist, component: {type.Name}");
+				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {type.Name}");
 			}
 
 			Component component = ComponentFactory.CreateWithParent(type, this);
@@ -66,15 +81,16 @@ namespace ETHotfix
 			{
 				this.components.Add(component);
 			}
-			this.componentDict.Add(component.GetType(), component);
+			this.componentDict.Add(type, component);
 			return component;
 		}
 
 		public K AddComponent<K>() where K : Component, new()
 		{
-			if (this.componentDict.ContainsKey(typeof(K)))
+			Type type = typeof (K);
+			if (this.componentDict.ContainsKey(type))
 			{
-				throw new Exception($"AddComponent, component already exist, component: {typeof(K).Name}");
+				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
 			K component = ComponentFactory.CreateWithParent<K>(this);
@@ -83,71 +99,75 @@ namespace ETHotfix
 			{
 				this.components.Add(component);
 			}
-			this.componentDict.Add(component.GetType(), component);
+			this.componentDict.Add(type, component);
 			return component;
 		}
 
 		public K AddComponent<K, P1>(P1 p1) where K : Component, new()
 		{
-			if (this.componentDict.ContainsKey(typeof(K)))
+			Type type = typeof (K);
+			if (this.componentDict.ContainsKey(type))
 			{
-				throw new Exception($"AddComponent, component already exist, component: {typeof(K).Name}");
+				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
 			K component = ComponentFactory.CreateWithParent<K, P1>(this, p1);
-
+			
 			if (component is ISerializeToEntity)
 			{
 				this.components.Add(component);
 			}
-			this.componentDict.Add(component.GetType(), component);
+			this.componentDict.Add(type, component);
 			return component;
 		}
 
 		public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
 		{
-			if (this.componentDict.ContainsKey(typeof(K)))
+			Type type = typeof (K);
+			if (this.componentDict.ContainsKey(type))
 			{
-				throw new Exception($"AddComponent, component already exist, component: {typeof(K).Name}");
+				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
 			K component = ComponentFactory.CreateWithParent<K, P1, P2>(this, p1, p2);
-
+			
 			if (component is ISerializeToEntity)
 			{
 				this.components.Add(component);
 			}
-			this.componentDict.Add(component.GetType(), component);
+			this.componentDict.Add(type, component);
 			return component;
 		}
 
 		public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
 		{
-			if (this.componentDict.ContainsKey(typeof(K)))
+			Type type = typeof (K);
+			if (this.componentDict.ContainsKey(type))
 			{
-				throw new Exception($"AddComponent, component already exist, component: {typeof(K).Name}");
+				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
 			K component = ComponentFactory.CreateWithParent<K, P1, P2, P3>(this, p1, p2, p3);
-
+			
 			if (component is ISerializeToEntity)
 			{
 				this.components.Add(component);
 			}
-			this.componentDict.Add(component.GetType(), component);
+			this.componentDict.Add(type, component);
 			return component;
 		}
 
 		public void RemoveComponent<K>() where K : Component
 		{
+			Type type = typeof (K);
 			Component component;
-			if (!this.componentDict.TryGetValue(typeof(K), out component))
+			if (!this.componentDict.TryGetValue(type, out component))
 			{
 				return;
 			}
 
 			this.components.Remove(component);
-			this.componentDict.Remove(typeof(K));
+			this.componentDict.Remove(type);
 
 			component.Dispose();
 		}
@@ -196,7 +216,7 @@ namespace ETHotfix
 			try
 			{
 				base.EndInit();
-
+				
 				this.componentDict.Clear();
 
 				if (this.components != null)
@@ -213,5 +233,25 @@ namespace ETHotfix
 				Log.Error(e);
 			}
 		}
+
+		public override void BeginSerialize()
+		{
+			base.BeginSerialize();
+
+			foreach (Component component in this.components)
+			{
+				component.BeginSerialize();
+			}
+		}
+
+		public override void EndDeSerialize()
+		{
+			base.EndDeSerialize();
+			
+			foreach (Component component in this.components)
+			{
+				component.EndDeSerialize();
+			}
+		}
 	}
 }

+ 13 - 0
Unity/Hotfix/Base/Object/IComponentSerialize.cs

@@ -0,0 +1,13 @@
+namespace ETHotfix
+{
+	// 在序列化前或者反序列化之后需要做一些操作,可以实现该接口,该接口的方法需要手动调用
+	// 相比ISupportInitialize接口,BeginSerialize在BeginInit之前调用,EndDeSerialize在EndInit之后调用
+	// 并且需要手动调用,可以在反序列化之后,在次方法中将注册组件到EventSystem之中等等
+	public interface IComponentSerialize
+	{
+		// 序列化之前调用
+		void BeginSerialize();
+		// 反序列化之后调用
+		void EndDeSerialize();
+	}
+}

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

@@ -55,6 +55,7 @@
     <Compile Include="Base\Helper\Log.cs" />
     <Compile Include="Base\Object\ComponentWithId.cs" />
     <Compile Include="Base\Object\IChangeSystem.cs" />
+    <Compile Include="Base\Object\IComponentSerialize.cs" />
     <Compile Include="Base\Object\IDestroySystem.cs" />
     <Compile Include="Base\Object\IStartSystem.cs" />
     <Compile Include="Module\FrameSync\OperaComponent.cs" />

+ 8 - 5
Unity/Unity.csproj

@@ -12,13 +12,16 @@
     <ProjectTypeGuids>{E097FAD1-6243-4DAD-9C02-E9B9EFC3FFC1};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
     <TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier>
     <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
-    <TargetFrameworkProfile></TargetFrameworkProfile>
-    <CompilerResponseFile></CompilerResponseFile>
+    <TargetFrameworkProfile>
+    </TargetFrameworkProfile>
+    <CompilerResponseFile>
+    </CompilerResponseFile>
     <UnityProjectGenerator>VSTU</UnityProjectGenerator>
     <UnityProjectType>Game:1</UnityProjectType>
     <UnityBuildTarget>StandaloneWindows:5</UnityBuildTarget>
     <UnityVersion>2017.1.3p2</UnityVersion>
-    <RootNamespace></RootNamespace>
+    <RootNamespace>
+    </RootNamespace>
     <LangVersion>6</LangVersion>
   </PropertyGroup>
   <PropertyGroup>
@@ -151,11 +154,11 @@
     <Compile Include="Assets\Scripts\Base\Object\ComponentWithId.cs" />
     <Compile Include="Assets\Scripts\Base\Object\Entity.cs" />
     <Compile Include="Assets\Scripts\Base\Object\EntityEventAttribute.cs" />
-    <Compile Include="Assets\Scripts\Base\Object\EntityType.cs" />
     <Compile Include="Assets\Scripts\Base\Object\EventProxy.cs" />
     <Compile Include="Assets\Scripts\Base\Object\EventSystem.cs" />
     <Compile Include="Assets\Scripts\Base\Object\IAwakeSystem.cs" />
     <Compile Include="Assets\Scripts\Base\Object\IChangeSystem.cs" />
+    <Compile Include="Assets\Scripts\Base\Object\IComponentSerialize.cs" />
     <Compile Include="Assets\Scripts\Base\Object\IDestroySystem.cs" />
     <Compile Include="Assets\Scripts\Base\Object\ILateUpdateSystem.cs" />
     <Compile Include="Assets\Scripts\Base\Object\ILoadSystem.cs" />
@@ -776,4 +779,4 @@
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Target Name="GenerateTargetFrameworkMonikerAttribute" />
-</Project>
+</Project>