This commit is contained in:
CortexCore
2024-11-13 17:47:45 +08:00
parent c4af12acd7
commit 416e3322db
208 changed files with 2591757 additions and 1497 deletions

View File

@@ -8,22 +8,6 @@ namespace BITKit.Entities
{
public class Entity : IEntity, IDisposable
{
private class EntityServiceProvider : IServiceProvider
{
public ServiceProvider ServiceProvider;
public readonly List<Object> Services = new();
public object GetService(Type serviceType)
{
var value = ServiceProvider.GetService(serviceType);
if (value != null)
{
Services.TryAdd(value);
}
return value;
}
}
public Entity()
{
ServiceCollection.AddSingleton<IEntity>(this);
@@ -36,26 +20,12 @@ namespace BITKit.Entities
public CancellationToken CancellationToken => _cancellationTokenSource.Token;
private readonly CancellationTokenSource _cancellationTokenSource = new();
public IServiceProvider ServiceProvider
{
get
{
if (_serviceProvider is not null)
{
return _serviceProvider;
}
var value = new EntityServiceProvider()
{
ServiceProvider = ServiceCollection.BuildServiceProvider()
};
_serviceProvider = value;
return _serviceProvider;
}
}
public IServiceProvider ServiceProvider => _serviceProvider ??= ServiceCollection.BuildServiceProvider();
private ServiceProvider _serviceProvider;
public IServiceCollection ServiceCollection { get; } = new ServiceCollection();
private EntityServiceProvider _serviceProvider;
public object[] GetServices()=> _serviceProvider.Services.ToArray();
public object[] GetServices() => ServiceCollection.ToArray()
.Select(x => _serviceProvider.GetService(x.ServiceType)).ToArray();
public void Inject(object obj)
{
foreach (var fieldInfo in obj.GetType().GetFields(ReflectionHelper.Flags))
@@ -70,11 +40,7 @@ namespace BITKit.Entities
public void Dispose()
{
_cancellationTokenSource.Cancel();
_serviceProvider.ServiceProvider.Dispose();
foreach (var x in GetServices().OfType<IDisposable>())
{
x.Dispose();
}
_serviceProvider.Dispose();
}
}
}

View File

@@ -39,5 +39,29 @@ namespace BITKit
{
return self?.GetInvocationList().Cast<Func<T0, T1, T2, T3>>();
}
public static IEnumerable<Func<T0, T1, T2, T3, T4>> CastAsFunc<T0, T1, T2, T3, T4>(
this Func<T0, T1, T2, T3, T4> self)
{
return self?.GetInvocationList().Cast<Func<T0, T1, T2, T3, T4>>();
}
public static IEnumerable<Func<T0, T1, T2, T3, T4, T5>> CastAsFunc<T0, T1, T2, T3, T4, T5>(
this Func<T0, T1, T2, T3, T4, T5> self)
{
return self?.GetInvocationList().Cast<Func<T0, T1, T2, T3, T4, T5>>();
}
public static IEnumerable<Func<T0, T1, T2, T3, T4, T5, T6>> CastAsFunc<T0, T1, T2, T3, T4, T5, T6>(
this Func<T0, T1, T2, T3, T4, T5, T6> self)
{
return self?.GetInvocationList().Cast<Func<T0, T1, T2, T3, T4, T5, T6>>();
}
public static IEnumerable<Func<T0, T1, T2, T3, T4, T5, T6, T7>> CastAsFunc<T0, T1, T2, T3, T4, T5, T6, T7>(
this Func<T0, T1, T2, T3, T4, T5, T6, T7> self)
{
return self?.GetInvocationList().Cast<Func<T0, T1, T2, T3, T4, T5, T6, T7>>();
}
}
}

View File

