BITKit/Src/Core/Ticker/ITicker.cs

207 lines
5.7 KiB
C#

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
{
/// <summary>
/// 循环
/// </summary>
public interface ITicker
{
/// <summary>
/// 总帧数
/// </summary>
ulong TickCount { get; }
/// <summary>
/// 在下一次循环时执行,仅执行一次
/// </summary>
/// <param name="action"></param>
void Add(Action action);
/// <summary>
/// 注册在下一次循环时执行,每次循环都会执行
/// </summary>
/// <param name="action"></param>
void Add(Action<float> action);
/// <summary>
/// 移除循环
/// </summary>
/// <param name="action"></param>
void Remove(Action<float> action);
/// <summary>
/// 手动调用循环
/// </summary>
/// <param name="delta"></param>
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 { }
/// <summary>
/// 线程池循环
/// </summary>
public interface IThreadTicker : ITicker { }
#if UNITY_5_3_OR_NEWER
/// <summary>
/// 最后执行的循环,通常用于旋转、位移等
/// </summary>
public interface IAfterTicker : ITicker{}
/// <summary>
/// Unity专用固定循环
/// </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.Interval = 1000d / TickRate;
_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 UNITY_EDITOR
if (UnityEditor.EditorApplication.isPaused)
{
_timer.Start();
return;
}
#endif
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()
{
_isDisposed = true;
_timer.Stop();
_timer.Dispose();
}
}
public class Ticker : ITicker
{
private readonly Queue<Action> _queue = new();
private event Action<float> TickEvents;
public ulong TickCount { get; private set; }
public void Add(Action action)
{
_queue.Enqueue(action);
}
public void Add(Action<float> action)=>TickEvents += action;
public void Remove(Action<float> action)=>TickEvents -= action;
public void ManualTick(float delta)
{
TickCount++;
while (_queue.TryDequeue(out var action))
{
action.Invoke();
}
TickEvents?.Invoke(delta);
}
}
}