This commit is contained in:
CortexCore
2024-03-31 23:31:00 +08:00
parent e179d2eb53
commit b7b89ee71a
641 changed files with 31286 additions and 22134 deletions

View File

@@ -4,11 +4,10 @@ namespace BITKit
{
public interface ITicker
{
ulong TickCount { get; }
void Add(Action action);
void AddAsUpdate(Action<float> action);
void AddAsFixedUpdate(Action<float> action);
void RemoveAsUpdate(Action<float> action);
void RemoveAsFixedUpdate(Action<float> action);
void Add(Action<float> action);
void Remove(Action<float> action);
}
public interface IMainTicker : ITicker { }
public interface IThreadTicker : ITicker { }

View File

@@ -1,107 +0,0 @@
using System;
using System.Collections.Generic;
using System.Threading;
using Timer = System.Timers.Timer;
namespace BITKit
{
public class ThreadHelper : IThreadTicker
{
internal static void InitAPP()
{
ThreadHelper helper = new();
singleton = helper;
DI.Register<ThreadHelper>(helper);
DI.Register<IThreadTicker>(helper);
}
static ThreadHelper singleton;
public static void LogCurrentThread()
{
var currentTheadID = Thread.CurrentThread.ManagedThreadId.ToString();
}
private readonly List<Action<float>> Updates = new();
private readonly List<Action<float>> FixedUpdates = new();
private readonly CancellationToken CancellationToken=new();
private readonly List<Timer> timers = new();
public void AddAsUpdate(Action<float> action)
{
Updates.Add(action);
}
public void AddAsFixedUpdate(Action<float> action)
{
FixedUpdates.Add(action);
}
public void RemoveAsUpdate(Action<float> action)
{
Updates.Remove(action);
}
public void RemoveAsFixedUpdate(Action<float> action)
{
FixedUpdates.Remove(action);
}
public void Add(Action action)
{
try
{
new Thread(action.Invoke).Start();
}
catch (System.Exception e)
{
BIT4Log.LogException(e);
}
}
[ExcuteOnAwake]
public static void Start()
{
var ticker = DI.Get<ThreadHelper>();
singleton = ticker;
new Thread(singleton.Init).Start();
}
[ExcuteOnStop]
public static void Stop()
{
foreach (var timer in singleton.timers)
{
timer.Stop();
timer.Dispose();
}
singleton.Updates.Clear();
singleton.FixedUpdates.Clear();
}
void Init()
{
CreateTimer(Updates, 1 / 64f);
CreateTimer(FixedUpdates, 1 / 32f);
}
Timer CreateTimer(List<Action<float>> actions, float deltaTime)
{
Timer timer = new();
timer.Interval = TimeSpan.FromSeconds(deltaTime).Milliseconds;
timer.Elapsed += (x, y) =>
{
try
{
CancellationToken.ThrowIfCancellationRequested();
foreach (var action in actions.ToArray())
{
action.Invoke(deltaTime);
}
}
catch (System.OperationCanceledException)
{
return;
}
catch (System.Exception e)
{
BIT4Log.LogException(e);
}
};
timer.AutoReset = true;
timer.Enabled = true;
timer.Start();
timers.Add(timer);
return timer;
}
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 80daba647c9a302418cae1bd15d9a078
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: