independent buffers for ragon properties, extended buffer functionality

This commit is contained in:
2023-05-07 23:54:07 +03:00
parent ecdafeab00
commit 91d8516ac9
3 changed files with 54 additions and 18 deletions
@@ -53,7 +53,7 @@ public sealed class RagonEntityState
{ {
var changed = buffer.ReadBool(); var changed = buffer.ReadBool();
if (changed) if (changed)
property.Deserialize(buffer); property.Read(buffer);
} }
} }
+36 -15
View File
@@ -29,22 +29,25 @@ namespace Ragon.Client
public bool IsFixed => _fixed; public bool IsFixed => _fixed;
public int Size => _size; public int Size => _size;
private bool _fixed;
private string _name;
protected bool _invokeLocal;
private RagonEntity _entity; private RagonEntity _entity;
private RagonBuffer _propertyBuffer;
private bool _dirty; private bool _dirty;
private int _size; private int _size;
private int _ticks; private int _ticks;
private int _priority; private int _priority;
private bool _fixed;
private string _name;
protected bool InvokeLocal;
protected RagonProperty(int priority, bool invokeLocal) protected RagonProperty(int priority, bool invokeLocal)
{ {
_size = 0; _size = 0;
_priority = priority; _priority = priority;
_fixed = false; _fixed = false;
_invokeLocal = invokeLocal; _propertyBuffer = new RagonBuffer();
InvokeLocal = invokeLocal;
} }
public void SetName(string name) public void SetName(string name)
@@ -60,7 +63,7 @@ namespace Ragon.Client
protected void InvokeChanged() protected void InvokeChanged()
{ {
if (!_invokeLocal) if (!InvokeLocal)
return; return;
Changed?.Invoke(); Changed?.Invoke();
@@ -97,20 +100,38 @@ namespace Ragon.Client
internal void Write(RagonBuffer buffer) internal void Write(RagonBuffer buffer)
{ {
_propertyBuffer.Clear();
if (_fixed) 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; return;
} }
var sizeOffset = buffer.WriteOffset; var propSize = buffer.ReadUShort();
buffer.Write(0, 16); buffer.ToBuffer(_propertyBuffer, propSize);
var propOffset = buffer.WriteOffset; Deserialize(_propertyBuffer);
Serialize(buffer);
var propSize = (uint)(buffer.WriteOffset - propOffset);
buffer.Write(propSize, 16, sizeOffset);
} }
public virtual void Serialize(RagonBuffer buffer) public virtual void Serialize(RagonBuffer buffer)
+15
View File
@@ -321,6 +321,7 @@ namespace Ragon.Protocol
_read += size; _read += size;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteSpan(ref ReadOnlySpan<uint> data, int size) public void WriteSpan(ref ReadOnlySpan<uint> data, int size)
{ {
var used = _write & 0x0000001F; var used = _write & 0x0000001F;
@@ -352,6 +353,20 @@ namespace Ragon.Protocol
_write = 0; _write = 0;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromBuffer(RagonBuffer buffer, int size)
{
ReadOnlySpan<uint> 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) public void FromArray(byte[] data)
{ {
var length = data.Length; var length = data.Length;