Преглед изворни кода

客户端中不在用mongo库,用litjson替代,只有编辑器中使用

tanghai пре 8 година
родитељ
комит
ee77bffb35

+ 27 - 0
Server/Model/Base/Helper/JsonHelper.cs

@@ -0,0 +1,27 @@
+using System;
+
+namespace Model
+{
+	public static class JsonHelper
+	{
+		public static string ToJson(object obj)
+		{
+			return MongoHelper.ToJson(obj);
+		}
+
+		public static T FromJson<T>(string str)
+		{
+			return MongoHelper.FromJson<T>(str);
+		}
+
+		public static object FromJson(Type type, string str)
+		{
+			return MongoHelper.FromJson(type, str);
+		}
+
+		public static T Clone<T>(T t)
+		{
+			return FromJson<T>(ToJson(t));
+		}
+	}
+}

+ 0 - 0
Unity/Assets/Scripts/Base/Helper/MongoHelper.cs → Server/Model/Base/Helper/MongoHelper.cs


+ 0 - 0
Unity/Assets/Scripts/Module/Message/MongoPacker.cs → Server/Model/Module/Message/MongoPacker.cs


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

@@ -33,7 +33,6 @@
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Helper\IdGenerater.cs" Link="Base\Helper\IdGenerater.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Helper\MD5Helper.cs" Link="Base\Helper\MD5Helper.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Helper\MethodInfoHelper.cs" Link="Base\Helper\MethodInfoHelper.cs" />
-    <Compile Include="..\..\Unity\Assets\Scripts\Base\Helper\MongoHelper.cs" Link="Base\Helper\MongoHelper.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Helper\NetHelper.cs" Link="Base\Helper\NetHelper.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Helper\ObjectHelper.cs" Link="Base\Helper\ObjectHelper.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Helper\ProtobufHelper.cs" Link="Base\Helper\ProtobufHelper.cs" />
@@ -83,7 +82,6 @@
     <Compile Include="..\..\Unity\Assets\Scripts\Module\Message\MessageDispatherComponent.cs" Link="Module\Message\MessageDispatherComponent.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Module\Message\MessageHandlerAttribute.cs" Link="Module\Message\MessageHandlerAttribute.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Module\Message\MessageInfo.cs" Link="Module\Message\MessageInfo.cs" />
-    <Compile Include="..\..\Unity\Assets\Scripts\Module\Message\MongoPacker.cs" Link="Module\Message\MongoPacker.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Module\Message\NetworkComponent.cs" Link="Module\Message\NetworkComponent.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Module\Message\Network\AChannel.cs" Link="Module\Message\Network\AChannel.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Module\Message\Network\AService.cs" Link="Module\Message\Network\AService.cs" />

+ 1 - 1
Unity/Assets/Editor/BuildEditor/BuildHelper.cs

@@ -94,7 +94,7 @@ namespace MyEditor
 
 			using (FileStream fileStream = new FileStream($"{dir}/Version.txt", FileMode.Create))
 			{
-				byte[] bytes = MongoHelper.ToJson(versionProto).ToByteArray();
+				byte[] bytes = JsonHelper.ToJson(versionProto).ToByteArray();
 				fileStream.Write(bytes, 0, bytes.Length);
 			}
 		}

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

@@ -0,0 +1,73 @@
+using System;
+using System.IO;
+using MongoDB.Bson;
+using MongoDB.Bson.IO;
+using MongoDB.Bson.Serialization;
+using MongoDB.Bson.Serialization.Serializers;
+
+namespace Model
+{
+	public static class MongoHelper
+	{
+		public static void Init()
+		{
+			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));
+		}
+	}
+}

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

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
-guid: 9c62dd348619a914083a44aa2fd8cd70
-timeCreated: 1501467370
+guid: 3d9c9dde09c5989459b651b179b85a0f
+timeCreated: 1519966034
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 17 - 2
Unity/Assets/Scripts/Base/Helper/JsonHelper.cs

