123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- using InABox.Configuration;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace InABox.Core
- {
- public interface IPosterEngine<TPostable>
- where TPostable : Entity, IPostable
- {
- bool Process(IEnumerable<TPostable> posts);
- }
- public abstract class PosterEngine<TPostable, TPoster, TSettings> : IPosterEngine<TPostable>
- where TPostable : Entity, IPostable
- where TPoster : IPoster<TPostable, TSettings>
- where TSettings : PosterSettings<TPostable>, 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<TSettings>(typeof(TPostable).Name).Load();
- }
- protected static string? GetScript()
- {
- return GetSettings().Script;
- }
- public abstract bool Process(IEnumerable<TPostable> posts);
- }
- }
|