public static void MathBlockHelper(TextWriter output, object root, HelperOptions options, dynamic context, object[] arguments) { if (arguments.Length != 3 && arguments.Length != 4) { throw new HandlebarsException("{{math}} helper must have exactly at least three arguments: (lvalue) op (rvalue) [digits]"); } try { var op = HandlebarsUtils.IsUndefinedBindingResult(arguments[1]) ? arguments[1].GetType().GetField("Value").GetValue(arguments[1]) : arguments[1]?.ToString(); if (arguments[0] is DateTime || arguments[0] is TimeSpan) { CalcWithTime(op, output, options, arguments); } else if (arguments[0] is Vector3) { CalcWithVector(op, output, options, arguments); } else { CalcWithDouble(op, output, options, arguments); } } catch (Exception error) { throw new HandlebarsException($"{{math}} [{arguments?.Aggregate(string.Empty, (s, a) => s + $"{a}")}]:{EmpyrionScripting.ErrorFilter(error)}"); } }
public static void CalcHelper(TextWriter output, dynamic context, object[] arguments) { if (arguments.Length != 3) { throw new HandlebarsException("{{calc}} 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 "+": output.Write(left + right); break; case "-": output.Write(left - right); break; case "*": output.Write(left * right); break; case "/": output.Write(left / right); break; case "%": output.Write(left % right); break; } } catch (Exception error) { throw new HandlebarsException($"{{calc}} [{arguments?.Aggregate(string.Empty, (s, a) => s + $"{a}")}]:{EmpyrionScripting.ErrorFilter(error)}"); } }
public static void TestBlockHelper(TextWriter output, object root, HelperOptions options, dynamic context, object[] arguments) { if (arguments.Length != 3) { throw new HandlebarsException("{{test}} helper must have exactly three argument: (testvalue) 'eq'|'le'|'leq'|'ge'|'geq'|'in' (compareto)"); } try { var left = arguments[0]; var op = HandlebarsUtils.IsUndefinedBindingResult(arguments[1]) ? arguments[1].GetType().GetField("Value").GetValue(arguments[1]) : arguments[1]?.ToString(); var right = arguments[2]; var renderTemplate = false; switch (op) { case "=": case "==": case "eq": renderTemplate = Compare(left, right) == 0; break; case "<>": case "!=": case "neq": renderTemplate = Compare(left, right) != 0; break; case "<": case "le": renderTemplate = Compare(left, right) < 0; break; case "<=": case "leq": renderTemplate = Compare(left, right) <= 0; break; case ">": case "ge": renderTemplate = Compare(left, right) > 0; break; case ">=": case "geq": renderTemplate = Compare(left, right) >= 0; break; case "in": renderTemplate = In(left, right); break; } if (renderTemplate) { options.Template(output, context as object); } else { options.Inverse(output, context as object); } } catch (Exception error) { throw new HandlebarsException($"{{test}} [{arguments?.Aggregate(string.Empty, (s, a) => s + $"{a} ")}]:{EmpyrionScripting.ErrorFilter(error)}"); } }