public void Execute(MacroEntry macro) { if (macro.IsBackground) { if (macro.IsRunning) { macro.Stop(); } else { macro.Execute(); } } else { if (CurrentMacro != null && CurrentMacro.IsRunning) { if (macro == CurrentMacro && macro.DoNotAutoInterrupt) { return; } CurrentMacro.Stop(); } CurrentMacro = macro; CurrentMacro.Execute(); } }
public void Stop(string name = null) { if (string.IsNullOrEmpty(name)) { CurrentMacro?.Stop(); } else { MacroEntry macro = Items.FirstOrDefault(m => m.Name.ToLower().Equals(name.ToLower())); macro?.Stop(); } }
public void Execute(MacroEntry macro) { _invoker = MacroInvoker.GetInstance(); void OnException(Exception exception) { UO.Commands.SystemMessage(string.Format(Strings.Macro_error___0_, exception.Message)); if (exception is SyntaxErrorException syntaxError) { UO.Commands.SystemMessage($"{Strings.Line_Number}: {syntaxError.RawSpan.Start.Line}"); } } _invoker.ExceptionEvent += OnException; _invoker.Execute(macro); _invoker.ExceptionEvent -= OnException; }
public void Execute(MacroEntry macro) { _macro = macro; if (Thread != null && Thread.IsAlive) { Stop(); } _cancellationToken = new CancellationTokenSource(); if (_importCache == null) { _importCache = InitializeImports(_engine); } ScriptSource source = _engine.CreateScriptSourceFromString(_macro.Macro, SourceCodeKind.Statements); Dictionary <string, object> importCache = new Dictionary <string, object>(_importCache); IsFaulted = false; Thread = new Thread(() => { Thread = Thread.CurrentThread; try { StartedEvent?.Invoke(); AliasCommands.SetDefaultAliases(); ScriptScope macroScope = _engine.CreateScope(importCache); _stopWatch.Reset(); _stopWatch.Start(); do { _cancellationToken.Token.ThrowIfCancellationRequested(); source.Execute(macroScope); _stopWatch.Stop(); bool willLoop = _macro.Loop && !IsFaulted && !_cancellationToken.IsCancellationRequested; if (!willLoop) { break; } if (Options.CurrentOptions.Debug) { UO.Commands.SystemMessage(string.Format(Strings.Loop_time___0_, _stopWatch.Elapsed)); } int diff = 50 - (int)_stopWatch.ElapsedMilliseconds; if (diff > 0) { Thread.Sleep(diff); } }while (_macro.Loop && !IsFaulted); } catch (TaskCanceledException) { IsFaulted = true; } catch (ThreadInterruptedException) { IsFaulted = true; } catch (ThreadAbortException) { IsFaulted = true; } catch (Exception e) { IsFaulted = true; Exception = e; ExceptionEvent?.Invoke(e); } finally { StoppedEvent?.Invoke(); } }) { IsBackground = true }; try { Thread.Start(); Thread.Join(); } catch (ThreadStartException) { // TODO } }