TimberlinePosterEngine.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using InABox.Core;
  2. using InABox.Core.Postable;
  3. using InABox.Scripting;
  4. using System.Collections.Generic;
  5. using System.Formats.Asn1;
  6. using System.Globalization;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace InABox.Poster.Timberline;
  11. public class TimberlinePosterEngine<TPostable, TSettings> : PosterEngine<TPostable, ITimberlinePoster<TPostable, TSettings>, TSettings>
  12. where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
  13. where TSettings : TimberlinePosterSettings<TPostable>, new()
  14. {
  15. private ScriptDocument? _script;
  16. private bool _hasCheckedScript;
  17. protected override ITimberlinePoster<TPostable, TSettings> CreatePoster()
  18. {
  19. var poster = base.CreatePoster();
  20. poster.Script = GetScriptDocument();
  21. return poster;
  22. }
  23. private ScriptDocument? GetScriptDocument()
  24. {
  25. if (_hasCheckedScript)
  26. {
  27. return _script;
  28. }
  29. var settings = GetSettings();
  30. if (settings.ScriptEnabled && !string.IsNullOrWhiteSpace(settings.Script))
  31. {
  32. var document = new ScriptDocument(settings.Script);
  33. if (!document.Compile())
  34. {
  35. throw new Exception("Script failed to compile!");
  36. }
  37. _script = document;
  38. }
  39. else
  40. {
  41. _script = null;
  42. }
  43. _hasCheckedScript = true;
  44. return _script;
  45. }
  46. public override bool BeforePost(IDataModel<TPostable> model)
  47. {
  48. return Poster.BeforePost(model);
  49. }
  50. protected override IPostResult<TPostable> DoProcess(IDataModel<TPostable> model)
  51. {
  52. return Poster.Process(model);
  53. }
  54. public override void AfterPost(IDataModel<TPostable> model, IPostResult<TPostable> result)
  55. {
  56. Poster.AfterPost(model, result);
  57. }
  58. }