瀏覽代碼

重载Component的ToString方法,方便repl时打印Component对象

tanghai 7 年之前
父節點
當前提交
f244253192

+ 1 - 3
Server/Model/Module/Demo/Unit.cs

@@ -1,5 +1,4 @@
-using MongoDB.Bson.Serialization.Attributes;
-using PF;
+using PF;
 
 namespace ETModel
 {
@@ -22,7 +21,6 @@ namespace ETModel
 	{
 		public UnitType UnitType { get; private set; }
 		
-		[BsonIgnore]
 		public Vector3 Position { get; set; }
 		
 		public void Awake(UnitType unitType)

+ 4 - 0
Server/Model/Module/Demo/UnitComponent.cs

@@ -1,10 +1,14 @@
 using System.Collections.Generic;
 using System.Linq;
+using MongoDB.Bson.Serialization.Attributes;
+using MongoDB.Bson.Serialization.Options;
 
 namespace ETModel
 {
 	public class UnitComponent: Component
 	{
+		[BsonElement]
+		[BsonDictionaryOptions(DictionaryRepresentation.ArrayOfArrays)]
 		private readonly Dictionary<long, Unit> idUnits = new Dictionary<long, Unit>();
 
 		public override void Dispose()

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

@@ -44,6 +44,9 @@
     <Compile Include="..\..\Unity\Assets\Model\Base\Async\MoveNextRunner.cs">
       <Link>Base\Async\MoveNextRunner.cs</Link>
     </Compile>
+    <Compile Include="..\..\Unity\Assets\Model\Base\Helper\MongoHelper.cs">
+      <Link>Base\Helper\MongoHelper.cs</Link>
+    </Compile>
     <Compile Include="..\..\Unity\Assets\Model\Base\Object\IDeserializeSystem.cs">
       <Link>Base\Base\IDeserializeSystem.cs</Link>
     </Compile>

+ 0 - 103
Unity/Assets/Editor/Helper/MongoHelper.cs

@@ -1,103 +0,0 @@
-using System;
-using System.IO;
-using MongoDB.Bson;
-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
-	{
-		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));
-		}
-
-		public static string ToJson(object obj)
-		{
-			return obj.ToJson();
-		}
-
-		public static string ToJson(object obj, JsonWriterSettings settings)
-		{
-			return obj.ToJson(settings);
-		}
-
-		public static T FromJson<T>(string str)
-		{
-			return BsonSerializer.Deserialize<T>(str);
-		}
-
-		public static object FromJson(Type type, string str)
-		{
-			return BsonSerializer.Deserialize(str, type);
-		}
-
-		public static byte[] ToBson(object obj)
-		{
-			return obj.ToBson();
-		}
-
-		public static object FromBson(Type type, byte[] bytes)
-		{
-			return BsonSerializer.Deserialize(bytes, type);
-		}
-
-		public static object FromBson(Type type, byte[] bytes, int index, int count)
-		{
-			using (MemoryStream memoryStream = new MemoryStream(bytes, index, count))
-			{
-				return BsonSerializer.Deserialize(memoryStream, type);
-			}
-		}
-
-		public static T FromBson<T>(byte[] bytes)
-		{
-			using (MemoryStream memoryStream = new MemoryStream(bytes))
-			{
-				return (T) BsonSerializer.Deserialize(memoryStream, typeof (T));
-			}
-		}
-
-		public static T FromBson<T>(byte[] bytes, int index, int count)
-		{
-			return (T) FromBson(typeof (T), bytes, index, count);
-		}
-
-		public static T Clone<T>(T t)
-		{
-			return FromBson<T>(ToBson(t));
-		}
-
-
-        public static void AvoidAOT()
-        {
-            ArraySerializer<int> aint = new ArraySerializer<int>();
-            ArraySerializer<string> astring = new ArraySerializer<string>();
-            ArraySerializer<long> along = new ArraySerializer<long>();
-
-            EnumerableInterfaceImplementerSerializer<List<int>> e =
-                new EnumerableInterfaceImplementerSerializer<List<int>>();
-
-            EnumerableInterfaceImplementerSerializer<List<int>, int> elistint =
-                new EnumerableInterfaceImplementerSerializer<List<int>, int>();
-        }
-
-	}
-}

+ 18 - 10
Server/Model/Base/Helper/MongoHelper.cs → Unity/Assets/Model/Base/Helper/MongoHelper.cs

