1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using InABox.Core;
- using InABox.Scripting;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace InABox.Poster.Custom
- {
- public class CustomPosterEngine<TPostable> : PosterEngine<TPostable, ICustomPoster<TPostable>, CustomPosterSettings>
- where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
- {
- private ScriptDocument? _script;
- private bool _hasCheckedScript;
- private ScriptDocument? GetScriptDocument()
- {
- if (_hasCheckedScript)
- {
- return _script;
- }
- var settings = GetSettings();
- if (settings.ScriptEnabled && !string.IsNullOrWhiteSpace(settings.Script))
- {
- var document = new ScriptDocument(settings.Script);
- if (!document.Compile())
- {
- throw new Exception("Script failed to compile!");
- }
- _script = document;
- }
- else
- {
- _script = null;
- }
- _hasCheckedScript = true;
- return _script;
- }
- public override bool BeforePost(IDataModel<TPostable> model)
- {
- if (GetScriptDocument() is ScriptDocument script)
- {
- return script.Execute(methodname: "BeforePost", parameters: new object[] { model });
- }
- return false;
- }
- protected override bool DoProcess(IDataModel<TPostable> model)
- {
- if (GetScriptDocument() is ScriptDocument script)
- {
- return script.Execute(methodname: "Process", parameters: new object[] { model });
- }
- return false;
- }
- public override void AfterPost(IDataModel<TPostable> model)
- {
- if (GetScriptDocument() is ScriptDocument script)
- {
- script.Execute(methodname: "AfterPost", parameters: new object[] { model });
- }
- }
- }
- }
|