Merge pull request #6 from edmand46/dev

feature: State -> Properties
This commit is contained in:
2022-08-13 23:28:55 +04:00
committed by GitHub
8 changed files with 223 additions and 143 deletions
+2 -1
View File
@@ -20,9 +20,10 @@ namespace Ragon.Common
PLAYER_JOINED,
PLAYER_LEAVED,
CREATE_STATES,
CREATE_ENTITY,
CREATE_STATIC_ENTITY,
DESTROY_ENTITY,
CREATE_STATIC_ENTITY,
SNAPSHOT,
+47 -2
View File
@@ -11,10 +11,15 @@ namespace Ragon.Common
{
[FieldOffset(0)] public int Int;
[FieldOffset(0)] public float Float;
[FieldOffset(0)] public long Long;
[FieldOffset(0)] public byte Byte0;
[FieldOffset(1)] public byte Byte1;
[FieldOffset(2)] public byte Byte2;
[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
@@ -90,11 +95,51 @@ namespace Ragon.Common
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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;
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)]
public void WriteFloat(float value)
{
@@ -228,7 +273,7 @@ namespace Ragon.Common
{
Clear();
ResizeIfNeed(data.Length);
Buffer.BlockCopy(data, 0, _data, 0, _offset);
Buffer.BlockCopy(data, 0, _data, 0, data.Length);
_size = data.Length;
}
+2
View File
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.IO;
using NLog;
@@ -16,5 +17,6 @@ namespace Ragon.Core
var app = new Application(factory, configuration);
return app;
}
}
}
+9 -9
View File
@@ -4,23 +4,23 @@ namespace Ragon.Core;
public class Entity
{
private static int _idGenerator = 0;
public int EntityId { get; private set; }
public int StaticId { get; private set; }
public uint OwnerId { get; private set; }
private static ushort _idGenerator = 0;
public ushort EntityId { get; private set; }
public ushort StaticId { get; private set; }
public ushort EntityType { get; private set; }
public uint OwnerId { get; private set; }
public RagonAuthority Authority { get; private set; }
public EntityState State { get; private set; }
public EntityState Payload { get; private set; }
public EntityProperty[] Properties { get; private set; }
public byte[] Payload { get; set; }
public Entity(uint ownerId, ushort entityType, int staticId, RagonAuthority stateAuthority, RagonAuthority eventAuthority)
public Entity(uint ownerId, ushort entityType, ushort staticId, RagonAuthority stateAuthority, RagonAuthority eventAuthority, int props)
{
OwnerId = ownerId;
StaticId = staticId;
EntityType = entityType;
EntityId = _idGenerator++;
State = new EntityState(stateAuthority);
Payload = new EntityState(stateAuthority);
Properties = new EntityProperty[props];
Payload = new byte[1024];
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;
}
}
+89 -52
View File
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using NLog;
using Ragon.Common;
@@ -28,6 +29,8 @@ namespace Ragon.Core
private uint[] _readyPlayers = Array.Empty<uint>();
private uint[] _allPlayers = Array.Empty<uint>();
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>();
public GameRoom(IGameThread gameThread, PluginBase pluginBase, string roomId, string map, int min, int max)
@@ -122,18 +125,31 @@ namespace Ragon.Core
_serializer.Clear();
_serializer.FromSpan(ref payloadRawData);
if (operation != RagonOperation.REPLICATE_ENTITY_STATE)
{
_logger.Trace(operation);
}
switch (operation)
{
case RagonOperation.REPLICATE_ENTITY_STATE:
{
var entityId = _serializer.ReadInt();
var entityId = _serializer.ReadUShort();
if (_entities.TryGetValue(entityId, out var ent))
{
if (ent.State.Authority == RagonAuthority.OWNER_ONLY && ent.OwnerId != peerId)
return;
var entityStateData = _serializer.ReadData(_serializer.Size);
ent.State.Write(ref entityStateData);
var mask = _serializer.ReadLong();
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;
@@ -259,13 +275,18 @@ namespace Ragon.Core
{
var entityType = _serializer.ReadUShort();
var staticId = _serializer.ReadUShort();
var stateAuthority = (RagonAuthority) _serializer.ReadByte();
var eventAuthority = (RagonAuthority) _serializer.ReadByte();
var entity = new Entity(peerId, entityType, staticId, stateAuthority, eventAuthority);
var propertiesCount = _serializer.ReadUShort();
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);
_logger.Trace($"Static Property: {i} {propertySize}");
}
{
var entityPayload = _serializer.ReadData(_serializer.Size);
entity.Payload.Write(ref entityPayload);
entity.Payload = entityPayload.ToArray();
}
var player = _players[peerId];
@@ -282,14 +303,12 @@ namespace Ragon.Core
_serializer.Clear();
_serializer.WriteOperation(RagonOperation.CREATE_STATIC_ENTITY);
_serializer.WriteUShort(entityType);
_serializer.WriteUShort(entity.EntityId);
_serializer.WriteUShort(staticId);
_serializer.WriteByte((byte) stateAuthority);
_serializer.WriteByte((byte) eventAuthority);
_serializer.WriteInt(entity.EntityId);
_serializer.WriteUShort(ownerId);
{
var entityPayload = entity.Payload.Read();
ReadOnlySpan<byte> entityPayload = entity.Payload.AsSpan();
_serializer.WriteData(ref entityPayload);
}
@@ -300,13 +319,20 @@ namespace Ragon.Core
case RagonOperation.CREATE_ENTITY:
{
var entityType = _serializer.ReadUShort();
var stateAuthority = (RagonAuthority) _serializer.ReadByte();
var eventAuthority = (RagonAuthority) _serializer.ReadByte();
var entity = new Entity(peerId, entityType, -1, stateAuthority, eventAuthority);
var propertiesCount = _serializer.ReadUShort();
var entity = new Entity(peerId, entityType, 0, RagonAuthority.ALL, RagonAuthority.ALL, propertiesCount);
for (var i = 0; i < propertiesCount; i++)
{
var propertySize = _serializer.ReadUShort();
entity.Properties[i] = new EntityProperty(propertySize);
_logger.Trace($"Property: {i} {propertySize}");
}
_logger.Trace($"Created object with type: {entityType} {propertiesCount}");
{
var entityPayload = _serializer.ReadData(_serializer.Size);
entity.Payload.Write(ref entityPayload);
entity.Payload = entityPayload.ToArray();
}
var player = _players[peerId];
@@ -323,13 +349,11 @@ namespace Ragon.Core
_serializer.Clear();
_serializer.WriteOperation(RagonOperation.CREATE_ENTITY);
_serializer.WriteUShort(entityType);
_serializer.WriteByte((byte) stateAuthority);
_serializer.WriteByte((byte) eventAuthority);
_serializer.WriteInt(entity.EntityId);
_serializer.WriteUShort(entity.EntityId);
_serializer.WriteUShort(ownerId);
{
var entityPayload = entity.Payload.Read();
ReadOnlySpan<byte> entityPayload = entity.Payload.AsSpan();
_serializer.WriteData(ref entityPayload);
}
@@ -372,7 +396,7 @@ namespace Ragon.Core
_serializer.Clear();
_serializer.WriteOperation(RagonOperation.SNAPSHOT);
_serializer.WriteInt(_allPlayers.Length);
_serializer.WriteUShort((ushort) _allPlayers.Length);
foreach (var playerPeerId in _allPlayers)
{
_serializer.WriteString(_players[playerPeerId].Id);
@@ -380,41 +404,31 @@ namespace Ragon.Core
_serializer.WriteString(_players[playerPeerId].PlayerName);
}
var dynamicCount = _entitiesAll.Where(e => e.StaticId == -1).ToArray();
_serializer.WriteInt(dynamicCount.Length);
foreach (var entity in dynamicCount)
var dynamicEntities = _entitiesAll.Where(e => e.StaticId == 0).ToArray();
_serializer.WriteUShort((ushort)dynamicEntities.Length);
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.EntityId);
_serializer.WriteUShort((ushort) entity.OwnerId);
_serializer.WriteUShort((ushort) payload.Length);
_serializer.WriteData(ref payload);
_serializer.WriteData(ref state);
}
var staticCount = _entitiesAll.Where(e => e.StaticId != -1).ToArray();
_serializer.WriteInt(staticCount.Length);
var staticCount = _entitiesAll.Where(e => e.StaticId != 0).ToArray();
_serializer.WriteUShort((ushort)staticCount.Length);
foreach (var entity in staticCount)
{
var payload = entity.Payload.Read();
var state = entity.State.Read();
ReadOnlySpan<byte> payload = entity.Payload.AsSpan();
_serializer.WriteUShort(entity.EntityType);
_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((ushort) entity.OwnerId);
_serializer.WriteUShort((ushort) payload.Length);
_serializer.WriteData(ref payload);
_serializer.WriteData(ref state);
}
var sendData = _serializer.ToArray();
@@ -432,22 +446,45 @@ namespace Ragon.Core
{
_scheduler.Tick(deltaTime);
foreach (var entity in _entitiesAll)
if (_entitiesDirty.Count > 0)
{
if (entity.State.isDirty)
{
var state = entity.State.Read();
_serializer.Clear();
_serializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE);
_serializer.WriteInt(entity.EntityId);
_serializer.WriteData(ref state);
_logger.Trace((ushort) _entitiesDirty.Count);
_serializer.WriteUShort((ushort) _entitiesDirty.Count);
for (var entityIndex = 0; entityIndex < _entitiesDirty.Count; entityIndex++)
{
var entity = _entitiesDirty[entityIndex];
var mask = 0L;
_serializer.WriteUShort(entity.EntityId);
var offset = _serializer.Lenght;
_serializer.WriteLong(mask);
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);
entity.State.Clear();
}
}
}