Compare commits

...

12 Commits

Author SHA1 Message Date
edmand46 65c1d9c6d4 feat: added checks and new ragon serializer methods 2022-08-14 15:39:22 +04:00
edmand46 f2934bc8ee fix: static in snapshot 2022-08-14 12:36:12 +04:00
edmand46 81879d4fe2 fix: event replication 2022-08-14 11:31:56 +04:00
edmand46 2d89d267f5 Merge remote-tracking branch 'origin/main' 2022-08-14 11:17:59 +04:00
edmand46 34107cbd5f feat: added checking of owner 2022-08-14 11:17:22 +04:00
edmand46 ba2ce25aec chore: removed some logs 2022-08-14 11:14:11 +04:00
edmand46 ca17e7de83 Merge pull request #6 from edmand46/dev
feature: State -> Properties
2022-08-13 23:28:55 +04:00
edmand46 dff6dbcf97 feat: support static entity 2022-08-13 23:22:46 +04:00
edmand46 ae485f96d4 wip 2022-08-13 18:54:02 +04:00
edmand46 74904de16b Merge pull request #5 from edmand46/dev
Target of replication event
2022-07-31 19:13:36 +04:00
edmand46 8fd8558323 Merge pull request #4 from edmand46/dev
Remove NetStack dependency
2022-07-31 18:20:43 +04:00
edmand46 877ebdcde2 Merge pull request #3 from edmand46/dev
Replication level
2022-07-31 09:37:35 +04:00
10 changed files with 244 additions and 132 deletions
+2 -1
View File
@@ -20,9 +20,10 @@ namespace Ragon.Common
PLAYER_JOINED, PLAYER_JOINED,
PLAYER_LEAVED, PLAYER_LEAVED,
CREATE_STATES,
CREATE_ENTITY, CREATE_ENTITY,
CREATE_STATIC_ENTITY,
DESTROY_ENTITY, DESTROY_ENTITY,
CREATE_STATIC_ENTITY,
SNAPSHOT, SNAPSHOT,
+72 -3
View File
@@ -11,10 +11,15 @@ namespace Ragon.Common
{ {
[FieldOffset(0)] public int Int; [FieldOffset(0)] public int Int;
[FieldOffset(0)] public float Float; [FieldOffset(0)] public float Float;
[FieldOffset(0)] public long Long;
[FieldOffset(0)] public byte Byte0; [FieldOffset(0)] public byte Byte0;
[FieldOffset(1)] public byte Byte1; [FieldOffset(1)] public byte Byte1;
[FieldOffset(2)] public byte Byte2; [FieldOffset(2)] public byte Byte2;
[FieldOffset(3)] public byte Byte3; [FieldOffset(3)] public byte Byte3;
[FieldOffset(4)] public byte Byte4;
[FieldOffset(5)] public byte Byte5;
[FieldOffset(6)] public byte Byte6;
[FieldOffset(7)] public byte Byte7;
} }
public class RagonSerializer public class RagonSerializer
@@ -79,7 +84,7 @@ namespace Ragon.Common
public void WriteInt(int value) public void WriteInt(int value)
{ {
ResizeIfNeed(4); ResizeIfNeed(4);
var converter = new ValueConverter() { Int = value }; var converter = new ValueConverter() {Int = value};
_data[_offset] = converter.Byte0; _data[_offset] = converter.Byte0;
_data[_offset + 1] = converter.Byte1; _data[_offset + 1] = converter.Byte1;
_data[_offset + 2] = converter.Byte2; _data[_offset + 2] = converter.Byte2;
@@ -90,11 +95,51 @@ namespace Ragon.Common
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public int ReadInt() public int ReadInt()
{ {
var converter = new ValueConverter() { Byte0 = _data[_offset], Byte1 = _data[_offset + 1], Byte2 = _data[_offset + 2], Byte3 = _data[_offset + 3] }; var converter = new ValueConverter {Byte0 = _data[_offset], Byte1 = _data[_offset + 1], Byte2 = _data[_offset + 2], Byte3 = _data[_offset + 3]};
_offset += 4; _offset += 4;
return converter.Int; return converter.Int;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteLong(long value)
{
ResizeIfNeed(8);
WriteLong(value, _offset);
_offset += 8;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteLong(long value, int offset)
{
var converter = new ValueConverter() {Long = value};
_data[offset] = converter.Byte0;
_data[offset + 1] = converter.Byte1;
_data[offset + 2] = converter.Byte2;
_data[offset + 3] = converter.Byte3;
_data[offset + 4] = converter.Byte4;
_data[offset + 5] = converter.Byte5;
_data[offset + 6] = converter.Byte6;
_data[offset + 7] = converter.Byte7;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long ReadLong()
{
var converter = new ValueConverter
{
Byte0 = _data[_offset],
Byte1 = _data[_offset + 1],
Byte2 = _data[_offset + 2],
Byte3 = _data[_offset + 3],
Byte4 = _data[_offset + 4],
Byte5 = _data[_offset + 5],
Byte6 = _data[_offset + 6],
Byte7 = _data[_offset + 7],
};
_offset += 8;
return converter.Long;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteFloat(float value) public void WriteFloat(float value)
{ {
@@ -111,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)
{ {
@@ -228,7 +297,7 @@ namespace Ragon.Common
{ {
Clear(); Clear();
ResizeIfNeed(data.Length); ResizeIfNeed(data.Length);
Buffer.BlockCopy(data, 0, _data, 0, _offset); Buffer.BlockCopy(data, 0, _data, 0, data.Length);
_size = data.Length; _size = data.Length;
} }
+2
View File
@@ -1,4 +1,5 @@
using System; using System;
using System.Diagnostics;
using System.IO; using System.IO;
using NLog; using NLog;
@@ -16,5 +17,6 @@ namespace Ragon.Core
var app = new Application(factory, configuration); var app = new Application(factory, configuration);
return app; return app;
} }
} }
} }
+9 -9
View File
@@ -4,23 +4,23 @@ namespace Ragon.Core;
public class Entity public class Entity
{ {
private static int _idGenerator = 0; private static ushort _idGenerator = 0;
public int EntityId { get; private set; } public ushort EntityId { get; private set; }
public int StaticId { get; private set; } public ushort StaticId { get; private set; }
public uint OwnerId { get; private set; }
public ushort EntityType { get; private set; } public ushort EntityType { get; private set; }
public ushort OwnerId { get; private set; }
public RagonAuthority Authority { get; private set; } public RagonAuthority Authority { get; private set; }
public EntityState State { get; private set; } public EntityProperty[] Properties { get; private set; }
public EntityState Payload { get; private set; } public byte[] Payload { get; set; }
public Entity(uint ownerId, ushort entityType, int staticId, RagonAuthority stateAuthority, RagonAuthority eventAuthority) public Entity(ushort ownerId, ushort entityType, ushort staticId, RagonAuthority stateAuthority, RagonAuthority eventAuthority, int props)
{ {
OwnerId = ownerId; OwnerId = ownerId;
StaticId = staticId; StaticId = staticId;
EntityType = entityType; EntityType = entityType;
EntityId = _idGenerator++; EntityId = _idGenerator++;
State = new EntityState(stateAuthority); Properties = new EntityProperty[props];
Payload = new EntityState(stateAuthority); Payload = new byte[1024];
Authority = eventAuthority; Authority = eventAuthority;
} }
} }
+33
View File
@@ -0,0 +1,33 @@
using System;
namespace Ragon.Core;
public class EntityProperty
{
public int Size => _data.Length;
public bool IsDirty => _dirty;
private bool _dirty;
private byte[] _data;
public EntityProperty(int size)
{
_data = new byte[size];
}
public ReadOnlySpan<byte> Read()
{
return _data.AsSpan();
}
public void Write(ref ReadOnlySpan<byte> src)
{
src.CopyTo(_data);
_dirty = true;
}
public void Clear()
{
_dirty = false;
}
}
-38
View File
@@ -1,38 +0,0 @@
using System;
using System.Runtime.Serialization;
using Ragon.Common;
namespace Ragon.Core;
public class EntityState
{
public bool isDirty { get; private set; }
public RagonAuthority Authority { get; private set; }
public int Size => _size;
private int _size = 0;
private byte[] _data = new byte[2048];
public EntityState(RagonAuthority ragonAuthority)
{
Authority = ragonAuthority;
isDirty = false;
}
public ReadOnlySpan<byte> Read()
{
return _data.AsSpan().Slice(0, _size);
}
public void Write(ref ReadOnlySpan<byte> src)
{
src.CopyTo(_data);
_size = src.Length;
isDirty = true;
}
public void Clear()
{
isDirty = false;
}
}
+100 -55
View File
@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using NLog; using NLog;
using Ragon.Common; using Ragon.Common;
@@ -28,6 +29,8 @@ namespace Ragon.Core
private uint[] _readyPlayers = Array.Empty<uint>(); private uint[] _readyPlayers = Array.Empty<uint>();
private uint[] _allPlayers = Array.Empty<uint>(); private uint[] _allPlayers = Array.Empty<uint>();
private Entity[] _entitiesAll = Array.Empty<Entity>(); private Entity[] _entitiesAll = Array.Empty<Entity>();
private HashSet<Entity> _entitiesDirtySet = new HashSet<Entity>();
private List<Entity> _entitiesDirty = new List<Entity>();
private List<uint> _peersCache = new List<uint>(); private List<uint> _peersCache = new List<uint>();
public GameRoom(IGameThread gameThread, PluginBase pluginBase, string roomId, string map, int min, int max) public GameRoom(IGameThread gameThread, PluginBase pluginBase, string roomId, string map, int min, int max)
@@ -117,7 +120,7 @@ namespace Ragon.Core
} }
} }
public void ProcessEvent(uint peerId, RagonOperation operation, ReadOnlySpan<byte> payloadRawData) public void ProcessEvent(ushort peerId, RagonOperation operation, ReadOnlySpan<byte> payloadRawData)
{ {
_serializer.Clear(); _serializer.Clear();
_serializer.FromSpan(ref payloadRawData); _serializer.FromSpan(ref payloadRawData);
@@ -126,14 +129,27 @@ namespace Ragon.Core
{ {
case RagonOperation.REPLICATE_ENTITY_STATE: case RagonOperation.REPLICATE_ENTITY_STATE:
{ {
var entityId = _serializer.ReadInt(); var entityId = _serializer.ReadUShort();
if (_entities.TryGetValue(entityId, out var ent)) if (_entities.TryGetValue(entityId, out var ent))
{ {
if (ent.State.Authority == RagonAuthority.OWNER_ONLY && ent.OwnerId != peerId) if (ent.OwnerId != peerId)
{
_logger.Warn($"Not owner can't change properties of object {entityId}");
return; return;
}
var entityStateData = _serializer.ReadData(_serializer.Size); var mask = _serializer.ReadLong();
ent.State.Write(ref entityStateData); for (var i = 0; i < ent.Properties.Length; i++)
{
if (((mask >> i) & 1) == 1)
{
var propertyPayload = _serializer.ReadData(ent.Properties[i].Size);
ent.Properties[i].Write(ref propertyPayload);
}
}
if (_entitiesDirtySet.Add(ent))
_entitiesDirty.Add(ent);
} }
break; break;
@@ -143,7 +159,7 @@ namespace Ragon.Core
var evntId = _serializer.ReadUShort(); var evntId = _serializer.ReadUShort();
var evntMode = _serializer.ReadByte(); var evntMode = _serializer.ReadByte();
var targetMode = (RagonTarget) _serializer.ReadByte(); var targetMode = (RagonTarget) _serializer.ReadByte();
var entityId = _serializer.ReadInt(); var entityId = _serializer.ReadUShort();
if (!_entities.TryGetValue(entityId, out var ent)) if (!_entities.TryGetValue(entityId, out var ent))
return; return;
@@ -164,7 +180,7 @@ namespace Ragon.Core
_serializer.WriteUShort(evntId); _serializer.WriteUShort(evntId);
_serializer.WriteUShort((ushort) peerId); _serializer.WriteUShort((ushort) peerId);
_serializer.WriteByte(evntMode); _serializer.WriteByte(evntMode);
_serializer.WriteInt(entityId); _serializer.WriteUShort(entityId);
_serializer.WriteData(ref payload); _serializer.WriteData(ref payload);
var sendData = _serializer.ToArray(); var sendData = _serializer.ToArray();
@@ -191,6 +207,7 @@ namespace Ragon.Core
break; break;
} }
} }
break; break;
} }
case RagonOperation.LOAD_SCENE: case RagonOperation.LOAD_SCENE:
@@ -253,19 +270,30 @@ namespace Ragon.Core
break; break;
} }
} }
break; break;
} }
case RagonOperation.CREATE_STATIC_ENTITY: case RagonOperation.CREATE_STATIC_ENTITY:
{ {
var entityType = _serializer.ReadUShort(); var entityType = _serializer.ReadUShort();
var staticId = _serializer.ReadUShort(); var staticId = _serializer.ReadUShort();
var stateAuthority = (RagonAuthority) _serializer.ReadByte(); var propertiesCount = _serializer.ReadUShort();
var eventAuthority = (RagonAuthority) _serializer.ReadByte(); if (propertiesCount > 63)
var entity = new Entity(peerId, entityType, staticId, stateAuthority, eventAuthority); {
_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);
for (var i = 0; i < propertiesCount; i++)
{
var propertySize = _serializer.ReadUShort();
entity.Properties[i] = new EntityProperty(propertySize);
}
{ {
var entityPayload = _serializer.ReadData(_serializer.Size); var entityPayload = _serializer.ReadData(_serializer.Size);
entity.Payload.Write(ref entityPayload); entity.Payload = entityPayload.ToArray();
} }
var player = _players[peerId]; var player = _players[peerId];
@@ -282,14 +310,12 @@ namespace Ragon.Core
_serializer.Clear(); _serializer.Clear();
_serializer.WriteOperation(RagonOperation.CREATE_STATIC_ENTITY); _serializer.WriteOperation(RagonOperation.CREATE_STATIC_ENTITY);
_serializer.WriteUShort(entityType); _serializer.WriteUShort(entityType);
_serializer.WriteUShort(entity.EntityId);
_serializer.WriteUShort(staticId); _serializer.WriteUShort(staticId);
_serializer.WriteByte((byte) stateAuthority);
_serializer.WriteByte((byte) eventAuthority);
_serializer.WriteInt(entity.EntityId);
_serializer.WriteUShort(ownerId); _serializer.WriteUShort(ownerId);
{ {
var entityPayload = entity.Payload.Read(); ReadOnlySpan<byte> entityPayload = entity.Payload.AsSpan();
_serializer.WriteData(ref entityPayload); _serializer.WriteData(ref entityPayload);
} }
@@ -300,13 +326,23 @@ namespace Ragon.Core
case RagonOperation.CREATE_ENTITY: case RagonOperation.CREATE_ENTITY:
{ {
var entityType = _serializer.ReadUShort(); var entityType = _serializer.ReadUShort();
var stateAuthority = (RagonAuthority) _serializer.ReadByte(); var propertiesCount = _serializer.ReadUShort();
var eventAuthority = (RagonAuthority) _serializer.ReadByte(); var entity = new Entity(peerId, entityType, 0, RagonAuthority.ALL, RagonAuthority.ALL, propertiesCount);
var entity = new Entity(peerId, entityType, -1, stateAuthority, eventAuthority); if (propertiesCount > 63)
{
_logger.Warn($"Allowed only 64 properties per entity. EntityType(Static) {entityType}");
return;
}
for (var i = 0; i < propertiesCount; i++)
{
var propertySize = _serializer.ReadUShort();
entity.Properties[i] = new EntityProperty(propertySize);
}
{ {
var entityPayload = _serializer.ReadData(_serializer.Size); var entityPayload = _serializer.ReadData(_serializer.Size);
entity.Payload.Write(ref entityPayload); entity.Payload = entityPayload.ToArray();
} }
var player = _players[peerId]; var player = _players[peerId];
@@ -323,13 +359,11 @@ namespace Ragon.Core
_serializer.Clear(); _serializer.Clear();
_serializer.WriteOperation(RagonOperation.CREATE_ENTITY); _serializer.WriteOperation(RagonOperation.CREATE_ENTITY);
_serializer.WriteUShort(entityType); _serializer.WriteUShort(entityType);
_serializer.WriteByte((byte) stateAuthority); _serializer.WriteUShort(entity.EntityId);
_serializer.WriteByte((byte) eventAuthority);
_serializer.WriteInt(entity.EntityId);
_serializer.WriteUShort(ownerId); _serializer.WriteUShort(ownerId);
{ {
var entityPayload = entity.Payload.Read(); ReadOnlySpan<byte> entityPayload = entity.Payload.AsSpan();
_serializer.WriteData(ref entityPayload); _serializer.WriteData(ref entityPayload);
} }
@@ -372,7 +406,7 @@ namespace Ragon.Core
_serializer.Clear(); _serializer.Clear();
_serializer.WriteOperation(RagonOperation.SNAPSHOT); _serializer.WriteOperation(RagonOperation.SNAPSHOT);
_serializer.WriteInt(_allPlayers.Length); _serializer.WriteUShort((ushort) _allPlayers.Length);
foreach (var playerPeerId in _allPlayers) foreach (var playerPeerId in _allPlayers)
{ {
_serializer.WriteString(_players[playerPeerId].Id); _serializer.WriteString(_players[playerPeerId].Id);
@@ -380,41 +414,31 @@ namespace Ragon.Core
_serializer.WriteString(_players[playerPeerId].PlayerName); _serializer.WriteString(_players[playerPeerId].PlayerName);
} }
var dynamicCount = _entitiesAll.Where(e => e.StaticId == -1).ToArray(); var dynamicEntities = _entitiesAll.Where(e => e.StaticId == 0).ToArray();
_serializer.WriteInt(dynamicCount.Length); _serializer.WriteUShort((ushort) dynamicEntities.Length);
foreach (var entity in dynamicCount) foreach (var entity in dynamicEntities)
{ {
if (entity.StaticId != -1) continue; ReadOnlySpan<byte> payload = entity.Payload.AsSpan();
var payload = entity.Payload.Read();
var state = entity.State.Read();
_serializer.WriteInt(entity.EntityId);
_serializer.WriteByte((byte) entity.State.Authority);
_serializer.WriteByte((byte) entity.Authority);
_serializer.WriteUShort(entity.EntityType); _serializer.WriteUShort(entity.EntityType);
_serializer.WriteUShort(entity.EntityId);
_serializer.WriteUShort((ushort) entity.OwnerId); _serializer.WriteUShort((ushort) entity.OwnerId);
_serializer.WriteUShort((ushort) payload.Length); _serializer.WriteUShort((ushort) payload.Length);
_serializer.WriteData(ref payload); _serializer.WriteData(ref payload);
_serializer.WriteData(ref state);
} }
var staticCount = _entitiesAll.Where(e => e.StaticId != -1).ToArray(); var staticCount = _entitiesAll.Where(e => e.StaticId != 0).ToArray();
_serializer.WriteInt(staticCount.Length); _serializer.WriteUShort((ushort) staticCount.Length);
foreach (var entity in staticCount) foreach (var entity in staticCount)
{ {
var payload = entity.Payload.Read(); ReadOnlySpan<byte> payload = entity.Payload.AsSpan();
var state = entity.State.Read();
_serializer.WriteInt(entity.EntityId);
_serializer.WriteUShort((ushort) entity.StaticId);
_serializer.WriteByte((byte) entity.State.Authority);
_serializer.WriteByte((byte) entity.Authority);
_serializer.WriteUShort(entity.EntityType); _serializer.WriteUShort(entity.EntityType);
_serializer.WriteUShort((ushort) entity.OwnerId); _serializer.WriteUShort(entity.EntityId);
_serializer.WriteUShort(entity.StaticId);
_serializer.WriteUShort(entity.OwnerId);
_serializer.WriteUShort((ushort) payload.Length); _serializer.WriteUShort((ushort) payload.Length);
_serializer.WriteData(ref payload); _serializer.WriteData(ref payload);
_serializer.WriteData(ref state);
} }
var sendData = _serializer.ToArray(); var sendData = _serializer.ToArray();
@@ -432,22 +456,43 @@ namespace Ragon.Core
{ {
_scheduler.Tick(deltaTime); _scheduler.Tick(deltaTime);
foreach (var entity in _entitiesAll) if (_entitiesDirty.Count > 0)
{ {
if (entity.State.isDirty) _serializer.Clear();
_serializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE);
_serializer.WriteUShort((ushort) _entitiesDirty.Count);
for (var entityIndex = 0; entityIndex < _entitiesDirty.Count; entityIndex++)
{ {
var state = entity.State.Read(); var entity = _entitiesDirty[entityIndex];
var mask = 0L;
_serializer.Clear(); _serializer.WriteUShort(entity.EntityId);
_serializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE);
_serializer.WriteInt(entity.EntityId);
_serializer.WriteData(ref state);
var sendData = _serializer.ToArray(); var offset = _serializer.Lenght;
Broadcast(_readyPlayers, sendData, DeliveryType.Unreliable); _serializer.WriteLong(mask);
entity.State.Clear(); for (int propertyIndex = 0; propertyIndex < entity.Properties.Length; propertyIndex++)
{
var property = entity.Properties[propertyIndex];
if (property.IsDirty)
{
mask |= (uint) (1 << propertyIndex);
var span = _serializer.GetWritableData(property.Size);
var data = property.Read();
data.CopyTo(span);
property.Clear();
}
}
_serializer.WriteLong(mask, offset);
} }
_entitiesDirty.Clear();
_entitiesDirtySet.Clear();
var sendData = _serializer.ToArray();
Broadcast(_readyPlayers, sendData, DeliveryType.Unreliable);
} }
} }
+1 -1
View File
@@ -104,7 +104,7 @@ namespace Ragon.Core
{ {
try try
{ {
var peerId = evnt.Peer.ID; var peerId = (ushort) evnt.Peer.ID;
var dataRaw = new byte[evnt.Packet.Length]; var dataRaw = new byte[evnt.Packet.Length];
evnt.Packet.CopyTo(dataRaw); evnt.Packet.CopyTo(dataRaw);
+1 -1
View File
@@ -23,7 +23,7 @@ public class Lobby : ILobby
_authorizationManager = new AuthorizationManager(provider, gameThread, this, _serializer); _authorizationManager = new AuthorizationManager(provider, gameThread, this, _serializer);
} }
public void ProcessEvent(uint peerId, RagonOperation op, ReadOnlySpan<byte> payload) public void ProcessEvent(ushort peerId, RagonOperation op, ReadOnlySpan<byte> payload)
{ {
_serializer.Clear(); _serializer.Clear();
_serializer.FromSpan(ref payload); _serializer.FromSpan(ref payload);