@@ -167,6 +167,13 @@ namespace BITKit
}
return false;
}
public static IEnumerable<T> RemoveIn<T>(this IEnumerable<T> self, T t)
{
var list = self.ToList();
list.Remove(t);
return list.ToArray();
}
public static bool TryRemove<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey t)
{
if (self.ContainsKey(t))

View File

@@ -1,8 +0,0 @@
namespace BITKit.IO
{
public interface IAsset
{
string Name { get; set; }
byte[] Buffer{ get; set; }
}
}

View File

@@ -94,6 +94,11 @@ namespace BITKit
/// </summary>
/// <returns></returns>
int Value => 10;
/// <summary>
/// 创建运行时物品
/// </summary>
/// <returns></returns>
IRuntimeItem CreateRuntimeItem();
}
public interface IRuntimeItem : ICloneable
@@ -101,7 +106,7 @@ namespace BITKit
/// <summary>
/// 运行时Id
/// </summary>
public int Id { get; }
public int Id { get; set; }
/// <summary>
/// 配置Id
@@ -111,7 +116,7 @@ namespace BITKit
/// <summary>
/// 数量
/// </summary>
public int Amount { get; }
public int Amount { get; set; }
/// <summary>
/// 运行时属性
@@ -133,14 +138,23 @@ namespace BITKit
/// 被托管的物品
/// </summary>
[Serializable]
public record RuntimeItem : IRuntimeItem
public struct RuntimeItem : IRuntimeItem
{
public int Id { get; set; } = new Random().Next();
public static RuntimeItem Create()
{
var item = new RuntimeItem()
{
Id = new Random().Next(),
RuntimeProperties =
new Dictionary<Type, IScriptableItemProperty>()
};
return item;
}
public int Id { get; set; }
public int ScriptableId { get; set; }
public int Amount { get; set; }
public IDictionary<Type, IScriptableItemProperty> RuntimeProperties { get; set; } =
new Dictionary<Type, IScriptableItemProperty>();
public IDictionary<Type, IScriptableItemProperty> RuntimeProperties { get; set; }
public event Action<IRuntimeItem> OnRuntimePropertiesChanged;

View File

@@ -1,8 +1,13 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.SqlServer.Server;
namespace BITKit
{
/// <summary>
/// 支持二进制化的的物品容器,适用于网络化物品容器
/// 支持属性
@@ -10,6 +15,7 @@ namespace BITKit
/// </summary>
public interface IRuntimeItemContainer:IBinarySerialize
{
public ValidHandle IsBusy { get; }
/// <summary>
/// 物品容器的唯一Id
/// </summary>
@@ -27,6 +33,11 @@ namespace BITKit
/// </summary>
bool Remove(int id);
/// <summary>
/// 设置物品
/// </summary>
/// <param name="id"></param>
void Set(IRuntimeItem id);
/// <summary>
/// 通过通过Id丢下物品
/// </summary>
bool Drop(int id);
@@ -67,4 +78,98 @@ namespace BITKit
/// </summary>
event Action<bool> OnRelease;
}
public class RuntimeItemContainer : IRuntimeItemContainer
{
public RuntimeItemContainer()
{
IsBusy.AddListener(x=>OnRelease?.Invoke(!x));
}
public readonly ConcurrentDictionary<int, IRuntimeItem> Items = new();
public void Read(BinaryReader r)
{
throw new NotImplementedException();
}
public void Write(BinaryWriter w)
{
throw new NotImplementedException();
}
public ValidHandle IsBusy { get; } = new();
public int Id { get; set; }
public IRuntimeItem[] GetItems()=>Items.Values.ToArray();
public bool Add(IRuntimeItem item)
{
using var _ = IsBusy.GetHandle();
foreach (var func in AddFactory.CastAsFunc())
{
if (func.Invoke(item) is false)
{
return false;
}
}
Items.Set(item.Id, item);
OnAdd?.Invoke(item);
//BIT4Log.Log<RuntimeItemContainer>($"添加了了:{item.Id}");
return true;
}
public bool Remove(int id)
{
using var _ = IsBusy.GetHandle();
if (Items.TryGetValue(id, out var item) is false) return false;
foreach (var func in RemoveFactory.CastAsFunc())
{
if (func.Invoke(item) is false)
{
return false;
}
}
Items.TryRemove(item.Id);
OnRemove?.Invoke(item);
//BIT4Log.Log<RuntimeItemContainer>($"移除了:{id}");
return true;
}
public void Set(IRuntimeItem id)
{
using var _ = IsBusy.GetHandle();
if (!Items.TryGetValue(id.Id, out var item)) return;
item = id;
OnSet?.Invoke(item);
}
public bool Drop(int id)
{
using var _ = IsBusy.GetHandle();
if (Items.TryGetValue(id, out var item) is false) return false;
foreach (var func in DropFactory.CastAsFunc())
{
if (func.Invoke(item) is false)
{
return false;
}
}
Items.TryRemove(item.Id);
OnDrop?.Invoke(item);
//BIT4Log.Log<RuntimeItemContainer>($"丢下了:{id}");
return true;
}
public event Func<IRuntimeItem, bool> AddFactory;
public event Func<IRuntimeItem, bool> RemoveFactory;
public event Func<IRuntimeItem, bool> DropFactory;
public event Action<IRuntimeItem> OnAdd;
public event Action<IRuntimeItem> OnRemove;
public event Action<IRuntimeItem> OnSet;
public event Action<IRuntimeItem> OnDrop;
public event Action<IRuntimeItemContainer> OnRebuild;
public event Action<bool> OnRelease;
}
}

View File

@@ -0,0 +1,13 @@
using System.Collections;
using System.Collections.Generic;
namespace BITKit
{
/// <summary>
/// 玩家向背包添加物品,但是被其他逻辑处理
/// </summary>
public sealed class PlayerInventoryAddItemOverridden : InGameException
{
}
}

View File

@@ -0,0 +1,32 @@
using System.Collections;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
namespace BITKit.Pool
{
/// <summary>
/// 对象池服务
/// </summary>
public interface IPoolService
{
/// <summary>
/// 生成对象
/// </summary>
/// <param name="path">可寻址路径</param>
/// <typeparam name="T">类型</typeparam>
/// <returns></returns>
UniTask<T> Spawn<T>(string path) where T : class;
/// <summary>
/// 回收对象
/// </summary>
/// <param name="obj">对象实例</param>
/// <param name="path">可寻址路径</param>
/// <typeparam name="T">类型</typeparam>
void Despawn<T>(T obj,string path) where T : class;
/// <summary>
/// 初始化,在此提前生成所有对象
/// </summary>
/// <returns></returns>
UniTask InitializeAsync();
}
}

View File

@@ -0,0 +1,176 @@
using System;
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Random = UnityEngine.Random;
namespace BITKit.StateMachine
{
/// <summary>
/// 动态异步状态机
/// </summary>
/// <typeparam name="T">异步状态</typeparam>
public class AsyncStateMachine<T>:IStateMachine<T> ,IDisposable where T : class, IStateAsync
{
private readonly IServiceProvider _serviceProvider;
public AsyncStateMachine(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public AsyncStateMachine()
{
_serviceProvider = new ServiceCollection().BuildServiceProvider();
}
public bool Enabled { get; set; } = true;
public T CurrentState { get;private set; }
public T NextOrCurrentState => _nextTargetState.IfNotAllow(CurrentState);
private T _nextState;
public event Action<T, T> OnStateChanging;
public event Action<T, T> OnStateChanged;
public event Action<T> OnStateRegistered;
public IReadOnlyDictionary<int, T> Dictionary => _dictionary;
public event Action<T> OnStateUnRegistered;
public IDictionary<Type, T> StateDictionary { get; } = new Dictionary<Type, T>();
public readonly ValidHandle IsBusy=new();
private readonly CancellationTokenSource _cancellationTokenSource=new();
private readonly Dictionary<int, T> _dictionary = new();
private readonly Optional<T> _nextTargetState = new();
public async void Initialize()
{
await IsBusy;
if(_cancellationTokenSource.IsCancellationRequested)return;
using var _ = IsBusy.GetHandle();
foreach (var (_,value) in StateDictionary)
{
await value.InitializeAsync();
value.Initialize();
}
}
public async void UpdateState(float deltaTime)
{
if (CurrentState is null) return;
using var _ = IsBusy.GetHandle();
await CurrentState.OnStateUpdateAsync(deltaTime);
CurrentState.OnStateUpdate(deltaTime);
}
public async void DisposeState()
{
await IsBusy;
if(_cancellationTokenSource.IsCancellationRequested)return;
if (CurrentState is null) return;
using var _ = IsBusy.GetHandle();
await CurrentState.OnStateExitAsync(CurrentState, null);
if(_cancellationTokenSource.IsCancellationRequested)return;
CurrentState.OnStateExit(CurrentState, null);
CurrentState = null;
}
public T TransitionState<TState>() where TState : T
{
T nextState;
foreach (var (type, value) in Dictionary)
{
if ((typeof(TState) == value.GetType()) is false) continue;
nextState = value;
TransitionState(nextState);
return nextState;
}
nextState = _serviceProvider.GetRequiredService<TState>();
return TransitionState(nextState);;
}
public async UniTask TransitionStateAsync(T nextState)
{
if (nextState is not null)
{
if (nextState.Identifier == 0)
{
nextState.Identifier = nextState.GetHashCode();
}
}
if (Equals(nextState, CurrentState)) return;
if(_nextTargetState.Allow && Equals(_nextTargetState.Value,nextState))return;
if (_nextTargetState.Allow)
{
_nextTargetState.Value = nextState;
return;
}
_nextTargetState.SetValueThenAllow(nextState);
await IsBusy;
if(_cancellationTokenSource.IsCancellationRequested)return;
using var _ = IsBusy.GetHandle();
OnStateChanging?.Invoke(CurrentState,nextState);
if (_dictionary.TryAdd(nextState.Identifier, nextState))
{
await nextState.InitializeAsync();
if(_cancellationTokenSource.IsCancellationRequested)return;
nextState.Initialize();
}
if (CurrentState is not null)
{
CurrentState.Enabled = false;
await CurrentState.OnStateExitAsync(CurrentState, nextState);
if(_cancellationTokenSource.IsCancellationRequested)return;
CurrentState.OnStateExit(CurrentState,nextState);
}
var tempState = CurrentState;
CurrentState = _nextTargetState.Value;
_nextTargetState.Clear();
nextState.Enabled = true;
await nextState.OnStateEntryAsync(tempState);
if(_cancellationTokenSource.IsCancellationRequested)return;
nextState.OnStateEntry(tempState);
OnStateChanged?.Invoke(tempState,nextState);
}
public T TransitionState(T nextState)
{
TransitionStateAsync(nextState).Forget();
return nextState;
}
public async void UnRegister(T newState)
{
if (newState is null) return;
if (Dictionary.ContainsKey(newState.Identifier) is false) return;
_dictionary.Remove(newState.Identifier);
if (Equals(CurrentState, newState))
{
await CurrentState.OnStateExitAsync(CurrentState, null);
CurrentState.OnStateExit(CurrentState, null);
if (CurrentState is IAsyncDisposable asyncDisposable)
{
await asyncDisposable.DisposeAsync();
}
if (CurrentState is IDisposable disposable)
{
disposable.Dispose();
}
CurrentState = null;
}
OnStateUnRegistered?.Invoke(newState);
}
public void Dispose()
{
if(_isDisposed)return;
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
_isDisposed = true;
}
private bool _isDisposed;
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
namespace BITKit.StateMachine
{
@@ -31,10 +32,22 @@ namespace BITKit.StateMachine
void OnStateExit(IState old, IState newState);
}
public interface IStateAsync:IState
{
/// <summary>
/// 识别符,用于识别多个相同状态但不同用途的状态机
/// </summary>
int Identifier { get; set; }
UniTask InitializeAsync();
UniTask OnStateEntryAsync(IState old);
UniTask OnStateUpdateAsync(float deltaTime);
UniTask OnStateExitAsync(IState old, IState newState);
}
public interface IStateMachine<T> where T:IState
{
bool Enabled { get; set; }
T CurrentState { get; set; }
T CurrentState { get; }
event Action<T,T> OnStateChanged;
event Action<T> OnStateRegistered;
event Action<T> OnStateUnRegistered;
@@ -43,14 +56,13 @@ namespace BITKit.StateMachine
void Initialize();
void UpdateState(float deltaTime);
void DisposeState();
void TransitionState<State>() where State : T;
void TransitionState(T state);
T TransitionState<TState>() where TState : T;
T TransitionState(T state);
void Register(T newState) => throw new NotImplementedException("未实现的接口");
void UnRegister(T newState) => throw new NotImplementedException("未实现的接口");
void InvokeOnStateRegistered(T state){}
void InvokeOnStateUnRegistered(T state){}
}
public static class StateMachineUtils
{
public static void Register<T>(this IStateMachine<T> stateMachine, T newState) where T : IState

View File

@@ -43,6 +43,14 @@ namespace BITKit.UX
/// </summary>
/// <param name="panelName">面板名称</param>
void Entry(string panelName);
/// <summary>
/// 当前面板
/// </summary>
IUXPanel CurrentPanel { get; }
/// <summary>
/// 面板改变回调
/// </summary>
public event Action<IUXPanel, IUXPanel> OnPanelChanged;
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
namespace BITKit
{
@@ -22,6 +23,41 @@ namespace BITKit
[CustomType(typeof(IValidHandle))]
public sealed class ValidHandle: IValidHandle
{
public class MyHandle:IDisposable
{
private readonly ValidHandle _validHandle;
private bool _isDisable = false;
public MyHandle(ValidHandle validHandle,bool isDisable = false)
{
_validHandle = validHandle;
_isDisable = isDisable;
if (isDisable)
{
_validHandle.AddDisableElements(this);
}
else
{
_validHandle.AddElement(this);
}
}
public void Dispose()
{
if (_isDisable)
{
_validHandle.RemoveDisableElements(this);
}
else
{
_validHandle.RemoveElement(this);
}
}
}
public MyHandle GetHandle() => new MyHandle(this);
public MyHandle GetDisableHandle()=> new MyHandle(this,true);
public override string ToString()
{
return $"Allow:{enableHandle}\nElements:{string.Join("\n",objs)}\nDisableElements:{string.Join("\n",disableObjs)}";
@@ -51,6 +87,7 @@ namespace BITKit
public readonly List<object> disableObjs = new List<object>();
private bool tempEnable;
private Action<bool> EventOnEnableChanged;
private readonly Queue<UniTaskCompletionSource> _completionSources = new();
public void AddElement(object obj)
@@ -73,7 +110,14 @@ namespace BITKit
{
enableHandle = tempEnable;
if (EventOnEnableChanged is not null)
{
EventOnEnableChanged.Invoke(enableHandle);
}
if (tempEnable) return;
if (_completionSources.TryDequeue(out var cs))
{
cs.TrySetResult();
}
}
}
public void RemoveElement(object obj)
@@ -164,7 +208,16 @@ namespace BITKit
EventOnEnableChanged -= action;
}
}
public UniTask.Awaiter GetAwaiter()
{
if (Allow is false)
{
return UniTask.CompletedTask.GetAwaiter();
}
var cs = new UniTaskCompletionSource();
_completionSources.Enqueue(cs);
return cs.Task.GetAwaiter();
}
public void Clear()
{
objs.Clear();