private RiveScript.RiveScript CreateRivescript(string path, RivescriptOptions options) { RiveScript.RiveScript engine = new RiveScript.RiveScript(options.Debug, options.Utf8, options.Strict); // set ourselves as a "script language" so that we can do the glue via reflection engine.setHandler("dialog", this); // only define methods from child class var ignoreMethods = new List <string>(typeof(RivescriptDialog).GetMethods().Select(m => m.Name)); // build method bindings using the "dialog" language StringBuilder sb = new StringBuilder(); foreach (var method in this.GetType().GetMethods()) { if (!ignoreMethods.Contains(method.Name)) { // this is "dialog" language method definition. Turns out that's all we need sb.AppendLine($"> object {method.Name} dialog"); sb.AppendLine($"< object\n"); } } // load method bindings engine.stream(sb.ToString()); // load referred path if (Directory.Exists(path)) { engine.loadDirectory(path); } else { engine.loadFile(path); } // sort engine.sortReplies(); return(engine); }
public RivescriptDialog(string dialogId, string path, IStatePropertyAccessor <RivescriptState> stateProperty, RivescriptOptions options) : base(dialogId) { if (string.IsNullOrWhiteSpace(path)) { throw new ArgumentNullException(nameof(path)); } this.rsEngine = this.CreateRivescript(path.Trim(), options); this.StateProperty = stateProperty; }