Эх сурвалжийг харах

1.去掉ComopnentDB跟EntityDB类,BsonKnowType标记放到ComponentAttribute跟EntityAttribute类上面
2.增加ISerializeToEntity接口,Component子类继承了该接口就会跟随Entity序列化,否则Entity序列化不会序列化这个Component
3.以上两个改动主要考虑到需要提供一种机制,Component不随着Entity序列化,但是Component本身又要进行序列化,比如背包,背包需要单独保存到一个数据库表中,所以不能跟随Player对象序列化,但是背包组件本身需要序列化保存到数据库中

tanghai 8 жил өмнө
parent
commit
3d2693bb17

+ 1 - 1
Server/Model/Base/Config/AConfigComponent.cs

@@ -10,7 +10,7 @@ namespace Model
 	[BsonKnownTypes(typeof (OuterConfig))]
 	[BsonKnownTypes(typeof (HttpConfig))]
 	[BsonKnownTypes(typeof (DBConfig))]
-	public abstract class AConfigComponent: ComponentDB
+	public abstract class AConfigComponent: Component, ISerializeToEntity
 	{
 	}
 }

+ 1 - 1
Server/Model/Base/Object/ComponentDB.cs → Server/Model/Base/Object/ComponentAttribute.cs

@@ -5,7 +5,7 @@ namespace Model
 	[BsonKnownTypes(typeof(AConfigComponent))]
 	[BsonKnownTypes(typeof(UnitGateComponent))]
 	[BsonKnownTypes(typeof(NumericComponent))]
-	public abstract class ComponentDB : Component
+	public partial class Component
 	{
 	}
 }

+ 8 - 0
Server/Model/Base/Object/EntityAttribute.cs

@@ -0,0 +1,8 @@
+using MongoDB.Bson.Serialization.Attributes;
+
+namespace Model
+{
+	public partial class Entity
+	{
+	}
+}

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

@@ -13,7 +13,7 @@ namespace Model
 		}
 	}
 
-	public class NumericComponent : ComponentDB
+	public class NumericComponent : Component, ISerializeToEntity
 	{
 		[BsonDictionaryOptions(DictionaryRepresentation.ArrayOfArrays)]
 		public Dictionary<NumericType, int> NumericDic;

+ 1 - 1
Server/Model/Component/Unit/UnitGateComponent.cs

@@ -9,7 +9,7 @@
 		}
 	}
 
-	public class UnitGateComponent : ComponentDB
+	public class UnitGateComponent : Component, ISerializeToEntity
 	{
 		public long GateSessionId;
 

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

@@ -34,6 +34,7 @@
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\IAwake.cs" Link="Base\Object\IAwake.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\ILateUpdate.cs" Link="Base\Object\ILateUpdate.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\ILoad.cs" Link="Base\Object\ILoad.cs" />
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\ISerializeToEntity.cs" Link="Base\Object\ISerializeToEntity.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\IStart.cs" Link="Base\Object\IStart.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\IUpdate.cs" Link="Base\Object\IUpdate.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\Object.cs" Link="Base\Object\Object.cs" />

+ 1 - 1
Unity/Assets/Scripts/Base/Config/AConfigComponent.cs

@@ -11,7 +11,7 @@ namespace Model
 	[BsonKnownTypes(typeof(HttpConfig))]
 	[BsonKnownTypes(typeof(DBConfig))]
 	[BsonKnownTypes(typeof(RunServerConfig))]
-	public abstract class AConfigComponent: ComponentDB
+	public abstract class AConfigComponent: Component, ISerializeToEntity
 	{
 	}
 }

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

