♻️ added checks in client sdk

This commit is contained in:
2023-05-07 12:46:39 +03:00
parent aa607a7eb9
commit fdb41649b2
6 changed files with 31 additions and 11 deletions
+3 -1
View File
@@ -3,4 +3,6 @@
.vs
obj
bin
*.user
*.user
*.dylib
*.dll
+17 -3
View File
@@ -105,6 +105,12 @@ namespace Ragon.Client
public void ReplicateEvent<TEvent>(TEvent evnt, RagonPlayer target, RagonReplicationMode replicationMode)
where TEvent : IRagonEvent, new()
{
if (!IsAttached)
{
RagonLog.Error("Entity not attached");
return;
}
var evntId = _client.Event.GetEventCode(evnt);
var buffer = _client.Buffer;
@@ -114,7 +120,7 @@ namespace Ragon.Client
buffer.WriteUShort(evntId);
buffer.WriteByte((byte) replicationMode);
buffer.WriteByte((byte) RagonTarget.Player);
buffer.WriteUShort((ushort) target.PeerId);
buffer.WriteUShort(target.PeerId);
evnt.Serialize(buffer);
@@ -128,6 +134,12 @@ namespace Ragon.Client
RagonReplicationMode replicationMode = RagonReplicationMode.Server)
where TEvent : IRagonEvent, new()
{
if (!IsAttached)
{
RagonLog.Error("Entity not attached");
return;
}
if (target != RagonTarget.ExceptOwner)
{
if (replicationMode == RagonReplicationMode.Local)
@@ -196,8 +208,10 @@ namespace Ragon.Client
internal void Event(ushort eventCode, RagonPlayer caller, RagonBuffer buffer)
{
if (_events.ContainsKey(eventCode))
_events[eventCode]?.Invoke(caller, buffer);
if (_events.TryGetValue(eventCode, out var evnt))
evnt?.Invoke(caller, buffer);
else
RagonLog.Warn($"Handler event on entity {Id} with eventCode {eventCode} not defined");
}
internal void TrackChangedProperty(RagonProperty property)
@@ -49,7 +49,7 @@ internal class EntityEventHandler : Handler
return;
}
if (player.IsMe && executionMode == RagonReplicationMode.LocalAndServer)
if (player.IsLocal && executionMode == RagonReplicationMode.LocalAndServer)
return;
_entityCache.OnEvent(player, entityId, eventCode, buffer);
+6 -2
View File
@@ -26,8 +26,12 @@ public class RagonEventCache
public ushort GetEventCode<TEvent>(TEvent _) where TEvent : IRagonEvent
{
var type = typeof(TEvent);
var evntCode = _eventsRegistryByType[type];
return evntCode;
if (!_eventsRegistryByType.TryGetValue(type, out var eventCode))
{
RagonLog.Error($"Event with type {type} not registered");
return 0;
}
return eventCode;
}
public void Register<T>() where T : IRagonEvent, new()
+3 -3
View File
@@ -23,13 +23,13 @@ namespace Ragon.Client
public string Name { get; set; }
public ushort PeerId { get; set; }
public bool IsRoomOwner { get; set; }
public bool IsMe { get; set; }
public bool IsLocal { get; set; }
public RagonPlayer(ushort peerId, string playerId, string name, bool isRoomOwner, bool isMe)
public RagonPlayer(ushort peerId, string playerId, string name, bool isRoomOwner, bool isLocal)
{
PeerId = peerId;
IsRoomOwner = isRoomOwner;
IsMe = isMe;
IsLocal = isLocal;
Name = name;
Id = playerId;
}
+1 -1
View File
@@ -50,7 +50,7 @@ public sealed class RagonPlayerCache
var player = new RagonPlayer(peerId, playerId, playerName, isOwner, isLocal);
if (player.IsMe)
if (player.IsLocal)
LocalPlayer = player;
if (player.IsRoomOwner)