@@ -1,4 +1,5 @@
 using System;
+using System.ComponentModel;
 using LitJson;
 
 namespace Model
@@ -12,12 +13,26 @@ namespace Model
 
 		public static T FromJson<T>(string str)
 		{
-			return JsonMapper.ToObject<T>(str);
+			T t = JsonMapper.ToObject<T>(str);
+			ISupportInitialize iSupportInitialize = t as ISupportInitialize;
+			if (iSupportInitialize == null)
+			{
+				return t;
+			}
+			iSupportInitialize.EndInit();
+			return t;
 		}
 
 		public static object FromJson(Type type, string str)
 		{
-			return JsonMapper.ToObject(type, str);
+			object t = JsonMapper.ToObject(type, str);
+			ISupportInitialize iSupportInitialize = t as ISupportInitialize;
+			if (iSupportInitialize == null)
+			{
+				return t;
+			}
+			iSupportInitialize.EndInit();
+			return t;
 		}
 
 		public static T Clone<T>(T t)

+ 5 - 5
Unity/Assets/Scripts/Component/BundleDownloaderComponent.cs

@@ -43,10 +43,10 @@ namespace Model
 				string versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt";
 				Log.Debug(versionUrl);
 				await request.DownloadAsync(versionUrl);
-				this.VersionConfig = MongoHelper.FromJson<VersionConfig>(request.Request.downloadHandler.text);
+				this.VersionConfig = JsonHelper.FromJson<VersionConfig>(request.Request.downloadHandler.text);
 			}
 			
-			Log.Debug("WebVersion:\n" + MongoHelper.ToJson(this.VersionConfig));
+			Log.Debug("WebVersion:\n" + JsonHelper.ToJson(this.VersionConfig));
 
 			// 对比本地的Version.txt
 			string versionPath = Path.Combine(PathHelper.AppHotfixResPath, "Version.txt");
@@ -65,8 +65,8 @@ namespace Model
 			else
 			{
 
-				VersionConfig localVersionConfig = MongoHelper.FromJson<VersionConfig>(File.ReadAllText(versionPath));
-				Log.Debug("LocalVersion:\n" + MongoHelper.ToJson(localVersionConfig));
+				VersionConfig localVersionConfig = JsonHelper.FromJson<VersionConfig>(File.ReadAllText(versionPath));
+				Log.Debug("LocalVersion:\n" + JsonHelper.ToJson(localVersionConfig));
 				// 先删除服务器端没有的ab
 				foreach (FileVersionInfo fileVersionInfo in localVersionConfig.FileVersionInfos)
 				{
@@ -156,7 +156,7 @@ namespace Model
 			using (FileStream fs = new FileStream(Path.Combine(PathHelper.AppHotfixResPath, "Version.txt"), FileMode.Create))
 			using (StreamWriter sw = new StreamWriter(fs))
 			{
-				sw.Write(MongoHelper.ToJson(this.VersionConfig));
+				sw.Write(JsonHelper.ToJson(this.VersionConfig));
 			}
 
 			this.Tcs?.SetResult(true);

+ 1 - 1
Unity/Assets/Scripts/Component/Config/VersionConfig.cs

@@ -10,7 +10,7 @@ namespace Model
 		public long Size;
 	}
 
-	public class VersionConfig : AConfig
+	public class VersionConfig : Object
 	{
 		public int Version;
 		

+ 0 - 12
Unity/Assets/Scripts/Module/Message/MongoPacker.cs.meta

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

+ 3 - 3
Unity/Assets/Scripts/Module/Message/ProtobufPacker.cs

@@ -11,7 +11,7 @@ namespace Model
 
 		public string SerializeToText(object obj)
 		{
-			return MongoHelper.ToJson(obj);
+			return JsonHelper.ToJson(obj);
 		}
 
 		public object DeserializeFrom(Type type, byte[] bytes)
@@ -36,12 +36,12 @@ namespace Model
 
 		public T DeserializeFrom<T>(string str)
 		{
-			return MongoHelper.FromJson<T>(str);
+			return JsonHelper.FromJson<T>(str);
 		}
 
 		public object DeserializeFrom(Type type, string str)
 		{
-			return MongoHelper.FromJson(type, str);
+			return JsonHelper.FromJson(type, str);
 		}
 	}
 }