@@ -3,8 +3,7 @@
 namespace Model
 {
 	[BsonIgnoreExtraElements]
-	[BsonKnownTypes(typeof(AConfigComponent))]
-	public abstract class Component: Disposer
+	public abstract partial class Component: Disposer
 	{
 		[BsonIgnore]
 		public Entity Entity { get; set; }

+ 9 - 0
Unity/Assets/Scripts/Base/Object/ComponentAttribute.cs

@@ -0,0 +1,9 @@
+using MongoDB.Bson.Serialization.Attributes;
+
+namespace Model
+{
+	[BsonKnownTypes(typeof(AConfigComponent))]
+	public partial class Component
+	{
+	}
+}

+ 2 - 2
Unity/Assets/Scripts/Base/Object/EntityDB.cs.meta → Unity/Assets/Scripts/Base/Object/ComponentAttribute.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
-guid: 9af83b3a01cc1ed4ebc656d3dde3deeb
-timeCreated: 1506651030
+guid: 7f862da8d48947748af29b67dcda2dd2
+timeCreated: 1512358818
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

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

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

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

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

+ 24 - 19
Unity/Assets/Scripts/Base/Object/Entity.cs

@@ -1,14 +1,12 @@
 using System;
 using System.Collections.Generic;
-using System.ComponentModel;
 using System.Linq;
 using MongoDB.Bson.Serialization.Attributes;
 
 namespace Model
 {
 	[BsonIgnoreExtraElements]
-	[BsonKnownTypes(typeof(EntityDB))]
-	public class Entity : Disposer, ISupportInitialize
+	public partial class Entity : Disposer
 	{
 		[BsonIgnore]
 		public Entity Parent { get; set; }
@@ -66,7 +64,7 @@ namespace Model
 				this.components = new HashSet<Component>();
 			}
 
-			if (component is ComponentDB)
+			if (component is ISerializeToEntity)
 			{
 				this.components.Add(component);
 			}
@@ -88,7 +86,7 @@ namespace Model
 				this.components = new HashSet<Component>();
 			}
 
-			if (component is ComponentDB)
+			if (component is ISerializeToEntity)
 			{
 				this.components.Add(component);
 			}
@@ -110,7 +108,7 @@ namespace Model
 				this.components = new HashSet<Component>();
 			}
 
-			if (component is ComponentDB)
+			if (component is ISerializeToEntity)
 			{
 				this.components.Add(component);
 			}
@@ -132,7 +130,7 @@ namespace Model
 				this.components = new HashSet<Component>();
 			}
 
-			if (component is ComponentDB)
+			if (component is ISerializeToEntity)
 			{
 				this.components.Add(component);
 			}
@@ -189,27 +187,34 @@ namespace Model
 			return this.componentDict.Values.ToArray();
 		}
 
-		public virtual void BeginInit()
+		public override void BeginInit()
 		{
 			this.components = new HashSet<Component>();
 			this.componentDict = new Dictionary<Type, Component>();
 		}
 
-		public virtual void EndInit()
+		public override void EndInit()
 		{
-			ObjectEvents.Instance.Add(this);
-
-			if (this.components != null && this.components.Count == 0)
-			{
-				this.components = null;
-			}
-			if (this.components != null)
+			try
 			{
-				foreach (Component component in this.components)
+				ObjectEvents.Instance.Add(this);
+				if (this.components != null && this.components.Count == 0)
 				{
-					component.Entity = this;
-					this.componentDict.Add(component.GetType(), component);
+					this.components = null;
 				}
+				this.componentDict.Clear();
+				if (this.components != null)
+				{
+					foreach (Component component in this.components)
+					{
+						component.Entity = this;
+						this.componentDict.Add(component.GetType(), component);
+					}
+				}
+			}
+			catch (Exception e)
+			{
+				Log.Error(e.ToString());
 			}
 		}
 	}

+ 8 - 0
Unity/Assets/Scripts/Base/Object/EntityAttribute.cs

@@ -0,0 +1,8 @@
+using MongoDB.Bson.Serialization.Attributes;
+
+namespace Model
+{
+	public partial class Entity
+	{
+	}
+}

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

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

+ 0 - 16
Unity/Assets/Scripts/Base/Object/EntityDB.cs

@@ -1,16 +0,0 @@
-using MongoDB.Bson.Serialization.Attributes;
-
-namespace Model
-{
-	[BsonIgnoreExtraElements]
-	public class EntityDB: Entity
-	{
-		protected EntityDB()
-		{
-		}
-
-		protected EntityDB(long id): base(id)
-		{
-		}
-	}
-}

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

@@ -0,0 +1,6 @@
+namespace Model
+{
+	public interface ISerializeToEntity
+	{
+	}
+}

+ 11 - 0
Unity/Assets/Scripts/Base/Object/ISerializeToEntity.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: aaf460608ababe44db0d6256d5af7c69
+timeCreated: 1504236405
+MonoImporter:
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

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

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

+ 4 - 4
Unity/Hotfix/Base/Object/Entity.cs

@@ -59,7 +59,7 @@ namespace Hotfix
 				this.components = new HashSet<Component>();
 			}
 
-			if (component is ComponentDB)
+			if (component is ISerializeToEntity)
 			{
 				this.components.Add(component);
 			}
@@ -81,7 +81,7 @@ namespace Hotfix
 				this.components = new HashSet<Component>();
 			}
 
