CustomPosterEngine.cs 2.0 KB

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