+ 0 - 1
Unity/Assets/ThirdParty/ILRuntime/Generated/CLRBindings.cs

@@ -101,7 +101,6 @@ namespace ILRuntime.Runtime.Generated
             Model_Actor_TestRequest_Binding.Register(app);
             System_Threading_Tasks_Task_1_IResponse_Binding.Register(app);
             System_Runtime_CompilerServices_TaskAwaiter_1_IResponse_Binding.Register(app);
-            Model_MongoHelper_Binding.Register(app);
             Model_Actor_TransferRequest_Binding.Register(app);
             Model_C2G_EnterMap_Binding.Register(app);
             Model_GlobalConfigComponent_Binding.Register(app);

+ 0 - 34
Unity/Assets/ThirdParty/ILRuntime/Generated/Model_MongoHelper_Binding.cs

@@ -13,38 +13,4 @@ using ILRuntime.CLR.Utils;
 
 namespace ILRuntime.Runtime.Generated
 {
-    unsafe class Model_MongoHelper_Binding
-    {
-        public static void Register(ILRuntime.Runtime.Enviorment.AppDomain app)
-        {
-            BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
-            MethodBase method;
-            FieldInfo field;
-            Type[] args;
-            Type type = typeof(Model.MongoHelper);
-            args = new Type[]{typeof(System.Object)};
-            method = type.GetMethod("ToJson", flag, null, args, null);
-            app.RegisterCLRMethodRedirection(method, ToJson_0);
-
-
-        }
-
-
-        static StackObject* ToJson_0(ILIntepreter __intp, StackObject* __esp, IList<object> __mStack, CLRMethod __method, bool isNewObj)
-        {
-            ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;
-            StackObject* ptr_of_this_method;
-            StackObject* __ret = ILIntepreter.Minus(__esp, 1);
-            ptr_of_this_method = ILIntepreter.Minus(__esp, 1);
-            System.Object obj = (System.Object)typeof(System.Object).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
-            __intp.Free(ptr_of_this_method);
-
-            var result_of_this_method = Model.MongoHelper.ToJson(obj);
-
-            return ILIntepreter.PushObject(__ret, __mStack, result_of_this_method);
-        }
-
-
-
-    }
 }

+ 16 - 2
Unity/Hotfix/Base/Helper/JsonHelper.cs

@@ -12,12 +12,26 @@ namespace Hotfix
 
 		public static T FromJson<T>(string str)
 		{
-			return JsonMapper.ToObject<T>(str);
+			T t = JsonMapper.ToObject<T>(str);
+			ISupportInitialize2 iSupportInitialize = t as ISupportInitialize2;
+			if (iSupportInitialize == null)
+			{
+				return t;
+			}
+			iSupportInitialize.EndInit();
+			return t;
 		}
 
 		public static object FromJson(Type type, string str)
 		{
-			return JsonMapper.ToObject(type, str);
+			object t = JsonMapper.ToObject(type, str);
+			ISupportInitialize2 iSupportInitialize = t as ISupportInitialize2;
+			if (iSupportInitialize == null)
+			{
+				return t;
+			}
+			iSupportInitialize.EndInit();
+			return t;
 		}
 
 		public static T Clone<T>(T t)

+ 3 - 3
Unity/Hotfix/UI/UILobby/Component/UILobbyComponent.cs

