using System; using System.Linq; using System.Reflection; using System.Threading; using BlueWest.Core.ComponentSystem; using BlueWest.Core.Threading; using BlueWest.Tools; namespace BlueWest.Core { public sealed class ThreadServer { public bool IsActive { get; private set; } private readonly EventManager _eventManager; public ThreadServer(EventManager eventManager) { _eventManager = eventManager; } public void Init() { InstantiateControllers(); IsActive = true; } /*public static readonly Expression 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(() => { object instantiatiable = Activator.CreateInstance(type); Artefact behavior = instantiatiable as Artefact; behavior.SetupEntity(_eventManager); behavior.RunLoop(); }); } public void SpawnThread( Action action, ThreadPriority threadPriority = ThreadPriority.BelowNormal) { var thread = new Thread(action.Invoke) { Priority = threadPriority }; thread.Start(); } } }