diff --git a/Unity/Assets/Scripts/Core/Serialize/MemoryPackHelper.cs b/Unity/Assets/Scripts/Core/Serialize/MemoryPackHelper.cs index 5ade5db168b..fa360732989 100644 --- a/Unity/Assets/Scripts/Core/Serialize/MemoryPackHelper.cs +++ b/Unity/Assets/Scripts/Core/Serialize/MemoryPackHelper.cs @@ -8,11 +8,19 @@ public static class MemoryPackHelper { 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); } diff --git a/Unity/Assets/Scripts/Hotfix/Share/LockStep/RoomSystem.cs b/Unity/Assets/Scripts/Hotfix/Share/LockStep/RoomSystem.cs index 239a04117d0..bb0829d1841 100644 --- a/Unity/Assets/Scripts/Hotfix/Share/LockStep/RoomSystem.cs +++ b/Unity/Assets/Scripts/Hotfix/Share/LockStep/RoomSystem.cs @@ -57,7 +57,7 @@ public static LSWorld GetLSWorld(this Room self, SceneType sceneType, int frame) { 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 @@ private static void SaveLSWorld(this Room self) 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); diff --git a/Unity/Assets/Scripts/Loader/Plugins/HybridCLR/Generated/link.xml b/Unity/Assets/Scripts/Loader/Plugins/HybridCLR/Generated/link.xml index fcaf5c6fd41..9350ee3c5c2 100644 --- a/Unity/Assets/Scripts/Loader/Plugins/HybridCLR/Generated/link.xml +++ b/Unity/Assets/Scripts/Loader/Plugins/HybridCLR/Generated/link.xml @@ -9,8 +9,11 @@ + + + @@ -317,6 +320,7 @@ + diff --git a/Unity/Assets/Scripts/Model/Share/LockStep/LSConstValue.cs b/Unity/Assets/Scripts/Model/Share/LockStep/LSConstValue.cs index 85891b2d508..18afc85166a 100644 --- a/Unity/Assets/Scripts/Model/Share/LockStep/LSConstValue.cs +++ b/Unity/Assets/Scripts/Model/Share/LockStep/LSConstValue.cs @@ -2,7 +2,7 @@ namespace ET { public static class LSConstValue { - public const int MatchCount = 1; + public const int MatchCount = 2; public const int UpdateInterval = 50; public const int FrameCountPerSecond = 1000 / UpdateInterval; public const int SaveLSWorldFrameCount = 60 * FrameCountPerSecond; diff --git a/Unity/Assets/Scripts/Model/Share/LockStep/LSInputComponent.cs b/Unity/Assets/Scripts/Model/Share/LockStep/LSInputComponent.cs index b97b46f4166..197df6d5251 100644 --- a/Unity/Assets/Scripts/Model/Share/LockStep/LSInputComponent.cs +++ b/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; } } diff --git a/Unity/Assets/Scripts/Model/Share/LockStep/LSUnitComponent.cs b/Unity/Assets/Scripts/Model/Share/LockStep/LSUnitComponent.cs index 65dfc2129fe..0e87dc1da40 100644 --- a/Unity/Assets/Scripts/Model/Share/LockStep/LSUnitComponent.cs +++ b/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 { } } \ No newline at end of file diff --git a/Unity/Assets/Scripts/Model/Share/LockStep/LSWorld.cs b/Unity/Assets/Scripts/Model/Share/LockStep/LSWorld.cs index 6522c773f7b..5784dd3a5e1 100644 --- a/Unity/Assets/Scripts/Model/Share/LockStep/LSWorld.cs +++ b/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 @@ public static TSRandom GetRandom(this LSEntity entity) [EnableMethod] [ChildOf] [ComponentOf] - public class LSWorld: Entity, IAwake, IScene + [MemoryPackable] + public partial class LSWorld: Entity, IAwake, IScene { + [MemoryPackConstructor] public LSWorld() { } @@ -42,9 +45,11 @@ public LSWorld(SceneType sceneType) private readonly LSUpdater updater = new(); [BsonIgnore] + [MemoryPackIgnore] public Fiber Fiber { get; set; } [BsonElement] + [MemoryPackInclude] private long idGenerator; public long GetId() @@ -55,6 +60,7 @@ public long GetId() public TSRandom Random { get; set; } [BsonIgnore] + [MemoryPackIgnore] public SceneType SceneType { get; set; } public int Frame { get; set; } diff --git a/Unity/Assets/Scripts/ThirdParty/TrueSync/TSRandom.cs b/Unity/Assets/Scripts/ThirdParty/TrueSync/TSRandom.cs index cb3bb0a93ae..347cedea8d2 100644 --- a/Unity/Assets/Scripts/ThirdParty/TrueSync/TSRandom.cs +++ b/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 @@ public class TSRandom { 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() { } diff --git a/Unity/Assets/Scripts/ThirdParty/Unity.ThirdParty.asmdef b/Unity/Assets/Scripts/ThirdParty/Unity.ThirdParty.asmdef index adcab73668c..e4ffe2515ac 100644 --- a/Unity/Assets/Scripts/ThirdParty/Unity.ThirdParty.asmdef +++ b/Unity/Assets/Scripts/ThirdParty/Unity.ThirdParty.asmdef @@ -1,7 +1,9 @@ { "name": "Unity.ThirdParty", "rootNamespace": "ET", - "references": [], + "references": [ + "MemoryPack" + ], "includePlatforms": [], "excludePlatforms": [], "allowUnsafeCode": true,