From f5675694b932fdab5ba7791c94106627cead1f72 Mon Sep 17 00:00:00 2001 From: "Dr. Jochen Manns" Date: Fri, 11 Oct 2024 11:36:09 +0200 Subject: [PATCH] Correctly respecting disabling blocks. --- Library/Core/Model/Block.cs | 11 +++++++++-- Library/Core/Model/Workspace.cs | 18 ++++++++++-------- Library/Core/XmlParser.cs | 8 ++++---- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/Library/Core/Model/Block.cs b/Library/Core/Model/Block.cs index 3bcfae6..59bf998 100644 --- a/Library/Core/Model/Block.cs +++ b/Library/Core/Model/Block.cs @@ -11,6 +11,11 @@ public abstract class Block : IFragment /// public string Id { get; set; } = null!; + /// + /// Unset to exclude this block from execution. + /// + public bool Enabled { get; set; } = true; + /// /// All fields (constant values) of the block. /// @@ -62,8 +67,10 @@ public abstract class Block : IFragment throw new ScriptStoppedEarlyException(); /* Run the next block if we are not forcefully exiting a loop. */ - if (Next != null && context.EscapeMode == EscapeMode.None) - return await Next.EvaluateAsync(context); + if (context.EscapeMode == EscapeMode.None) + for (var next = Next; next != null; next = next.Next) + if (next.Enabled) + return await next.EvaluateAsync(context); return null; } diff --git a/Library/Core/Model/Workspace.cs b/Library/Core/Model/Workspace.cs index 2ad8c27..7569079 100644 --- a/Library/Core/Model/Workspace.cs +++ b/Library/Core/Model/Workspace.cs @@ -29,18 +29,19 @@ public class Workspace : IFragment var functions = new List(); foreach (var block in Blocks.OfType()) - { - /* Create the function itself and remember it. */ - context.Cancellation.ThrowIfCancellationRequested(); + if (block.Enabled) + { + /* Create the function itself and remember it. */ + context.Cancellation.ThrowIfCancellationRequested(); - await block.EvaluateAsync(context); + await block.EvaluateAsync(context); - functions.Add(block); - } + functions.Add(block); + } /* Process any block which is not a function. */ foreach (var block in Blocks) - if (!functions.Contains(block)) + if (!functions.Contains(block) && block.Enabled) { /* Remember the result and report the last result afterwards. */ context.Cancellation.ThrowIfCancellationRequested(); @@ -107,7 +108,8 @@ public async Task> GetGroupTreeAsync() /* Find all functions and generate the block list. */ foreach (var block in Blocks.OfType()) - await block.EvaluateAsync(context); + if (block.Enabled) + await block.EvaluateAsync(context); /* Inspect all blocks. */ foreach (var block in Blocks) diff --git a/Library/Core/XmlParser.cs b/Library/Core/XmlParser.cs index a03555d..de1541c 100644 --- a/Library/Core/XmlParser.cs +++ b/Library/Core/XmlParser.cs @@ -68,16 +68,16 @@ public override Workspace Parse(string xml, bool preserveWhitespace = false) private Block? ParseBlock(XmlNode node) { - if (bool.Parse(node.GetAttribute("disabled") ?? "false")) - return null; - var type = node.GetAttribute("type"); + if (!blocks.ContainsKey(type)) throw new ApplicationException($"block type not registered: '{type}'"); + var block = blocks[type](); - block.Type = type; + block.Enabled = node.GetAttribute("disabled-reasons") == null; block.Id = node.GetAttribute("id"); + block.Type = type; foreach (XmlNode childNode in node.ChildNodes) {