Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions LiteEntitySystem/ClientEntityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,11 @@ private unsafe void GoToNextState()
if (field.FieldType == FieldType.SyncableSyncVar)
{
var syncableField = RefMagic.RefFieldValue<SyncableField>(entity, field.Offset);
field.TypeProcessor.SetFrom(syncableField, field.SyncableSyncVarOffset, predictedData + field.PredictedOffset);
field.TypeProcessor.SetFrom(syncableField, field.SyncableSyncVarOffset, predictedData + field.PredictedOffset, field.Name);
}
else
{
field.TypeProcessor.SetFrom(entity, field.Offset, predictedData + field.PredictedOffset);
field.TypeProcessor.SetFrom(entity, field.Offset, predictedData + field.PredictedOffset, field.Name);
}
}
}
Expand Down Expand Up @@ -649,7 +649,7 @@ protected override unsafe void OnLogicTick()
for(int i = 0; i < classData.InterpolatedCount; i++)
{
var field = classData.Fields[i];
field.TypeProcessor.SetFrom(entity, field.Offset, currentDataPtr + field.FixedOffset);
field.TypeProcessor.SetFrom(entity, field.Offset, currentDataPtr + field.FixedOffset, field.Name);
}

//update
Expand Down Expand Up @@ -1002,7 +1002,7 @@ private unsafe bool ReadEntityState(byte* rawData, bool fistSync)
if (field.FieldType == FieldType.SyncableSyncVar)
{
var syncableField = RefMagic.RefFieldValue<SyncableField>(entity, field.Offset);
field.TypeProcessor.SetFrom(syncableField, field.SyncableSyncVarOffset, readDataPtr);
field.TypeProcessor.SetFrom(syncableField, field.SyncableSyncVarOffset, readDataPtr, field.Name);
}
else
{
Expand All @@ -1023,7 +1023,7 @@ private unsafe bool ReadEntityState(byte* rawData, bool fistSync)
}
else
{
field.TypeProcessor.SetFrom(entity, field.Offset, readDataPtr);
field.TypeProcessor.SetFrom(entity, field.Offset, readDataPtr, field.Name);
}
}
//Logger.Log($"E {entity.Id} Field updated: {field.Name}");
Expand Down
38 changes: 29 additions & 9 deletions LiteEntitySystem/Extensions/SyncNetSerializable.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using System;
using System;
using System.Collections.Generic;
using K4os.Compression.LZ4;
using LiteNetLib.Utils;

namespace LiteEntitySystem.Extensions
{
public class SyncNetSerializable<T> : SyncableField where T : INetSerializable
public class SyncNetSerializable<T> : SyncableField, ISyncFieldChanged<T> where T : INetSerializable
{
private static readonly NetDataWriter WriterCache = new();
private static readonly NetDataReader ReaderCache = new();
private static byte[] CompressionBuffer;

private T _value;

public event Action<T, T> ValueChanged;

public T Value
{
get => _value;
Expand All @@ -30,7 +33,7 @@ public SyncNetSerializable(Func<T> constructor)
{
_constructor = constructor;
}

protected internal override void RegisterRPC(ref SyncableRPCRegistrator r)
{
r.CreateClientAction(this, Init, ref _initAction);
Expand All @@ -45,8 +48,9 @@ protected internal override void OnSyncRequested()
Logger.LogError("Too much sync data!");
return;
}

int bufSize = LZ4Codec.MaximumOutputSize(WriterCache.Length) + 2;
if(CompressionBuffer == null || CompressionBuffer.Length < bufSize)
if (CompressionBuffer == null || CompressionBuffer.Length < bufSize)
CompressionBuffer = new byte[bufSize];
FastBitConverter.GetBytes(CompressionBuffer, 0, (ushort)WriterCache.Length);
int encodedLength = LZ4Codec.Encode(
Expand All @@ -55,20 +59,36 @@ protected internal override void OnSyncRequested()
WriterCache.Length,
CompressionBuffer,
2,
CompressionBuffer.Length-2,
CompressionBuffer.Length - 2,
LZ4Level.L00_FAST);
ExecuteRPC(_initAction, new ReadOnlySpan<byte>(CompressionBuffer, 0, encodedLength+2));
ExecuteRPC(_initAction, new ReadOnlySpan<byte>(CompressionBuffer, 0, encodedLength + 2));
}

private void Init(ReadOnlySpan<byte> data)
{
// Read uncompressed size
ushort origSize = BitConverter.ToUInt16(data);

if (CompressionBuffer == null || CompressionBuffer.Length < origSize)
CompressionBuffer = new byte[origSize];
LZ4Codec.Decode(data[2..], new Span<byte>(CompressionBuffer));
ReaderCache.SetSource(CompressionBuffer, 0, origSize);
_value ??= _constructor();
_value.Deserialize(ReaderCache);

// Capture the old reference
T oldValue = _value;

// Always create a fresh instance for deserialization
T newValue = _constructor();
newValue.Deserialize(ReaderCache);

// Update _value
_value = newValue;

// Compare old and new. If changed, raise event.
if (oldValue == null || !EqualityComparer<T>.Default.Equals(oldValue, _value))
{
ValueChanged?.Invoke(oldValue, newValue);
}
}

public static implicit operator T(SyncNetSerializable<T> field)
Expand Down
9 changes: 7 additions & 2 deletions LiteEntitySystem/Extensions/SyncString.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Runtime.CompilerServices;
using System.Text;
using LiteEntitySystem.Internal;

namespace LiteEntitySystem.Extensions
{
public class SyncString : SyncableField
public class SyncString : SyncableField, ISyncFieldChanged<string>
{
private static readonly UTF8Encoding Encoding = new(false, true);
private byte[] _stringData;
Expand All @@ -12,6 +14,7 @@ public class SyncString : SyncableField

private static RemoteCallSpan<byte> _setStringClientCall;

public event Action<string, string> ValueChanged;
public string Value
{
get => _string;
Expand Down Expand Up @@ -43,7 +46,9 @@ public static implicit operator string(SyncString s)

private void SetNewString(ReadOnlySpan<byte> data)
{
_string = Encoding.GetString(data);
var newString = Encoding.GetString(data);
ValueChanged?.Invoke(_string, newString);
_string = newString;
}

protected internal override void OnSyncRequested()
Expand Down
Loading