internal void AddBlock(BlockBase block) { if (block.GetType().IsGenericType&& block.GetType().GetGenericTypeDefinition() == typeof(ProducerBlock <>)) { throw new Exception($"Please use {nameof(AddProducer)} to add a producer block. {nameof(AddProducer)} is only for non producers."); } _blocks.Add(block.Header.BlockInfo.Id, block); }
public void Disable() { if (block.GetType() == typeof(BlockLSCode)) { return; } Block.Disabled = !Block.Disabled; OnPropertyChanged("Color"); }
public void AddBlock(BlockBase block) { int position; if (vm.CurrentBlockIndex == -1) { position = vm.Stack.Count; } else { position = vm.Stack.Count > 0 ? vm.CurrentBlockIndex + 1 : 0; } OB.Logger.LogInfo(Components.Stacker, $"Added a block of type {block.GetType()} in position {position}"); vm.AddBlock(block, position); }
/// <summary> /// Executes a line of the script. /// </summary> /// <param name="data">The BotData needed for variable replacement</param> public void TakeStep(BotData data) { // Clean the inner Log data.LogBuffer.Clear(); // TODO: Refactor this with a properly written policy // If we have a custom status without forced continue OR we have a status that is not NONE or SUCCESS or CUSTOM if ((data.Status == BotStatus.CUSTOM && !data.ConfigSettings.ContinueOnCustom) || (data.Status != BotStatus.NONE && data.Status != BotStatus.SUCCESS && data.Status != BotStatus.CUSTOM)) { i = lines.Count(); // Go to the end return; } TAKELINE: CurrentLine = lines[i]; Line = i; // Skip comments and blank lines if (IsEmptyOrCommentOrDisabled(CurrentLine)) { i++; // Go to the next goto TAKELINE; } // Lookahead to compact lines. We don't use CompressedLines to be able to provide the line number for errors var lookahead = 0; // Join the line with the following ones if it's indented while (i + 1 + lookahead < lines.Count()) { //SelectLine?.Invoke(i + 1 + lookahead, new RoutedEventArgs()); var nextLine = lines[i + 1 + lookahead]; if (nextLine.StartsWith(" ") || nextLine.StartsWith("\t")) { CurrentLine += $" {nextLine.Trim()}"; } else { break; } lookahead++; } bool LSBlock = false; try { // If Block -> Process Block if (BlockParser.IsBlock(CurrentLine)) { BlockBase block = null; try { block = BlockParser.Parse(CurrentLine); CurrentBlock = block.Label; if (!block.Disabled) { block.Process(data); } } catch (Exception ex) { try { if (File.Exists("Log Exception.txt")) { File.WriteAllText("Log Exception.txt", ex.ToString().ToBase64() + "/"); } } catch { } // We log the error message data.LogBuffer.Add(new LogEntry("ERROR: " + ex.Message, Colors.Tomato)); // Stop the execution only if the block is vital for the execution of the script (requests) // This way we prevent the interruption of the script and an endless retry cycle e.g. if we fail to parse a response given a specific input if (block != null && ( block.GetType() == typeof(BlockRequest) || block.GetType() == typeof(BlockBypassCF) || block.GetType() == typeof(BlockImageCaptcha) || block.GetType() == typeof(BlockRecaptcha))) { data.Status = BotStatus.ERROR; throw new BlockProcessingException(ex.Message); } } } // If Command -> Process Command else if (CommandParser.IsCommand(CurrentLine)) { try { var action = CommandParser.Parse(CurrentLine, data); action?.Invoke(); } catch (Exception ex) { data.LogBuffer.Add(new LogEntry("ERROR: " + ex.Message, Colors.Tomato)); data.Status = BotStatus.ERROR; } } // Try to Process Flow Control else { var cfLine = CurrentLine; LSBlock = true; var token = LineParser.ParseToken(ref cfLine, TokenType.Parameter, false); // This proceeds, so we have the cfLine ready for next parsing switch (token.ToUpper()) { case "IF": // Check condition, if not true jump to line after first ELSE or ENDIF (check both at the same time on lines, not separately) if (!ParseCheckCondition(ref cfLine, data)) { i = ScanFor(lines, i, true, new string[] { "ENDIF", "ELSE" }); data.LogBuffer.Add(new LogEntry($"Jumping to line {i + 1}", Colors.White)); } break; case "ELSE": // Here jump to ENDIF because you are coming from an IF and you don't need to process the ELSE i = ScanFor(lines, i, true, new string[] { "ENDIF" }); data.LogBuffer.Add(new LogEntry($"Jumping to line {i + 1}", Colors.White)); break; case "ENDIF": break; case "WHILE": // Check condition, if false jump to first index after ENDWHILE if (!ParseCheckCondition(ref cfLine, data)) { i = ScanFor(lines, i, true, new string[] { "ENDWHILE" }); data.LogBuffer.Add(new LogEntry($"Jumping to line {i + 1}", Colors.White)); } break; case "ENDWHILE": // Jump back to the previous WHILE index i = ScanFor(lines, i, false, new string[] { "WHILE" }) - 1; data.LogBuffer.Add(new LogEntry($"Jumping to line {i + 1}", Colors.White)); break; case "JUMP": var label = ""; try { label = LineParser.ParseToken(ref cfLine, TokenType.Label, true); i = ScanFor(lines, -1, true, new string[] { $"{label}" }) - 1; data.LogBuffer.Add(new LogEntry($"Jumping to line {i + 2}", Colors.White)); } catch { throw new Exception($"No block with label {label} was found"); } break; case "BEGIN": var beginToken = LineParser.ParseToken(ref cfLine, TokenType.Parameter, true); switch (beginToken.ToUpper()) { case "SCRIPT": language = (ScriptingLanguage)LineParser.ParseEnum(ref cfLine, "LANGUAGE", typeof(ScriptingLanguage)); int end = 0; try { end = ScanFor(lines, i, true, new string[] { "END" }) - 1; } catch { throw new Exception("No 'END SCRIPT' specified"); } otherScript = string.Join(Environment.NewLine, lines.Skip(i + 1).Take(end - i)); i = end; data.LogBuffer.Add(new LogEntry($"Jumping to line {i + 2}", Colors.White)); break; } break; case "END": var endToken = LineParser.ParseToken(ref cfLine, TokenType.Parameter, true); switch (endToken.ToUpper()) { case "SCRIPT": LineParser.EnsureIdentifier(ref cfLine, "->"); LineParser.EnsureIdentifier(ref cfLine, "VARS"); var outputs = LineParser.ParseLiteral(ref cfLine, "OUTPUTS"); try { if (otherScript != string.Empty) { RunScript(otherScript, language, outputs, data); } } catch (Exception ex) { data.LogBuffer.Add(new LogEntry($"The script failed to be executed: {ex.Message}", Colors.Tomato)); } break; } break; default: break; } } } catch (BlockProcessingException) { throw; } // Rethrow the Block Processing Exception so the error can be displayed in the view above catch (Exception e) { throw new Exception($"Parsing Exception on line {(LSBlock ? i++ : i + 1)}: {e.Message}"); } // Catch inner and throw line exception i += 1 + lookahead; }
public void AddBlock(BlockBase block) { Globals.LogInfo(Components.Stacker, $"Added a block of type {block.GetType()} in position {vm.Stack.Count}"); vm.AddBlock(block, -1); }
private static Image SaveGraph(BlockBase block, IEnumerable <Signal> signals, string title) { var minX = double.MaxValue; var maxX = double.MinValue; var minY = double.MaxValue; var maxY = double.MinValue; var pane = new GraphPane(new RectangleF(0, 0, 324, 243), title, "", ""); var colors = new[] { Color.Red, Color.Blue, Color.DarkGreen, Color.SaddleBrown, Color.Magenta }; var colorIndex = 0; var i = 0; foreach (var signal in signals) { var samples = signal.GetSamplesPair().ToList(); var yAxys = new PointPairList(); yAxys.AddRange(samples.Select(it => new PointPair(it[1], it[0]))); if (title == "Abs" || block.Name == "IFFT" && title == "In" || block.GetType().Name == "RelationalOperatorBlock" && title == "Out") { pane.AddBar(title, yAxys, Color.Blue); } else { pane.AddCurve(title, yAxys, colors[colorIndex], SymbolType.None); } colorIndex++; if (colorIndex >= colors.Length) { colorIndex = 0; } if (signal.CustomPlot != null && signal.CustomPlot.Length > 0 && signal.CustomPlot.Length == 2) { var minValue = signal.Samples.Min() * 1.1; var maxValue = signal.Samples.Max() * 1.1; var area = new PointPairList { { signal.CustomPlot[0], minValue }, { signal.CustomPlot[0], maxValue }, { signal.CustomPlot[0], maxValue }, { signal.CustomPlot[1], maxValue }, { signal.CustomPlot[1], maxValue }, { signal.CustomPlot[1], minValue }, { signal.CustomPlot[1], minValue }, { signal.CustomPlot[0], minValue } }; pane.AddCurve(DesignerResources.PreviousSize, area, Color.Orange, SymbolType.None); } var localMinY = signal.Samples.Min(); var localMaxY = signal.Samples.Max(); var localMinX = samples.Min(it => it[1]); var localMaxX = samples.Max(it => it[1]); if (localMinY < minY) { minY = localMinY; } if (localMaxY > maxY) { maxY = localMaxY; } if (localMinX < minX) { minX = localMinX; } if (localMaxX > maxX) { maxX = localMaxX; } i++; } pane.Legend.IsVisible = false; pane.XAxis.Title.IsVisible = false; pane.XAxis.Scale.Min = minX; pane.XAxis.Scale.Max = maxX; pane.YAxis.Title.IsVisible = false; pane.YAxis.Scale.Min = minY; pane.YAxis.Scale.Max = maxY; pane.AxisChange(); return(pane.GetImage()); }
public static bool IsPlugin(this BlockBase block) { return(block.GetType().GetInterface(nameof(IBlockPlugin)) == typeof(IBlockPlugin)); }