PosterUtils.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #region Postable Settings
  14. private static PostableSettings FixPostableSettings(Type TPostable, PostableSettings settings)
  15. {
  16. if (string.IsNullOrWhiteSpace(settings.PostableType))
  17. {
  18. settings.PostableType = TPostable.EntityName();
  19. }
  20. return settings;
  21. }
  22. public static PostableSettings LoadPostableSettings<T>()
  23. where T : Entity, IPostable
  24. {
  25. return FixPostableSettings(typeof(T), new GlobalConfiguration<PostableSettings>(typeof(T).Name).Load());
  26. }
  27. public static void SavePostableSettings<T>(PostableSettings settings)
  28. where T : Entity, IPostable
  29. {
  30. new GlobalConfiguration<PostableSettings>(typeof(T).Name).Save(FixPostableSettings(typeof(T), settings));
  31. }
  32. #endregion
  33. #region Poster Settings
  34. private static TSettings FixPosterSettings<TPostable, TSettings>(TSettings settings)
  35. where TPostable : IPostable
  36. where TSettings : PosterSettings, new()
  37. {
  38. if (string.IsNullOrWhiteSpace(settings.PostableType))
  39. {
  40. settings.PostableType = typeof(TPostable).EntityName();
  41. }
  42. return settings;
  43. }
  44. private static MethodInfo _loadPosterSettingsMethod = typeof(PosterUtils).GetMethods(BindingFlags.Static | BindingFlags.Public)
  45. .Where(x => x.Name == nameof(LoadPosterSettings) && x.IsGenericMethod)
  46. .Single();
  47. private static MethodInfo _savePosterSettingsMethod = typeof(PosterUtils).GetMethods(BindingFlags.Static | BindingFlags.Public)
  48. .Where(x => x.Name == nameof(SavePosterSettings) && x.IsGenericMethod)
  49. .Single();
  50. public static TSettings LoadPosterSettings<TPostable, TSettings>()
  51. where TPostable : IPostable
  52. where TSettings : PosterSettings, new()
  53. {
  54. return FixPosterSettings<TPostable, TSettings>(new GlobalConfiguration<TSettings>(typeof(TPostable).Name).Load());
  55. }
  56. public static PosterSettings LoadPosterSettings(Type TPostable, Type TSettings)
  57. {
  58. return (_loadPosterSettingsMethod.MakeGenericMethod(TPostable, TSettings).Invoke(null, Array.Empty<object>()) as PosterSettings)!;
  59. }
  60. public static void SavePosterSettings<TPostable, TSettings>(TSettings settings)
  61. where TPostable : IPostable
  62. where TSettings : PosterSettings, new()
  63. {
  64. new GlobalConfiguration<TSettings>(typeof(TPostable).Name).Save(FixPosterSettings<TPostable, TSettings>(settings));
  65. }
  66. public static void SavePosterSettings(Type TPostable, Type TSettings, PosterSettings settings)
  67. {
  68. _savePosterSettingsMethod.MakeGenericMethod(TPostable, TSettings).Invoke(null, new object[] { settings });
  69. }
  70. #endregion
  71. #region Global Poster Settings
  72. private static MethodInfo _loadGlobalPosterSettingsMethod = typeof(PosterUtils).GetMethods(BindingFlags.Static | BindingFlags.Public)
  73. .Where(x => x.Name == nameof(LoadGlobalPosterSettings) && x.IsGenericMethod)
  74. .Single();
  75. private static MethodInfo _saveGlobalPosterSettingsMethod = typeof(PosterUtils).GetMethods(BindingFlags.Static | BindingFlags.Public)
  76. .Where(x => x.Name == nameof(SaveGlobalPosterSettings) && x.IsGenericMethod)
  77. .Single();
  78. public static IGlobalPosterSettings LoadGlobalPosterSettings(Type TGlobalSettings)
  79. {
  80. return (_loadGlobalPosterSettingsMethod.MakeGenericMethod(TGlobalSettings).Invoke(null, Array.Empty<object>()) as IGlobalPosterSettings)!;
  81. }
  82. public static TGlobalSettings LoadGlobalPosterSettings<TGlobalSettings>()
  83. where TGlobalSettings : BaseObject, IGlobalPosterSettings, new()
  84. {
  85. return new GlobalConfiguration<TGlobalSettings>().Load();
  86. }
  87. public static void SaveGlobalPosterSettings<TGlobalSettings>(TGlobalSettings settings)
  88. where TGlobalSettings : BaseObject, IGlobalPosterSettings, new()
  89. {
  90. new GlobalConfiguration<TGlobalSettings>().Save(settings);
  91. }
  92. public static void SaveGlobalPosterSettings(Type TGlobalSettings, IGlobalPosterSettings settings)
  93. {
  94. _saveGlobalPosterSettingsMethod.MakeGenericMethod(TGlobalSettings).Invoke(null, new object[] { settings });
  95. }
  96. #endregion
  97. #region Poster Engines
  98. private class EngineType
  99. {
  100. public Type Engine { get; set; }
  101. public Type Entity { get; set; }
  102. public Type Poster { get; set; }
  103. }
  104. private static EngineType[]? _posterEngines;
  105. private static Type[]? _posters = null;
  106. public static Type[] GetPosters()
  107. {
  108. _posters ??= CoreUtils.TypeList(
  109. AppDomain.CurrentDomain.GetAssemblies(),
  110. x => x.IsClass
  111. && !x.IsAbstract
  112. && !x.IsGenericType
  113. && x.HasInterface(typeof(IPoster<,>))).ToArray();
  114. return _posters;
  115. }
  116. private static EngineType[] GetPosterEngines()
  117. {
  118. _posterEngines ??= CoreUtils.TypeList(
  119. AppDomain.CurrentDomain.GetAssemblies(),
  120. x => x.IsClass
  121. && !x.IsAbstract
  122. && x.GetTypeInfo().GenericTypeParameters.Length == 1
  123. && x.HasInterface(typeof(IPosterEngine<,,>))
  124. ).Select(x => new EngineType
  125. {
  126. Engine = x,
  127. Entity = x.GetInterfaceDefinition(typeof(IPosterEngine<,,>))!.GenericTypeArguments[0],
  128. Poster = x.GetInterfaceDefinition(typeof(IPosterEngine<,,>))!.GenericTypeArguments[1].GetGenericTypeDefinition()
  129. }).ToArray();
  130. return _posterEngines;
  131. }
  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. #endregion
  168. #region Auto-Refresh
  169. /// <summary>
  170. /// Check if <paramref name="item"/> needs to be reposted; if it does, <see cref="IPostable.PostedStatus"/> will be set to
  171. /// <see cref="PostedStatus.RequiresRepost"/> once this function is finished.
  172. /// </summary>
  173. /// <remarks>
  174. /// This will only do something if the currently set poster for this type has the <see cref="IAutoRefreshPoster{TEntity}"/> interface.
  175. /// </remarks>
  176. /// <typeparam name="T"></typeparam>
  177. /// <returns><see langword="true"/> if the item's status has been updated and needs to be reposted.</returns>
  178. public static bool CheckPostedStatus<T>(PostableSettings settings, T item)
  179. where T : BaseObject
  180. {
  181. if (!(item is IPostable postable))
  182. {
  183. return false;
  184. }
  185. if (postable.PostedStatus != PostedStatus.Posted || item?.HasOriginalValue(nameof(IPostable.PostedStatus)) == true)
  186. {
  187. return false;
  188. }
  189. settings = FixPostableSettings(typeof(T), settings);
  190. if (settings.PosterType.IsNullOrWhiteSpace())
  191. {
  192. return false;
  193. }
  194. var poster = GetPosters()?.FirstOrDefault(x => x.EntityName() == settings.PosterType);
  195. if (poster is null)
  196. {
  197. return false;
  198. }
  199. var iautoRefresh = poster.GetInterfaceDefinition(typeof(IAutoRefreshPoster<,>));
  200. if(iautoRefresh != null)
  201. {
  202. var autoRefresher = Activator.CreateInstance(iautoRefresh.GenericTypeArguments[1]) as IAutoRefresher<T>;
  203. if (autoRefresher != null && autoRefresher.ShouldRepost(item))
  204. {
  205. postable.PostedStatus = PostedStatus.RequiresRepost;
  206. return true;
  207. }
  208. }
  209. return false;
  210. }
  211. #endregion
  212. #region Process
  213. /// <summary>
  214. /// Process <paramref name="model"/> with the currently set <see cref="IPosterEngine{TPostable, TPoster, TSettings}"/>
  215. /// for <typeparamref name="T"/>.
  216. /// </summary>
  217. /// <typeparam name="T">The type of <paramref name="model"/> that needs to be processed.</typeparam>
  218. /// <param name="model"></param>
  219. /// <exception cref="EmptyPostException">If there are no items to post. In this case, nothing happens.</exception>
  220. /// <exception cref="RepostedException">If any of the <typeparamref name="T"/> have already been processed. In this case, nothing happens.</exception>
  221. /// <exception cref="MissingSettingsException">If the <see cref="PostableSettings"/> for <typeparamref name="T"/> do not exist.</exception>
  222. /// <exception cref="PostCancelledException">If the post has been cancelled by the user.</exception>
  223. /// <returns><see langword="null"/> if post was unsuccessful.</returns>
  224. ///
  225. public static IPostResult<T>? Process<T>(IDataModel<T> model)
  226. where T : Entity, IPostable, IRemotable, IPersistent, new()
  227. {
  228. return CreateEngine<T>().Process(model);
  229. }
  230. #endregion
  231. }
  232. }