PosterUtils.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #region Postable Settings
  48. private static PostableSettings FixPostableSettings<TPostable>(PostableSettings settings)
  49. where TPostable : Entity, IPostable
  50. {
  51. if (string.IsNullOrWhiteSpace(settings.PostableType))
  52. {
  53. settings.PostableType = typeof(TPostable).EntityName();
  54. }
  55. return settings;
  56. }
  57. public static PostableSettings LoadPostableSettings<T>()
  58. where T : Entity, IPostable
  59. {
  60. return FixPostableSettings<T>(new GlobalConfiguration<PostableSettings>(typeof(T).Name).Load());
  61. }
  62. public static void SavePostableSettings<T>(PostableSettings settings)
  63. where T : Entity, IPostable
  64. {
  65. new GlobalConfiguration<PostableSettings>(typeof(T).Name).Save(FixPostableSettings<T>(settings));
  66. }
  67. #endregion
  68. #region Poster Settings
  69. private static TSettings FixPosterSettings<TPostable, TSettings>(TSettings settings)
  70. where TPostable : IPostable
  71. where TSettings : PosterSettings, new()
  72. {
  73. if (string.IsNullOrWhiteSpace(settings.PostableType))
  74. {
  75. settings.PostableType = typeof(TPostable).EntityName();
  76. }
  77. return settings;
  78. }
  79. private static MethodInfo _loadPosterSettingsMethod = typeof(PosterUtils).GetMethods(BindingFlags.Static | BindingFlags.Public)
  80. .Where(x => x.Name == nameof(LoadPosterSettings) && x.IsGenericMethod)
  81. .Single();
  82. private static MethodInfo _savePosterSettingsMethod = typeof(PosterUtils).GetMethods(BindingFlags.Static | BindingFlags.Public)
  83. .Where(x => x.Name == nameof(SavePosterSettings) && x.IsGenericMethod)
  84. .Single();
  85. public static TSettings LoadPosterSettings<TPostable, TSettings>()
  86. where TPostable : IPostable
  87. where TSettings : PosterSettings, new()
  88. {
  89. return FixPosterSettings<TPostable, TSettings>(new GlobalConfiguration<TSettings>(typeof(TPostable).Name).Load());
  90. }
  91. public static PosterSettings LoadPosterSettings(Type TPostable, Type TSettings)
  92. {
  93. return (_loadPosterSettingsMethod.MakeGenericMethod(TPostable, TSettings).Invoke(null, Array.Empty<object>()) as PosterSettings)!;
  94. }
  95. public static void SavePosterSettings<TPostable, TSettings>(TSettings settings)
  96. where TPostable : IPostable
  97. where TSettings : PosterSettings, new()
  98. {
  99. new GlobalConfiguration<TSettings>(typeof(TPostable).Name).Save(FixPosterSettings<TPostable, TSettings>(settings));
  100. }
  101. public static void SavePosterSettings(Type TPostable, Type TSettings, PosterSettings settings)
  102. {
  103. _savePosterSettingsMethod.MakeGenericMethod(TPostable, TSettings).Invoke(null, new object[] { settings });
  104. }
  105. #endregion
  106. #region Global Poster Settings
  107. private static MethodInfo _loadGlobalPosterSettingsMethod = typeof(PosterUtils).GetMethods(BindingFlags.Static | BindingFlags.Public)
  108. .Where(x => x.Name == nameof(LoadGlobalPosterSettings) && x.IsGenericMethod)
  109. .Single();
  110. private static MethodInfo _saveGlobalPosterSettingsMethod = typeof(PosterUtils).GetMethods(BindingFlags.Static | BindingFlags.Public)
  111. .Where(x => x.Name == nameof(SaveGlobalPosterSettings) && x.IsGenericMethod)
  112. .Single();
  113. public static IGlobalPosterSettings LoadGlobalPosterSettings(Type TGlobalSettings)
  114. {
  115. return (_loadGlobalPosterSettingsMethod.MakeGenericMethod(TGlobalSettings).Invoke(null, Array.Empty<object>()) as IGlobalPosterSettings)!;
  116. }
  117. public static TGlobalSettings LoadGlobalPosterSettings<TGlobalSettings>()
  118. where TGlobalSettings : BaseObject, IGlobalPosterSettings, new()
  119. {
  120. return new GlobalConfiguration<TGlobalSettings>().Load();
  121. }
  122. public static void SaveGlobalPosterSettings<TGlobalSettings>(TGlobalSettings settings)
  123. where TGlobalSettings : BaseObject, IGlobalPosterSettings, new()
  124. {
  125. new GlobalConfiguration<TGlobalSettings>().Save(settings);
  126. }
  127. public static void SaveGlobalPosterSettings(Type TGlobalSettings, IGlobalPosterSettings settings)
  128. {
  129. _saveGlobalPosterSettingsMethod.MakeGenericMethod(TGlobalSettings).Invoke(null, new object[] { settings });
  130. }
  131. #endregion
  132. /// <summary>
  133. /// Get the <see cref="IPosterEngine{TPostable,TPoster,TSettings}"/> for <typeparamref name="T"/>
  134. /// based on the current <see cref="PostableSettings"/> for <typeparamref name="T"/>.
  135. /// </summary>
  136. /// <typeparam name="T"></typeparam>
  137. /// <returns></returns>
  138. public static Type GetEngine<T>()
  139. where T : Entity, IPostable, IRemotable, IPersistent, new()
  140. {
  141. var settings = LoadPostableSettings<T>();
  142. if (string.IsNullOrWhiteSpace(settings.PosterType))
  143. {
  144. throw new MissingSettingsException(typeof(T));
  145. }
  146. var poster = GetPosters()?.FirstOrDefault(x => x.EntityName() == settings.PosterType)!;
  147. var engines = GetPosterEngines().Where(x => poster.HasInterface(x.Poster)).ToList();
  148. if (!engines.Any())
  149. {
  150. throw new Exception("No poster for the given settings.");
  151. }
  152. else if(engines.Count == 1)
  153. {
  154. return engines[0].Engine.MakeGenericType(typeof(T));
  155. }
  156. else
  157. {
  158. return engines.Single(x => x.Entity == typeof(T)).Engine.MakeGenericType(typeof(T));
  159. }
  160. }
  161. public static IPosterEngine<T> CreateEngine<T>()
  162. where T : Entity, IPostable, IRemotable, IPersistent, new()
  163. {
  164. var engine = GetEngine<T>();
  165. return (Activator.CreateInstance(engine) as IPosterEngine<T>)!;
  166. }
  167. /// <summary>
  168. /// Process <paramref name="model"/> with the currently set <see cref="IPosterEngine{TPostable, TPoster, TSettings}"/>
  169. /// for <typeparamref name="T"/>.
  170. /// </summary>
  171. /// <typeparam name="T">The type of <paramref name="model"/> that needs to be processed.</typeparam>
  172. /// <param name="model"></param>
  173. /// <exception cref="EmptyPostException">If there are no items to post. In this case, nothing happens.</exception>
  174. /// <exception cref="RepostedException">If any of the <typeparamref name="T"/> have already been processed. In this case, nothing happens.</exception>
  175. /// <exception cref="MissingSettingsException">If the <see cref="PostableSettings"/> for <typeparamref name="T"/> do not exist.</exception>
  176. /// <exception cref="PostCancelledException">If the post has been cancelled by the user.</exception>
  177. /// <returns><see langword="null"/> if post was unsuccessful.</returns>
  178. ///
  179. public static IPostResult<T>? Process<T>(IDataModel<T> model)
  180. where T : Entity, IPostable, IRemotable, IPersistent, new()
  181. {
  182. return CreateEngine<T>().Process(model);
  183. }
  184. }
  185. }