Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a51d7c73cd | |||
| 957622a170 | |||
| 4a07424293 | |||
| 568a3282c3 |
@@ -48,7 +48,7 @@ public class AuthorizationManager : IAuthorizationManager
|
|||||||
PeerId = peerId,
|
PeerId = peerId,
|
||||||
IsLoaded = false,
|
IsLoaded = false,
|
||||||
Entities = new List<Entity>(),
|
Entities = new List<Entity>(),
|
||||||
EntitiesIds = new List<int>(),
|
EntitiesIds = new List<ushort>(),
|
||||||
};
|
};
|
||||||
|
|
||||||
_playersByIds.Add(playerId, player);
|
_playersByIds.Add(playerId, player);
|
||||||
|
|||||||
@@ -6,17 +6,19 @@ namespace Ragon.Core;
|
|||||||
public class EntityProperty
|
public class EntityProperty
|
||||||
{
|
{
|
||||||
public int Size { get; set; }
|
public int Size { get; set; }
|
||||||
|
public int Capacity { get; set; }
|
||||||
public bool IsDirty { get; private set; }
|
public bool IsDirty { get; private set; }
|
||||||
public bool IsFixed { get; private set; }
|
public bool IsFixed { get; private set; }
|
||||||
private byte[] _data;
|
private byte[] _data;
|
||||||
|
|
||||||
public EntityProperty(int size, bool isFixed)
|
public EntityProperty(int size, bool isFixed)
|
||||||
{
|
{
|
||||||
_data = new byte[512];
|
Capacity = 512;
|
||||||
|
|
||||||
Size = size;
|
Size = size;
|
||||||
IsFixed = isFixed;
|
IsFixed = isFixed;
|
||||||
IsDirty = true;
|
IsDirty = true;
|
||||||
|
|
||||||
|
_data = new byte[Capacity];
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReadOnlySpan<byte> Read()
|
public ReadOnlySpan<byte> Read()
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
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;
|
||||||
@@ -109,7 +108,7 @@ namespace Ragon.Core
|
|||||||
_serializer.WriteUShort((ushort) player.EntitiesIds.Count);
|
_serializer.WriteUShort((ushort) player.EntitiesIds.Count);
|
||||||
foreach (var entityId in player.EntitiesIds)
|
foreach (var entityId in player.EntitiesIds)
|
||||||
{
|
{
|
||||||
_serializer.WriteInt(entityId);
|
_serializer.WriteUShort(entityId);
|
||||||
_entities.Remove(entityId);
|
_entities.Remove(entityId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,11 +160,19 @@ namespace Ragon.Core
|
|||||||
if (_serializer.ReadBool())
|
if (_serializer.ReadBool())
|
||||||
{
|
{
|
||||||
var property = ent.Properties[i];
|
var property = ent.Properties[i];
|
||||||
|
var size = property.Size;
|
||||||
if (!property.IsFixed)
|
if (!property.IsFixed)
|
||||||
property.Size = _serializer.ReadUShort();
|
size = _serializer.ReadUShort();
|
||||||
|
|
||||||
var propertyPayload = _serializer.ReadData(property.Size);
|
if (size > property.Capacity)
|
||||||
|
{
|
||||||
|
_logger.Warn($"Property {i} payload too large, size: {size}");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var propertyPayload = _serializer.ReadData(size);
|
||||||
property.Write(ref propertyPayload);
|
property.Write(ref propertyPayload);
|
||||||
|
property.Size = size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -369,7 +376,7 @@ namespace Ragon.Core
|
|||||||
player.Entities.Add(entity);
|
player.Entities.Add(entity);
|
||||||
player.EntitiesIds.Add(entity.EntityId);
|
player.EntitiesIds.Add(entity.EntityId);
|
||||||
|
|
||||||
var ownerId = (ushort) peerId;
|
var ownerId = peerId;
|
||||||
|
|
||||||
_entities.Add(entity.EntityId, entity);
|
_entities.Add(entity.EntityId, entity);
|
||||||
_entitiesAll = _entities.Values.ToArray();
|
_entitiesAll = _entities.Values.ToArray();
|
||||||
@@ -481,6 +488,11 @@ namespace Ragon.Core
|
|||||||
{
|
{
|
||||||
_scheduler.Tick(deltaTime);
|
_scheduler.Tick(deltaTime);
|
||||||
|
|
||||||
|
ReplicateProperties();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ReplicateProperties()
|
||||||
|
{
|
||||||
if (_entitiesDirty.Count > 0)
|
if (_entitiesDirty.Count > 0)
|
||||||
{
|
{
|
||||||
_serializer.Clear();
|
_serializer.Clear();
|
||||||
@@ -532,11 +544,18 @@ namespace Ragon.Core
|
|||||||
for (int propertyIndex = 0; propertyIndex < entity.Properties.Length; propertyIndex++)
|
for (int propertyIndex = 0; propertyIndex < entity.Properties.Length; propertyIndex++)
|
||||||
{
|
{
|
||||||
var property = entity.Properties[propertyIndex];
|
var property = entity.Properties[propertyIndex];
|
||||||
_serializer.WriteBool(true);
|
var hasPayload = property.IsFixed || property.Size > 0 && !property.IsFixed;
|
||||||
|
if (hasPayload)
|
||||||
var span = _serializer.GetWritableData(property.Size);
|
{
|
||||||
var data = property.Read();
|
_serializer.WriteBool(true);
|
||||||
data.CopyTo(span);
|
var span = _serializer.GetWritableData(property.Size);
|
||||||
|
var data = property.Read();
|
||||||
|
data.CopyTo(span);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_serializer.WriteBool(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -568,19 +587,13 @@ namespace Ragon.Core
|
|||||||
|
|
||||||
public IScheduler GetScheduler() => _scheduler;
|
public IScheduler GetScheduler() => _scheduler;
|
||||||
|
|
||||||
public void Send(uint peerId, byte[] rawData, DeliveryType deliveryType = DeliveryType.Unreliable)
|
public void Send(uint peerId, byte[] rawData, DeliveryType deliveryType = DeliveryType.Unreliable) =>
|
||||||
{
|
|
||||||
_gameThread.Server.Send(peerId, rawData, deliveryType);
|
_gameThread.Server.Send(peerId, rawData, deliveryType);
|
||||||
}
|
|
||||||
|
|
||||||
public void Broadcast(uint[] peersIds, byte[] rawData, DeliveryType deliveryType = DeliveryType.Unreliable)
|
public void Broadcast(uint[] peersIds, byte[] rawData, DeliveryType deliveryType = DeliveryType.Unreliable) =>
|
||||||
{
|
|
||||||
_gameThread.Server.Broadcast(peersIds, rawData, deliveryType);
|
_gameThread.Server.Broadcast(peersIds, rawData, deliveryType);
|
||||||
}
|
|
||||||
|
|
||||||
public void Broadcast(byte[] rawData, DeliveryType deliveryType = DeliveryType.Unreliable)
|
public void Broadcast(byte[] rawData, DeliveryType deliveryType = DeliveryType.Unreliable) =>
|
||||||
{
|
|
||||||
_gameThread.Server.Broadcast(_allPlayers, rawData, deliveryType);
|
_gameThread.Server.Broadcast(_allPlayers, rawData, deliveryType);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -11,6 +11,6 @@ namespace Ragon.Core
|
|||||||
public bool IsLoaded { get; set; }
|
public bool IsLoaded { get; set; }
|
||||||
|
|
||||||
public List<Entity> Entities;
|
public List<Entity> Entities;
|
||||||
public List<int> EntitiesIds;
|
public List<ushort> EntitiesIds;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user