private void IfAskYesNoQuestions(TextWriter writer, HelperOptions options, dynamic context, params object[] args) { if (!currentAsk.IsAskYesNoQuestions) { return; } var list = ((AskYesNoQuestions)currentAsk).list .Where(a => RegPerson.YesNoQuestion.ContainsKey(a.SmallGroup)).ToList(); if (!list.Any()) { return; } var lastKey = list.Last().Question; foreach (var item in list) { RowStyle = lastKey == item.Question ? CssStyle.BottomBorder : CssStyle.PadBottom; options.Template(writer, new { item.Question, Answer = RegPerson.YesNoQuestion[item.SmallGroup] == true ? "Yes" : "No", }); } }
private void IfAskParents(TextWriter writer, HelperOptions options, dynamic context, params object[] args) { if (currentAsk.IsAskParents) { options.Template(writer, OrgMember.Person.GetRecReg()); } }
private void IfAskSize(TextWriter writer, HelperOptions options, dynamic context, params object[] args) { if (currentAsk.IsAskSize) { options.Template(writer, OrgMember); } }
private void IfAskInstruction(TextWriter writer, HelperOptions options, dynamic context, params object[] args) { if (currentAsk.IsAskInstruction) { options.Template(writer, new { ((AskInstruction)currentAsk).Label }); } }
private void IfAskMenu(TextWriter writer, HelperOptions options, dynamic context, params object[] args) { if (currentAsk.IsAskMenu) { options.Template(writer, (object)context); } }
public void HelperFunction(TextWriter output, HelperOptions options, object context, params object[] arguments) { if (arguments.Length != 2) { throw new HandlebarsException(string.Format("{{{{{0}}}}} helper must have exactly two arguments", HelperName)); } string category = (arguments[0] ?? string.Empty).ToString(); string key = (arguments[1] ?? string.Empty).ToString(); string resourceValue = _localizationProvider.GetLocalizedString(new GetLocalizedParam { CultureInfo = Thread.CurrentThread.CurrentCulture, Category = category, Key = key }); if (!string.IsNullOrEmpty(resourceValue)) { options.Template(output, context); } else { options.Inverse(output, context); } }
private void IfAskGradeOptions(TextWriter writer, HelperOptions options, dynamic context, params object[] args) { if (currentAsk.IsAskGradeOptions) { options.Template(writer, new { GradeOption = RegPerson.GradeOptions(currentAsk)[RegPerson.gradeoption ?? "00"] }); } }
public static void MathBlockHelper(TextWriter output, HelperOptions options, dynamic context, object[] arguments) { if (arguments.Length != 3) { throw new HandlebarsException("{{math}} helper must have exactly three argument: (lvalue) op (rvalue)"); } try { double.TryParse(arguments[0]?.ToString(), out var left); var op = HandlebarsUtils.IsUndefinedBindingResult(arguments[1]) ? arguments[1].GetType().GetField("Value").GetValue(arguments[1]) : arguments[1]?.ToString(); double.TryParse(arguments[2]?.ToString(), out var right); switch (op) { case "+": options.Template(output, left + right); break; case "-": options.Template(output, left - right); break; case "*": options.Template(output, left * right); break; case "/": options.Template(output, left / right); break; case "%": options.Template(output, left % right); break; } } catch (Exception error) { throw new HandlebarsException($"{{math}} [{arguments?.Aggregate(string.Empty, (s, a) => s + $"{a}")}]:{EmpyrionScripting.ErrorFilter(error)}"); } }
public IWhere <T> From <T>(Action <IHelperOptions> cfg = null) { var data = new HelperOptions(_infos.GetInfo(typeof(T))); cfg?.Invoke(data); return(SqlBuilder <T>(data)); }
public static void ScrollBlockHelper(TextWriter output, HelperOptions options, dynamic context, object[] arguments) { if (arguments.Length != 2) { throw new HandlebarsException("{{scroll lines delay}} helper must have exactly two argument: (lines) (delay)"); } int.TryParse(arguments[0] as string, out int lines); int.TryParse(arguments[1] as string, out int delay); try { var content = new StringWriter(); options.Template(content, context as object); var textlines = content.ToString().Split('\n'); var overlapp = textlines.Length - lines; if (overlapp <= 0) { output.Write(content.ToString()); } else { var skip = (ScriptExecQueue.Iteration % (delay * overlapp)) / delay; output.Write(string.Join("\n", textlines.Skip((int)skip).Take(lines))); output.Write("\n"); } } catch (Exception error) { output.Write("{{scroll}} error " + EmpyrionScripting.ErrorFilter(error)); } }
public static void SignalEventsHelper(TextWriter output, object rootObject, HelperOptions options, dynamic context, object[] arguments) { if (arguments.Length != 1) { throw new HandlebarsException("{{signalevents names}} helper must have exactly one argument: (name1;name2...)"); } var root = rootObject as IScriptRootData; var namesSearch = arguments[0].ToString(); try { var uniqueNames = root.SignalEventStore.GetEvents().Keys.GetUniqueNames(namesSearch).ToDictionary(N => N); var signals = root.SignalEventStore.GetEvents().Where(S => uniqueNames.ContainsKey(S.Key)).Select(S => S.Value).ToArray(); if (signals != null && signals.Length > 0) { signals.ForEach(S => options.Template(output, S.OfType <SignalEventBase>().Select(subS => root.IsElevatedScript ? new SignalEventElevated(root.GetCurrentPlayfield(), subS) : (object)new SignalEvent(root.GetCurrentPlayfield(), subS)).Reverse().ToArray())); } else { options.Inverse(output, context as object); } } catch (Exception error) { if (!CsScriptFunctions.FunctionNeedsMainThread(error, root)) { output.Write("{{signalevents}} error " + EmpyrionScripting.ErrorFilter(error)); } } }
public static void IntervallBlockHelper(TextWriter output, HelperOptions options, dynamic context, object[] arguments) { if (arguments.Length != 1) { throw new HandlebarsException("{{intervall seconds}} helper must have exactly one argument: (value)"); } double.TryParse(arguments[0]?.ToString(), out double intervall); try { if (ScriptExecQueue.Iteration % (2 * intervall) < intervall) { options.Template(output, context as object); } else { options.Inverse(output, context as object); } } catch (Exception error) { output.Write("{{intervall}} error " + EmpyrionScripting.ErrorFilter(error)); } }
public static void FileListHelper(TextWriter output, object rootObject, HelperOptions options, dynamic context, object[] arguments) { if (arguments.Length != 2 && arguments.Length != 3) { throw new HandlebarsException("{{filelist dir filename [recursive]}} helper must have at least three arguments: @root (dir) (filename) [recursive]"); } if (!(rootObject is ScriptSaveGameRootData root)) { throw new HandlebarsException("{{filelist}} only allowed in SaveGame scripts"); } try { bool.TryParse(arguments.Length == 3 ? arguments[2].ToString() : "false", out var recursive); options.Template(output, Directory.EnumerateFiles( Path.Combine(root.MainScriptPath, arguments[0].ToString()), arguments[1].ToString(), recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)); } catch (Exception error) { if (!CsScriptFunctions.FunctionNeedsMainThread(error, root)) { output.Write("{{filelist}} error " + EmpyrionScripting.ErrorFilter(error)); } } }
public static void FileExistsHelper(TextWriter output, object rootObject, HelperOptions options, dynamic context, object[] arguments) { if (arguments.Length != 2) { throw new HandlebarsException("{{fileexists dir filename}} helper must have two argument: (dir) (filename)"); } if (!(rootObject is ScriptSaveGameRootData root)) { throw new HandlebarsException("{{readfile dir filename}} only allowed in SaveGame scripts"); } try { if (File.Exists(Path.Combine(root.MainScriptPath, arguments[0].ToString(), arguments[1].ToString()))) { options.Template(output, context as object); } else { options.Inverse(output, context as object); } } catch (Exception error) { if (!CsScriptFunctions.FunctionNeedsMainThread(error, root)) { output.Write("{{fileexists}} error " + EmpyrionScripting.ErrorFilter(error)); } } }
private void Checkboxes(TextWriter writer, HelperOptions options, dynamic context, params object[] args) { if (!currentAsk.IsAskCheckboxes) { return; } var list = ((AskCheckboxes)currentAsk).CheckboxItemsChosen(RegPerson.Checkbox).ToList(); if (!list.Any()) { return; } var lastDesc = list.Last().Description; foreach (var item in list) { RowStyle = lastDesc == item.Description ? "" : CssStyle.PadBottom; options.Template(writer, new { DisplayDescription = item.Fee > 0 ? $"{item.Description} (${item.Fee.ToString2("N2")}<br/>({item.SmallGroup})" : $"{item.Description}<br/>({item.SmallGroup})", }); } }
public IWhere <T> FromTemplate <T>(IQueryTemplate <T> template, Action <IHelperOptions> cfg = null) { var data = new HelperOptions(); cfg?.Invoke(data); return(SqlBuilder <T>(data, template)); }
public override void Helper(TextWriter output, HelperOptions options, object context, object[] arguments) { if (arguments.Length % 2 != 1) { throw new CodeGenHelperException(Name, $"Arguments number for the {Name} helper must be an odd number"); } var value = GetArgumentAs <string>(arguments, 0) ?? ""; var pair_position = 1; while (pair_position < arguments.Length) { var match_key = GetArgumentAs <string>(arguments, pair_position) ?? ""; if (string.Compare(value, match_key, StringComparison.InvariantCultureIgnoreCase) == 0) { options.Template(output, arguments[pair_position + 1]); return; } pair_position += 2; } options.Inverse(output, context); }
public static void RandomHelper(TextWriter output, object root, HelperOptions options, dynamic context, object[] arguments) { if (arguments.Length != 2) { throw new HandlebarsException("{{random start end}} helper must have two argument: (start) (end)"); } var format = arguments.Length > 0 ? arguments[0]?.ToString() : null; var add = arguments.Length > 1 ? arguments[1]?.ToString() : null; try { int.TryParse(arguments[0]?.ToString(), out var start); int.TryParse(arguments[1]?.ToString(), out var end); options.Template(output, new Random().Next(start, end)); } catch (Exception error) { if (!CsScriptFunctions.FunctionNeedsMainThread(error, root)) { output.Write("{{random}} error " + EmpyrionScripting.ErrorFilter(error)); } } }
/// <inheritdoc /> protected override void Render(Block block, object[] arguments, Dictionary<string, object> maps, RenderContext context, TextWriter writer) { if (context.Service == null) { // There is nothing we can do, no service has been assigned. return; } var options = new HelperOptions() { Arguments = arguments, Parameters = maps, RenderContext = context }; if (block.Type == BlockType.Tag) { var children = block.Children.ToList(); children.RemoveAt(0); children.RemoveAt(children.Count - 1); options.Render = (data) => RenderHelperChildren(children, context, data); } string result = context.Service.RunHelper(block.Name, options); Write(context, writer, new SafeString(result)); }
public static void SplitHelper(TextWriter output, object root, HelperOptions options, dynamic context, object[] arguments) { if (arguments.Length < 2) { throw new HandlebarsException("{{split string separator [removeemptyentries] [trimchars]}} helper must have at least two argument: (string) (separator) [true|false] [trimchars]"); } var data = arguments[0].ToString(); var separator = Regex.Unescape(arguments[1].ToString()); var trimchars = arguments.Get(3)?.ToString().ToCharArray(); try { bool.TryParse(arguments.Length > 2 ? arguments[2]?.ToString() : null, out var removeemptyentries); options.Template(output, data.Split(new[] { separator }, removeemptyentries ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None) .Select(item => trimchars == null ? item : item.Trim(trimchars)) .ToArray()); } catch (Exception error) { if (!CsScriptFunctions.FunctionNeedsMainThread(error, root)) { output.Write("{{split}} error " + EmpyrionScripting.ErrorFilter(error)); } } }
void isType(TextWriter writer, HelperOptions options, dynamic context, params object[] arguments) { Type[] expectedType; var strType = (string)arguments[1]; switch (strType) { case "IEnumerable<string>": expectedType = new[] { typeof(IEnumerable <string>) }; break; case "IEnumerable<KeyValuePair<string, string>>": expectedType = new[] { typeof(IEnumerable <KeyValuePair <string, string> >) }; break; default: throw new ArgumentException("Invalid type: " + strType); } var t = arguments[0]?.GetType(); if (expectedType.Any(x => x.IsAssignableFrom(t))) { options.Template(writer, (object)context); } else { options.Inverse(writer, (object)context); } }
public static void GetTextHelper(TextWriter output, HelperOptions options, dynamic context, object[] arguments) { if (arguments.Length != 1) { throw new HandlebarsException("{{gettext lcddevice}} helper must have exactly one argument: (structure)"); } var block = arguments[0] as BlockData; var lcd = block?.GetStructure()?.GetDevice <ILcd>(block.Position); try { if (lcd == null) { options.Inverse(output, context as object); } else { options.Template(output, lcd.GetText()); } } catch (Exception error) { output.Write("{{settext}} error " + EmpyrionScripting.ErrorFilter(error)); } }
public static void LightsBlockHelper(TextWriter output, HelperOptions options, dynamic context, object[] arguments) { if (arguments.Length != 2) { throw new HandlebarsException("{{lights structure names}} helper must have exactly two argument: (structure) (name;name*;*;name)"); } var structure = arguments[0] as IStructureData; var namesSearch = arguments[1]?.ToString(); try { var uniqueNames = structure.AllCustomDeviceNames.GetUniqueNames(namesSearch); var devices = uniqueNames.Select(N => structure.GetCurrent().GetDevice <ILight>(N)).ToArray(); if (devices.Length > 0) { devices.ForEach(L => options.Template(output, L)); } else { options.Inverse(output, context as object); } } catch (Exception error) { output.Write("{{lights}} error " + EmpyrionScripting.ErrorFilter(error)); } }
public static void GetColorBlockHelper(TextWriter output, object root, HelperOptions options, dynamic context, object[] arguments) { if (arguments.Length != 2) throw new HandlebarsException("{{getcolor block pos}} helper must have exactly two argument: (block) '(T|B|N|S|W|E)'"); var block = arguments[0] as BlockData; var pos = arguments[1].ToString(); try { var colorId = -1; switch (pos.ToUpper()) { case "T": colorId = block.TopColor; break; case "B": colorId = block.BottomColor; break; case "N": colorId = block.NorthColor; break; case "S": colorId = block.SouthColor; break; case "W": colorId = block.WestColor; break; case "E": colorId = block.EastColor; break; } if (colorId != -1) options.Template(output, colorId); else options.Inverse(output, context as object); } catch (Exception error) { if (!CsScriptFunctions.FunctionNeedsMainThread(error, root)) output.Write("{{block}} error " + EmpyrionScripting.ErrorFilter(error)); } }
public static void ReadFileHelper(TextWriter output, HelperOptions options, dynamic context, object[] arguments) { if (arguments.Length != 3) { throw new HandlebarsException("{{readfile @root dir filename}} helper must have two argument: @root (dir) (filename)"); } if (!(arguments[0] is ScriptSaveGameRootData root)) { throw new HandlebarsException("{{readfile @root dir filename}} only allowed in SaveGame scripts"); } try { var filename = Path.Combine(arguments[1].ToString(), arguments[2].ToString()); var fileContent = HelpersTools.GetFileContent(filename)?.Lines; if (fileContent == null) { options.Inverse(output, context as object); } else { options.Template(output, fileContent); } } catch (Exception error) { output.Write("{{readfile}} error " + EmpyrionScripting.ErrorFilter(error)); } }
private void QueryBlockHelper(TextWriter output, HelperOptions options, object context, object[] arguments) { var transformObject = QueryCore(context, arguments); var items = transformObject as IEnumerable <object>; if (items != null) { if (items.Any()) { foreach (var item in items) { options.Template(output, item); } } else { options.Inverse(output, context); } } else { if (transformObject != null) { options.Template(output, transformObject); } else { options.Inverse(output, context); } } }
public static void SendMessageToPlayerHelper(TextWriter output, HelperOptions options, dynamic context, object[] arguments) { if (arguments.Length < 2) { throw new HandlebarsException("{{sendmessagetoplayer @root playerid}} helper must have at least two argument: @root (playerid)"); } if (!(arguments[0] is ScriptSaveGameRootData root)) { throw new HandlebarsException("{{sendmessagetoplayer @root playerid}} only allowed in SaveGame scripts"); } try { int.TryParse(arguments[1].ToString(), out var playerId); using (var text = new StringWriter()) { options.Template(text, context as object); root.ModApi.Application.SendChatMessage(new Eleon.MessageData() { SenderType = Eleon.SenderType.ServerInfo, Channel = Eleon.MsgChannel.Server, RecipientEntityId = playerId, Text = text.ToString() }); } } catch (Exception error) { output.Write("{{sendmessagetoplayer}} error " + EmpyrionScripting.ErrorFilter(error)); } }
public static void ConfigByIdHelper(TextWriter output, object rootObject, HelperOptions options, dynamic context, object[] arguments) { if (arguments.Length != 1) { throw new HandlebarsException("{{configbyid id}} helper must have exactly one argument: (id)"); } var root = rootObject as IScriptModData; int.TryParse(arguments[0]?.ToString(), out var id); try { if (root.ConfigEcfAccess.FlatConfigBlockById.TryGetValue(id, out var config)) { options.Template(output, config); } else { options.Inverse(output, (object)context); } } catch (Exception error) { if (!CsScriptFunctions.FunctionNeedsMainThread(error, root)) { output.Write("{{configattr}} error " + EmpyrionScripting.ErrorFilter(error)); } } }
public IWhere <T> From <T>(Action <IHelperOptions> cfg = null) { var data = new HelperOptions(); cfg?.Invoke(data); return(SqlBuilder <T>(data)); }
public static void UseHelper(TextWriter output, object root, HelperOptions options, dynamic context, object[] arguments) { if (arguments.Length != 1) { throw new HandlebarsException("{{use data}} helper must have one argument: (data)"); } try { if (arguments[0] == null) { options.Inverse(output, context as object); } else { options.Template(output, arguments[0]); } } catch (Exception error) { if (!CsScriptFunctions.FunctionNeedsMainThread(error, root)) { output.Write("{{use}} error " + EmpyrionScripting.ErrorFilter(error)); } } }
public static void SetTextBlockHelper(TextWriter output, object rootObject, HelperOptions options, dynamic context, object[] arguments) { if (arguments.Length != 1) { throw new HandlebarsException("{{settextblock lcddevice}} helper must have exactly one argument: (structure)"); } var root = rootObject as IScriptRootData; if (!root.Running) { return; // avoid flicker displays with part of informations } try { var block = arguments[0] as BlockData; var lcd = block?.GetStructure()?.GetDevice <ILcd>(block.Position); using var text = new StringWriter(); options.Template(text, context as object); if (!root.Running) { return; // avoid flicker displays with part of informations } lcd?.SetText(text.ToString()); } catch (Exception error) { if (!CsScriptFunctions.FunctionNeedsMainThread(error, root)) { output.Write("{{settextblock}} error " + EmpyrionScripting.ErrorFilter(error)); } } }
public HelperContext(LoadedConfiguration configuration, IReferenceLocator locator, bool debug) { Configuration = configuration; Locator = locator; Debug = debug; DefaultOptions = new HelperOptions { GenerateHeaderAndFooter = true }; }
public static string Reference( HelperContext context, HelperOptions options, string root, IEnumerable<string> names) { var package = Package.Build(context.Configuration, root); var references = new List<SystemReference>(); if (context.Debug) { foreach (var name in names) { var cleanedName = name.Reslash(); string file = cleanedName.StartsWith("/") ? cleanedName.Substring(1) : Path.Combine(context.Configuration.ScriptPath.Reslash(), cleanedName).Reslash(); file = file.StartsWith("/") ? file.Substring(1) : file; cleanedName = file.Substring(file.LastIndexOf("/", StringComparison.Ordinal) + 1); var reference = new SystemReference(new DirectoryInfo(root), new FileInfo(Path.Combine(root, file)), cleanedName); references.AddRange(context.Locator.Load(root, reference)); } return package.GenerateReferences(references, options); } foreach (var name in names) { var appName = name + "-glue"; var appDirectory = new DirectoryInfo(root); var appFile = new FileInfo(Path.Combine(root + context.Configuration.ScriptPath, appName)); var appReference = new SystemReference(appDirectory, appFile, appName) { Type = Model.Reference.TypeEnum.App }; references.Add(appReference); } return package.GenerateReferences(references, options); }
public static string Reference(HelperOptions options, params string[] names) { var root = HotGlueNancyStartup.Root; return ScriptHelper.Reference(Context.Value, options, root, names); }