From 91d8516ac9bf9e08501a05dcd160ddcd90e09795 Mon Sep 17 00:00:00 2001 From: edmand46 Date: Sun, 7 May 2023 23:54:07 +0300 Subject: [PATCH] :sparkles: independent buffers for ragon properties, extended buffer functionality --- .../Sources/Entity/RagonEntityState.cs | 2 +- Ragon.Client/Sources/Entity/RagonProperty.cs | 51 +++++++++++++------ Ragon.Protocol/Sources/RagonBuffer.cs | 19 ++++++- 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/Ragon.Client/Sources/Entity/RagonEntityState.cs b/Ragon.Client/Sources/Entity/RagonEntityState.cs index f70099a..87afa9e 100644 --- a/Ragon.Client/Sources/Entity/RagonEntityState.cs +++ b/Ragon.Client/Sources/Entity/RagonEntityState.cs @@ -53,7 +53,7 @@ public sealed class RagonEntityState { var changed = buffer.ReadBool(); if (changed) - property.Deserialize(buffer); + property.Read(buffer); } } diff --git a/Ragon.Client/Sources/Entity/RagonProperty.cs b/Ragon.Client/Sources/Entity/RagonProperty.cs index e7b5b40..8669482 100644 --- a/Ragon.Client/Sources/Entity/RagonProperty.cs +++ b/Ragon.Client/Sources/Entity/RagonProperty.cs @@ -29,22 +29,25 @@ namespace Ragon.Client public bool IsFixed => _fixed; public int Size => _size; - private bool _fixed; - private string _name; - protected bool _invokeLocal; - private RagonEntity _entity; + private RagonBuffer _propertyBuffer; private bool _dirty; private int _size; private int _ticks; private int _priority; + private bool _fixed; + private string _name; + + protected bool InvokeLocal; protected RagonProperty(int priority, bool invokeLocal) { _size = 0; _priority = priority; _fixed = false; - _invokeLocal = invokeLocal; + _propertyBuffer = new RagonBuffer(); + + InvokeLocal = invokeLocal; } public void SetName(string name) @@ -60,7 +63,7 @@ namespace Ragon.Client protected void InvokeChanged() { - if (!_invokeLocal) + if (!InvokeLocal) return; Changed?.Invoke(); @@ -97,20 +100,38 @@ namespace Ragon.Client internal void Write(RagonBuffer buffer) { + _propertyBuffer.Clear(); + if (_fixed) { - Serialize(buffer); + Serialize(_propertyBuffer); + + buffer.FromBuffer(_propertyBuffer, _size); + } + else + { + Serialize(_propertyBuffer); + + var propSize = _propertyBuffer.WriteOffset; + buffer.WriteUShort((ushort) propSize); + buffer.FromBuffer(_propertyBuffer, propSize); + } + } + + internal void Read(RagonBuffer buffer) + { + _propertyBuffer.Clear(); + + if (_fixed) + { + buffer.ToBuffer(_propertyBuffer, _size); + Deserialize(_propertyBuffer); return; } - var sizeOffset = buffer.WriteOffset; - buffer.Write(0, 16); - var propOffset = buffer.WriteOffset; - - Serialize(buffer); - - var propSize = (uint)(buffer.WriteOffset - propOffset); - buffer.Write(propSize, 16, sizeOffset); + var propSize = buffer.ReadUShort(); + buffer.ToBuffer(_propertyBuffer, propSize); + Deserialize(_propertyBuffer); } public virtual void Serialize(RagonBuffer buffer) diff --git a/Ragon.Protocol/Sources/RagonBuffer.cs b/Ragon.Protocol/Sources/RagonBuffer.cs index 24e04b8..27aabb6 100644 --- a/Ragon.Protocol/Sources/RagonBuffer.cs +++ b/Ragon.Protocol/Sources/RagonBuffer.cs @@ -290,7 +290,7 @@ namespace Ragon.Protocol return data; } - + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void ReadSpan(ref Span data, int size) { @@ -320,7 +320,8 @@ namespace Ragon.Protocol _read += size; } - + + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteSpan(ref ReadOnlySpan data, int size) { var used = _write & 0x0000001F; @@ -352,6 +353,20 @@ namespace Ragon.Protocol _write = 0; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromBuffer(RagonBuffer buffer, int size) + { + ReadOnlySpan data = buffer._buckets.AsSpan(); + WriteSpan(ref data, size); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void ToBuffer(RagonBuffer buffer, int size) + { + var data = buffer._buckets.AsSpan(); + ReadSpan(ref data, size); + } + public void FromArray(byte[] data) { var length = data.Length;