2023-03-06 10:06:43 +04:00
|
|
|
/*
|
2024-05-19 12:26:42 +03:00
|
|
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
2023-03-06 10:06:43 +04:00
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using Ragon.Protocol;
|
|
|
|
|
|
|
|
|
|
namespace Ragon.Client
|
|
|
|
|
{
|
2023-10-13 10:58:15 +03:00
|
|
|
public sealed class RagonEntity : IDisposable
|
2023-03-06 10:06:43 +04:00
|
|
|
{
|
2023-10-13 10:58:15 +03:00
|
|
|
private class EventSubscription : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private List<Action<RagonPlayer, IRagonEvent>> _callbacks;
|
|
|
|
|
private List<Action<RagonPlayer, IRagonEvent>> _localCallbacks;
|
|
|
|
|
private Action<RagonPlayer, IRagonEvent> _callback;
|
|
|
|
|
|
|
|
|
|
public EventSubscription(
|
|
|
|
|
List<Action<RagonPlayer, IRagonEvent>> callbacks,
|
|
|
|
|
List<Action<RagonPlayer, IRagonEvent>> localCallbacks,
|
|
|
|
|
Action<RagonPlayer, IRagonEvent> callback)
|
|
|
|
|
{
|
|
|
|
|
_callbacks = callbacks;
|
|
|
|
|
_localCallbacks = localCallbacks;
|
|
|
|
|
_callback = callback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2024-01-13 15:15:29 +03:00
|
|
|
_callbacks?.Remove(_callback);
|
|
|
|
|
_localCallbacks?.Remove(_callback);
|
2023-10-13 11:02:56 +03:00
|
|
|
|
|
|
|
|
_callbacks = null!;
|
|
|
|
|
_localCallbacks = null!;
|
|
|
|
|
_callback = null!;
|
2023-10-13 10:58:15 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer);
|
2023-07-09 07:40:06 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
private RagonClient _client;
|
2023-07-09 07:40:06 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
public ushort Id { get; private set; }
|
|
|
|
|
public ushort Type { get; private set; }
|
2024-05-12 10:57:46 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
public RagonAuthority Authority { get; private set; }
|
|
|
|
|
public RagonPlayer Owner { get; private set; }
|
|
|
|
|
public RagonEntityState State { get; private set; }
|
2023-07-09 07:40:06 +03:00
|
|
|
|
2024-05-12 10:57:46 +03:00
|
|
|
public bool IsStatic => SceneId > 0;
|
2023-10-16 23:37:35 +03:00
|
|
|
public bool IsReplicated { get; private set; }
|
2023-03-06 10:06:43 +04:00
|
|
|
public bool IsAttached { get; private set; }
|
|
|
|
|
public bool HasAuthority { get; private set; }
|
|
|
|
|
|
|
|
|
|
public event Action<RagonEntity> Attached;
|
|
|
|
|
public event Action Detached;
|
|
|
|
|
public event Action<RagonPlayer, RagonPlayer> OwnershipChanged;
|
|
|
|
|
|
|
|
|
|
internal bool PropertiesChanged => _propertiesChanged;
|
|
|
|
|
internal ushort SceneId => _sceneId;
|
2023-07-09 07:40:06 +03:00
|
|
|
|
2024-05-12 10:57:46 +03:00
|
|
|
private readonly ushort _sceneId;
|
2023-03-06 10:06:43 +04:00
|
|
|
private bool _propertiesChanged;
|
|
|
|
|
|
|
|
|
|
private RagonPayload _spawnPayload;
|
|
|
|
|
private RagonPayload _destroyPayload;
|
|
|
|
|
|
2024-05-12 10:57:46 +03:00
|
|
|
private readonly Dictionary<int, OnEventDelegate> _events = new();
|
|
|
|
|
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _localListeners = new();
|
|
|
|
|
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _listeners = new();
|
2023-03-06 10:06:43 +04:00
|
|
|
|
2023-10-16 23:37:35 +03:00
|
|
|
public RagonEntity(ushort type = 0, ushort sceneId = 0, bool replicated = true)
|
2023-03-06 10:06:43 +04:00
|
|
|
{
|
|
|
|
|
State = new RagonEntityState(this);
|
|
|
|
|
Type = type;
|
2023-10-16 23:37:35 +03:00
|
|
|
IsReplicated = replicated;
|
2024-05-12 10:57:46 +03:00
|
|
|
|
2023-07-30 16:56:11 +03:00
|
|
|
_spawnPayload = new RagonPayload(0);
|
|
|
|
|
_destroyPayload = new RagonPayload(0);
|
2023-03-06 10:06:43 +04:00
|
|
|
_sceneId = sceneId;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-16 23:37:35 +03:00
|
|
|
internal void Attach()
|
2023-03-06 10:06:43 +04:00
|
|
|
{
|
|
|
|
|
IsAttached = true;
|
2024-05-12 10:57:46 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
Attached?.Invoke(this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-16 23:37:35 +03:00
|
|
|
public void SetReplication(bool enabled)
|
2023-10-11 19:37:50 +03:00
|
|
|
{
|
2023-10-16 23:37:35 +03:00
|
|
|
IsReplicated = enabled;
|
2023-10-11 19:37:50 +03:00
|
|
|
}
|
2023-10-16 23:37:35 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
internal void Detach(RagonPayload payload)
|
|
|
|
|
{
|
|
|
|
|
_destroyPayload = payload;
|
2024-05-12 10:57:46 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
Detached?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal T GetPayload<T>(RagonPayload data) where T : IRagonPayload, new()
|
|
|
|
|
{
|
2023-07-23 15:56:08 +03:00
|
|
|
var payload = new T();
|
|
|
|
|
if (data.Size <= 0) return payload;
|
2023-10-16 23:37:35 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
var buffer = new RagonBuffer();
|
2023-10-16 23:37:35 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
data.Write(buffer);
|
2023-10-16 23:37:35 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
payload.Deserialize(buffer);
|
2023-10-16 23:37:35 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
return payload;
|
|
|
|
|
}
|
2023-10-16 23:37:35 +03:00
|
|
|
|
2024-05-12 10:57:46 +03:00
|
|
|
public void Prepare(
|
|
|
|
|
RagonClient client,
|
|
|
|
|
ushort entityId,
|
|
|
|
|
ushort entityType,
|
|
|
|
|
bool hasAuthority,
|
|
|
|
|
RagonPlayer player,
|
|
|
|
|
RagonPayload payload
|
|
|
|
|
)
|
2023-07-09 07:40:06 +03:00
|
|
|
{
|
2023-10-16 23:37:35 +03:00
|
|
|
Type = entityType;
|
|
|
|
|
Id = entityId;
|
|
|
|
|
HasAuthority = hasAuthority;
|
|
|
|
|
|
|
|
|
|
_client = client;
|
2023-07-29 10:58:06 +03:00
|
|
|
_spawnPayload = payload;
|
2024-05-12 10:57:46 +03:00
|
|
|
|
2023-10-16 23:37:35 +03:00
|
|
|
Owner = player;
|
|
|
|
|
}
|
2023-03-06 10:06:43 +04:00
|
|
|
|
2023-05-07 18:16:46 +03:00
|
|
|
public T GetAttachPayload<T>() where T : IRagonPayload, new()
|
2023-03-06 10:06:43 +04:00
|
|
|
{
|
|
|
|
|
return GetPayload<T>(_spawnPayload);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-07 18:16:46 +03:00
|
|
|
public T GetDetachPayload<T>() where T : IRagonPayload, new()
|
2023-03-06 10:06:43 +04:00
|
|
|
{
|
|
|
|
|
return GetPayload<T>(_destroyPayload);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ReplicateEvent<TEvent>(TEvent evnt, RagonPlayer target, RagonReplicationMode replicationMode)
|
|
|
|
|
where TEvent : IRagonEvent, new()
|
|
|
|
|
{
|
2023-05-07 12:46:39 +03:00
|
|
|
if (!IsAttached)
|
|
|
|
|
{
|
|
|
|
|
RagonLog.Error("Entity not attached");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-07-09 07:40:06 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
var evntId = _client.Event.GetEventCode(evnt);
|
|
|
|
|
var buffer = _client.Buffer;
|
|
|
|
|
|
|
|
|
|
buffer.Clear();
|
|
|
|
|
buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
|
|
|
|
|
buffer.WriteUShort(Id);
|
|
|
|
|
buffer.WriteUShort(evntId);
|
2023-07-09 07:40:06 +03:00
|
|
|
buffer.WriteByte((byte)replicationMode);
|
|
|
|
|
buffer.WriteByte((byte)RagonTarget.Player);
|
2023-05-07 12:46:39 +03:00
|
|
|
buffer.WriteUShort(target.PeerId);
|
2023-03-06 10:06:43 +04:00
|
|
|
|
|
|
|
|
evnt.Serialize(buffer);
|
2023-07-09 07:40:06 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
var sendData = buffer.ToArray();
|
|
|
|
|
_client.Reliable.Send(sendData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ReplicateEvent<TEvent>(
|
|
|
|
|
TEvent evnt,
|
|
|
|
|
RagonTarget target = RagonTarget.All,
|
|
|
|
|
RagonReplicationMode replicationMode = RagonReplicationMode.Server)
|
|
|
|
|
where TEvent : IRagonEvent, new()
|
|
|
|
|
{
|
2023-05-07 12:46:39 +03:00
|
|
|
if (!IsAttached)
|
|
|
|
|
{
|
|
|
|
|
RagonLog.Error("Entity not attached");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-07-09 07:40:06 +03:00
|
|
|
|
2023-07-30 21:46:42 +03:00
|
|
|
var eventCode = _client.Event.GetEventCode(evnt);
|
2023-03-06 10:06:43 +04:00
|
|
|
if (target != RagonTarget.ExceptOwner)
|
|
|
|
|
{
|
|
|
|
|
if (replicationMode == RagonReplicationMode.Local)
|
|
|
|
|
{
|
2023-10-07 20:20:02 +03:00
|
|
|
var localListeners = _localListeners[eventCode];
|
|
|
|
|
foreach (var listener in localListeners)
|
|
|
|
|
listener.Invoke(_client.Room.Local, evnt);
|
2023-03-06 10:06:43 +04:00
|
|
|
return;
|
|
|
|
|
}
|
2023-10-16 23:37:35 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
if (replicationMode == RagonReplicationMode.LocalAndServer)
|
|
|
|
|
{
|
2023-10-07 20:20:02 +03:00
|
|
|
var localListeners = _localListeners[eventCode];
|
|
|
|
|
foreach (var listener in localListeners)
|
|
|
|
|
listener.Invoke(_client.Room.Local, evnt);
|
2023-03-06 10:06:43 +04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var buffer = _client.Buffer;
|
2023-10-16 23:37:35 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
buffer.Clear();
|
|
|
|
|
buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
|
|
|
|
|
buffer.WriteUShort(Id);
|
2023-07-30 21:46:42 +03:00
|
|
|
buffer.WriteUShort(eventCode);
|
2023-07-09 07:40:06 +03:00
|
|
|
buffer.WriteByte((byte)replicationMode);
|
|
|
|
|
buffer.WriteByte((byte)target);
|
2023-03-06 10:06:43 +04:00
|
|
|
|
|
|
|
|
evnt.Serialize(buffer);
|
|
|
|
|
|
|
|
|
|
var sendData = buffer.ToArray();
|
|
|
|
|
_client.Reliable.Send(sendData);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 10:58:15 +03:00
|
|
|
public IDisposable OnEvent<TEvent>(Action<RagonPlayer, TEvent> callback) where TEvent : IRagonEvent, new()
|
2023-03-06 10:06:43 +04:00
|
|
|
{
|
|
|
|
|
var t = new TEvent();
|
|
|
|
|
var eventCode = _client.Event.GetEventCode(t);
|
2023-10-07 20:20:02 +03:00
|
|
|
var action = (RagonPlayer player, IRagonEvent eventData) => callback.Invoke(player, (TEvent)eventData);
|
2023-10-13 10:58:15 +03:00
|
|
|
|
2023-10-11 19:37:50 +03:00
|
|
|
if (!_listeners.TryGetValue(eventCode, out var callbacks))
|
2023-03-06 10:06:43 +04:00
|
|
|
{
|
2023-10-07 20:20:02 +03:00
|
|
|
callbacks = new List<Action<RagonPlayer, IRagonEvent>>();
|
|
|
|
|
_listeners.Add(eventCode, callbacks);
|
|
|
|
|
}
|
2023-10-13 10:58:15 +03:00
|
|
|
|
2023-10-11 19:37:50 +03:00
|
|
|
if (!_localListeners.TryGetValue(eventCode, out var localCallbacks))
|
2023-10-07 20:20:02 +03:00
|
|
|
{
|
|
|
|
|
localCallbacks = new List<Action<RagonPlayer, IRagonEvent>>();
|
2023-10-11 19:37:50 +03:00
|
|
|
_localListeners.Add(eventCode, localCallbacks);
|
2023-03-06 10:06:43 +04:00
|
|
|
}
|
2023-07-09 07:40:06 +03:00
|
|
|
|
2023-10-07 20:20:02 +03:00
|
|
|
callbacks.Add(action);
|
|
|
|
|
localCallbacks.Add(action);
|
|
|
|
|
|
2023-10-11 19:37:50 +03:00
|
|
|
if (!_events.ContainsKey(eventCode))
|
2023-03-06 10:06:43 +04:00
|
|
|
{
|
2023-10-11 19:37:50 +03:00
|
|
|
_events.Add(eventCode, (player, serializer) =>
|
|
|
|
|
{
|
|
|
|
|
t.Deserialize(serializer);
|
2023-10-07 20:20:02 +03:00
|
|
|
|
2023-10-11 19:37:50 +03:00
|
|
|
foreach (var callbackListener in callbacks)
|
|
|
|
|
callbackListener.Invoke(player, t);
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-10-07 20:20:02 +03:00
|
|
|
|
2023-10-13 10:58:15 +03:00
|
|
|
return new EventSubscription(callbacks, localCallbacks, action);
|
2023-03-06 10:06:43 +04:00
|
|
|
}
|
2023-07-09 07:40:06 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
internal void Write(RagonBuffer buffer)
|
|
|
|
|
{
|
|
|
|
|
buffer.WriteUShort(Id);
|
|
|
|
|
|
|
|
|
|
State.WriteState(buffer);
|
|
|
|
|
|
|
|
|
|
_propertiesChanged = false;
|
|
|
|
|
}
|
2024-05-12 10:57:46 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
internal void Read(RagonBuffer buffer)
|
|
|
|
|
{
|
|
|
|
|
State.ReadState(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void Event(ushort eventCode, RagonPlayer caller, RagonBuffer buffer)
|
|
|
|
|
{
|
2023-10-16 23:37:35 +03:00
|
|
|
if (!IsReplicated) return;
|
2024-05-12 10:57:46 +03:00
|
|
|
|
2023-05-07 12:46:39 +03:00
|
|
|
if (_events.TryGetValue(eventCode, out var evnt))
|
|
|
|
|
evnt?.Invoke(caller, buffer);
|
2023-07-09 07:40:06 +03:00
|
|
|
else
|
2023-05-07 12:46:39 +03:00
|
|
|
RagonLog.Warn($"Handler event on entity {Id} with eventCode {eventCode} not defined");
|
2023-03-06 10:06:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void TrackChangedProperty(RagonProperty property)
|
|
|
|
|
{
|
|
|
|
|
_propertiesChanged = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnOwnershipChanged(RagonPlayer player)
|
|
|
|
|
{
|
|
|
|
|
var prevOwner = Owner;
|
2023-07-09 07:40:06 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
Owner = player;
|
2023-07-01 07:47:57 +03:00
|
|
|
HasAuthority = player.IsLocal;
|
2023-07-09 07:40:06 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
OwnershipChanged?.Invoke(prevOwner, player);
|
|
|
|
|
}
|
2023-10-09 09:17:43 +03:00
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_events.Clear();
|
|
|
|
|
_listeners.Clear();
|
|
|
|
|
_localListeners.Clear();
|
|
|
|
|
}
|
2023-03-06 10:06:43 +04:00
|
|
|
}
|
|
|
|
|
}
|