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

兼容Protobuf-net跟MemoryPack两种序列化,在SerializeHelper.UseMemoryPack进行切换
MemoryPack只能在EnableCodes模式中起作用,原因是AssemblyBuilder编译dll模式无法进行Source generator,
导致无法生成MemoryPack的patial代码

tanghai 2 лет назад
Родитель
Сommit
8d7a861bae

+ 0 - 12
Unity/Assets/Scripts/Codes/Model/Share/Entry.cs

@@ -18,18 +18,6 @@ namespace ET
         } 
     }
     
-    [MemoryPackable]
-    public partial class BB
-    {
-        public int a;
-    }
-
-    [MemoryPackable]
-    public partial class AA
-    {
-        public int a;
-    }
-
     public static class Entry
     {
         public static void Init()

+ 0 - 2
Unity/Assets/Scripts/Core/Serialize/MemoryPackHelper.cs

@@ -1,10 +1,8 @@
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
-using System.IO;
 using System.Reflection;
 using MemoryPack;
-using MongoDB.Bson.Serialization;
 
 namespace ET
 {

+ 40 - 8
Unity/Assets/Scripts/Core/Serialize/SerializeHelper.cs

@@ -1,29 +1,61 @@
 using System.IO;
 using System;
 using MemoryPack;
+#pragma warning disable CS0162
 
 namespace ET
 {
     public static class SerializeHelper
-    {
+	{
+		public const bool UseMemoryPack = false;
+		
 		public static object Deserialize(Type type, byte[] bytes, int index, int count)
 		{
-			return MemoryPackHelper.Deserialize(type, bytes, index, count);
+			if (UseMemoryPack)
+			{
+				return MemoryPackHelper.Deserialize(type, bytes, index, count);
+			}
+			else
+			{
+				using MemoryStream memoryStream = new MemoryStream(bytes, index, count);
+				return ProtoBuf.Serializer.Deserialize(type, memoryStream);
+			}
 		}
 
         public static byte[] Serialize(object message)
 		{
-			return MemoryPackHelper.Serialize(message);
+			if (UseMemoryPack)
+			{
+				return MemoryPackHelper.Serialize(message);
+			}
+			else
+			{
+				return ProtobufHelper.Serialize(message);	
+			}
 		}
 
         public static void Serialize(object message, MemoryBuffer stream)
         {
-			MemoryPackHelper.Serialize(message, stream);
-        }
+			if (UseMemoryPack)
+			{
+				MemoryPackHelper.Serialize(message, stream);
+			}
+			else
+			{
+				ProtobufHelper.Serialize(message, stream);	
+			}
+		}
 
-        public static object Deserialize(Type type, MemoryBuffer stream)
-        {
-	        return MemoryPackHelper.Deserialize(type, stream);
+		public static object Deserialize(Type type, MemoryBuffer stream)
+		{
+			if (UseMemoryPack)
+			{
+				return MemoryPackHelper.Deserialize(type, stream);
+			}
+			else
+			{
+				return ProtobufHelper.Deserialize(type, stream);	
+			}
         }
     }
 }

+ 1 - 3
Unity/Assets/Scripts/Empty/Hotfix/Unity.Hotfix.asmdef

@@ -9,9 +9,7 @@
         "Unity.Loader",
         "MemoryPack"
     ],
-    "includePlatforms": [
-        "Editor"
-    ],
+    "includePlatforms": [],
     "excludePlatforms": [],
     "allowUnsafeCode": true,
     "overrideReferences": false,

+ 1 - 3
Unity/Assets/Scripts/Empty/HotfixView/Unity.HotfixView.asmdef

@@ -11,9 +11,7 @@
         "Unity.Loader",
         "MemoryPack"
     ],
-    "includePlatforms": [
-        "Editor"
-    ],
+    "includePlatforms": [],
     "excludePlatforms": [],
     "allowUnsafeCode": true,
     "overrideReferences": false,

+ 1 - 3
Unity/Assets/Scripts/Empty/Model/Unity.Model.asmdef

@@ -7,9 +7,7 @@
         "Unity.Mathematics",
         "MemoryPack"
     ],
-    "includePlatforms": [
-        "Editor"
-    ],
+    "includePlatforms": [],
     "excludePlatforms": [],
     "allowUnsafeCode": true,
     "overrideReferences": false,

+ 1 - 3
Unity/Assets/Scripts/Empty/ModelView/Unity.ModelView.asmdef

@@ -9,9 +9,7 @@
         "Unity.Loader",
         "MemoryPack"
     ],
-    "includePlatforms": [
-        "Editor"
-    ],
+    "includePlatforms": [],
     "excludePlatforms": [],
     "allowUnsafeCode": true,
     "overrideReferences": false,

+ 2 - 0
Unity/Assets/Scripts/Loader/Define.cs

@@ -4,6 +4,8 @@
 	{
 		public const string BuildOutputDir = "./Temp/Bin/Debug";
 
+		public const bool UseMemoryPack = false;
+
 #if UNITY_EDITOR && !ASYNC
 		public static bool IsAsync = false;
 #else

+ 6 - 1
Unity/Assets/Scripts/Loader/MonoBehaviour/Init.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Threading;
 using CommandLine;
 using UnityEngine;
 
@@ -34,6 +33,12 @@ namespace ET
 			
 			ETTask.ExceptionHandler += Log.Error;
 
+			if (SerializeHelper.UseMemoryPack && !Define.EnableCodes)
+			{
+				Log.Error("MemoryPack must use ENABLE_CODES mode");
+				return;
+			}
+
 			Game.AddSingleton<CodeLoader>().Start();
 		}
 

+ 16 - 0
Unity/ProjectSettings/BurstAotSettings_Android.json

@@ -0,0 +1,16 @@
+{
+  "MonoBehaviour": {
+    "Version": 4,
+    "EnableBurstCompilation": true,
+    "EnableOptimisations": true,
+    "EnableSafetyChecks": false,
+    "EnableDebugInAllBuilds": false,
+    "EnableArmv9SecurityFeatures": false,
+    "CpuMinTargetX32": 0,
+    "CpuMaxTargetX32": 0,
+    "CpuMinTargetX64": 0,
+    "CpuMaxTargetX64": 0,
+    "CpuTargetsArm64": 512,
+    "OptimizeFor": 0
+  }
+}

+ 15 - 0
Unity/ProjectSettings/BurstAotSettings_iOS.json

@@ -0,0 +1,15 @@
+{
+  "MonoBehaviour": {
+    "Version": 4,
+    "EnableBurstCompilation": true,
+    "EnableOptimisations": true,
+    "EnableSafetyChecks": false,
+    "EnableDebugInAllBuilds": false,
+    "EnableArmv9SecurityFeatures": false,
+    "CpuMinTargetX32": 0,
+    "CpuMaxTargetX32": 0,
+    "CpuMinTargetX64": 0,
+    "CpuMaxTargetX64": 0,
+    "OptimizeFor": 0
+  }
+}

+ 1 - 1
Unity/ProjectSettings/ProjectSettings.asset

@@ -841,7 +841,7 @@ PlayerSettings:
   scriptingDefineSymbols:
     Android: UNITY;SINGLE_THREAD
     Server: UNITY
-    Standalone: UNITY;SINGLE_THREAD;ENABLE_CODES
+    Standalone: UNITY;SINGLE_THREAD
     WebGL: UNITY
     iPhone: UNITY;SINGLE_THREAD
   additionalCompilerArguments: {}