public static void ViewInBrowser([CanBeNull] string url) { if (string.IsNullOrWhiteSpace(url)) { return; } try { Process.Start(url); } catch (Exception) { TypoLogging.NonFatalErrorNotify("Can’t open link", $"App tried to open: “{url}”"); } }
public static Script CreateState() { var state = new Script(); state.Globals["showMsg"] = new Action <string>(m => MessageBox.Show(m)); state.Globals["report"] = new Action <string>(m => TypoLogging.NonFatalErrorNotify(m, null)); state.Globals["log"] = new Action <string>(m => TypoLogging.Write(m)); state.Globals["regex"] = new LuaRegex(); state.Globals["unicode"] = new LuaUnicode(); return(state); }
private void Reload(string filename) { if (filename?.EndsWith(".lua", StringComparison.OrdinalIgnoreCase) == false) { return; } _lua = Directory.GetFiles(_directory, "*.lua").Select(x => { try { return(ScriptExtension.CreateState().DoString(File.ReadAllText(x), codeFriendlyName: Path.GetFileName(x)).Function); } catch (Exception e) { TypoLogging.NonFatalErrorNotify("Can’t execute script", null, e); return(null); } }).NonNull().ToArray(); }
private string ReplaceCallback([CanBeNull] string a, [CanBeNull] string b, [CanBeNull] Closure c, bool ignoreCase) { return(a == null || b == null || c == null ? null : GetRegex(b, ignoreCase).Replace(a, x => { var args = new object[x.Groups.Count]; for (var i = 0; i < args.Length; i++) { args[i] = x.Groups[i].Value; } try { return c.Call(args).String; } catch (Exception e) { TypoLogging.NonFatalErrorNotify("Can’t execute script", null, e); return x.Value; } })); }
public async Task <string> ReplaceAsync(int scriptId, string originalText, CancellationToken cancellation) { try { var interpreter = GetInterpreter(scriptId, out var filename); TypoLogging.Write("Run command: " + filename + ", orig.=" + originalText + ", iterp.=" + interpreter); if (interpreter != null) { return(await interpreter.ExecuteAsync(filename, originalText, cancellation)); } TypoLogging.Write($"Supported script not found: {scriptId}"); return(null); } catch (Exception e) { TypoLogging.NonFatalErrorNotify("Failed to run a script", "Check if all required libraries are ready.", e); return(null); } }