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

以后再也不需要手动添加BsonKnowType标签啦!

tanghai 7 лет назад
Родитель
Сommit
c9e5a9b2e6

+ 1 - 2
Server/App/Program.cs

@@ -2,6 +2,7 @@
 using System.Net;
 using System.Threading;
 using ETModel;
+using MongoDB.Bson.Serialization;
 using NLog;
 
 namespace App
@@ -13,8 +14,6 @@ namespace App
 			// 异步方法全部会回掉到主线程
 			OneThreadSynchronizationContext contex = new OneThreadSynchronizationContext();
 			SynchronizationContext.SetSynchronizationContext(contex);
-
-			MongoHelper.Init();
 			
 			try
 			{

+ 15 - 1
Server/Model/Base/Helper/MongoHelper.cs

@@ -1,5 +1,6 @@
 using System;
 using System.IO;
+using System.Reflection;
 using MongoDB.Bson;
 using MongoDB.Bson.IO;
 using MongoDB.Bson.Serialization;
@@ -9,8 +10,21 @@ namespace ETModel
 {
 	public static class MongoHelper
 	{
-		public static void Init()
+		static MongoHelper()
 		{
+			Type bsonClassMap = typeof(BsonClassMap);
+			MethodInfo methodInfo = bsonClassMap.GetMethod("RegisterClassMap", new Type[] { });
+
+			Type[] types = typeof(Game).Assembly.GetTypes();
+			foreach (Type type in types)
+			{
+				if (!type.IsSubclassOf(typeof(Component)))
+				{
+					continue;
+				}
+				methodInfo.MakeGenericMethod(type).Invoke(null, null);
+			}
+
 			BsonSerializer.RegisterSerializer(new EnumSerializer<NumericType>(BsonType.String));
 		}
 

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

@@ -1,12 +0,0 @@
-using MongoDB.Bson.Serialization.Attributes;
-
-namespace ETModel
-{
-	[BsonKnownTypes(typeof(AConfigComponent))]
-	[BsonKnownTypes(typeof(UnitGateComponent))]
-	[BsonKnownTypes(typeof(NumericComponent))]
-	[BsonKnownTypes(typeof(ComponentWithId))]
-	public partial class Component
-	{
-	}
-}

+ 0 - 9
Server/Model/Base/Object/ComponentWithIdAttribute.cs

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

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

@@ -1,11 +0,0 @@
-using MongoDB.Bson.Serialization.Attributes;
-
-namespace ETModel
-{
-	[BsonKnownTypes(typeof(Location))]
-	[BsonKnownTypes(typeof(Recharge))]
-	[BsonKnownTypes(typeof(RechargeRecord))]
-	public partial class Entity
-	{
-	}
-}

+ 15 - 1
Unity/Assets/Editor/Helper/MongoHelper.cs

@@ -5,13 +5,27 @@ using MongoDB.Bson.IO;
 using MongoDB.Bson.Serialization;
 using MongoDB.Bson.Serialization.Serializers;
 using System.Collections.Generic;
+using System.Reflection;
 
 namespace ETModel
 {
 	public static class MongoHelper
 	{
-		public static void Init()
+		static MongoHelper()
 		{
+			Type bsonClassMap = typeof(BsonClassMap);
+			MethodInfo methodInfo = bsonClassMap.GetMethod("RegisterClassMap", new Type[] { });
+
+			Type[] types = typeof(Game).Assembly.GetTypes();
+			foreach (Type type in types)
+			{
+				if (!type.IsSubclassOf(typeof(Component)))
+				{
+					continue;
+				}
+				methodInfo.MakeGenericMethod(type).Invoke(null, null);
+			}
+
 			BsonSerializer.RegisterSerializer(new EnumSerializer<NumericType>(BsonType.String));
 		}
 

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

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

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

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

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

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

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

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

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

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

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

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

+ 1 - 9
Unity/Assets/Scripts/Module/Config/AConfigComponent.cs

@@ -1,16 +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))]
-	[BsonKnownTypes(typeof(RunServerConfig))]
 	public abstract class AConfigComponent: Component, ISerializeToEntity
 	{
 	}

+ 7 - 7
Unity/Unity.csproj

@@ -12,12 +12,15 @@
     <ProjectTypeGuids>{E097FAD1-6243-4DAD-9C02-E9B9EFC3FFC1};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
     <TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier>
     <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
-    <TargetFrameworkProfile></TargetFrameworkProfile>
-    <CompilerResponseFile></CompilerResponseFile>
+    <TargetFrameworkProfile>
+    </TargetFrameworkProfile>
+    <CompilerResponseFile>
+    </CompilerResponseFile>
     <UnityProjectType>Game:1</UnityProjectType>
     <UnityBuildTarget>Android:13</UnityBuildTarget>
     <UnityVersion>2017.1.3p2</UnityVersion>
-    <RootNamespace></RootNamespace>
+    <RootNamespace>
+    </RootNamespace>
     <LangVersion>6</LangVersion>
   </PropertyGroup>
   <PropertyGroup>
@@ -146,12 +149,9 @@
     <Compile Include="Assets\Scripts\Base\Math\VRect.cs" />
     <Compile Include="Assets\Scripts\Base\MultiMap.cs" />
     <Compile Include="Assets\Scripts\Base\Object\Component.cs" />
-    <Compile Include="Assets\Scripts\Base\Object\ComponentAttribute.cs" />
     <Compile Include="Assets\Scripts\Base\Object\ComponentFactory.cs" />
     <Compile Include="Assets\Scripts\Base\Object\ComponentWithId.cs" />
-    <Compile Include="Assets\Scripts\Base\Object\ComponentWithIdAttribute.cs" />
     <Compile Include="Assets\Scripts\Base\Object\Entity.cs" />
-    <Compile Include="Assets\Scripts\Base\Object\EntityAttribute.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" />
@@ -774,4 +774,4 @@
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Target Name="GenerateTargetFrameworkMonikerAttribute" />
-</Project>
+</Project>