diff --git a/.gitignore b/.gitignore index 42c30e2..dfc25ee 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ .vs obj bin -*.user \ No newline at end of file +*.user +*.dylib +*.dll \ No newline at end of file diff --git a/Ragon.Client/Sources/Entity/RagonEntity.cs b/Ragon.Client/Sources/Entity/RagonEntity.cs index 818a6ec..06a3ccb 100644 --- a/Ragon.Client/Sources/Entity/RagonEntity.cs +++ b/Ragon.Client/Sources/Entity/RagonEntity.cs @@ -105,6 +105,12 @@ namespace Ragon.Client public void ReplicateEvent(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) diff --git a/Ragon.Client/Sources/Handler/EntityEventHandler.cs b/Ragon.Client/Sources/Handler/EntityEventHandler.cs index d08e115..fb8593d 100644 --- a/Ragon.Client/Sources/Handler/EntityEventHandler.cs +++ b/Ragon.Client/Sources/Handler/EntityEventHandler.cs @@ -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); diff --git a/Ragon.Client/Sources/RagonEventCache.cs b/Ragon.Client/Sources/RagonEventCache.cs index b0b752e..13cbc56 100644 --- a/Ragon.Client/Sources/RagonEventCache.cs +++ b/Ragon.Client/Sources/RagonEventCache.cs @@ -26,8 +26,12 @@ public class RagonEventCache public ushort GetEventCode(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() where T : IRagonEvent, new() diff --git a/Ragon.Client/Sources/RagonPlayer.cs b/Ragon.Client/Sources/RagonPlayer.cs index e8b8f4a..c81ce99 100644 --- a/Ragon.Client/Sources/RagonPlayer.cs +++ b/Ragon.Client/Sources/RagonPlayer.cs @@ -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; } diff --git a/Ragon.Client/Sources/RagonPlayerCache.cs b/Ragon.Client/Sources/RagonPlayerCache.cs index c5d8f0a..fca23cc 100644 --- a/Ragon.Client/Sources/RagonPlayerCache.cs +++ b/Ragon.Client/Sources/RagonPlayerCache.cs @@ -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)