This commit is contained in:
CortexCore 2024-06-22 17:58:05 +08:00
parent 46b2da988e
commit 07b6fbc2f3
9 changed files with 256 additions and 6 deletions

View File

@ -317,7 +317,7 @@ namespace BITKit
}
#if NET5_0_OR_GREATER
ServiceProvider = ServiceCollection.BuildServiceProvider();
#endif
#endif
}
public static void Stop()
{
@ -330,7 +330,7 @@ namespace BITKit
BITCommands.Dispose();
BIT4Log.Log<BITApp>($"已停止{nameof(BITApp)}");
BIT4Log.Log<BITApp>($"运行时间:{runTime.ToString("hh\\:mm\\:ss")}");
BIT4Log.Log<BITApp>($"运行时间:{runTime.ToString("hh\\mm\\ss")}");
BIT4Log.Log<BITApp>("Exit Code:0");
}

View File

@ -2,6 +2,8 @@ using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.AccessControl;
using System.Threading;
namespace BITKit.Entities
@ -47,7 +49,7 @@ namespace BITKit.Entities
public IEntity[] Query<T>()
{
throw new NotImplementedException();
throw new NotImplementedException("Obsoleted");
}
public T[] QueryComponents<T>()
@ -77,6 +79,55 @@ namespace BITKit.Entities
}
return list.ToArray();
}
public (T, T1, T2, T3)[] QueryComponents<T, T1, T2, T3>()
{
List<(T, T1, T2, T3)> list = new();
foreach (var entity in _entities.Values)
{
if (entity.TryGetComponent(out T t) && entity.TryGetComponent(out T1 t1) && entity.TryGetComponent(out T2 t2) && entity.TryGetComponent(out T3 t3))
list.Add((t, t1, t2, t3));
}
return list.ToArray();
}
public (T, T1, T2, T3, T4)[] QueryComponents<T, T1, T2, T3, T4>()
{
List<(T, T1, T2, T3, T4)> list = new();
foreach (var entity in _entities.Values)
{
if (entity.TryGetComponent(out T t) && entity.TryGetComponent(out T1 t1) && entity.TryGetComponent(out T2 t2) && entity.TryGetComponent(out T3 t3) && entity.TryGetComponent(out T4 t4))
list.Add((t, t1, t2, t3, t4));
}
return list.ToArray();
}
public (T, T1, T2, T3, T4, T5)[] QueryComponents<T, T1, T2, T3, T4, T5>()
{
List<(T, T1, T2, T3, T4, T5)> list = new();
foreach (var entity in _entities.Values)
{
if (entity.TryGetComponent(out T t) && entity.TryGetComponent(out T1 t1) && entity.TryGetComponent(out T2 t2) && entity.TryGetComponent(out T3 t3) && entity.TryGetComponent(out T4 t4) && entity.TryGetComponent(out T5 t5))
list.Add((t, t1, t2, t3, t4, t5));
}
return list.ToArray();
}
public (T, T1, T2, T3, T4, T5, T6)[] QueryComponents<T, T1, T2, T3, T4, T5, T6>()
{
List<(T, T1, T2, T3, T4, T5, T6)> list = new();
foreach (var entity in _entities.Values)
{
if (entity.TryGetComponent(out T t) && entity.TryGetComponent(out T1 t1) && entity.TryGetComponent(out T2 t2) && entity.TryGetComponent(out T3 t3) && entity.TryGetComponent(out T4 t4) && entity.TryGetComponent(out T5 t5) && entity.TryGetComponent(out T6 t6))
list.Add((t, t1, t2, t3, t4, t5, t6));
}
return list.ToArray();
}
public ValueTuple<T, T1, T2, T3, T4, T5, T6, TRest>[] QueryComponents<T, T1, T2, T3, T4, T5, T6, TRest>() where TRest : struct
{
throw new NotImplementedException();
}
}
}

View File

