Compare commits
2 Commits
v1.0.17-rc
...
v1.0.18-rc
| Author | SHA1 | Date | |
|---|---|---|---|
| 54786c065d | |||
| 0b3a0dd1ac |
@@ -2,6 +2,7 @@ namespace Ragon.Common
|
||||
{
|
||||
public enum RagonAuthority: byte
|
||||
{
|
||||
NONE,
|
||||
OWNER_ONLY,
|
||||
ALL,
|
||||
}
|
||||
|
||||
@@ -15,14 +15,12 @@ namespace Ragon.Common
|
||||
JOIN_FAILED,
|
||||
|
||||
LOAD_SCENE,
|
||||
SCENE_IS_LOADED,
|
||||
SCENE_IS_PROCESSED,
|
||||
SCENE_LOADED,
|
||||
|
||||
PLAYER_JOINED,
|
||||
PLAYER_LEAVED,
|
||||
|
||||
CREATE_ENTITY,
|
||||
CREATE_SCENE_ENTITY,
|
||||
DESTROY_ENTITY,
|
||||
SNAPSHOT,
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Ragon.Common
|
||||
{
|
||||
public enum RagonReplicationMode: byte
|
||||
{
|
||||
LOCAL,
|
||||
SERVER,
|
||||
LOCAL_AND_SERVER,
|
||||
BUFFERED,
|
||||
}
|
||||
}
|
||||
@@ -101,7 +101,7 @@ namespace Stress
|
||||
simulationClient.InRoom = true;
|
||||
|
||||
ragonSerializer.Clear();
|
||||
ragonSerializer.WriteOperation(RagonOperation.SCENE_IS_LOADED);
|
||||
ragonSerializer.WriteOperation(RagonOperation.SCENE_LOADED);
|
||||
|
||||
var sendData = ragonSerializer.ToArray();
|
||||
var packet = new Packet();
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using NLog.Targets;
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core;
|
||||
|
||||
public class AuthorizationManager : IAuthorizationManager
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
private IAuthorizationProvider _provider;
|
||||
private IGameThread _gameThread;
|
||||
private Lobby _lobby;
|
||||
@@ -27,6 +27,12 @@ public class AuthorizationManager : IAuthorizationManager
|
||||
|
||||
public void OnAuthorization(uint peerId, string key, string name)
|
||||
{
|
||||
if (_playersByPeers.ContainsKey(peerId))
|
||||
{
|
||||
_logger.Warn($"Connection already authorized {peerId}");
|
||||
return;
|
||||
}
|
||||
|
||||
var dispatcher = _gameThread.ThreadDispatcher;
|
||||
|
||||
_provider.OnAuthorizationRequest(key, name, Array.Empty<byte>(),
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace Ragon.Core
|
||||
public static class ConfigurationLoader
|
||||
{
|
||||
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
private static readonly string _serverVersion = "1.0.17-rc";
|
||||
private static readonly string _serverVersion = "1.0.18-rc";
|
||||
|
||||
private static void CopyrightInfo()
|
||||
{
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core;
|
||||
@@ -12,9 +13,11 @@ public class Entity
|
||||
public ushort OwnerId { get; private set; }
|
||||
public RagonAuthority Authority { get; private set; }
|
||||
public EntityProperty[] Properties { get; private set; }
|
||||
public List<EntityEvent> BufferedEvents = new List<EntityEvent>();
|
||||
|
||||
public byte[] Payload { get; set; }
|
||||
|
||||
public Entity(ushort ownerId, ushort entityType, ushort staticId, RagonAuthority stateAuthority, RagonAuthority eventAuthority, int props)
|
||||
public Entity(ushort ownerId, ushort entityType, ushort staticId, RagonAuthority eventAuthority, int props)
|
||||
{
|
||||
OwnerId = ownerId;
|
||||
StaticId = staticId;
|
||||
@@ -24,4 +27,47 @@ public class Entity
|
||||
Payload = Array.Empty<byte>();
|
||||
Authority = eventAuthority;
|
||||
}
|
||||
|
||||
public void ReplicateProperties(RagonSerializer serializer)
|
||||
{
|
||||
serializer.WriteUShort(EntityId);
|
||||
|
||||
for (int propertyIndex = 0; propertyIndex < Properties.Length; propertyIndex++)
|
||||
{
|
||||
var property = Properties[propertyIndex];
|
||||
if (property.IsDirty)
|
||||
{
|
||||
serializer.WriteBool(true);
|
||||
var span = serializer.GetWritableData(property.Size);
|
||||
var data = property.Read();
|
||||
data.CopyTo(span);
|
||||
property.Clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
serializer.WriteBool(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Snapshot(RagonSerializer serializer)
|
||||
{
|
||||
for (int propertyIndex = 0; propertyIndex < Properties.Length; propertyIndex++)
|
||||
{
|
||||
var property = Properties[propertyIndex];
|
||||
var hasPayload = property.IsFixed || property.Size > 0 && !property.IsFixed;
|
||||
if (hasPayload)
|
||||
{
|
||||
serializer.WriteBool(true);
|
||||
var span = serializer.GetWritableData(property.Size);
|
||||
var data = property.Read();
|
||||
data.CopyTo(span);
|
||||
}
|
||||
else
|
||||
{
|
||||
serializer.WriteBool(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core;
|
||||
|
||||
public class EntityEvent
|
||||
{
|
||||
public ushort EventId { get; set; }
|
||||
public byte[] EventData { get; set; }
|
||||
public RagonTarget Target { set; get; }
|
||||
}
|
||||
+211
-254
@@ -1,11 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core
|
||||
{
|
||||
// TODO: Replace all serialization and packing into dedicated structures
|
||||
// TODO: Split class on different managers
|
||||
public class GameRoom : IGameRoom
|
||||
{
|
||||
public int PlayersMin { get; private set; }
|
||||
@@ -32,7 +35,7 @@ namespace Ragon.Core
|
||||
private HashSet<Entity> _entitiesDirtySet = new HashSet<Entity>();
|
||||
private List<Entity> _entitiesDirty = new List<Entity>();
|
||||
private List<uint> _peersCache = new List<uint>();
|
||||
private List<uint> _loadedPeers = new List<uint>();
|
||||
private List<uint> _awaitingPeers = new List<uint>();
|
||||
|
||||
public GameRoom(IGameThread gameThread, PluginBase pluginBase, string roomId, string map, int min, int max)
|
||||
{
|
||||
@@ -48,7 +51,7 @@ namespace Ragon.Core
|
||||
_plugin.Attach(this);
|
||||
}
|
||||
|
||||
public void Joined(Player player, ReadOnlySpan<byte> payload)
|
||||
public void AddPlayer(Player player, ReadOnlySpan<byte> payload)
|
||||
{
|
||||
if (_players.Count == 0)
|
||||
{
|
||||
@@ -81,7 +84,7 @@ namespace Ragon.Core
|
||||
}
|
||||
}
|
||||
|
||||
public void Leave(uint peerId)
|
||||
public void RemovePlayer(uint peerId)
|
||||
{
|
||||
if (_players.Remove(peerId, out var player))
|
||||
{
|
||||
@@ -95,11 +98,12 @@ namespace Ragon.Core
|
||||
_serializer.WriteOperation(RagonOperation.PLAYER_LEAVED);
|
||||
_serializer.WriteString(player.Id);
|
||||
|
||||
_serializer.WriteUShort((ushort) player.EntitiesIds.Count);
|
||||
foreach (var entityId in player.EntitiesIds)
|
||||
var entitiesToDelete = player.Entities.Where(e => e.StaticId == 0).ToArray();
|
||||
_serializer.WriteUShort((ushort) entitiesToDelete.Length);
|
||||
foreach (var entity in entitiesToDelete)
|
||||
{
|
||||
_serializer.WriteUShort(entityId);
|
||||
_entities.Remove(entityId);
|
||||
_serializer.WriteUShort(entity.EntityId);
|
||||
_entities.Remove(entity.EntityId);
|
||||
}
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
@@ -124,13 +128,136 @@ namespace Ragon.Core
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Move this processing to specialized classes with structures
|
||||
public void ProcessEvent(ushort peerId, RagonOperation operation, ReadOnlySpan<byte> payloadRawData)
|
||||
{
|
||||
_serializer.Clear();
|
||||
_serializer.FromSpan(ref payloadRawData);
|
||||
|
||||
switch (operation)
|
||||
{
|
||||
case RagonOperation.LOAD_SCENE:
|
||||
{
|
||||
var sceneName = _serializer.ReadString();
|
||||
_readyPlayers = Array.Empty<uint>();
|
||||
_entitiesAll = Array.Empty<Entity>();
|
||||
_entities.Clear();
|
||||
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.LOAD_SCENE);
|
||||
_serializer.WriteString(sceneName);
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
Broadcast(_allPlayers, sendData, DeliveryType.Reliable);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.SCENE_LOADED:
|
||||
{
|
||||
var player = _players[peerId];
|
||||
if (peerId == _owner)
|
||||
{
|
||||
var statics = _serializer.ReadUShort();
|
||||
for (var staticIndex = 0; staticIndex < statics; staticIndex++)
|
||||
{
|
||||
var entityType = _serializer.ReadUShort();
|
||||
var entityAuthority = (RagonAuthority) _serializer.ReadByte();
|
||||
var staticId = _serializer.ReadUShort();
|
||||
|
||||
var propertiesCount = _serializer.ReadUShort();
|
||||
var entity = new Entity(peerId, entityType, staticId, entityAuthority, propertiesCount);
|
||||
for (var propertyIndex = 0; propertyIndex < propertiesCount; propertyIndex++)
|
||||
{
|
||||
var propertyType = _serializer.ReadBool();
|
||||
var propertySize = _serializer.ReadUShort();
|
||||
entity.Properties[propertyIndex] = new EntityProperty(propertySize, propertyType);
|
||||
}
|
||||
|
||||
player.Entities.Add(entity);
|
||||
player.EntitiesIds.Add(entity.EntityId);
|
||||
|
||||
_entities.Add(entity.EntityId, entity);
|
||||
}
|
||||
|
||||
_entitiesAll = _entities.Values.ToArray();
|
||||
_logger.Trace($"Scene entities: {statics}");
|
||||
|
||||
_awaitingPeers.Add(peerId);
|
||||
|
||||
foreach (var peer in _awaitingPeers)
|
||||
{
|
||||
var joinedPlayer = _players[peer];
|
||||
joinedPlayer.IsLoaded = true;
|
||||
_plugin.OnPlayerJoined(joinedPlayer);
|
||||
_logger.Trace($"[{_owner}][{peer}] Player {joinedPlayer.Id} restored");
|
||||
{
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.PLAYER_JOINED);
|
||||
_serializer.WriteUShort((ushort) player.PeerId);
|
||||
_serializer.WriteString(player.Id);
|
||||
_serializer.WriteString(player.PlayerName);
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
var readyPlayersWithExcludedPeer = _readyPlayers.Where(p => p != peerId).ToArray();
|
||||
Broadcast(readyPlayersWithExcludedPeer, sendData, DeliveryType.Reliable);
|
||||
}
|
||||
}
|
||||
|
||||
_readyPlayers = _players.Where(p => p.Value.IsLoaded).Select(p => p.Key).ToArray();
|
||||
foreach (var peer in _awaitingPeers)
|
||||
{
|
||||
ReplicateSnapshot(peer);
|
||||
}
|
||||
|
||||
_awaitingPeers.Clear();
|
||||
}
|
||||
else if (GetOwner().IsLoaded)
|
||||
{
|
||||
_logger.Trace($"[{_owner}][{peerId}] Player {player.Id} restored instantly");
|
||||
player.IsLoaded = true;
|
||||
|
||||
{
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.PLAYER_JOINED);
|
||||
_serializer.WriteUShort((ushort) player.PeerId);
|
||||
_serializer.WriteString(player.Id);
|
||||
_serializer.WriteString(player.PlayerName);
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
var readyPlayersWithExcludedPeer = _readyPlayers.Where(p => p != peerId).ToArray();
|
||||
Broadcast(readyPlayersWithExcludedPeer, sendData, DeliveryType.Reliable);
|
||||
}
|
||||
|
||||
_readyPlayers = _players.Where(p => p.Value.IsLoaded).Select(p => p.Key).ToArray();
|
||||
_plugin.OnPlayerJoined(player);
|
||||
|
||||
ReplicateSnapshot(peerId);
|
||||
|
||||
foreach (var (key, value) in _entities)
|
||||
{
|
||||
foreach (var bufferedEvent in value.BufferedEvents)
|
||||
{
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
|
||||
_serializer.WriteUShort(bufferedEvent.EventId);
|
||||
_serializer.WriteUShort(peerId);
|
||||
_serializer.WriteByte((byte) RagonReplicationMode.SERVER);
|
||||
_serializer.WriteUShort(value.EntityId);
|
||||
|
||||
ReadOnlySpan<byte> data = bufferedEvent.EventData.AsSpan();
|
||||
_serializer.WriteData(ref data);
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
SendEvent(value, bufferedEvent.Target, sendData);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Trace($"[{_owner}][{peerId}] Player {player.Id} waiting");
|
||||
_awaitingPeers.Add(peerId);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case RagonOperation.REPLICATE_ENTITY_STATE:
|
||||
{
|
||||
var entitiesCount = _serializer.ReadUShort();
|
||||
@@ -180,176 +307,63 @@ namespace Ragon.Core
|
||||
}
|
||||
case RagonOperation.REPLICATE_ENTITY_EVENT:
|
||||
{
|
||||
var evntId = _serializer.ReadUShort();
|
||||
var evntMode = _serializer.ReadByte();
|
||||
var eventId = _serializer.ReadUShort();
|
||||
var eventMode = (RagonReplicationMode) _serializer.ReadByte();
|
||||
var targetMode = (RagonTarget) _serializer.ReadByte();
|
||||
var entityId = _serializer.ReadUShort();
|
||||
|
||||
if (!_entities.TryGetValue(entityId, out var ent))
|
||||
{
|
||||
_logger.Warn($"Entity not found for event with Id {eventId}");
|
||||
return;
|
||||
}
|
||||
|
||||
if (ent.Authority == RagonAuthority.OWNER_ONLY && ent.OwnerId != peerId)
|
||||
{
|
||||
_logger.Warn($"Player have not enought authority for event with Id {eventId}");
|
||||
return;
|
||||
}
|
||||
|
||||
Span<byte> payloadRaw = stackalloc byte[_serializer.Size];
|
||||
var payloadData = _serializer.ReadData(_serializer.Size);
|
||||
payloadData.CopyTo(payloadRaw);
|
||||
|
||||
ReadOnlySpan<byte> payload = payloadRaw;
|
||||
if (_plugin.InternalHandle(peerId, entityId, evntId, ref payload))
|
||||
if (_plugin.InternalHandle(peerId, entityId, eventId, ref payload))
|
||||
return;
|
||||
|
||||
if (eventMode == RagonReplicationMode.BUFFERED && targetMode != RagonTarget.OWNER)
|
||||
{
|
||||
var bufferedEvent = new EntityEvent()
|
||||
{
|
||||
EventData = payload.ToArray(),
|
||||
Target = targetMode,
|
||||
EventId = eventId,
|
||||
};
|
||||
ent.BufferedEvents.Add(bufferedEvent);
|
||||
}
|
||||
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
|
||||
_serializer.WriteUShort(evntId);
|
||||
_serializer.WriteUShort(eventId);
|
||||
_serializer.WriteUShort(peerId);
|
||||
_serializer.WriteByte(evntMode);
|
||||
_serializer.WriteByte((byte) eventMode);
|
||||
_serializer.WriteUShort(entityId);
|
||||
_serializer.WriteData(ref payload);
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
|
||||
switch (targetMode)
|
||||
{
|
||||
case RagonTarget.OWNER:
|
||||
{
|
||||
Send(ent.OwnerId, sendData, DeliveryType.Reliable);
|
||||
break;
|
||||
}
|
||||
case RagonTarget.EXCEPT_OWNER:
|
||||
{
|
||||
_peersCache.Clear();
|
||||
foreach (var playerPeerId in _readyPlayers)
|
||||
if (playerPeerId != ent.OwnerId)
|
||||
_peersCache.Add(playerPeerId);
|
||||
|
||||
Broadcast(_peersCache.ToArray(), sendData, DeliveryType.Reliable);
|
||||
break;
|
||||
}
|
||||
case RagonTarget.ALL:
|
||||
{
|
||||
Broadcast(_readyPlayers, sendData, DeliveryType.Reliable);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case RagonOperation.LOAD_SCENE:
|
||||
{
|
||||
var sceneName = _serializer.ReadString();
|
||||
_readyPlayers = Array.Empty<uint>();
|
||||
_entitiesAll = Array.Empty<Entity>();
|
||||
_entities.Clear();
|
||||
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.LOAD_SCENE);
|
||||
_serializer.WriteString(sceneName);
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
Broadcast(_allPlayers, sendData, DeliveryType.Reliable);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.REPLICATE_EVENT:
|
||||
{
|
||||
var evntId = _serializer.ReadUShort();
|
||||
var evntMode = _serializer.ReadByte();
|
||||
var targetMode = (RagonTarget) _serializer.ReadByte();
|
||||
|
||||
Span<byte> payloadRaw = stackalloc byte[_serializer.Size];
|
||||
var payloadData = _serializer.ReadData(_serializer.Size);
|
||||
payloadData.CopyTo(payloadRaw);
|
||||
|
||||
ReadOnlySpan<byte> payload = payloadRaw;
|
||||
if (_plugin.InternalHandle(peerId, evntId, ref payload))
|
||||
return;
|
||||
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.REPLICATE_EVENT);
|
||||
_serializer.WriteUShort((ushort) peerId);
|
||||
_serializer.WriteByte(evntMode);
|
||||
_serializer.WriteUShort(evntId);
|
||||
_serializer.WriteData(ref payload);
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
switch (targetMode)
|
||||
{
|
||||
case RagonTarget.OWNER:
|
||||
{
|
||||
Send(_owner, sendData, DeliveryType.Reliable);
|
||||
break;
|
||||
}
|
||||
case RagonTarget.EXCEPT_OWNER:
|
||||
{
|
||||
_peersCache.Clear();
|
||||
foreach (var playerPeerId in _readyPlayers)
|
||||
if (playerPeerId != _owner)
|
||||
_peersCache.Add(playerPeerId);
|
||||
|
||||
Broadcast(_peersCache.ToArray(), sendData, DeliveryType.Reliable);
|
||||
break;
|
||||
}
|
||||
case RagonTarget.ALL:
|
||||
{
|
||||
Broadcast(_readyPlayers, sendData, DeliveryType.Reliable);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case RagonOperation.CREATE_SCENE_ENTITY:
|
||||
{
|
||||
var entityType = _serializer.ReadUShort();
|
||||
var staticId = _serializer.ReadUShort();
|
||||
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 propertyType = _serializer.ReadBool();
|
||||
var propertySize = _serializer.ReadUShort();
|
||||
entity.Properties[i] = new EntityProperty(propertySize, propertyType);
|
||||
}
|
||||
|
||||
{
|
||||
var entityPayload = _serializer.ReadData(_serializer.Size);
|
||||
entity.Payload = entityPayload.ToArray();
|
||||
}
|
||||
|
||||
var player = _players[peerId];
|
||||
player.Entities.Add(entity);
|
||||
player.EntitiesIds.Add(entity.EntityId);
|
||||
|
||||
var ownerId = peerId;
|
||||
|
||||
_entities.Add(entity.EntityId, entity);
|
||||
_entitiesAll = _entities.Values.ToArray();
|
||||
|
||||
_plugin.OnEntityCreated(player, entity);
|
||||
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.CREATE_SCENE_ENTITY);
|
||||
_serializer.WriteUShort(entityType);
|
||||
_serializer.WriteUShort(entity.EntityId);
|
||||
_serializer.WriteUShort(staticId);
|
||||
_serializer.WriteUShort(ownerId);
|
||||
|
||||
{
|
||||
ReadOnlySpan<byte> entityPayload = entity.Payload.AsSpan();
|
||||
_serializer.WriteUShort((ushort) entityPayload.Length);
|
||||
_serializer.WriteData(ref entityPayload);
|
||||
}
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
Send(peerId, sendData, DeliveryType.Reliable);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.CREATE_ENTITY:
|
||||
{
|
||||
var entityType = _serializer.ReadUShort();
|
||||
var eventAuthority = (RagonAuthority) _serializer.ReadByte();
|
||||
var propertiesCount = _serializer.ReadUShort();
|
||||
var entity = new Entity(peerId, entityType, 0, RagonAuthority.ALL, RagonAuthority.ALL, propertiesCount);
|
||||
|
||||
_logger.Trace($"[{peerId}] Create Entity {entityType}");
|
||||
|
||||
var entity = new Entity(peerId, entityType, 0, eventAuthority, propertiesCount);
|
||||
for (var i = 0; i < propertiesCount; i++)
|
||||
{
|
||||
var propertyType = _serializer.ReadBool();
|
||||
@@ -417,44 +431,6 @@ namespace Ragon.Core
|
||||
var sendData = _serializer.ToArray();
|
||||
Broadcast(_readyPlayers, sendData, DeliveryType.Reliable);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case RagonOperation.SCENE_IS_PROCESSED:
|
||||
{
|
||||
var player = _players[peerId];
|
||||
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.PLAYER_JOINED);
|
||||
_serializer.WriteUShort((ushort) player.PeerId);
|
||||
_serializer.WriteString(player.Id);
|
||||
_serializer.WriteString(player.PlayerName);
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
Broadcast(_readyPlayers, sendData, DeliveryType.Reliable);
|
||||
|
||||
player.IsLoaded = true;
|
||||
|
||||
_readyPlayers = _players.Where(p => p.Value.IsLoaded).Select(p => p.Key).ToArray();
|
||||
_plugin.OnPlayerJoined(player);
|
||||
|
||||
var isOwner = _owner == peerId;
|
||||
if (isOwner)
|
||||
{
|
||||
foreach (var peer in _loadedPeers)
|
||||
ReplicateSnapshot(peer);
|
||||
|
||||
_loadedPeers.Clear();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case RagonOperation.SCENE_IS_LOADED:
|
||||
{
|
||||
var player = GetOwner();
|
||||
if (player.IsLoaded || _owner == peerId)
|
||||
ReplicateSnapshot(peerId);
|
||||
else
|
||||
_loadedPeers.Add(peerId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -467,21 +443,22 @@ namespace Ragon.Core
|
||||
ReplicateProperties();
|
||||
}
|
||||
|
||||
public void ReplicateSnapshot(uint peerId)
|
||||
// TODO: Move this to specialized class
|
||||
void ReplicateSnapshot(uint peerId)
|
||||
{
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.SNAPSHOT);
|
||||
|
||||
_serializer.WriteUShort((ushort) _allPlayers.Length);
|
||||
foreach (var playerPeerId in _allPlayers)
|
||||
_serializer.WriteUShort((ushort) _readyPlayers.Length);
|
||||
foreach (var playerPeerId in _readyPlayers)
|
||||
{
|
||||
_serializer.WriteString(_players[playerPeerId].Id);
|
||||
_serializer.WriteUShort((ushort) playerPeerId);
|
||||
_serializer.WriteString(_players[playerPeerId].Id);
|
||||
_serializer.WriteString(_players[playerPeerId].PlayerName);
|
||||
}
|
||||
|
||||
var dynamicEntities = _entitiesAll.Where(e => e.StaticId == 0).ToArray();
|
||||
_serializer.WriteUShort((ushort) dynamicEntities.Length);
|
||||
var dynamicEntitiesCount = (ushort) dynamicEntities.Length;
|
||||
_serializer.WriteUShort(dynamicEntitiesCount);
|
||||
foreach (var entity in dynamicEntities)
|
||||
{
|
||||
ReadOnlySpan<byte> payload = entity.Payload.AsSpan();
|
||||
@@ -491,11 +468,14 @@ namespace Ragon.Core
|
||||
_serializer.WriteUShort((ushort) entity.OwnerId);
|
||||
_serializer.WriteUShort((ushort) payload.Length);
|
||||
_serializer.WriteData(ref payload);
|
||||
|
||||
entity.Snapshot(_serializer);
|
||||
}
|
||||
|
||||
var staticCount = _entitiesAll.Where(e => e.StaticId != 0).ToArray();
|
||||
_serializer.WriteUShort((ushort) staticCount.Length);
|
||||
foreach (var entity in staticCount)
|
||||
var staticEntities = _entitiesAll.Where(e => e.StaticId != 0).ToArray();
|
||||
var staticEntitiesCount = (ushort) staticEntities.Length;
|
||||
_serializer.WriteUShort(staticEntitiesCount);
|
||||
foreach (var entity in staticEntities)
|
||||
{
|
||||
ReadOnlySpan<byte> payload = entity.Payload.AsSpan();
|
||||
|
||||
@@ -505,44 +485,25 @@ namespace Ragon.Core
|
||||
_serializer.WriteUShort(entity.OwnerId);
|
||||
_serializer.WriteUShort((ushort) payload.Length);
|
||||
_serializer.WriteData(ref payload);
|
||||
|
||||
entity.Snapshot(_serializer);
|
||||
}
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
Send(peerId, sendData, DeliveryType.Reliable);
|
||||
|
||||
RestoreProperties(peerId);
|
||||
}
|
||||
|
||||
private void ReplicateProperties()
|
||||
{
|
||||
if (_entitiesDirty.Count > 0)
|
||||
var entities = (ushort) _entitiesDirty.Count;
|
||||
if (entities > 0)
|
||||
{
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE);
|
||||
_serializer.WriteUShort((ushort) _entitiesDirty.Count);
|
||||
_serializer.WriteUShort(entities);
|
||||
|
||||
for (var entityIndex = 0; entityIndex < _entitiesDirty.Count; entityIndex++)
|
||||
{
|
||||
var entity = _entitiesDirty[entityIndex];
|
||||
_serializer.WriteUShort(entity.EntityId);
|
||||
|
||||
for (int propertyIndex = 0; propertyIndex < entity.Properties.Length; propertyIndex++)
|
||||
{
|
||||
var property = entity.Properties[propertyIndex];
|
||||
if (property.IsDirty)
|
||||
{
|
||||
_serializer.WriteBool(true);
|
||||
var span = _serializer.GetWritableData(property.Size);
|
||||
var data = property.Read();
|
||||
data.CopyTo(span);
|
||||
property.Clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
_serializer.WriteBool(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (var entity in _entitiesDirty)
|
||||
entity.ReplicateProperties(_serializer);
|
||||
|
||||
_entitiesDirty.Clear();
|
||||
_entitiesDirtySet.Clear();
|
||||
@@ -552,39 +513,6 @@ namespace Ragon.Core
|
||||
}
|
||||
}
|
||||
|
||||
public void RestoreProperties(uint peerId)
|
||||
{
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE);
|
||||
_serializer.WriteUShort((ushort) _entitiesAll.Length);
|
||||
|
||||
for (var entityIndex = 0; entityIndex < _entitiesAll.Length; entityIndex++)
|
||||
{
|
||||
var entity = _entitiesAll[entityIndex];
|
||||
_serializer.WriteUShort(entity.EntityId);
|
||||
|
||||
for (int propertyIndex = 0; propertyIndex < entity.Properties.Length; propertyIndex++)
|
||||
{
|
||||
var property = entity.Properties[propertyIndex];
|
||||
var hasPayload = property.IsFixed || property.Size > 0 && !property.IsFixed;
|
||||
if (hasPayload)
|
||||
{
|
||||
_serializer.WriteBool(true);
|
||||
var span = _serializer.GetWritableData(property.Size);
|
||||
var data = property.Read();
|
||||
data.CopyTo(span);
|
||||
}
|
||||
else
|
||||
{
|
||||
_serializer.WriteBool(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
Send(peerId, sendData, DeliveryType.Reliable);
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
_plugin.OnStart();
|
||||
@@ -609,6 +537,35 @@ namespace Ragon.Core
|
||||
|
||||
public IScheduler GetScheduler() => _scheduler;
|
||||
|
||||
|
||||
// TODO: Move this to Entity Event Manager
|
||||
public void SendEvent(Entity ent, RagonTarget targetMode, byte[] sendData)
|
||||
{
|
||||
switch (targetMode)
|
||||
{
|
||||
case RagonTarget.OWNER:
|
||||
{
|
||||
Send(ent.OwnerId, sendData, DeliveryType.Reliable);
|
||||
break;
|
||||
}
|
||||
case RagonTarget.EXCEPT_OWNER:
|
||||
{
|
||||
_peersCache.Clear();
|
||||
foreach (var playerPeerId in _readyPlayers)
|
||||
if (playerPeerId != ent.OwnerId)
|
||||
_peersCache.Add(playerPeerId);
|
||||
|
||||
Broadcast(_peersCache.ToArray(), sendData, DeliveryType.Reliable);
|
||||
break;
|
||||
}
|
||||
case RagonTarget.ALL:
|
||||
{
|
||||
Broadcast(_readyPlayers, sendData, DeliveryType.Reliable);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Send(uint peerId, byte[] rawData, DeliveryType deliveryType = DeliveryType.Unreliable) =>
|
||||
_gameThread.Server.Send(peerId, rawData, deliveryType);
|
||||
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Ragon.Core;
|
||||
|
||||
public interface ILobby
|
||||
{
|
||||
|
||||
}
|
||||
@@ -5,7 +5,7 @@ using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core;
|
||||
|
||||
public class Lobby : ILobby
|
||||
public class Lobby
|
||||
{
|
||||
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
|
||||
private readonly RagonSerializer _serializer;
|
||||
|
||||
@@ -34,7 +34,7 @@ public class RoomManager
|
||||
{
|
||||
if (existRoom.Id == roomId && existRoom.PlayersCount < existRoom.PlayersMax)
|
||||
{
|
||||
existRoom.Joined(player, payload);
|
||||
existRoom.AddPlayer(player, payload);
|
||||
_roomsBySocket.Add(player.PeerId, existRoom);
|
||||
return;
|
||||
}
|
||||
@@ -55,7 +55,7 @@ public class RoomManager
|
||||
throw new NullReferenceException($"Plugin for map {map} is null");
|
||||
|
||||
var room = new GameRoom(_gameThread, plugin, roomId, map, min, max);
|
||||
room.Joined(creator, payload);
|
||||
room.AddPlayer(creator, payload);
|
||||
room.Start();
|
||||
|
||||
_roomsBySocket.Add(creator.PeerId, room);
|
||||
@@ -76,7 +76,7 @@ public class RoomManager
|
||||
{
|
||||
_logger.Trace($"Player ({player.PlayerName}|{player.Id}) joined to room with Id {roomId}");
|
||||
|
||||
existRoom.Joined(player, payload);
|
||||
existRoom.AddPlayer(player, payload);
|
||||
_roomsBySocket.Add(player.PeerId, existRoom);
|
||||
return;
|
||||
}
|
||||
@@ -90,7 +90,7 @@ public class RoomManager
|
||||
throw new NullReferenceException($"Plugin for map {map} is null");
|
||||
|
||||
var room = new GameRoom(_gameThread, plugin, roomId, map, min, max);
|
||||
room.Joined(player, payload);
|
||||
room.AddPlayer(player, payload);
|
||||
room.Start();
|
||||
|
||||
_roomsBySocket.Add(player.PeerId, room);
|
||||
@@ -102,7 +102,7 @@ public class RoomManager
|
||||
if (_roomsBySocket.Remove(player.PeerId, out var room))
|
||||
{
|
||||
_logger.Trace($"Player ({player.PlayerName}|{player.Id}) left room with Id {room.Id}");
|
||||
room.Leave(player.PeerId);
|
||||
room.RemovePlayer(player.PeerId);
|
||||
if (room.PlayersCount < room.PlayersMin)
|
||||
{
|
||||
_logger.Trace($"Room with Id {room.Id} destroyed");
|
||||
|
||||
Reference in New Issue
Block a user