Files
Ragon/Ragon.Server/Sources/Data/RagonData.cs
T

76 lines
1.6 KiB
C#
Raw Normal View History

2024-04-14 19:07:48 +03:00
using Ragon.Protocol;
namespace Ragon.Server.Data;
public class RagonData
{
private readonly Dictionary<string, byte[]> _data = new Dictionary<string, byte[]>();
public bool IsDirty { get; private set; }
public RagonData()
{
}
2025-10-04 15:08:53 +03:00
public void Read(RagonBuffer buffer, int maxSize = 0)
2024-04-14 19:07:48 +03:00
{
var len = buffer.ReadUShort();
2025-10-04 15:08:53 +03:00
var totalSize = 0;
for (int i = 0; i < len; i++)
2024-04-14 19:07:48 +03:00
{
var key = buffer.ReadString();
var valueSize = buffer.ReadUShort();
2025-10-04 15:08:53 +03:00
if (maxSize > 0)
{
totalSize += valueSize;
if (totalSize > maxSize)
{
throw new InvalidOperationException($"User data exceeds limit: {totalSize} > {maxSize}");
}
}
if (valueSize > 0)
{
var value = buffer.ReadBytes(valueSize);
_data[key] = value;
}
else
{
_data[key] = Array.Empty<byte>();
}
2024-04-14 19:07:48 +03:00
}
}
public void Write(RagonBuffer buffer)
2024-04-14 19:07:48 +03:00
{
buffer.WriteUShort((ushort)_data.Count);
foreach (var prop in _data)
{
buffer.WriteString(prop.Key);
buffer.WriteUShort((ushort)prop.Value.Length);
buffer.WriteBytes(prop.Value);
}
var toDelete = _data
.Where(p => p.Value.Length == 0)
.Select(p => p.Key);
foreach (var prop in toDelete)
_data.Remove(prop);
IsDirty = false;
}
public void Snapshot(RagonBuffer buffer)
{
buffer.WriteUShort((ushort)_data.Count);
foreach (var prop in _data)
{
buffer.WriteString(prop.Key);
buffer.WriteUShort((ushort)prop.Value.Length);
buffer.WriteBytes(prop.Value);
}
2024-04-14 19:07:48 +03:00
}
}