From fc35c92efe69e36e7c087a389af772d9dd08588c Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Wed, 12 Feb 2025 15:47:54 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B5=D0=BC=D0=BD=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B0=20=D0=BB=D0=BE?= =?UTF-8?q?=D0=B3=D0=B8=D0=BA=D0=B0=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B?= =?UTF-8?q?=20TransmittedSharedObject,=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87?= =?UTF-8?q?=D0=B0=D0=B5=D1=82=D1=81=D1=8F=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D1=8C,=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82=20?= =?UTF-8?q?=D0=B4=D0=B0=D0=B6=D0=B5=20=D0=B1=D1=8B=D1=81=D1=82=D1=80=D0=B5?= =?UTF-8?q?=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Backend/JsonSerialisationModule.cs | 4 +--- .../Implementation/TransmittedSharedObject.cs | 20 +++++++++++++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/mROA/Implementation/Backend/JsonSerialisationModule.cs b/mROA/Implementation/Backend/JsonSerialisationModule.cs index 731b971..9779539 100644 --- a/mROA/Implementation/Backend/JsonSerialisationModule.cs +++ b/mROA/Implementation/Backend/JsonSerialisationModule.cs @@ -41,9 +41,7 @@ public class JsonSerialisationModule : ISerialisationModule public void PostResponse(NetworkMessage message, int clientId) { - var texted = JsonSerializer.Serialize(message); - var binary = Encoding.UTF8.GetBytes(texted); - _dataSource!.SendTo(clientId, binary); + _dataSource!.SendTo(clientId, JsonSerializer.SerializeToUtf8Bytes(message)); } public void Inject(T dependency) diff --git a/mROA/Implementation/TransmittedSharedObject.cs b/mROA/Implementation/TransmittedSharedObject.cs index 6b2228c..f64f79c 100644 --- a/mROA/Implementation/TransmittedSharedObject.cs +++ b/mROA/Implementation/TransmittedSharedObject.cs @@ -14,21 +14,33 @@ public class TransmittedSharedObject where T : notnull throw new NullReferenceException( "DefaultContextRepository was not defined"); + private int _contextId = -1; + // ReSharper disable once MemberCanBePrivate.Global - public int ContextId { get; init; } = -1; + public int ContextId + { + get => _contextId; + init + { + _contextId = value; + Value = GetDefaultContextRepository().GetObject(_contextId)!; + } + } [JsonIgnore] - public T? Value => GetDefaultContextRepository().GetObject(ContextId); + public T Value { get; private set; } public TransmittedSharedObject() { } + // ReSharper disable once UnusedMember.Global public TransmittedSharedObject(T value) { - ContextId = GetDefaultContextRepository().GetObjectIndex(value); + Value = value; + _contextId = GetDefaultContextRepository().GetObjectIndex(value); } - public static implicit operator T(TransmittedSharedObject value) => value.Value!; + public static implicit operator T(TransmittedSharedObject value) => value.Value; public static implicit operator TransmittedSharedObject(T value) => new() { ContextId = GetDefaultContextRepository().GetObjectIndex(value) };