PosterUtils.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using InABox.Configuration;
  2. using InABox.Core.Postable;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Runtime;
  8. using System.Text;
  9. namespace InABox.Core
  10. {
  11. public static class PosterUtils
  12. {
  13. private class EngineType
  14. {
  15. public Type Engine { get; set; }
  16. public Type Entity { get; set; }
  17. public Type Poster { get; set; }
  18. }
  19. private static EngineType[]? _posterEngines;
  20. private static Type[]? _posters = null;
  21. public static Type[] GetPosters()
  22. {
  23. _posters ??= CoreUtils.TypeList(
  24. AppDomain.CurrentDomain.GetAssemblies(),
  25. x => x.IsClass
  26. && !x.IsAbstract
  27. && !x.IsGenericType
  28. && x.HasInterface(typeof(IPoster<,>))).ToArray();
  29. return _posters;
  30. }
  31. private static EngineType[] GetPosterEngines()
  32. {
  33. _posterEngines ??= CoreUtils.TypeList(
  34. AppDomain.CurrentDomain.GetAssemblies(),
  35. x => x.IsClass
  36. && !x.IsAbstract
  37. && x.GetTypeInfo().GenericTypeParameters.Length == 1
  38. && x.HasInterface(typeof(IPosterEngine<,,>))
  39. ).Select(x => new EngineType
  40. {
  41. Engine = x,
  42. Entity = x.GetInterfaceDefinition(typeof(IPosterEngine<,,>))!.GenericTypeArguments[0],
  43. Poster = x.GetInterfaceDefinition(typeof(IPosterEngine<,,>))!.GenericTypeArguments[1].GetGenericTypeDefinition()
  44. }).ToArray();
  45. return _posterEngines;
  46. }
  47. private static PostableSettings FixPostableSettings<TPostable>(PostableSettings settings)
  48. where TPostable : Entity, IPostable
  49. {
  50. if (string.IsNullOrWhiteSpace(settings.PostableType))
  51. {
  52. settings.PostableType = typeof(TPostable).EntityName();
  53. }
  54. return settings;
  55. }
  56. public static PostableSettings LoadPostableSettings<T>()
  57. where T : Entity, IPostable
  58. {
  59. return FixPostableSettings<T>(new GlobalConfiguration<PostableSettings>(typeof(T).Name).Load());
  60. }
  61. public static void SavePostableSettings<T>(PostableSettings settings)
  62. where T : Entity, IPostable
  63. {
  64. new GlobalConfiguration<PostableSettings>(typeof(T).Name).Save(FixPostableSettings<T>(settings));
  65. }
  66. private static TSettings FixPosterSettings<TPostable, TSettings>(TSettings settings)
  67. where TPostable : IPostable
  68. where TSettings : PosterSettings, new()
  69. {
  70. if (string.IsNullOrWhiteSpace(settings.PostableType))
  71. {
  72. settings.PostableType = typeof(TPostable).EntityName();
  73. }
  74. return settings;
  75. }
  76. private static MethodInfo _loadPosterSettingsMethod = typeof(PosterUtils).GetMethods(BindingFlags.Static | BindingFlags.Public)
  77. .Where(x => x.Name == nameof(LoadPosterSettings) && x.IsGenericMethod)
  78. .Single();
  79. private static MethodInfo _savePosterSettingsMethod = typeof(PosterUtils).GetMethods(BindingFlags.Static | BindingFlags.Public)
  80. .Where(x => x.Name == nameof(SavePosterSettings) && x.IsGenericMethod)
  81. .Single();
  82. public static TSettings LoadPosterSettings<TPostable, TSettings>()
  83. where TPostable : IPostable
  84. where TSettings : PosterSettings, new()
  85. {
  86. return FixPosterSettings<TPostable, TSettings>(new GlobalConfiguration<TSettings>(typeof(TPostable).Name).Load());
  87. }
  88. public static PosterSettings LoadPosterSettings(Type TPostable, Type TSettings)
  89. {
  90. return (_loadPosterSettingsMethod.MakeGenericMethod(TPostable, TSettings).Invoke(null, Array.Empty<object>()) as PosterSettings)!;
  91. }
  92. public static void SavePosterSettings<TPostable, TSettings>(TSettings settings)
  93. where TPostable : IPostable
  94. where TSettings : PosterSettings, new()
  95. {
  96. new GlobalConfiguration<TSettings>(typeof(TPostable).Name).Save(FixPosterSettings<TPostable, TSettings>(settings));
  97. }
  98. public static void SavePosterSettings(Type TPostable, Type TSettings, PosterSettings settings)
  99. {
  100. _savePosterSettingsMethod.MakeGenericMethod(TPostable, TSettings).Invoke(null, new object[] { settings });
  101. }
  102. /// <summary>
  103. /// Get the <see cref="IPosterEngine{TPostable,TPoster,TSettings}"/> for <typeparamref name="T"/>
  104. /// based on the current <see cref="PostableSettings"/> for <typeparamref name="T"/>.
  105. /// </summary>
  106. /// <typeparam name="T"></typeparam>
  107. /// <returns></returns>
  108. public static Type GetEngine<T>()
  109. where T : Entity, IPostable, IRemotable, IPersistent, new()
  110. {
  111. var settings = LoadPostableSettings<T>();
  112. if (string.IsNullOrWhiteSpace(settings.PosterType))
  113. {
  114. throw new MissingSettingsException(typeof(T));
  115. }
  116. var poster = GetPosters()?.FirstOrDefault(x => x.EntityName() == settings.PosterType)!;
  117. var engines = GetPosterEngines().Where(x => poster.HasInterface(x.Poster)).ToList();
  118. if (!engines.Any())
  119. {
  120. throw new Exception("No poster for the given settings.");
  121. }
  122. else if(engines.Count == 1)
  123. {
  124. return engines[0].Engine.MakeGenericType(typeof(T));
  125. }
  126. else
  127. {
  128. return engines.Single(x => x.Entity == typeof(T)).Engine.MakeGenericType(typeof(T));
  129. }
  130. }
  131. public static IPosterEngine<T> CreateEngine<T>()
  132. where T : Entity, IPostable, IRemotable, IPersistent, new()
  133. {
  134. var engine = GetEngine<T>();
  135. return (Activator.CreateInstance(engine) as IPosterEngine<T>)!;
  136. }
  137. /// <summary>
  138. /// Process <paramref name="model"/> with the currently set <see cref="IPosterEngine{TPostable, TPoster, TSettings}"/>
  139. /// for <typeparamref name="T"/>.
  140. /// </summary>
  141. /// <typeparam name="T">The type of <paramref name="model"/> that needs to be processed.</typeparam>
  142. /// <param name="model"></param>
  143. /// <exception cref="EmptyPostException">If there are no items to post. In this case, nothing happens.</exception>
  144. /// <exception cref="RepostedException">If any of the <typeparamref name="T"/> have already been processed. In this case, nothing happens.</exception>
  145. /// <exception cref="MissingSettingsException">If the <see cref="PostableSettings"/> for <typeparamref name="T"/> do not exist.</exception>
  146. /// <exception cref="PostCancelledException">If the post has been cancelled by the user.</exception>
  147. /// <returns><see langword="null"/> if post was unsuccessful.</returns>
  148. ///
  149. public static IPostResult<T>? Process<T>(IDataModel<T> model)
  150. where T : Entity, IPostable, IRemotable, IPersistent, new()
  151. {
  152. return CreateEngine<T>().Process(model);
  153. }
  154. }
  155. }