using InABox.Configuration; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace InABox.Core { public interface IPosterEngine where TPostable : Entity, IPostable { bool Process(IEnumerable posts); } public abstract class PosterEngine : IPosterEngine where TPostable : Entity, IPostable where TPoster : IPoster where TSettings : PosterSettings, new() { protected static TPoster Poster = GetPoster(); private static Type[]? _posters; private static TPoster GetPoster() { _posters ??= CoreUtils.TypeList( AppDomain.CurrentDomain.GetAssemblies(), x => x.IsClass && !x.IsAbstract && !x.IsGenericType && x.HasInterface(typeof(IPoster<,>)) ).ToArray(); var type = _posters.Where(x => typeof(TPoster).IsAssignableFrom(x)).FirstOrDefault() ?? throw new Exception($"No poster of type {typeof(TPoster)}."); return (TPoster)Activator.CreateInstance(type); } protected static TSettings GetSettings() { return new GlobalConfiguration(typeof(TPostable).Name).Load(); } protected static string? GetScript() { return GetSettings().Script; } public abstract bool Process(IEnumerable posts); } }