Compare commits
12 Commits
v1.0.10-rc
...
v1.0.11-rc
| Author | SHA1 | Date | |
|---|---|---|---|
| 65c1d9c6d4 | |||
| f2934bc8ee | |||
| 81879d4fe2 | |||
| 2d89d267f5 | |||
| 34107cbd5f | |||
| ba2ce25aec | |||
| ca17e7de83 | |||
| dff6dbcf97 | |||
| ae485f96d4 | |||
| 74904de16b | |||
| 8fd8558323 | |||
| 877ebdcde2 |
@@ -20,10 +20,11 @@ namespace Ragon.Common
|
||||
PLAYER_JOINED,
|
||||
PLAYER_LEAVED,
|
||||
|
||||
CREATE_STATES,
|
||||
CREATE_ENTITY,
|
||||
CREATE_STATIC_ENTITY,
|
||||
DESTROY_ENTITY,
|
||||
|
||||
CREATE_STATIC_ENTITY,
|
||||
|
||||
SNAPSHOT,
|
||||
|
||||
REPLICATE_ENTITY_STATE,
|
||||
|
||||
@@ -11,12 +11,17 @@ 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
|
||||
{
|
||||
private byte[] _data;
|
||||
@@ -24,7 +29,7 @@ namespace Ragon.Common
|
||||
private int _size;
|
||||
public int Lenght => _offset;
|
||||
public int Size => _size - _offset;
|
||||
|
||||
|
||||
public RagonSerializer(int capacity = 256)
|
||||
{
|
||||
_data = new byte[capacity];
|
||||
@@ -37,13 +42,13 @@ namespace Ragon.Common
|
||||
{
|
||||
_size = _offset;
|
||||
_offset = 0;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WriteByte(byte value)
|
||||
{
|
||||
ResizeIfNeed(1);
|
||||
|
||||
|
||||
_data[_offset] = value;
|
||||
_offset += 1;
|
||||
}
|
||||
@@ -55,17 +60,17 @@ namespace Ragon.Common
|
||||
_offset += 1;
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WriteBool(bool value)
|
||||
{
|
||||
ResizeIfNeed(1);
|
||||
|
||||
|
||||
_data[_offset] = value ? (byte) 1 : (byte) 0;
|
||||
_offset += 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool ReadBool()
|
||||
{
|
||||
@@ -73,13 +78,13 @@ namespace Ragon.Common
|
||||
_offset += 1;
|
||||
return value == 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WriteInt(int value)
|
||||
{
|
||||
ResizeIfNeed(4);
|
||||
var converter = new ValueConverter() { Int = value };
|
||||
var converter = new ValueConverter() {Int = value};
|
||||
_data[_offset] = converter.Byte0;
|
||||
_data[_offset + 1] = converter.Byte1;
|
||||
_data[_offset + 2] = converter.Byte2;
|
||||
@@ -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)
|
||||
{
|
||||
@@ -111,12 +156,36 @@ namespace Ragon.Common
|
||||
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)]
|
||||
public void WriteString(string value)
|
||||
{
|
||||
var stringRaw = Encoding.UTF8.GetBytes(value).AsSpan();
|
||||
ResizeIfNeed(2 + stringRaw.Length);
|
||||
|
||||
|
||||
WriteUShort((ushort) stringRaw.Length);
|
||||
var data = _data.AsSpan().Slice(_offset, stringRaw.Length);
|
||||
stringRaw.CopyTo(data);
|
||||
@@ -138,7 +207,7 @@ namespace Ragon.Common
|
||||
{
|
||||
var data = _data.AsSpan();
|
||||
var payloadData = data.Slice(_offset, lenght);
|
||||
|
||||
|
||||
_offset += payloadData.Length;
|
||||
return payloadData;
|
||||
}
|
||||
@@ -147,19 +216,19 @@ namespace Ragon.Common
|
||||
public void WriteData(ref ReadOnlySpan<byte> payload)
|
||||
{
|
||||
ResizeIfNeed(payload.Length);
|
||||
|
||||
|
||||
var data = _data.AsSpan();
|
||||
var payloadData = data.Slice(_offset, payload.Length);
|
||||
|
||||
payload.CopyTo(payloadData);
|
||||
_offset += payload.Length;
|
||||
}
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Span<byte> GetWritableData(int lenght)
|
||||
{
|
||||
ResizeIfNeed(lenght);
|
||||
|
||||
|
||||
var data = _data.AsSpan();
|
||||
var payloadData = data.Slice(_offset, lenght);
|
||||
|
||||
@@ -171,7 +240,7 @@ namespace Ragon.Common
|
||||
public void WriteOperation(RagonOperation ragonOperation)
|
||||
{
|
||||
ResizeIfNeed(1);
|
||||
|
||||
|
||||
_data[_offset] = (byte) ragonOperation;
|
||||
_offset += 1;
|
||||
}
|
||||
@@ -188,7 +257,7 @@ namespace Ragon.Common
|
||||
public void WriteUShort(ushort value)
|
||||
{
|
||||
ResizeIfNeed(2);
|
||||
|
||||
|
||||
_data[_offset] = (byte) (value & 0x00FF);
|
||||
_data[_offset + 1] = (byte) ((value & 0xFF00) >> 8);
|
||||
_offset += 2;
|
||||
@@ -223,16 +292,16 @@ namespace Ragon.Common
|
||||
data.CopyTo(dataSpan);
|
||||
_size = data.Length;
|
||||
}
|
||||
|
||||
|
||||
public void FromArray(byte[] data)
|
||||
{
|
||||
Clear();
|
||||
ResizeIfNeed(data.Length);
|
||||
Buffer.BlockCopy(data, 0, _data, 0, _offset);
|
||||
Buffer.BlockCopy(data, 0, _data, 0, data.Length);
|
||||
_size = data.Length;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public byte[] ToArray()
|
||||
{
|
||||
var bytes = new byte[_offset];
|
||||
@@ -244,7 +313,7 @@ namespace Ragon.Common
|
||||
{
|
||||
if (_offset + lenght < _data.Length)
|
||||
return;
|
||||
|
||||
|
||||
var newData = new byte[_data.Length * 4 + lenght];
|
||||
Buffer.BlockCopy(_data, 0, newData, 0, _data.Length);
|
||||
_data = newData;
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace Game.Source
|
||||
}
|
||||
|
||||
public override void OnPlayerJoined(Player player)
|
||||
{
|
||||
{
|
||||
// _logger.Info($"Player({player.PlayerName}) joined to Room({GameRoom.Id})");
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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 ushort 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(ushort 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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
+103
-58
@@ -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)
|
||||
@@ -94,7 +97,7 @@ namespace Ragon.Core
|
||||
{
|
||||
_allPlayers = _players.Select(p => p.Key).ToArray();
|
||||
_readyPlayers = _players.Where(p => p.Value.IsLoaded).Select(p => p.Key).ToArray();
|
||||
|
||||
|
||||
{
|
||||
_plugin.OnPlayerLeaved(player);
|
||||
|
||||
@@ -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.FromSpan(ref payloadRawData);
|
||||
@@ -126,14 +129,27 @@ namespace Ragon.Core
|
||||
{
|
||||
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)
|
||||
if (ent.OwnerId != peerId)
|
||||
{
|
||||
_logger.Warn($"Not owner can't change properties of object {entityId}");
|
||||
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;
|
||||
@@ -143,8 +159,8 @@ namespace Ragon.Core
|
||||
var evntId = _serializer.ReadUShort();
|
||||
var evntMode = _serializer.ReadByte();
|
||||
var targetMode = (RagonTarget) _serializer.ReadByte();
|
||||
var entityId = _serializer.ReadInt();
|
||||
|
||||
var entityId = _serializer.ReadUShort();
|
||||
|
||||
if (!_entities.TryGetValue(entityId, out var ent))
|
||||
return;
|
||||
|
||||
@@ -164,7 +180,7 @@ namespace Ragon.Core
|
||||
_serializer.WriteUShort(evntId);
|
||||
_serializer.WriteUShort((ushort) peerId);
|
||||
_serializer.WriteByte(evntMode);
|
||||
_serializer.WriteInt(entityId);
|
||||
_serializer.WriteUShort(entityId);
|
||||
_serializer.WriteData(ref payload);
|
||||
var sendData = _serializer.ToArray();
|
||||
|
||||
@@ -191,6 +207,7 @@ namespace Ragon.Core
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case RagonOperation.LOAD_SCENE:
|
||||
@@ -243,7 +260,7 @@ namespace Ragon.Core
|
||||
foreach (var playerPeerId in _readyPlayers)
|
||||
if (playerPeerId != _owner)
|
||||
_peersCache.Add(playerPeerId);
|
||||
|
||||
|
||||
Broadcast(_peersCache.ToArray(), sendData, DeliveryType.Reliable);
|
||||
break;
|
||||
}
|
||||
@@ -253,19 +270,30 @@ namespace Ragon.Core
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case RagonOperation.CREATE_STATIC_ENTITY:
|
||||
{
|
||||
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();
|
||||
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);
|
||||
for (var i = 0; i < propertiesCount; i++)
|
||||
{
|
||||
var propertySize = _serializer.ReadUShort();
|
||||
entity.Properties[i] = new EntityProperty(propertySize);
|
||||
}
|
||||
|
||||
{
|
||||
var entityPayload = _serializer.ReadData(_serializer.Size);
|
||||
entity.Payload.Write(ref entityPayload);
|
||||
entity.Payload = entityPayload.ToArray();
|
||||
}
|
||||
|
||||
var player = _players[peerId];
|
||||
@@ -282,14 +310,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 +326,23 @@ 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);
|
||||
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);
|
||||
entity.Payload.Write(ref entityPayload);
|
||||
entity.Payload = entityPayload.ToArray();
|
||||
}
|
||||
|
||||
var player = _players[peerId];
|
||||
@@ -323,13 +359,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 +406,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 +414,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.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(entity.EntityId);
|
||||
_serializer.WriteUShort(entity.StaticId);
|
||||
_serializer.WriteUShort(entity.OwnerId);
|
||||
_serializer.WriteUShort((ushort) payload.Length);
|
||||
_serializer.WriteData(ref payload);
|
||||
_serializer.WriteData(ref state);
|
||||
}
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
@@ -432,22 +456,43 @@ namespace Ragon.Core
|
||||
{
|
||||
_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.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE);
|
||||
_serializer.WriteInt(entity.EntityId);
|
||||
_serializer.WriteData(ref state);
|
||||
_serializer.WriteUShort(entity.EntityId);
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
Broadcast(_readyPlayers, sendData, DeliveryType.Unreliable);
|
||||
var offset = _serializer.Lenght;
|
||||
_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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -104,7 +104,7 @@ namespace Ragon.Core
|
||||
{
|
||||
try
|
||||
{
|
||||
var peerId = evnt.Peer.ID;
|
||||
var peerId = (ushort) evnt.Peer.ID;
|
||||
var dataRaw = new byte[evnt.Packet.Length];
|
||||
evnt.Packet.CopyTo(dataRaw);
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ public class Lobby : ILobby
|
||||
_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.FromSpan(ref payload);
|
||||
|
||||
Reference in New Issue
Block a user