CodeLiturgy.Dashboard/CodeLiturgy/Core/System/ThreadServer.cs

84 lines
2.2 KiB
C#
Raw Normal View History

2021-12-06 02:49:27 +03:00
using System;
using System.Linq;
using System.Reflection;
using System.Threading;
using BlueWest.Core.ComponentSystem;
using BlueWest.Tools;
namespace BlueWest.Core
{
public sealed class ThreadServer
{
public bool IsActive { get; private set; }
2022-08-19 19:47:35 +03:00
public EventManager EventManager => _eventManager;
2021-12-06 02:49:27 +03:00
private readonly EventManager _eventManager;
public ThreadServer(EventManager eventManager)
{
_eventManager = eventManager;
}
public void Init()
{
InstantiateControllers();
IsActive = true;
}
/*public static readonly Expression<Entity, EntityDto> MappingExpression = x => new EntityDto
{
Name = x.Name,
...
};*/
private static Type[]? GetTypesFromBehaviors()
{
return Assembly.GetAssembly(typeof(Artefact))
?.GetTypes()
.Where(aType => aType.IsClass
&& !aType.IsAbstract
&& aType.IsSubclassOf(typeof(Artefact)))
.ToArray();
}
private void InstantiateControllers()
{
var types = GetTypesFromBehaviors();
if (types == null)
{
return;
}
foreach (var t in types)
{
InstantiateBehavior(t);
}
}
private void InstantiateBehavior(Type type)
{
SpawnThread(() =>
{
2022-09-10 00:33:17 +03:00
#pragma warning disable CS8600
object instantiate = Activator.CreateInstance(type);
#pragma warning restore CS8600
#pragma warning disable CS8600
Artefact behavior = instantiate as Artefact;
#pragma warning restore CS8600
#pragma warning disable CS8602
2021-12-06 02:49:27 +03:00
behavior.SetupEntity(_eventManager);
2022-09-10 00:33:17 +03:00
#pragma warning restore CS8602
2021-12-06 02:49:27 +03:00
behavior.RunLoop();
});
}
public void SpawnThread( Action action, ThreadPriority threadPriority = ThreadPriority.BelowNormal)
{
var thread = new Thread(action.Invoke)
{
Priority = threadPriority
};
2022-08-19 19:47:35 +03:00
2021-12-06 02:49:27 +03:00
thread.Start();
}
}
}