Prechádzať zdrojové kódy

帧同步使用MemoryPack做序列化,提升性能,稍加修改甚至可以做到无GC

tanghai 2 rokov pred
rodič
commit
12ab0087de

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

@@ -8,11 +8,19 @@ namespace ET
     {
         public static byte[] Serialize(object message)
         {
+            if (message is ISupportInitialize supportInitialize)
+            {
+                supportInitialize.BeginInit();
+            }
             return MemoryPackSerializer.Serialize(message.GetType(), message);
         }
 
         public static void Serialize(object message, MemoryBuffer stream)
         {
+            if (message is ISupportInitialize supportInitialize)
+            {
+                supportInitialize.BeginInit();
+            }
             MemoryPackSerializer.Serialize(message.GetType(), stream, message);
         }
         

+ 2 - 2
Unity/Assets/Scripts/Hotfix/Share/LockStep/RoomSystem.cs

@@ -57,7 +57,7 @@ namespace ET
         {
             MemoryBuffer memoryBuffer = self.FrameBuffer.Snapshot(frame);
             memoryBuffer.Seek(0, SeekOrigin.Begin);
-            LSWorld lsWorld = MongoHelper.Deserialize(typeof (LSWorld), memoryBuffer) as LSWorld;
+            LSWorld lsWorld = MemoryPackHelper.Deserialize(typeof (LSWorld), memoryBuffer) as LSWorld;
             lsWorld.SceneType = sceneType;
             memoryBuffer.Seek(0, SeekOrigin.Begin);
             return lsWorld;
@@ -70,7 +70,7 @@ namespace ET
             memoryBuffer.Seek(0, SeekOrigin.Begin);
             memoryBuffer.SetLength(0);
             
-            MongoHelper.Serialize(self.LSWorld, memoryBuffer);
+            MemoryPackHelper.Serialize(self.LSWorld, memoryBuffer);
             memoryBuffer.Seek(0, SeekOrigin.Begin);
 
             long hash = memoryBuffer.GetBuffer().Hash(0, (int) memoryBuffer.Length);

+ 4 - 0
Unity/Assets/Scripts/Loader/Plugins/HybridCLR/Generated/link.xml

@@ -9,8 +9,11 @@
     <type fullname="MemoryPack.IMemoryPackFormatterRegister" preserve="all" />
     <type fullname="MemoryPack.IMemoryPackable`1" preserve="all" />
     <type fullname="MemoryPack.Internal.PreserveAttribute" preserve="all" />
+    <type fullname="MemoryPack.MemoryPackConstructorAttribute" preserve="all" />
     <type fullname="MemoryPack.MemoryPackFormatterProvider" preserve="all" />
     <type fullname="MemoryPack.MemoryPackFormatter`1" preserve="all" />
+    <type fullname="MemoryPack.MemoryPackIgnoreAttribute" preserve="all" />
+    <type fullname="MemoryPack.MemoryPackIncludeAttribute" preserve="all" />
     <type fullname="MemoryPack.MemoryPackOrderAttribute" preserve="all" />
     <type fullname="MemoryPack.MemoryPackReader" preserve="all" />
     <type fullname="MemoryPack.MemoryPackSerializationException" preserve="all" />
@@ -317,6 +320,7 @@
     <type fullname="System.Runtime.CompilerServices.RuntimeCompatibilityAttribute" preserve="all" />
     <type fullname="System.Runtime.CompilerServices.TaskAwaiter" preserve="all" />
     <type fullname="System.Runtime.CompilerServices.TaskAwaiter`1" preserve="all" />
+    <type fullname="System.Runtime.Versioning.TargetFrameworkAttribute" preserve="all" />
     <type fullname="System.RuntimeTypeHandle" preserve="all" />
     <type fullname="System.Security.Permissions.SecurityAction" preserve="all" />
     <type fullname="System.Security.Permissions.SecurityPermissionAttribute" preserve="all" />

+ 4 - 1
Unity/Assets/Scripts/Model/Share/LockStep/LSInputComponent.cs

@@ -1,7 +1,10 @@
+using MemoryPack;
+
 namespace ET
 {
     [ComponentOf(typeof(LSUnit))]
-    public class LSInputComponent: LSEntity, ILSUpdate, IAwake, ISerializeToEntity
+    [MemoryPackable]
+    public partial class LSInputComponent: LSEntity, ILSUpdate, IAwake, ISerializeToEntity
     {
         public LSInput LSInput { get; set; }
     }

+ 5 - 2
Unity/Assets/Scripts/Model/Share/LockStep/LSUnitComponent.cs

@@ -1,7 +1,10 @@
-namespace ET
+using MemoryPack;
+
+namespace ET
 {
 	[ComponentOf(typeof(LSWorld))]
-	public class LSUnitComponent: LSEntity, IAwake, ISerializeToEntity
+	[MemoryPackable]
+	public partial class LSUnitComponent: LSEntity, IAwake, ISerializeToEntity
 	{
 	}
 }

+ 7 - 1
Unity/Assets/Scripts/Model/Share/LockStep/LSWorld.cs

@@ -1,6 +1,7 @@
 using MongoDB.Bson.Serialization.Attributes;
 using System;
 using System.Collections.Generic;
+using MemoryPack;
 using TrueSync;
 
 namespace ET
@@ -26,8 +27,10 @@ namespace ET
     [EnableMethod]
     [ChildOf]
     [ComponentOf]
-    public class LSWorld: Entity, IAwake, IScene
+    [MemoryPackable]
+    public partial class LSWorld: Entity, IAwake, IScene
     {
+        [MemoryPackConstructor]
         public LSWorld()
         {
         }
@@ -42,9 +45,11 @@ namespace ET
         private readonly LSUpdater updater = new();
         
         [BsonIgnore]
+        [MemoryPackIgnore]
         public Fiber Fiber { get; set; }
         
         [BsonElement]
+        [MemoryPackInclude]
         private long idGenerator;
 
         public long GetId()
@@ -55,6 +60,7 @@ namespace ET
         public TSRandom Random { get; set; }
         
         [BsonIgnore]
+        [MemoryPackIgnore]
         public SceneType SceneType { get; set; }
         
         public int Frame { get; set; }

+ 7 - 1
Unity/Assets/Scripts/ThirdParty/TrueSync/TSRandom.cs

@@ -1,4 +1,5 @@
 using System;
+using MemoryPack;
 using MongoDB.Bson.Serialization.Attributes;
 
 namespace TrueSync {
@@ -6,7 +7,8 @@ namespace TrueSync {
     /**
      *  @brief Generates random numbers based on a deterministic approach.
      **/
-    public class TSRandom {
+    [MemoryPackable]
+    public partial class TSRandom {
         // From http://www.codeproject.com/Articles/164087/Random-Number-Generation
         // Class TSRandom generates random numbers
         // from a uniform distribution using the Mersenne
@@ -18,12 +20,16 @@ namespace TrueSync {
         private const uint LOWER_MASK = 0x7fffffffU;
         private const int MAX_RAND_INT = 0x7fffffff;
         [BsonElement]
+        [MemoryPackInclude]
         private uint[] mag01 = { 0x0U, MATRIX_A };
         [BsonElement]
+        [MemoryPackInclude]
         private uint[] mt = new uint[N];
         [BsonElement]
+        [MemoryPackInclude]
         private int mti = N + 1;
 
+        [MemoryPackConstructor]
         private TSRandom() {
         }
 

+ 3 - 1
Unity/Assets/Scripts/ThirdParty/Unity.ThirdParty.asmdef

@@ -1,7 +1,9 @@
 {
     "name": "Unity.ThirdParty",
     "rootNamespace": "ET",
-    "references": [],
+    "references": [
+        "MemoryPack"
+    ],
     "includePlatforms": [],
     "excludePlatforms": [],
     "allowUnsafeCode": true,