refactor: network entity packets

This commit is contained in:
2025-05-17 22:17:25 +03:00
parent 03082ab43b
commit 721d63cf66
8 changed files with 207 additions and 33 deletions

View File

@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Network;
@@ -7,6 +9,8 @@ namespace Syntriax.Engine.Network;
public class NetworkManager : UniverseObject, INetworkManager
{
private INetworkCommunicator networkCommunicator = null!;
private readonly List<(Type packetType, Delegate callback)> delegates = [];
public INetworkCommunicator NetworkCommunicator
{
get => networkCommunicator;
@@ -18,8 +22,8 @@ public class NetworkManager : UniverseObject, INetworkManager
var previousCommunicator = networkCommunicator;
networkCommunicator = value;
if (previousCommunicator is not null) previousCommunicator.OnPacketReceived -= OnPacketReceived;
if (networkCommunicator is not null) networkCommunicator.OnPacketReceived += OnPacketReceived;
if (previousCommunicator is not null) UnsubscribeDelegates(networkCommunicator);
if (networkCommunicator is not null) SubscribeDelegates(networkCommunicator);
}
}
@@ -31,16 +35,62 @@ public class NetworkManager : UniverseObject, INetworkManager
public NetworkManager()
{
CacheDelegates();
_networkEntityCollector.OnCollected += OnCollected;
_networkEntityCollector.OnRemoved += OnRemoved;
}
private void OnPacketReceived(INetworkCommunicator sender, object packet, string from)
private void CacheDelegates()
{
// if (packet is not EntityDataPacket entityDataPacket)
// return;
// Find network packets implementing EntityDataPacket<>
IEnumerable<Type> packetTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes())
.Where(t => typeof(IEntityNetworkPacket).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract && !t.IsGenericType);
// _networkEntities[entityDataPacket.Entity].ReceiveData(entityDataPacket.Data);
MethodInfo onPacketArrivedMethod = GetType()
.GetMethod(nameof(OnPacketReceived), BindingFlags.NonPublic | BindingFlags.Instance)!;
foreach (Type packetType in packetTypes)
{
MethodInfo genericOnPacketArrivedMethod = onPacketArrivedMethod.MakeGenericMethod(packetType);
Type genericDelegateType = typeof(Action<,>).MakeGenericType(packetType, typeof(string));
Delegate genericPacketReceivedDelegate = Delegate.CreateDelegate(genericDelegateType, this, genericOnPacketArrivedMethod);
delegates.Add((packetType, genericPacketReceivedDelegate));
}
}
private void SubscribeDelegates(INetworkCommunicator networkCommunicator)
{
MethodInfo subscribeToPacketsMethod = typeof(INetworkCommunicator)
.GetMethod(nameof(INetworkCommunicator.SubscribeToPackets), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)!;
foreach ((Type packetType, Delegate callback) in delegates)
{
MethodInfo genericSubscribeMethod = subscribeToPacketsMethod.MakeGenericMethod(packetType);
genericSubscribeMethod.Invoke(networkCommunicator, [callback]);
}
}
private void UnsubscribeDelegates(INetworkCommunicator networkCommunicator)
{
MethodInfo unsubscribeFromPacketsMethod = typeof(INetworkCommunicator)
.GetMethod(nameof(INetworkCommunicator.UnsubscribeFromPackets), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)!;
foreach ((Type packetType, Delegate callback) in delegates)
{
MethodInfo genericUnsubscribeMethod = unsubscribeFromPacketsMethod.MakeGenericMethod(packetType);
genericUnsubscribeMethod.Invoke(networkCommunicator, [callback]);
}
}
private void OnPacketReceived<T>(T entityDataPacket, string fromClientId)
{
if (entityDataPacket is IEntityNetworkPacket entityNetworkPacket)
_networkEntities[entityNetworkPacket.EntityId].ReceiveDataClient(entityDataPacket);
}
private void OnCollected(IBehaviourCollector<INetworkEntity> sender, INetworkEntity behaviourCollected)