public bool TryEvaluate(object sender, FunctionEvaluationEventArg e) { if (e.Name.Equals("clipboard", StringComparison.OrdinalIgnoreCase) || e.Name.Equals("cb", StringComparison.OrdinalIgnoreCase)) { object arg = null; if (e.This != null) { arg = e.This; } if (e.Args.Count > 0) { arg = e.EvaluateArg(0); } if (arg is Bitmap bitmap) { Clipboard.SetDataObject(bitmap); } else { Clipboard.SetText(arg?.ToString() ?? string.Empty); } e.Value = arg; } return(e.FunctionReturnedValue); }
public bool TryEvaluate(object sender, FunctionEvaluationEventArg e) { if (convertToImageFactoryRegex.IsMatch(e.Name) && e.This is Image image) { e.Value = new ImageFactory().Load(image); } else if (e.This is ImageFactory factory) { if (convertToBitmapRegex.IsMatch(e.Name)) { using MemoryStream ms = new MemoryStream(); factory.Save(ms); e.Value = new Bitmap(ms); } else if (e.Name.Equals("Resize", Config.Instance.CaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase) && e.Args.Count == 2 && e.EvaluateArg(0) is int x && e.EvaluateArg(1) is int y) { e.Value = factory.Resize(new Size(x, y)); } } return(e.FunctionReturnedValue); }
public bool TryEvaluate(object sender, FunctionEvaluationEventArg e) { Match plotMatch = plotRegex.Match(e.Name); if (plotMatch.Success) { object[] args = e.EvaluateArgs(); e.Value = GetChart(plotMatch, e.This ?? args[0], (Array.Find(args, a => a is IEnumerable <string>) as IEnumerable <string> ?? args.OfType <string>())?.ToArray()); } else if (plotListKeywords.Any(name => e.Name.Equals(name, StringComparison.OrdinalIgnoreCase))) { e.Value = string.Join("\r\n", Enum.GetNames(typeof(SeriesChartType))); } else if (toPicKeywords.Any(name => e.Name.Equals(name, StringComparison.OrdinalIgnoreCase)) && e.This is Chart chart) { using MemoryStream ms = new MemoryStream(); chart.SaveImage(ms, ChartImageFormat.Png); e.Value = new Bitmap(ms); } return(e.FunctionReturnedValue); }
public static void ExpressionEvaluator_EvaluateFunction(object sender, FunctionEvaluationEventArg e) { //Log4Net log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); // Size if (e.Name.Equals("Size") && e.Args.Count == 2) { // Get Expressions string widthExpression = e.EvaluateArg(0).ToString(); string heightExpression = e.EvaluateArg(1).ToString(); //if (heightExpression.Contains("startupWindowObjectsTablePadMarginLeftTop.Height + ")) //{ // log.Debug("BREAK"); //} // Get Results object widthResult = GlobalApp.ExpressionEvaluator.Evaluate(widthExpression); object heightResult = GlobalApp.ExpressionEvaluator.Evaluate(heightExpression); // Convert Results int width = Convert.ToInt32(e.EvaluateArg(0)); int height = Convert.ToInt32(e.EvaluateArg(1)); // Format Results e.Value = string.Format("{0},{1}", widthResult, heightResult); } }
public bool TryEvaluate(object sender, FunctionEvaluationEventArg e) { if ((e.Name.Equals("ujson", System.StringComparison.OrdinalIgnoreCase) || e.Name.Equals("unjson", System.StringComparison.OrdinalIgnoreCase) || e.Name.Equals("dejson", System.StringComparison.OrdinalIgnoreCase) || e.Name.Equals("djson", System.StringComparison.OrdinalIgnoreCase)) && e.Args.Count > 0) { e.Value = JsonConvert.DeserializeObject(e.EvaluateArg(0).ToString()); } return(e.FunctionReturnedValue); }
private static void ExpressionEvaluator_EvaluateFunction(object sender, FunctionEvaluationEventArg e) { Character player = GetPlayer(e.Evaluator.Variables); CastedSpell castedSpell = GetCastedSpell(e.Evaluator.Variables); Creature target = GetTargetCreature(e.Evaluator.Variables); DndFunction function = functions.FirstOrDefault(x => x.Handles(e.Name, player, castedSpell)); if (function != null) { e.Value = function.Evaluate(e.Args, e.Evaluator, player, target, castedSpell); Log($" {e.Name}({GetArgsStr(e.Args)}) => {GetValueStr(e.Value)}"); } }
public bool TryEvaluate(object sender, FunctionEvaluationEventArg e) { Match excelSheetVariableMatch; Match toExcelMatch; Match excelMatch; if ((excelSheetVariableMatch = excelSheetVariableRegex.Match(e.Name)).Success && (e.This is ExcelPackage || e.This is ExcelWorkbook)) { ExcelWorkbook excelWorkbook = (e.This as ExcelWorkbook) ?? (e.This as ExcelPackage)?.Workbook; string sheetName = e.EvaluateArgs().OfType <string>().FirstOrDefault(); e.Value = excelSheetVariableMatch.Groups["index"].Success ? excelWorkbook.Worksheets[int.Parse(excelSheetVariableMatch.Groups["index"].Value)] : string.IsNullOrEmpty(sheetName) ? (object)excelWorkbook.Worksheets : excelWorkbook.Worksheets[sheetName]; } else if (e.This is ExcelPackage pack4Book) { if (e.Name.Equals("saveandopen", StringComparison.OrdinalIgnoreCase) || e.Name.Equals("saveopen", StringComparison.OrdinalIgnoreCase)) { pack4Book.File = e.EvaluateArgs().OfType <string>().Select(fileName => new FileInfo(fileName)).FirstOrDefault() ?? new FileInfo(Config.Instance.ExcelDefaultFileName); if (pack4Book.Workbook.Worksheets.Count == 0) { pack4Book.Workbook.Worksheets.Add(string.Format(Config.Instance.ExcelDefaultSheetName, 1)); } pack4Book.Save(); Process.Start(pack4Book.File.FullName); e.Value = pack4Book; } } else if ((excelMatch = baseExcelRegex.Match(e.Name)).Success && e.This == null) { var fileName = e.EvaluateArgs().OfType <string>().FirstOrDefault() ?? Config.Instance.ExcelDefaultFileName; if (excelMatch.Groups["new"].Success) { File.Delete(fileName); } e.Value = new ExcelPackage(new FileInfo(fileName)); } else if (e.This is IEnumerable rowEnumerable && (toExcelMatch = toExcelRegex.Match(e.Name)).Success) { e.Value = IEnumerableToExcel(rowEnumerable, toExcelMatch, e.EvaluateArgs().ToList()); } return(e.FunctionReturnedValue); }
public bool TryEvaluate(object sender, FunctionEvaluationEventArg e) { if (e.Name.Equals("html", StringComparison.OrdinalIgnoreCase)) { HtmlDocument htmlDocument = new HtmlDocument(); if (e.This is string sHTML || e.Args.Count > 0 && !string.IsNullOrEmpty(sHTML = e.EvaluateArg <string>(0))) { htmlDocument.LoadHtml(sHTML); } e.Value = htmlDocument.DocumentNode; } return(e.FunctionReturnedValue); }
public bool TryEvaluate(object sender, FunctionEvaluationEventArg e) { Match tabFuncMatch = tabFuncRegex.Match(e.Name); if (tabFuncMatch.Success) { string currentTab = BNpp.NotepadPP.CurrentFileName; if (tabFuncMatch.Groups["tabIndex"].Success) { e.Value = e.Args.Count > 0 ? BNpp.NotepadPP.GetTabIndex(e.EvaluateArg <string>(0), true) : (object)BNpp.NotepadPP.GetTabIndex(currentTab); } else if (tabFuncMatch.Groups["fileName"].Success) { e.Value = e.Args.Count > 0 ? BNpp.NotepadPP.GetAllOpenedDocuments[e.EvaluateArg <int>(0)] : currentTab; } else { if (e.Args.Count > 0) { object arg = e.EvaluateArg(0); if (arg is int iArg) { BNpp.NotepadPP.ShowTab(iArg); } else { BNpp.NotepadPP.ShowTab(arg.ToStringOutput(), true); } } string text = BNpp.Text; BNpp.NotepadPP.ShowTab(currentTab); e.Value = text; } return(true); } return(false); }
public bool TryEvaluate(object sender, FunctionEvaluationEventArg e) { Match loopVariableEvalMatch = loopVariableEvalRegex.Match(e.Name); if (loopVariableEvalMatch.Success) { List <object> results = new List <object>(); int from = loopVariableEvalMatch.Groups["from"].Success ? int.Parse(loopVariableEvalMatch.Groups["from"].Value, CultureInfo.InvariantCulture) : 1; if (loopVariableEvalMatch.Groups["to"].Success) { for (int i = from; i <= int.Parse(loopVariableEvalMatch.Groups["to"].Value, CultureInfo.InvariantCulture); i++) { e.Evaluator.Variables[e.Args.Count > 1 ? e.Args[1].Trim() : "i"] = i; results.Add(e.Evaluator.Evaluate(e.Args[0]).ToString()); } } else { int count = loopVariableEvalMatch.Groups["count"].Success ? int.Parse(loopVariableEvalMatch.Groups["count"].Value, CultureInfo.InvariantCulture) : 10; for (int i = 0; i < count; i++) { e.Evaluator.Variables[e.Args.Count > 1 ? e.Args[1].Trim() : "i"] = i + from; results.Add(e.Evaluator.Evaluate(e.Args[0])); } } e.Value = results; return(true); } else { return(false); } }
public bool TryEvaluate(object sender, FunctionEvaluationEventArg e) { if ((e.Name.Equals("stringjoin", StringComparison.OrdinalIgnoreCase) || e.Name.Equals("sjoin", StringComparison.OrdinalIgnoreCase) || e.Name.Equals("sj", StringComparison.OrdinalIgnoreCase) || e.Name.Equals("j", StringComparison.OrdinalIgnoreCase)) && (e.This is IEnumerable || (e.Args.Count > 1 && e.EvaluateArg(1) is IEnumerable))) { if (e.This is IEnumerable enumerable) { e.Value = string.Join(e.Args.Count > 0 ? e.EvaluateArg <string>(0) : "", enumerable.Cast <object>()); } else if (e.Args.Count > 1 && e.EvaluateArg(1) is IEnumerable enumerable2) { e.Value = string.Join(e.EvaluateArg <string>(0), enumerable2.Cast <object>()); } } else if (e.Name.Equals("format", StringComparison.OrdinalIgnoreCase) && e.This is string format) { e.Value = string.Format(format, e.EvaluateArgs()); } return(e.FunctionReturnedValue); }
private static void ExpressionEvaluator_EvaluateFunction(object sender, FunctionEvaluationEventArg e) { Creature player = GetPlayer(e.Evaluator.Variables); CastedSpell castedSpell = GetCastedSpell(e.Evaluator.Variables); Target target = GetTargetCreature(e.Evaluator.Variables); RollResults dice = GetDiceStoppedRollingData(e.Evaluator.Variables); DndFunction function = functions.FirstOrDefault(x => x.Handles(e.Name, player, castedSpell)); if (function != null) { try { e.Value = function.Evaluate(e.Args, e.Evaluator, player, target, castedSpell, dice); Log($" {e.Name}({GetArgsStr(e.Args)}) => {GetValueStr(e.Value)}"); } catch //(Exception ex) { e.Value = null; Log($" Exception thrown trying to evaluate {e.Name}({GetArgsStr(e.Args)})"); } } }
public bool TryEvaluate(object sender, FunctionEvaluationEventArg e) { Match match = sqlRegex.Match(e.Name); if (match.Success) { DBConfig dBConfig = Config.Instance.DBConfigs.FirstOrDefault(dBConfig => dBConfig.Id.Equals(match.Groups["connection"].Value, StringComparison.OrdinalIgnoreCase)); if (dBConfig != null) { using var dbConnection = dBConfig.GetConnection(); dbConnection.Open(); if (!string.IsNullOrWhiteSpace(dBConfig.InitCommands)) { using var initCommand = dbConnection.CreateCommand(); initCommand.CommandText = dBConfig.InitCommands; initCommand.ExecuteNonQuery(); } using var command = dbConnection.CreateCommand(); command.CommandText = e.EvaluateArg <string>(0); Func <int, bool> numberLimit; if (match.Groups["full"].Success) { numberLimit = _ => true; } else if (match.Groups["number"].Success) { int number = int.Parse(match.Groups["number"].Value); numberLimit = i => i < number; } else if (Config.Instance.DBAutoLimitRequests) { numberLimit = i => i < Config.Instance.DBAutoLimitRequestsValue; } else { numberLimit = _ => true; } if (e.Args.Count > 1) { Delegate lambda = e.EvaluateArg <Delegate>(1); } else { var reader = command.ExecuteReader(); List <dynamic> result = new List <dynamic>(); for (int i = 0; reader.Read() && numberLimit(i); i++) { result.Add(ToExpando(reader)); } e.Value = new DBResultViewModel() { ColumnsNames = Enumerable.Range(0, reader.FieldCount).Select(n => reader.GetName(n)).ToList(), Results = result }; } } else { throw new ExpressionEvaluatorSyntaxErrorException($"No DB Connection with ID = {match.Groups["connection"].Value}"); } } else if (e.This is DBResultViewModel dBResultViewModel) { Match csvMatch; Match excelMatch; if (valuePropRegex.IsMatch(e.Name)) { if (dBResultViewModel.Results.Count == 1 && dBResultViewModel.Results[0] is IDictionary <string, object> dict) { if (dict.Count == 1) { e.Value = dict[dict.Keys.First()]; } else { e.Value = dBResultViewModel.Results[0]; } } else { e.Value = dBResultViewModel.Results; } } else if ((csvMatch = csvRegex.Match(e.Name)).Success) { StringBuilder stringBuilder = new StringBuilder(); object[] args = e.EvaluateArgs(); string separator = args.OfType <string>().FirstOrDefault() ?? ";"; if (!csvMatch.Groups["noheader"].Success) { stringBuilder.AppendLine(string.Join(separator, dBResultViewModel.ColumnsNames.Select(n => "\"" + n + "\""))); } foreach (IDictionary <string, object> result in dBResultViewModel.Results) { stringBuilder.AppendLine(string.Join(separator, dBResultViewModel.ColumnsNames.Select(n => { if (result[n] is string s) { return("\"" + s.Replace("\"", "\"\"")); } else { return(result[n]?.ToString() ?? string.Empty); } }))); } e.Value = stringBuilder.ToString(); } else if ((excelMatch = excelRegex.Match(e.Name)).Success) { var args = e.EvaluateArgs(); var options = args.OfType <IDictionary <string, object> >().FirstOrDefault(); string fileName = args.OfType <string>().FirstOrDefault(); e.Value = CreateExcel(dBResultViewModel, excelMatch, options, fileName); } } return(e.FunctionReturnedValue); }
public bool TryEvaluate(object sender, FunctionEvaluationEventArg e) { if (!(e.Name.StartsWith("http", StringComparison.OrdinalIgnoreCase) && (e.This != null || e.Args.Count > 0))) { return(false); } HttpClientHandler httpClientHandler = new HttpClientHandler(); if (Config.Instance.UseProxy) { httpClientHandler.Proxy = Config.Instance.UseDefaultProxy ? WebRequest.GetSystemWebProxy() : new WebProxy(Config.Instance.ProxyPort == null ? Config.Instance.ProxyAddress : $"{Config.Instance.ProxyAddress}:{Config.Instance.ProxyPort}", Config.Instance.ProxyBypassOnLocal) { BypassList = Config.Instance.ProxyBypassList.Split(';'), UseDefaultCredentials = Config.Instance.UseDefaultCredentials }; if (!Config.Instance.UseDefaultCredentials && !string.IsNullOrEmpty(Config.Instance.ProxyUserName)) { string[] UserAndDomains = Config.Instance.ProxyUserName.Split('\\'); httpClientHandler.Proxy.Credentials = UserAndDomains.Length > 1 ? new NetworkCredential(UserAndDomains.Last(), Config.Instance.ProxyPassword, UserAndDomains[0]) : new NetworkCredential(Config.Instance.ProxyUserName, Config.Instance.ProxyPassword); } } HttpClient client = new HttpClient(httpClientHandler); HttpRequestMessage httpRequestMessage = null; List <object> args = e.EvaluateArgs().ToList(); string url = e.This?.ToString() ?? args[0].ToString(); IDictionary <string, object> config = args.Find(a => a is IDictionary <string, object>) as IDictionary <string, object>; IDictionary <string, object> header = null; if (e.Name.Equals("httppost", StringComparison.OrdinalIgnoreCase)) { httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, url); } else if (e.Name.Equals("httpput", StringComparison.OrdinalIgnoreCase)) { httpRequestMessage = new HttpRequestMessage(HttpMethod.Put, url); } else if (e.Name.Equals("httpoptions", StringComparison.OrdinalIgnoreCase)) { httpRequestMessage = new HttpRequestMessage(HttpMethod.Options, url); } else if (e.Name.Equals("httphead", StringComparison.OrdinalIgnoreCase)) { httpRequestMessage = new HttpRequestMessage(HttpMethod.Head, url); } else if (e.Name.Equals("httpdelete", StringComparison.OrdinalIgnoreCase)) { httpRequestMessage = new HttpRequestMessage(HttpMethod.Delete, url); } else if (e.Name.Equals("httptrace", StringComparison.OrdinalIgnoreCase)) { httpRequestMessage = new HttpRequestMessage(HttpMethod.Trace, url); } else if (e.Name.Equals("http", StringComparison.OrdinalIgnoreCase) || e.Name.Equals("httpget", StringComparison.OrdinalIgnoreCase)) { httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, url); } if (httpRequestMessage != null) { try { if (config != null) { config = new Dictionary <string, object>(config, StringComparer.OrdinalIgnoreCase); if (config.ContainsKey("headers")) { header = config["headers"] as IDictionary <string, object>; } else if (config.ContainsKey("header")) { header = config["header"] as IDictionary <string, object>; } else if (config.ContainsKey("head")) { header = config["head"] as IDictionary <string, object>; } else if (config.ContainsKey("h")) { header = config["h"] as IDictionary <string, object>; } if (header != null) { header.Keys.ToList().ForEach(key => httpRequestMessage.Headers.Add(key, header[key].ToString())); } } HttpResponseMessage response = client.SendAsync(httpRequestMessage).Result; if (!e.Args.Last().Trim().Equals("f", StringComparison.OrdinalIgnoreCase)) { e.Value = response.Content.Headers.ContentType.MediaType.IndexOf("image", StringComparison.OrdinalIgnoreCase) >= 0 ? new Bitmap(response.Content.ReadAsStreamAsync().Result) : (object)response.Content.ReadAsStringAsync().Result; } else { e.Value = response; } } catch (Exception exception) { e.Value = exception; } return(true); } return(false); }
private void Evaluator_EvaluateFunction(object sender, FunctionEvaluationEventArg e) { }
public bool TryEvaluate(object sender, FunctionEvaluationEventArg e) { Match iniVariableEvalMatch = iniVariableEvalRegex.Match(e.Name); if (iniVariableEvalMatch.Success && e.This == null) { if (iniVariableEvalMatch.Groups["string"].Success) { IniFile inifile = new IniFile(); inifile.LoadFromString(e.EvaluateArg <string>(0)); e.Value = inifile; } else { e.Value = new IniFile(e.EvaluateArg <string>(0)); } } else if ((e.Name.Equals("gv", StringComparison.OrdinalIgnoreCase) || e.Name.Equals("v", StringComparison.OrdinalIgnoreCase)) && e.This is IniFile inifile) { if (e.Args.Count == 3) { e.Value = inifile.GetValue(e.EvaluateArg <string>(0), e.EvaluateArg <string>(1), e.EvaluateArg <string>(2)); } if (e.Args.Count == 2) { e.Value = inifile.GetValue(e.EvaluateArg <string>(0), e.EvaluateArg <string>(1)); } } else if ((e.Name.Equals("gvw", StringComparison.OrdinalIgnoreCase) || e.Name.Equals("vw", StringComparison.OrdinalIgnoreCase)) && e.This is IniFile inifile2) { if (e.Args.Count == 3) { e.Value = inifile2.GetValueWithoutCreating(e.EvaluateArg <string>(0), e.EvaluateArg <string>(1), e.EvaluateArg <string>(2)); } if (e.Args.Count == 2) { e.Value = inifile2.GetValueWithoutCreating(e.EvaluateArg <string>(0), e.EvaluateArg <string>(1)); } } else if ((e.Name.Equals("gb", StringComparison.OrdinalIgnoreCase) || e.Name.Equals("b", StringComparison.OrdinalIgnoreCase)) && e.This is IniFile inifile3) { if (e.Args.Count == 3) { e.Value = inifile3.GetBool(e.EvaluateArg <string>(0), e.EvaluateArg <string>(1), e.EvaluateArg <bool>(2)); } if (e.Args.Count == 2) { e.Value = inifile3.GetBool(e.EvaluateArg <string>(0), e.EvaluateArg <string>(1)); } } else if ((e.Name.Equals("gbw", StringComparison.OrdinalIgnoreCase) || e.Name.Equals("bw", StringComparison.OrdinalIgnoreCase)) && e.This is IniFile inifile4) { if (e.Args.Count == 3) { e.Value = inifile4.GetBoolWithoutCreating(e.EvaluateArg <string>(0), e.EvaluateArg <string>(1), e.EvaluateArg <bool>(2)); } if (e.Args.Count == 2) { e.Value = inifile4.GetBoolWithoutCreating(e.EvaluateArg <string>(0), e.EvaluateArg <string>(1)); } } else if ((e.Name.Equals("sections", StringComparison.OrdinalIgnoreCase) || e.Name.Equals("sec", StringComparison.OrdinalIgnoreCase) || e.Name.Equals("s", StringComparison.OrdinalIgnoreCase)) && e.This is IniFile inifile5) { e.Value = inifile5.SectionsNames; } else if ((e.Name.Equals("kfs", StringComparison.OrdinalIgnoreCase) || e.Name.Equals("keys", StringComparison.OrdinalIgnoreCase) || e.Name.Equals("kos", StringComparison.OrdinalIgnoreCase) || e.Name.Equals("ks", StringComparison.OrdinalIgnoreCase) || e.Name.Equals("k", StringComparison.OrdinalIgnoreCase)) && e.This is IniFile inifile6) { e.Value = inifile6.GetKeysOfSection(e.EvaluateArg <string>(0)); } return(e.FunctionReturnedValue); }
public bool TryEvaluate(object sender, FunctionEvaluationEventArg e) { Match qrMatch = qrRegex.Match(e.Name); if (qrMatch.Success) { string text = string.Empty; if (e.Args.Count > 0) { text = e.EvaluateArg <string>(0); } QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(text, QRCodeGenerator.ECCLevel.Q); QRCode qrCode = new QRCode(qrCodeData); int size = Config.Instance.QrCodeDefaultSize; Color darkColor = Config.Instance.QRCodeDarkColor; Color lightColor = Config.Instance.QRCodeLightColor; if (qrMatch.Groups["size"].Success) { size = int.Parse(qrMatch.Groups["size"].Value); } Color GetColor(int i, Color defaultColor) { Color temp = e.Args[i].ToColor(); if (temp.A == 0 && temp.R == 0 && temp.G == 0 && temp.B == 0) { object result = e.EvaluateArg(i); temp = result is Color color ? color : e.EvaluateArg(i).ToString().ToColor(); } if (temp.A == 0 && temp.R == 0 && temp.G == 0 && temp.B == 0) { return(defaultColor); } return(temp); } if (e.Args.Count > 1) { darkColor = GetColor(1, Config.Instance.QRCodeDarkColor); } if (e.Args.Count > 2) { lightColor = GetColor(2, Config.Instance.QRCodeLightColor); } e.Value = qrCode.GetGraphic(Math.Max(Math.Min(size, 500), 1), darkColor, lightColor, true); return(true); } else { return(false); } }