@@ -1,10 +1,10 @@
 using System;
 using System.IO;
-using System.Reflection;
 using MongoDB.Bson;
 using MongoDB.Bson.IO;
 using MongoDB.Bson.Serialization;
 using MongoDB.Bson.Serialization.Serializers;
+using System.Collections.Generic;
 
 namespace ETModel
 {
@@ -12,9 +12,6 @@ namespace ETModel
 	{
 		static MongoHelper()
 		{
-			Type bsonClassMap = typeof(BsonClassMap);
-			MethodInfo methodInfo = bsonClassMap.GetMethod("RegisterClassMap", new Type[] { });
-
 			Type[] types = typeof(Game).Assembly.GetTypes();
 			foreach (Type type in types)
 			{
@@ -22,12 +19,8 @@ namespace ETModel
 				{
 					continue;
 				}
-				
-				if(type == typeof(ComponentWithId) || type == typeof(Component) || type == typeof(Entity))
-				{
-					continue;
-				}
-				methodInfo.MakeGenericMethod(type).Invoke(null, null);
+
+				BsonClassMap.LookupClassMap(type);
 			}
 
 			BsonSerializer.RegisterSerializer(new EnumSerializer<NumericType>(BsonType.String));
@@ -118,5 +111,20 @@ namespace ETModel
 		{
 			return FromBson<T>(ToBson(t));
 		}
+
+
+        public static void AvoidAOT()
+        {
+            ArraySerializer<int> aint = new ArraySerializer<int>();
+            ArraySerializer<string> astring = new ArraySerializer<string>();
+            ArraySerializer<long> along = new ArraySerializer<long>();
+
+            EnumerableInterfaceImplementerSerializer<List<int>> e =
+                new EnumerableInterfaceImplementerSerializer<List<int>>();
+
+            EnumerableInterfaceImplementerSerializer<List<int>, int> elistint =
+                new EnumerableInterfaceImplementerSerializer<List<int>, int>();
+        }
+
 	}
 }

+ 2 - 3
Unity/Assets/Editor/Helper/MongoHelper.cs.meta → Unity/Assets/Model/Base/Helper/MongoHelper.cs.meta

@@ -1,8 +1,7 @@
 fileFormatVersion: 2
-guid: 3d9c9dde09c5989459b651b179b85a0f
-timeCreated: 1519966034
-licenseType: Free
+guid: baa7a22e74c5c0b46ba147e47f65febc
 MonoImporter:
+  externalObjects: {}
   serializedVersion: 2
   defaultReferences: []
   executionOrder: 0

+ 5 - 0
Unity/Assets/Model/Base/Object/Component.cs

@@ -88,5 +88,10 @@ namespace ETModel
 		{
 			Game.EventSystem.Deserialize(this);
 		}
+		
+		public override string ToString()
+		{
+			return MongoHelper.ToJson(this);
+		}
 	}
 }

+ 0 - 1
Unity/Unity.Editor.csproj

@@ -82,7 +82,6 @@
     <Compile Include="Assets\Editor\ExcelExporterEditor\ExcelExporterEditor.cs" />
     <Compile Include="Assets\Editor\GlobalConfigEditor\GlobalConfigEditor.cs" />
     <Compile Include="Assets\Editor\Helper\EditorResHelper.cs" />
-    <Compile Include="Assets\Editor\Helper\MongoHelper.cs" />
     <Compile Include="Assets\Editor\Helper\ShellHelper.cs" />
     <Compile Include="Assets\Editor\ILRuntimeEditor\ILRuntimeCLRBinding.cs" />
     <Compile Include="Assets\Editor\LogRedirection.cs" />

+ 1 - 0
Unity/Unity.Model.csproj

@@ -77,6 +77,7 @@
     <Compile Include="Assets\Model\Base\Helper\JsonHelper.cs" />
     <Compile Include="Assets\Model\Base\Helper\MD5Helper.cs" />
     <Compile Include="Assets\Model\Base\Helper\MethodInfoHelper.cs" />
+    <Compile Include="Assets\Model\Base\Helper\MongoHelper.cs" />
     <Compile Include="Assets\Model\Base\Helper\NetHelper.cs" />
     <Compile Include="Assets\Model\Base\Helper\ObjectHelper.cs" />
     <Compile Include="Assets\Model\Base\Helper\RandomHelper.cs" />