feature: first
All checks were successful
Build / Compute Version (push) Successful in 4s
Build / Build Nuget package (CringeBootstrap.Abstractions) (push) Successful in 2m47s
Build / Build Nuget package (CringePlugins) (push) Successful in 5m31s
Build / Build Nuget package (NuGet) (push) Successful in 6m2s
Build / Build Nuget package (SharedCringe) (push) Successful in 7m25s
Build / Build Launcher (push) Successful in 9m11s
All checks were successful
Build / Compute Version (push) Successful in 4s
Build / Build Nuget package (CringeBootstrap.Abstractions) (push) Successful in 2m47s
Build / Build Nuget package (CringePlugins) (push) Successful in 5m31s
Build / Build Nuget package (NuGet) (push) Successful in 6m2s
Build / Build Nuget package (SharedCringe) (push) Successful in 7m25s
Build / Build Launcher (push) Successful in 9m11s
This commit is contained in:
48
CringePlugins/Render/RenderHandler.cs
Normal file
48
CringePlugins/Render/RenderHandler.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System.Collections.Concurrent;
|
||||
using CringePlugins.Abstractions;
|
||||
using NLog;
|
||||
|
||||
namespace CringePlugins.Render;
|
||||
|
||||
public sealed class RenderHandler : IRootRenderComponent
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
||||
void IRenderComponent.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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user