TimberlinePosterEngine.cs 2.0 KB

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