@ -18,6 +18,7 @@ namespace BITKit.Entities
int Id { get; }
CancellationToken CancellationToken { get; }
bool TryGetComponent<T>(out T component);
bool TryGetComponent(Type type, out IEntityComponent component);
IEntityComponent[] Components { get; }
bool RegisterComponent<T>(T component);
IServiceProvider ServiceProvider { get; }
@ -124,5 +125,63 @@ namespace BITKit.Entities
/// <returns></returns>
ValueTuple<T, T1, T2>[] QueryComponents<T, T1, T2>();
/// <summary>
/// 查询4个组件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <typeparam name="T3"></typeparam>
/// <returns></returns>
ValueTuple<T, T1, T2, T3>[] QueryComponents<T, T1, T2, T3>();
/// <summary>
/// 查询5个组件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <typeparam name="T3"></typeparam>
/// <typeparam name="T4"></typeparam>
/// <returns></returns>
ValueTuple<T, T1, T2, T3, T4>[] QueryComponents<T, T1, T2, T3, T4>();
/// <summary>
/// 查询6个组件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <typeparam name="T3"></typeparam>
/// <typeparam name="T4"></typeparam>
/// <typeparam name="T5"></typeparam>
/// <returns></returns>
ValueTuple<T, T1, T2, T3, T4, T5>[] QueryComponents<T, T1, T2, T3, T4, T5>();
/// <summary>
/// 查询7个组件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <typeparam name="T3"></typeparam>
/// <typeparam name="T4"></typeparam>
/// <typeparam name="T5"></typeparam>
/// <typeparam name="T6"></typeparam>
/// <returns></returns>
ValueTuple<T, T1, T2, T3, T4, T5, T6>[] QueryComponents<T, T1, T2, T3, T4, T5, T6>();
/// <summary>
/// 查询8个组件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <typeparam name="T3"></typeparam>
/// <typeparam name="T4"></typeparam>
/// <typeparam name="T5"></typeparam>
/// <typeparam name="T6"></typeparam>
/// <typeparam name="TRest">剩余实例</typeparam>
/// <returns></returns>
ValueTuple<T, T1, T2, T3, T4, T5, T6, TRest>[] QueryComponents<T, T1, T2, T3, T4, T5, T6, TRest>() where TRest : struct;
}
}

View File

@ -7,7 +7,7 @@ namespace BITKit.Net
public static readonly KcpConfig Config = new KcpConfig(
NoDelay: true,
DualMode: false,
Interval: 32, // 1ms so at interval code at least runs.
Interval: 8, // 1ms so at interval code at least runs.
Timeout: 8000,
CongestionWindow: false

View File

@ -9,6 +9,7 @@ using System.Threading.Tasks;
using System.IO;
using System.Numerics;
using System.Reflection;
using System.Text;
using BITKit.Net.Examples;
using Newtonsoft.Json;
using Unity.Mathematics;
@ -456,12 +457,17 @@ namespace BITKit.Net
public void AddRpcHandle(object rpcHandle)
{
var reportBuilder = new StringBuilder();
reportBuilder.AppendLine($"正在通过反射注册{rpcHandle.GetType().Name}");
foreach (var methodInfo in rpcHandle.GetType().GetMethods())
{
var att = methodInfo.GetCustomAttribute<NetRpcAttribute>();
if(att is null)continue;
_rpcMethods.AddOrUpdate(methodInfo.Name, methodInfo, (s, info) => methodInfo);
_rpcHandles.AddOrUpdate(methodInfo.Name, rpcHandle, (s, o) => rpcHandle);
reportBuilder.AppendLine($"Add [{methodInfo.Name}] as MethodInfo");
}
foreach (var eventInfo in rpcHandle.GetType().GetEvents())
@ -470,7 +476,12 @@ namespace BITKit.Net
if(att is null)continue;
var handle = eventInfo.EventHandlerType.GetMethod("Invoke");
_rpcMethods.AddOrUpdate(handle.Name, handle, (s, info) => handle);
_rpcHandles.AddOrUpdate(handle.Name, rpcHandle, (s, info) => rpcHandle);
reportBuilder.AppendLine($"Add [{eventInfo.Name} as EventInfo]");
}
BIT4Log.Log<KcpNetClient>(reportBuilder);
}
public void AddCommandListener<T>(Action<T> handle)

