Added UI Toolkit Progress

This commit is contained in:
CortexCore
2024-12-13 16:14:20 +08:00
parent 21b4f9091a
commit d502501b27
20 changed files with 660 additions and 134 deletions

View File

@@ -0,0 +1,55 @@
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Reflection;
namespace BITKit
{
public static class DisplayAttributeExtensions
{
public static string GetDisplayName(this PropertyInfo self)
{
if (self.GetCustomAttribute<DisplayNameAttribute>() is not { } displayNameAttribute) return self.Name;
var name = displayNameAttribute.DisplayName;
return string.IsNullOrEmpty(name) ? self.Name : name;
}
public static string GetDisplayName(this FieldInfo self)
{
if (self.GetCustomAttribute<DisplayNameAttribute>() is not { } displayNameAttribute) return self.Name;
var name = displayNameAttribute.DisplayName;
return string.IsNullOrEmpty(name) ? self.Name : name;
}
public static string GetDisplayName(this object self)
{
{
if (self.GetType().GetCustomAttribute<DisplayNameAttribute>() is { } displayNameAttribute)
{
return displayNameAttribute.DisplayName;
}
if (self.GetType().GetCustomAttribute<DisplayAttribute>() is { } displayAttribute)
{
return displayAttribute.Name;
}
}
{
if (self is Enum)
{
var field = self.GetType().GetField(self.ToString()!);
if (field.GetCustomAttribute<DisplayNameAttribute>() is DisplayNameAttribute displayNameAttribute)
{
return displayNameAttribute.DisplayName;
}
if (field.GetCustomAttribute<DisplayAttribute>() is DisplayAttribute displayAttribute)
{
return displayAttribute.Name;
}
}
}
return self.ToString();
}
}
}

View File

@@ -11,6 +11,10 @@ namespace BITKit
public static async UniTask<string> PostJsonAsync(this HttpClient self, string requestUrl, object value , CancellationToken cancellationToken=default)
{
var json = JsonConvert.SerializeObject(value);
if (value is string j)
{
json = j;
}
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response =await self.PostAsync(requestUrl, content, cancellationToken);
return await response.Content.ReadAsStringAsync();

View File

@@ -1,5 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Timers;
using Cysharp.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace BITKit
{
@@ -34,6 +39,16 @@ namespace BITKit
void ManualTick(float delta);
}
/// <summary>
/// 异步循环
/// </summary>
public interface IAsyncTicker
{
ulong TickCount { get; }
int TickRate { get; set; }
bool IsConcurrent { get; set; }
event Func<float, UniTask> OnTickAsync;
}
/// <summary>
/// 主线程循环
/// </summary>
public interface IMainTicker : ITicker { }
@@ -53,7 +68,106 @@ namespace BITKit
/// </summary>
public interface IFixedTicker : ITicker{}
#endif
public class AsyncTicker : IAsyncTicker,IDisposable
{
private readonly ILogger<AsyncTicker> _logger;
private readonly Timer _timer=new();
private bool _isDisposed;
private readonly Stopwatch _stopwatch=new();
public AsyncTicker(ILogger<AsyncTicker> logger)
{
_logger = logger;
if (TickRate <= 0)
{
TickRate = 1;
}
_timer.Elapsed += OnElapsed;
if (_isDisposed is false)
_timer.Start();
_logger.LogInformation($"异步循环就绪,TickRate:{TickRate}");
}
#if UNITY_5_3_OR_NEWER
[UnityEngine.HideInCallstack]
#endif
private async void OnElapsed(object sender, ElapsedEventArgs e)
{
try
{
if(_isDisposed)return;
if (IsSyncContext)
{
await BITApp.SwitchToMainThread();
if(_isDisposed)return;
}
var deltaTime = 1f / TickRate;
if (_stopwatch.IsRunning)
{
deltaTime = (float)_stopwatch.Elapsed.TotalSeconds;
_stopwatch.Reset();
}
_stopwatch.Start();
if (IsConcurrent)
{
var tasks = OnTickAsync.CastAsFunc().ToArray();
foreach (var func in tasks)
{
if (_isDisposed) return;
try
{
await func.Invoke(deltaTime);
}
catch (Exception exception)
{
_logger.LogCritical(exception,exception.Message);
}
}
}
else
{
try
{
await OnTickAsync.UniTaskFunc(deltaTime);
}
catch (Exception exception)
{
_logger.LogCritical(exception,exception.Message);
}
}
}
catch (Exception exception)
{
_logger.LogCritical(exception,exception.Message);
}
TickCount++;
if(_isDisposed)return;
_timer.Start();
}
public bool IsSyncContext { get; set; } = true;
public ulong TickCount { get; set; }
public int TickRate { get; set; }
public bool IsConcurrent { get; set; }
public event Func<float, UniTask> OnTickAsync;
public void Dispose()
{
}
}
public class Ticker : ITicker
{

View File

@@ -18,7 +18,7 @@ namespace BITKit
public void Dispose()
{
_stopwatch.Stop();
_logger.LogInformation($"已完成:{_message},耗时{_stopwatch.Elapsed}");
_logger.LogInformation($"已完成:{_message},耗时{_stopwatch.ElapsedMilliseconds}ms");
}
}
}