-			if (component is ComponentDB)
+			if (component is ISerializeToEntity)
 			{
 				this.components.Add(component);
 			}
@@ -103,7 +103,7 @@ namespace Hotfix
 				this.components = new HashSet<Component>();
 			}
 
-			if (component is ComponentDB)
+			if (component is ISerializeToEntity)
 			{
 				this.components.Add(component);
 			}
@@ -125,7 +125,7 @@ namespace Hotfix
 				this.components = new HashSet<Component>();
 			}
 
-			if (component is ComponentDB)
+			if (component is ISerializeToEntity)
 			{
 				this.components.Add(component);
 			}

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

@@ -0,0 +1,6 @@
+namespace Hotfix
+{
+	public interface ISerializeToEntity
+	{
+	}
+}

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

@@ -64,7 +64,6 @@
     <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" />
@@ -73,6 +72,7 @@
     <Compile Include="Base\Object\IAwake.cs" />
     <Compile Include="Base\Object\ILateUpdate.cs" />
     <Compile Include="Base\Object\ILoad.cs" />
+    <Compile Include="Base\Object\ISerializeToEntity.cs" />
     <Compile Include="Base\Object\IUpdate.cs" />
     <Compile Include="Base\Object\Object.cs" />
     <Compile Include="Base\Object\ObjectEvents.cs" />

+ 5 - 4
Unity/Unity.csproj

@@ -228,12 +228,12 @@
     <Compile Include="Assets\Scripts\Base\MultiMap.cs" />
     <Compile Include="Assets\Scripts\Base\Network\AChannel.cs" />
     <Compile Include="Assets\Scripts\Base\Network\AService.cs" />
-    <Compile Include="Assets\Scripts\Base\Network\KNet\Kcp.cs" />
+    <Compile Include="Assets\Scripts\Base\Network\Circularbuffer.cs" />
     <Compile Include="Assets\Scripts\Base\Network\KNet\KChannel.cs" />
+    <Compile Include="Assets\Scripts\Base\Network\KNet\Kcp.cs" />
     <Compile Include="Assets\Scripts\Base\Network\KNet\KService.cs" />
     <Compile Include="Assets\Scripts\Base\Network\NetworkHelper.cs" />
     <Compile Include="Assets\Scripts\Base\Network\TNet\PacketParser.cs" />
-    <Compile Include="Assets\Scripts\Base\Network\Circularbuffer.cs" />
     <Compile Include="Assets\Scripts\Base\Network\TNet\TChannel.cs" />
     <Compile Include="Assets\Scripts\Base\Network\TNet\TService.cs" />
     <Compile Include="Assets\Scripts\Base\Network\UNet\Library.cs" />
@@ -247,17 +247,18 @@
     <Compile Include="Assets\Scripts\Base\Network\UNet\USocket.cs" />
     <Compile Include="Assets\Scripts\Base\Network\UNet\USocketManager.cs" />
     <Compile Include="Assets\Scripts\Base\Object\Component.cs" />
-    <Compile Include="Assets\Scripts\Base\Object\ComponentDB.cs" />
+    <Compile Include="Assets\Scripts\Base\Object\ComponentAttribute.cs" />
     <Compile Include="Assets\Scripts\Base\Object\ComponentFactory.cs" />
     <Compile Include="Assets\Scripts\Base\Object\Disposer.cs" />
     <Compile Include="Assets\Scripts\Base\Object\Entity.cs" />
-    <Compile Include="Assets\Scripts\Base\Object\EntityDB.cs" />
+    <Compile Include="Assets\Scripts\Base\Object\EntityAttribute.cs" />
     <Compile Include="Assets\Scripts\Base\Object\EntityEventAttribute.cs" />
     <Compile Include="Assets\Scripts\Base\Object\EntityFactory.cs" />
     <Compile Include="Assets\Scripts\Base\Object\EntityType.cs" />
     <Compile Include="Assets\Scripts\Base\Object\IAwake.cs" />
     <Compile Include="Assets\Scripts\Base\Object\ILateUpdate.cs" />
     <Compile Include="Assets\Scripts\Base\Object\ILoad.cs" />
+    <Compile Include="Assets\Scripts\Base\Object\ISerializeToEntity.cs" />
     <Compile Include="Assets\Scripts\Base\Object\IStart.cs" />
     <Compile Include="Assets\Scripts\Base\Object\IUpdate.cs" />
     <Compile Include="Assets\Scripts\Base\Object\Object.cs" />