View File

@ -40,6 +40,7 @@ namespace BITKit.Net
private readonly ConcurrentDictionary<int,DateTime> _lastHeartbeat = new();
private readonly ConcurrentQueue<(int id,byte[] bytes)> _sendQueue = new();
private readonly ConcurrentDictionary<int,int> _dropCount = new();
private DateTime _now=DateTime.Now;
private TimeSpan _interval=TimeSpan.FromSeconds(0.32);
@ -91,6 +92,7 @@ namespace BITKit.Net
//BIT4Log.Log<KCPNetServer>($"{Name}目前有{server.connections.Count}个链接");
_dropCount.Clear();
while (_sendQueue.TryDequeue(out var value))
{
@ -100,9 +102,15 @@ namespace BITKit.Net
}
else
{
BIT4Log.Log<KCPNetServer>($"链接{value.id}已丢失,丢弃了{value.bytes.Length}个字节");
int UpdateValueFactory(int i, int i1) => i1 + value.bytes.Length;
_dropCount.AddOrUpdate(value.id,value.bytes.Length,UpdateValueFactory);
}
}
foreach (var (id,length) in _dropCount)
{
BIT4Log.Log<KCPNetServer>($"未找到链接:{id},已丢弃字节数量:{length}");
}
}
catch (SocketException)
{
@ -114,6 +122,8 @@ namespace BITKit.Net
}
}
public void StartServer(ushort port = 27014)
{
if (IsRunningServer is false)

View File

@ -53,8 +53,13 @@ namespace BITKit.Entities
public int HealthPoint
{
get => healthPoint;
set => OnHealthPointChangedInternal(healthPoint, value);
set
{
if(value == healthPoint)return;
OnHealthPointChangedInternal(healthPoint, value);
}
}
public int MaxHealthPoint
{
get => maxHealthPoint;

View File

@ -26,6 +26,18 @@ namespace BITKit.Entities
}
public CancellationToken CancellationToken { get; private set; }
public IEntityBehavior[] Behaviors { get;private set; }
public bool TryGetComponent(Type type, out IEntityComponent component)
{
var value = genericEvent.Get(type.FullName);
if (value is not null)
{
component = (IEntityComponent)value;
return true;
}
component = default!;
return false;
}
public IEntityComponent[] Components => Behaviors.Cast<IEntityComponent>().ToArray();
private bool _initialized;
bool Entities.IEntity.RegisterComponent<T>(T component)

View File

@ -63,6 +63,31 @@ public class UnityEntitiesServiceSingleton:IEntitiesService
{
return UnityEntitiesService.QueryComponents<T, T1, T2>();
}
public (T, T1, T2, T3)[] QueryComponents<T, T1, T2, T3>()
{
return UnityEntitiesService.QueryComponents<T, T1, T2, T3>();
}
public (T, T1, T2, T3, T4)[] QueryComponents<T, T1, T2, T3, T4>()
{
return UnityEntitiesService.QueryComponents<T, T1, T2, T3, T4>();
}
public (T, T1, T2, T3, T4, T5)[] QueryComponents<T, T1, T2, T3, T4, T5>()
{
return UnityEntitiesService.QueryComponents<T, T1, T2, T3, T4, T5>();
}
public (T, T1, T2, T3, T4, T5, T6)[] QueryComponents<T, T1, T2, T3, T4, T5, T6>()
{
return UnityEntitiesService.QueryComponents<T, T1, T2, T3, T4, T5, T6>();
}
public ValueTuple<T, T1, T2, T3, T4, T5, T6, TRest>[] QueryComponents<T, T1, T2, T3, T4, T5, T6, TRest>() where TRest : struct
{
throw new NotImplementedException();
}
}
public class UnityEntitiesService : MonoBehaviour,IEntitiesService
@ -198,4 +223,81 @@ public class UnityEntitiesService : MonoBehaviour,IEntitiesService
ListPool<(T t, T1 t1, T2 t2)>.Release(list);
return value;
}
(T, T1, T2, T3)[] IEntitiesService.QueryComponents<T, T1, T2, T3>() => QueryComponents<T, T1, T2, T3>();
public static (T, T1, T2, T3)[] QueryComponents<T, T1, T2, T3>()
{
var list = ListPool<(T, T1, T2, T3)>.Get();
foreach (var iEntity in Entities)
{
switch (iEntity as Entity)
{
case var x when x != null && x.TryGetComponentAny<T>(out var t) && x.TryGetComponentAny<T1>(out var t1) && x.TryGetComponentAny<T2>(out var t2) && x.TryGetComponentAny<T3>(out var t3):
list.Add((t, t1, t2, t3));
break;
}
}
var value = list.ToArray();
list.Clear();
ListPool<(T, T1, T2, T3)>.Release(list);
return value;
}
(T, T1, T2, T3, T4)[] IEntitiesService.QueryComponents<T, T1, T2, T3, T4>() => QueryComponents<T, T1, T2, T3, T4>();
public static (T, T1, T2, T3, T4)[] QueryComponents<T, T1, T2, T3, T4>()
{
var list = ListPool<(T, T1, T2, T3, T4)>.Get();
foreach (var iEntity in Entities)
{
switch (iEntity as Entity)
{
case var x when x != null && x.TryGetComponentAny<T>(out var t) && x.TryGetComponentAny<T1>(out var t1) && x.TryGetComponentAny<T2>(out var t2) && x.TryGetComponentAny<T3>(out var t3) && x.TryGetComponentAny<T4>(out var t4):
list.Add((t, t1, t2, t3, t4));
break;
}
}
var value = list.ToArray();
list.Clear();
ListPool<(T, T1, T2, T3, T4)>.Release(list);
return value;
}
(T, T1, T2, T3, T4, T5)[] IEntitiesService.QueryComponents<T, T1, T2, T3, T4, T5>() => QueryComponents<T, T1, T2, T3, T4, T5>();
public static (T, T1, T2, T3, T4, T5)[] QueryComponents<T, T1, T2, T3, T4, T5>()
{
var list = ListPool<(T, T1, T2, T3, T4, T5)>.Get();
foreach (var iEntity in Entities)
{
switch (iEntity as Entity)
{
case var x when x != null && x.TryGetComponentAny<T>(out var t) && x.TryGetComponentAny<T1>(out var t1) && x.TryGetComponentAny<T2>(out var t2) && x.TryGetComponentAny<T3>(out var t3) && x.TryGetComponentAny<T4>(out var t4) && x.TryGetComponentAny<T5>(out var t5):
list.Add((t, t1, t2, t3, t4, t5));
break;
}
}
var value = list.ToArray();
list.Clear();
ListPool<(T, T1, T2, T3, T4, T5)>.Release(list);
return value;
}
(T, T1, T2, T3, T4, T5, T6)[] IEntitiesService.QueryComponents<T, T1, T2, T3, T4, T5, T6>() => QueryComponents<T, T1, T2, T3, T4, T5, T6>();
public ValueTuple<T, T1, T2, T3, T4, T5, T6, TRest>[] QueryComponents<T, T1, T2, T3, T4, T5, T6, TRest>() where TRest : struct
{
throw new NotImplementedException();
}
public static (T, T1, T2, T3, T4, T5, T6)[] QueryComponents<T, T1, T2, T3, T4, T5, T6>()
{
var list = ListPool<(T, T1, T2, T3, T4, T5, T6)>.Get();
foreach (var iEntity in Entities)
{
switch (iEntity as Entity)
{
case var x when x != null && x.TryGetComponentAny<T>(out var t) && x.TryGetComponentAny<T1>(out var t1) && x.TryGetComponentAny<T2>(out var t2) && x.TryGetComponentAny<T3>(out var t3) && x.TryGetComponentAny<T4>(out var t4) && x.TryGetComponentAny<T5>(out var t5) && x.TryGetComponentAny<T6>(out var t6):
list.Add((t, t1, t2, t3, t4, t5, t6));
break;
}
}
var value = list.ToArray();
list.Clear();
ListPool<(T, T1, T2, T3, T4, T5, T6)>.Release(list);
return value;
}
}