PosterEngine.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using InABox.Clients;
  2. using InABox.Configuration;
  3. using InABox.Core.Postable;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. namespace InABox.Core
  10. {
  11. public interface IGlobalSettingsPosterEngine
  12. {
  13. IGlobalPosterSettings GetGlobalSettings();
  14. void SaveGlobalSettings(IGlobalPosterSettings settings);
  15. }
  16. /// <summary>
  17. /// Marks an <see cref="IPosterEngine{TPostable}"/> as having global settings, not specific to certain entities.
  18. /// </summary>
  19. /// <typeparam name="TGlobalSettings"></typeparam>
  20. public interface IGlobalSettingsPosterEngine<TPoster, TGlobalSettings> : IGlobalSettingsPosterEngine
  21. where TPoster : IGlobalSettingsPoster<TGlobalSettings>
  22. where TGlobalSettings : BaseObject, IGlobalPosterSettings, new()
  23. {
  24. new TGlobalSettings GetGlobalSettings() => PosterUtils.LoadGlobalPosterSettings<TGlobalSettings>();
  25. void SaveGlobalSettings(TGlobalSettings settings) => PosterUtils.SaveGlobalPosterSettings(settings);
  26. IGlobalPosterSettings IGlobalSettingsPosterEngine.GetGlobalSettings() => GetGlobalSettings();
  27. void IGlobalSettingsPosterEngine.SaveGlobalSettings(IGlobalPosterSettings settings)
  28. {
  29. if(settings is TGlobalSettings globalSettings)
  30. {
  31. SaveGlobalSettings(globalSettings);
  32. }
  33. }
  34. }
  35. public interface IPosterEngine<TPostable>
  36. where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
  37. {
  38. IPostResult<TPostable>? Process(IDataModel<TPostable> model);
  39. }
  40. public interface IPosterEngine<TPostable, TPoster, TSettings> : IPosterEngine<TPostable>
  41. where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
  42. where TPoster : IPoster<TPostable, TSettings>
  43. where TSettings : PosterSettings, new()
  44. {
  45. }
  46. internal static class PosterEngineUtils
  47. {
  48. private static Type[]? _posters;
  49. public static Type GetPoster(Type TPoster)
  50. {
  51. _posters ??= CoreUtils.TypeList(
  52. AppDomain.CurrentDomain.GetAssemblies(),
  53. x => x.IsClass
  54. && !x.IsAbstract
  55. && !x.IsGenericType
  56. && x.HasInterface(typeof(IPoster<,>))
  57. ).ToArray();
  58. return _posters.Where(x => TPoster.IsAssignableFrom(x)).FirstOrDefault()
  59. ?? throw new Exception($"No poster of type {TPoster}.");
  60. }
  61. }
  62. /// <summary>
  63. /// A base class for all <see cref="IPosterEngine{TPostable}"/>. A concrete instance of this will be loaded by
  64. /// <see cref="PosterUtils.Process{T}(IDataModel{T})"/>; a new instance is guaranteed to be created each time that method is called.
  65. /// </summary>
  66. /// <typeparam name="TPostable"></typeparam>
  67. /// <typeparam name="TPoster"></typeparam>
  68. /// <typeparam name="TSettings"></typeparam>
  69. public abstract class PosterEngine<TPostable, TPoster, TSettings> : IPosterEngine<TPostable, TPoster, TSettings>
  70. where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
  71. where TPoster : class, IPoster<TPostable, TSettings>
  72. where TSettings : PosterSettings, new()
  73. {
  74. protected TPoster Poster;
  75. public PosterEngine()
  76. {
  77. Poster = CreatePoster();
  78. }
  79. private static readonly Type? PosterType = PosterEngineUtils.GetPoster(typeof(TPoster));
  80. protected virtual TPoster CreatePoster()
  81. {
  82. var poster = (Activator.CreateInstance(PosterType!) as TPoster)!;
  83. poster.Settings = GetSettings();
  84. if(this is IGlobalSettingsPosterEngine globalSettingsEngine && poster is IGlobalSettingsPoster globalSettingsPoster)
  85. {
  86. globalSettingsPoster.GlobalSettings = globalSettingsEngine.GetGlobalSettings();
  87. }
  88. return poster;
  89. }
  90. private TSettings? _settings;
  91. protected TSettings GetSettings()
  92. {
  93. _settings ??= PosterUtils.LoadPosterSettings<TPostable, TSettings>();
  94. return _settings;
  95. }
  96. protected void SaveSettings(TSettings settings)
  97. {
  98. _settings = settings;
  99. PosterUtils.SavePosterSettings<TPostable, TSettings>(_settings);
  100. }
  101. /// <summary>
  102. /// Returns the <see cref="TSettings.Script"/>, if <see cref="TSettings.ScriptEnabled"/> is <see langword="true"/>;
  103. /// otherwise, returns <see langword="null"/>.
  104. /// </summary>
  105. protected string? GetScript()
  106. {
  107. var settings = GetSettings();
  108. return settings.ScriptEnabled ? settings.Script : null;
  109. }
  110. protected abstract IPostResult<TPostable> DoProcess(IDataModel<TPostable> model);
  111. /// <summary>
  112. /// Process the <paramref name="model"/> before loading;
  113. /// </summary>
  114. /// <param name="model"></param>
  115. /// <returns><see langword="false"/> if the processing must be cancelled.</returns>
  116. public abstract bool BeforePost(IDataModel<TPostable> model);
  117. /// <summary>
  118. /// Prior to saving the <typeparamref name="TPostable"/> entities, make any necessary changes to those entities.
  119. /// This is only called if <see cref="Process(IDataModel{TPostable})"/> returned <see langword="true"/>.
  120. /// </summary>
  121. /// <param name="model"></param>
  122. /// <param name="result">The result object returned by <see cref="DoProcess(IDataModel{TPostable})"/></param>
  123. public abstract void AfterPost(IDataModel<TPostable> model, IPostResult<TPostable> result);
  124. private static void SetFailed(IList<TPostable> entities)
  125. {
  126. foreach (var post in entities)
  127. {
  128. post.PostedStatus = PostedStatus.PostFailed;
  129. post.PostedNote = "Post failed.";
  130. }
  131. new Client<TPostable>().Save(entities, "Post failed by user.");
  132. }
  133. public IPostResult<TPostable>? Process(IDataModel<TPostable> model)
  134. {
  135. if (!BeforePost(model))
  136. {
  137. return null;
  138. }
  139. // Add posted flags; if the columns is null, then we don't need to worry, because it will be loaded.
  140. model.GetColumns<TPostable>()?.Add(x => x.PostedStatus)
  141. .Add(x => x.Posted)
  142. .Add(x => x.PostedNote);
  143. model.LoadModel();
  144. var data = model.GetTable<TPostable>();
  145. if (!data.Rows.Any())
  146. {
  147. throw new EmptyPostException();
  148. }
  149. if(data.Rows.Any(x => x.Get<TPostable, PostedStatus>(x => x.PostedStatus) == PostedStatus.Posted))
  150. {
  151. throw new RepostedException();
  152. }
  153. try
  154. {
  155. var result = DoProcess(model);
  156. AfterPost(model, result);
  157. Client.Save(result.PostedEntities, "Posted by user.");
  158. foreach (var (type, fragments) in result.Fragments)
  159. {
  160. Client.Create(type).Save(fragments.Cast<Entity>(), "");
  161. }
  162. return result;
  163. }
  164. catch (PostCancelledException)
  165. {
  166. var entities = data.ToObjects<TPostable>().ToList();
  167. SetFailed(entities);
  168. throw;
  169. }
  170. catch (MissingSettingException)
  171. {
  172. throw;
  173. }
  174. catch (PostFailedMessageException)
  175. {
  176. throw;
  177. }
  178. catch(Exception e)
  179. {
  180. Logger.Send(LogType.Error, "", $"Post Failed: {CoreUtils.FormatException(e)}");
  181. var entities = data.ToObjects<TPostable>().ToList();
  182. SetFailed(entities);
  183. throw;
  184. }
  185. }
  186. }
  187. }