немного изменена логика работы TransmittedSharedObject, получается теперь, работает даже быстрее

This commit is contained in:
2025-02-12 15:47:54 +03:00
parent 5032fa28e8
commit fc35c92efe
2 changed files with 17 additions and 7 deletions
@@ -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>(T dependency)
+16 -4
View File
@@ -14,21 +14,33 @@ public class TransmittedSharedObject<T> 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<T>(_contextId)!;
}
}
[JsonIgnore]
public T? Value => GetDefaultContextRepository().GetObject<T>(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<T> value) => value.Value!;
public static implicit operator T(TransmittedSharedObject<T> value) => value.Value;
public static implicit operator TransmittedSharedObject<T>(T value) =>
new() { ContextId = GetDefaultContextRepository().GetObjectIndex(value) };