BasePosterEngine.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using InABox.Core;
  2. using InABox.Scripting;
  3. namespace InABox.Poster.Shared;
  4. public abstract class BasePosterEngine<TPostable, TPoster, TSettings> : PosterEngine<TPostable, TPoster, TSettings>
  5. where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
  6. where TPoster : class, IPoster<TPostable, TSettings>
  7. where TSettings : PosterSettings, new()
  8. {
  9. private ScriptDocument? _script;
  10. private bool _hasCheckedScript;
  11. protected ScriptDocument? GetScriptDocument()
  12. {
  13. if (_hasCheckedScript)
  14. {
  15. return _script;
  16. }
  17. var settings = GetSettings();
  18. _script = GetScriptDocument(settings);
  19. _hasCheckedScript = true;
  20. return _script;
  21. }
  22. public static ScriptDocument? GetScriptDocument(TSettings settings)
  23. {
  24. if (settings.ScriptEnabled && !string.IsNullOrWhiteSpace(settings.Script))
  25. {
  26. var document = new ScriptDocument(settings.Script);
  27. if (!document.Compile())
  28. {
  29. throw new Exception("Script failed to compile!");
  30. }
  31. return document;
  32. }
  33. else
  34. {
  35. return null;
  36. }
  37. }
  38. }