Files
RobustToolbox/Robust.Shared/Timing/TimerManager.cs
T
83c2a1be11 [Dependency] source generator part 2 (#6550)
* [Dependency] source generator

No more reflection, no more codegen at runtime

Also various changes to Roslyn helpers to make this easier to write.

Requires all types with dependencies to be partial and not have readonly dependency fields. An analyzer enforces this at warning level, the previous injection strategies have remained in the code *for now* as a fallback.

No fallback is available for [field: Dependency] properties, due to a Roslyn bug.

Code Fixes exist. We love Roslyn

* Apply dependencies generator changes to all code

* Release notes

* Preprocessor got hands

* Handle nullable dependencies

These are bad but gotta deal with it.

* Apply suggestions from code review

Co-authored-by: Moony <moony@hellomouse.net>

* Fine, let's not use collection expressions

---------

Co-authored-by: Moony <moony@hellomouse.net>
2026-05-08 12:38:33 +02:00

40 lines
1.2 KiB
C#

using System.Collections.Generic;
using System.Threading;
using Robust.Shared.Exceptions;
using Robust.Shared.IoC;
namespace Robust.Shared.Timing
{
internal sealed partial class TimerManager : ITimerManager
{
[Dependency] private IRuntimeLog _runtimeLog = default!;
private readonly List<(Timer, CancellationToken)> _timers
= new();
public void AddTimer(Timer timer, CancellationToken cancellationToken = default)
{
_timers.Add((timer, cancellationToken));
}
public void UpdateTimers(FrameEventArgs frameEventArgs)
{
// Manual for loop so we can modify the list while enumerating.
// ReSharper disable once ForCanBeConvertedToForeach
for (var i = 0; i < _timers.Count; i++)
{
var (timer, cancellationToken) = _timers[i];
if (cancellationToken.IsCancellationRequested)
{
continue;
}
timer.Update(frameEventArgs.DeltaSeconds, _runtimeLog);
}
_timers.RemoveAll(timer => !timer.Item1.IsActive || timer.Item2.IsCancellationRequested);
}
}
}