This commit is contained in:
CortexCore
2023-10-24 23:38:22 +08:00
parent 2c4710bc5d
commit bd40165ade
152 changed files with 3681 additions and 1531 deletions

View File

@@ -299,7 +299,7 @@ namespace BITKit
BITCommands.Dispose();
BIT4Log.Log<BITApp>($"已停止{nameof(BITApp)}");
BIT4Log.Log<BITApp>($"运行时间:{runTime}");
BIT4Log.Log<BITApp>($"运行时间:{runTime.ToString("hh\\:mm\\:ss")}");
BIT4Log.Log<BITApp>("Exit Code:0");
}

View File

@@ -4,11 +4,22 @@ namespace BITKit
{
[AttributeUsage(AttributeTargets.Method)]
public class BITCommandAttribute : Attribute { }
/// <summary>
/// 自动注入依赖
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
public class InjectAttribute : System.Attribute
{
public readonly bool CanBeNull;
public InjectAttribute()
{
}
public InjectAttribute(bool canBeNull)
{
CanBeNull = canBeNull;
}
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true, Inherited = true)]
public class CustomTypeAttribute : System.Attribute
{
@@ -19,4 +30,11 @@ namespace BITKit
Type = type;
}
}
#if UNITY_64
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true, Inherited = true)]
public class ExportAttribute : System.Attribute
{
}
#endif
}

View File

@@ -1,11 +1,15 @@
using System;
using System.Diagnostics;
#if UNITY_64
using UnityEngine;
#endif
namespace BITKit
{
public static class BIT4Log
{
#if UNITY_EDITOR
[UnityEngine.RuntimeInitializeOnLoadMethod]
[RuntimeInitializeOnLoadMethod]
private static void Reload()
{
OnLog = null;
@@ -21,12 +25,17 @@ namespace BITKit
public static event Action<ConsoleColor> OnSetConsoleColor;
public static event Action OnNextLine;
private static Type currentType;
#if UNITY_64
[HideInCallstack]
#endif
public static void Log(object x, int debugLevel = 0, ConsoleColor color = ConsoleColor.White)
{
OnSetConsoleColor?.Invoke(color);
OnLog?.Invoke(x?.ToString());
}
#if UNITY_64
[HideInCallstack]
#endif
public static void Log<T>(object x, int debugLevel = 0, ConsoleColor color = ConsoleColor.White)
{
if (currentType != typeof(T))
@@ -41,15 +50,23 @@ namespace BITKit
currentType = typeof(T);
}
#if UNITY_64
[HideInCallstack]
#endif
public static void LogException(Exception e)
{
OnException?.Invoke(e);
}
#if UNITY_64
[HideInCallstack]
#endif
public static void Warning(object x, int debugLevel = 0)
{
OnWarning?.Invoke(x.ToString());
}
#if UNITY_64
[HideInCallstack]
#endif
public static void Warning<T>(object x, int debugLevel = 0)
{
Warning($"{typeof(T).Name}:{x}", debugLevel);

View File

@@ -9,6 +9,7 @@ namespace BITKit
/// <typeparam name="T"></typeparam>
public interface IDoubleBuffer<T>
{
bool CanRelease { get; }
T Current { get; }
void Release(T newValue);
event Action<T> OnRelease;
@@ -20,6 +21,8 @@ namespace BITKit
/// <typeparam name="T"></typeparam>
public class DoubleBuffer<T> : IDoubleBuffer<T>
{
public bool CanRelease => _release.Allow;
public T Current
{
get;
@@ -34,6 +37,11 @@ namespace BITKit
_release.SetValueThenAllow(newValue);
}
public void Clear()
{
_release.Clear();
}
public event Action<T> OnRelease;
private readonly Optional<T> _release=new();

View File

@@ -17,6 +17,7 @@ namespace BITKit.Core.Entites
IEntityComponent[] Components { get; }
bool RegisterComponent<T>(T component);
IServiceProvider ServiceProvider { get; }
void Inject(object obj);
}
/// <summary>
/// 基本实体组件

View File

@@ -1,24 +1,38 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace BITKit
{
public static class FuncExtensions
{
public static IEnumerable<Func<T0,T1>> CastAsFunc<T0,T1>(this Func<T0,T1> self)
{
return self.GetInvocationList().Cast<Func<T0, T1>>();
}
public static IEnumerable<Func<T0,T1,T2>> CastAsFunc<T0,T1,T2>(this Func<T0,T1,T2> self)
{
return self.GetInvocationList().Cast<Func<T0, T1,T2>>();
}
public static IEnumerable<Func<T0,T1,T2,T3>> CastAsFunc<T0,T1,T2,T3>(this Func<T0,T1,T2,T3> self)
{
return self.GetInvocationList().Cast<Func<T0, T1,T2,T3>>();
}
}
public static class FuncExtensions
{
public static IEnumerable<Func<T0, T1>> CastAsFunc<T0, T1>(this Func<T0, T1> self)
{
return self is null ? Array.Empty<Func<T0,T1>>() : self?.GetInvocationList().Cast<Func<T0, T1>>();
}
public static IEnumerable<Func<T0, T1, T2>> CastAsFunc<T0, T1, T2>(this Func<T0, T1, T2> self)
{
if (self is null)
{
yield break;
}
Delegate[] invocationList = self.GetInvocationList();
// 遍历委托数组,将每个委托转换为 Func<T0, T1, T2> 并返回
foreach (var delegateItem in invocationList)
{
if (delegateItem is not null && delegateItem is Func<T0, T1, T2> func)
{
yield return func;
}
}
}
public static IEnumerable<Func<T0, T1, T2, T3>> CastAsFunc<T0, T1, T2, T3>(this Func<T0, T1, T2, T3> self)
{
return self?.GetInvocationList().Cast<Func<T0, T1, T2, T3>>();
}
}
}

View File

@@ -205,11 +205,11 @@ namespace BITKit
}
public static IEnumerable<T> TrySelectFirst<T>(this IEnumerable<T> self, Action<T> action)
{
if (self.Count() > 0)
if (self.Any())
{
action.Invoke(self.First());
}
return self;
return self;
}
public static bool TryGetAny<T>(this IEnumerable<T> self, Func<T, bool> factory, out T any)
{

View File

@@ -1,13 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Cysharp.Threading.Tasks;
// ReSharper disable MethodHasAsyncOverload
namespace BITKit
{
public interface IEntryGroup { }
public interface IEntryGroup
{
}
public interface IEntryElement
{
bool IsEntered { get; set; }
void Entry();
UniTask EntryAsync();
void Exit();
UniTask ExitAsync();
}
[System.Serializable]
@@ -15,7 +24,11 @@ namespace BITKit
{
public int index = -1;
public List<T> list = new();
int m_index;
private int m_index = -1;
private bool completed=true;
public event Action<T> OnEntry;
public event Action<T> OnExit;
public void Entry(T t)
{
if (t is not null)
@@ -60,24 +73,53 @@ namespace BITKit
value = default;
return false;
}
private void EnsureConfiguration()
private async void EnsureConfiguration()
{
if (MathE.Equals(this.index, m_index))
try
{
if(completed is false) return;
completed = false;
if (index == m_index)
{
}
else
{
var currentIndex = m_index;
m_index = index;
if (currentIndex is not -1 && list.TryGetElementAt(currentIndex, out var element))
{
element.Exit();
try
{
await element.ExitAsync();
}
catch (OperationCanceledException)
{
}
element.IsEntered = false;
OnExit?.Invoke(element);
}
if (index is not -1 && list.TryGetElementAt(index, out element))
{
element.IsEntered = true;
element.Entry();
try
{
await element.EntryAsync();
}
catch (OperationCanceledException){}
OnEntry?.Invoke(element);
}
}
completed = true;
}
else
catch (Exception e)
{
if (m_index is not -1)
{
list[m_index].Exit();
}
if (index is not -1)
{
list[index].Entry();
}
BIT4Log.LogException(e);
}
m_index = index;
}
}
}

View File

@@ -73,10 +73,10 @@ namespace BITKit
#endregion
#region
/// <summary>
/// 可序列化的物品
/// 被托管的物品
/// </summary>
[Serializable]
public record SerializableItem : IBasicItem
public record ManagedItem : IBasicItem
{
#region
public int Id;

View File

@@ -41,6 +41,7 @@ namespace BITKit
/// 通过通过Id丢下物品
/// </summary>
bool Drop(int Id);
bool DropOrSpawn(IBasicItem item);
/// <summary>
/// 注册添加物品的工厂方法,
/// </summary>
@@ -57,6 +58,7 @@ namespace BITKit
/// 已添加Item的回调
/// </summary>
event Action<IBasicItem> OnAdd;
/// <summary>
/// 已移除Item的回调
/// </summary>

View File

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

View File

@@ -48,14 +48,17 @@ namespace BITKit.Net
});
}
private async void Tick(object sender, ElapsedEventArgs e)
private void Tick(object sender, ElapsedEventArgs e)
{
await UniTask.SwitchToThreadPool();
//await UniTask.SwitchToThreadPool();
while (commandQueue.TryDequeue(out var bytes))
{
client.Send(bytes, KcpChannel.Reliable);
}
client.Tick();
//for (var i = 0; i < 32; i++)
{
client.Tick();
}
}
public async void Disconnect()
@@ -88,6 +91,10 @@ namespace BITKit.Net
if (BITApp.SynchronizationContext is not null)
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext);
OnConnected?.Invoke();
if (client.connected)
{
SendServerMessage(Environment.MachineName);
}
return client.connected;
}
catch (Exception e)
@@ -136,7 +143,7 @@ namespace BITKit.Net
break;
case NetCommandType.Heartbeat:
//client.Send(new[] { (byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
client.Send(new[] { (byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
break;
default:
BIT4Log.Log<KcpClient>($"未知消息类型:{type},字节:{(byte)type}");

View File

@@ -41,7 +41,7 @@ namespace BITKit.Net
private void Tick(object sender, ElapsedEventArgs e)
{
if (server.IsActive() is false) return;
server.Tick();
server.Tick();
}
public void StartServer(ushort port = 27014)
@@ -66,7 +66,7 @@ namespace BITKit.Net
{
foreach (var Id in server.connections.Keys)
{
SendMessageToClient(Id,message);
}
}
@@ -108,9 +108,11 @@ namespace BITKit.Net
using var ms = new MemoryStream(bytes.ToArray());
using var reader = new BinaryReader(ms);
BIT4Log.Log<INetServer>(Id);
//BIT4Log.Log<INetServer>(Id);
var type = (NetCommandType)ms.ReadByte();
//BIT4Log.Log<INetServer>(type);
switch (type)
{
case NetCommandType.Message:
@@ -119,7 +121,7 @@ namespace BITKit.Net
case NetCommandType.Command:
var command = BITBinary.Read(reader);
if (command is object[] { Length: 1 } objs) command = objs[0];
BIT4Log.Log<KCPNetServer>($"已收到指令:{command},值:\n{JsonConvert.SerializeObject(command, Formatting.Indented)}");
//BIT4Log.Log<KCPNetServer>($"已收到指令:{command},值:\n{JsonConvert.SerializeObject(command, Formatting.Indented)}");
_events.Invoke(command.GetType().FullName, command);
(int Id,object Command) tuple = (Id,command);
@@ -189,13 +191,20 @@ namespace BITKit.Net
public void AddCommandListenerWithId<T>(Action<int, T> handle)
{
_events.AddListenerDirect(typeof(T).FullName, x =>
_events.AddListenerDirect(typeof(T).FullName, Callback);
return;
void Callback(object value)
{
if (x is ValueTuple<int, T> tuple)
if (value is ValueTuple<int, object> tuple && tuple.Item2 is T)
{
handle.Invoke(tuple.Item1,tuple.Item2);
handle.Invoke(tuple.Item1, (T)tuple.Item2);
}
});
else
{
Console.WriteLine(value);
}
}
}
public void RemoveCommandListener<T>(Action<T> handle)

View File

@@ -4,6 +4,6 @@ namespace BITKit
{
public static class MathO
{
public static T As<T>(this object self) where T : class => self as T;
}
}

View File

@@ -45,8 +45,8 @@ namespace BITKit.UX
}
public abstract class UXPanelImplement:IUXPanel
{
protected virtual IUXPanel _iuxPanelImplementation1 { get; }
protected abstract IUXPanel _iuxPanelImplementation { get; }
protected abstract IUXPanel service { get; }
private IUXPanel _iuxPanelImplementation => service;
public bool IsAnimate => _iuxPanelImplementation.IsAnimate;
public bool IsValid => _iuxPanelImplementation.IsValid;
@@ -69,14 +69,14 @@ namespace BITKit.UX
public event Action OnEntry
{
add => _iuxPanelImplementation1.OnEntry += value;
remove => _iuxPanelImplementation1.OnEntry -= value;
add => _iuxPanelImplementation.OnEntry += value;
remove => _iuxPanelImplementation.OnEntry -= value;
}
public event Action OnExit
{
add => _iuxPanelImplementation1.OnExit += value;
remove => _iuxPanelImplementation1.OnExit -= value;
add => _iuxPanelImplementation.OnExit += value;
remove => _iuxPanelImplementation.OnExit -= value;
}
}
}

View File

@@ -30,6 +30,7 @@ namespace BITKit
public const string Input = "Input";
public const string Output = "Output";
public const string Graphic = "Graphic";
public const string HotFix = "HotFix";
}
public partial struct Animation
{

View File

@@ -61,14 +61,29 @@ namespace BITKit
}
public void InvokeDirect(string key,object value)
{
var list = events.Get(key);
list.ToArray().ForEach(x =>
foreach (var x in events.Get(key))
{
if (x is Action<object> action)
{
//Console.WriteLine(action.Method);
action.Invoke(value);
}
});
else
{
throw new ArgumentException($"事件{key}的监听器{x}不是Action<object>类型");
}
}
// list.ToArray().ForEach(x =>
// {
// if (x is Action<object> action)
// {
// action.Invoke(value);
// }
// else
// {
// Console.WriteLine(x);
// }
// });
}
public void Invoke(string typeName, object value)
{