feat: added checks and new ragon serializer methods

This commit is contained in:
2022-08-14 15:39:22 +04:00
parent f2934bc8ee
commit 65c1d9c6d4
2 changed files with 36 additions and 0 deletions
+24
View File
@@ -156,6 +156,30 @@ namespace Ragon.Common
return value; return value;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteString(string value, ushort size)
{
var stringRaw = Encoding.UTF8.GetBytes(value).AsSpan();
ResizeIfNeed(2 + size);
WriteUShort((ushort) stringRaw.Length);
var data = _data.AsSpan().Slice(_offset, size);
stringRaw.CopyTo(data);
_offset += stringRaw.Length;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadString(ushort size)
{
var lenght = ReadUShort();
var stringRaw = _data.AsSpan().Slice(_offset, size);
var strData = stringRaw.Slice(0, lenght);
var str = Encoding.UTF8.GetString(strData);
_offset += size;
return str;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteString(string value) public void WriteString(string value)
{ {
+12
View File
@@ -278,6 +278,12 @@ namespace Ragon.Core
var entityType = _serializer.ReadUShort(); var entityType = _serializer.ReadUShort();
var staticId = _serializer.ReadUShort(); var staticId = _serializer.ReadUShort();
var propertiesCount = _serializer.ReadUShort(); var propertiesCount = _serializer.ReadUShort();
if (propertiesCount > 63)
{
_logger.Warn($"Allowed only 64 properties per entity. EntityType(Static) {entityType}");
return;
}
var entity = new Entity(peerId, entityType, staticId, RagonAuthority.ALL, RagonAuthority.OWNER_ONLY, propertiesCount); var entity = new Entity(peerId, entityType, staticId, RagonAuthority.ALL, RagonAuthority.OWNER_ONLY, propertiesCount);
for (var i = 0; i < propertiesCount; i++) for (var i = 0; i < propertiesCount; i++)
{ {
@@ -322,6 +328,12 @@ namespace Ragon.Core
var entityType = _serializer.ReadUShort(); var entityType = _serializer.ReadUShort();
var propertiesCount = _serializer.ReadUShort(); var propertiesCount = _serializer.ReadUShort();
var entity = new Entity(peerId, entityType, 0, RagonAuthority.ALL, RagonAuthority.ALL, propertiesCount); var entity = new Entity(peerId, entityType, 0, RagonAuthority.ALL, RagonAuthority.ALL, propertiesCount);
if (propertiesCount > 63)
{
_logger.Warn($"Allowed only 64 properties per entity. EntityType(Static) {entityType}");
return;
}
for (var i = 0; i < propertiesCount; i++) for (var i = 0; i < propertiesCount; i++)
{ {
var propertySize = _serializer.ReadUShort(); var propertySize = _serializer.ReadUShort();