@@ -50,7 +50,7 @@ namespace Hotfix
 			{
 				// 向actor发起一次rpc调用
 				Actor_TestResponse response = (Actor_TestResponse) await SessionComponent.Instance.Session.Call(new Actor_TestRequest() { request = "request actor test rpc" });
-				Log.Info($"recv response: {MongoHelper.ToJson(response)}");
+				Log.Info($"recv response: {JsonHelper.ToJson(response)}");
 			}
 			catch (Exception e)
 			{
@@ -63,7 +63,7 @@ namespace Hotfix
 			try
 			{
 				Actor_TransferResponse response = (Actor_TransferResponse) await SessionComponent.Instance.Session.Call(new Actor_TransferRequest() {MapIndex = 0});
-				Log.Info($"传送成功! {MongoHelper.ToJson(response)}");
+				Log.Info($"传送成功! {JsonHelper.ToJson(response)}");
 			}
 			catch (Exception e)
 			{
@@ -74,7 +74,7 @@ namespace Hotfix
 		private async void OnTransfer2()
 		{
 			Actor_TransferResponse response = (Actor_TransferResponse)await SessionComponent.Instance.Session.Call(new Actor_TransferRequest() { MapIndex = 1 });
-			Log.Info($"传送成功! {MongoHelper.ToJson(response)}");
+			Log.Info($"传送成功! {JsonHelper.ToJson(response)}");
 		}
 
 		private async void EnterMap()

+ 8 - 4
Unity/Unity.Editor.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>Editor:5</UnityProjectType>
     <UnityBuildTarget>StandaloneWindows:5</UnityBuildTarget>
     <UnityVersion>2017.1.1p4</UnityVersion>
-    <RootNamespace></RootNamespace>
+    <RootNamespace>
+    </RootNamespace>
     <LangVersion>6</LangVersion>
   </PropertyGroup>
   <PropertyGroup>
@@ -220,6 +223,7 @@
     <Compile Include="Assets\Editor\ExportNavmesh.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\Proto2CsEditor\Proto2CSEditor.cs" />
     <Compile Include="Assets\Editor\ReferenceCollectorEditor\ReferenceCollectorEditor.cs" />
     <Compile Include="Assets\Editor\RsyncEditor\RsyncConfig.cs" />
@@ -247,4 +251,4 @@
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Target Name="GenerateTargetFrameworkMonikerAttribute" />
-</Project>
+</Project>

+ 7 - 6
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>StandaloneWindows:5</UnityBuildTarget>
     <UnityVersion>2017.1.1p4</UnityVersion>
-    <RootNamespace></RootNamespace>
+    <RootNamespace>
+    </RootNamespace>
     <LangVersion>6</LangVersion>
   </PropertyGroup>
   <PropertyGroup>
@@ -129,7 +132,6 @@
     <Compile Include="Assets\Scripts\Base\Helper\JsonHelper.cs" />
     <Compile Include="Assets\Scripts\Base\Helper\MD5Helper.cs" />
     <Compile Include="Assets\Scripts\Base\Helper\MethodInfoHelper.cs" />
-    <Compile Include="Assets\Scripts\Base\Helper\MongoHelper.cs" />
     <Compile Include="Assets\Scripts\Base\Helper\NetHelper.cs" />
     <Compile Include="Assets\Scripts\Base\Helper\ObjectHelper.cs" />
     <Compile Include="Assets\Scripts\Base\Helper\ProtobufHelper.cs" />
@@ -302,7 +304,6 @@
     <Compile Include="Assets\Scripts\Module\Message\MessageHandlerAttribute.cs" />
     <Compile Include="Assets\Scripts\Module\Message\MessageInfo.cs" />
     <Compile Include="Assets\Scripts\Module\Message\MessageProxy.cs" />
-    <Compile Include="Assets\Scripts\Module\Message\MongoPacker.cs" />
     <Compile Include="Assets\Scripts\Module\Message\NetOuterComponent.cs" />
     <Compile Include="Assets\Scripts\Module\Message\Network\AChannel.cs" />
     <Compile Include="Assets\Scripts\Module\Message\Network\AService.cs" />
@@ -782,4 +783,4 @@
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Target Name="GenerateTargetFrameworkMonikerAttribute" />
-</Project>
+</Project>