Files
se-launcher/CringePlugins/Render/RenderHandler.cs
2024-10-22 21:39:31 +07:00

48 lines
1.3 KiB
C#

using System.Collections.Concurrent;
using CringePlugins.Abstractions;
using NLog;
namespace CringePlugins.Render;
public sealed class RenderHandler : IDisposable
{
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
private static RenderHandler? _current;
public static RenderHandler Current => _current ?? throw new InvalidOperationException("Render is not yet initialized");
private readonly ConcurrentBag<ComponentRegistration> _components = [];
internal RenderHandler()
{
_current = this;
}
public void RegisterComponent<TComponent>(TComponent instance) where TComponent : IRenderComponent
{
_components.Add(new ComponentRegistration(typeof(TComponent), instance));
}
internal void OnFrame()
{
foreach (var (instanceType, renderComponent) in _components)
{
try
{
renderComponent.OnFrame();
}
catch (Exception e)
{
Log.Error(e, "Component {TypeName} failed to render a new frame", instanceType);
}
}
}
private record ComponentRegistration(Type InstanceType, IRenderComponent Instance);
public void Dispose()
{
_current = null;
_components.Clear();
}
}