babalae/better-genshin-impact · warning · InvalidOperationException

当前动作已经设置完成条件,不能再次修改或添加

Error message

当前动作已经设置完成条件,不能再次修改或添加

What it means

Thrown by DecodeImage when both the WPF BitmapDecoder and the SixLabors.ImageSharp fallback fail to decode the byte array into a valid image. The WPF path catches NotSupportedException or FileFormatException and falls back to ImageSharp; if ImageSharp also throws, an InvalidDataException wraps the ImageSharp exception. This means the bytes are not a recognizable or supported image format.

Source

Thrown at BetterGenshinImpact/Core/BgiVision/BvFlowAction.cs:257

            retryInterval = _retryInterval ?? _flow.DefaultRetryInterval;
        }

        var targetDescription = DescribeTargets(targets, condition);
        return _flow.AddActionStep(new BvFlowActionSnapshot(
            _description,
            _action,
            targets,
            targetDescription,
            timeout,
            retryInterval,
            condition));
    }

    private void EnsureNotCompleted()
    {
        if (_completed)
        {
            throw new InvalidOperationException("当前动作已经设置完成条件,不能再次修改或添加");
        }
    }

    private static void ValidateWaitOptions(int? timeout, int? retryInterval)
    {
        if (timeout is { } timeoutValue)
        {
            BvFlow.ValidatePositive(timeoutValue, nameof(timeout));
        }

        if (retryInterval is { } retryIntervalValue)
        {
            BvFlow.ValidatePositive(retryIntervalValue, nameof(retryInterval));
        }
    }

    internal static string DescribeTargets(IReadOnlyList<BvLocator> targets, BvFlowCondition condition)
    {

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Inspect the InnerException (the ImageSharp exception) for the specific format error.
  2. Verify the source URL/path points to a real image (open it in a browser/image viewer).
  3. Check the first few bytes for a valid image magic number (PNG: 89 50 4E 47, JPEG: FF D8 FF).
  4. Convert the image to PNG or JPEG before referencing it in Markdown.

Example fix

// before
catch (Exception imageSharpException)
{
    throw new InvalidDataException("无法解码 Markdown 图片。", imageSharpException);
}

// after — include format hint and degrade
catch (Exception imageSharpException)
{
    _owner.ReportDiagnostic(new MarkdownDiagnostic(MarkdownDiagnosticSeverity.Warning,
        $"无法解码图片(格式不支持或数据损坏):{imageSharpException.Message}"));
    return MarkdownImageResult.Placeholder;
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-validate image magic number before decoding
if (bytes.Length < 4) return Placeholder;
var isPng = bytes[0] == 0x89 && bytes[1] == 0x50;
var isJpeg = bytes[0] == 0xFF && bytes[1] == 0xD8;
var isGif = bytes[0] == 0x47 && bytes[1] == 0x49;
if (!(isPng || isJpeg || isGif)) return Placeholder;

Type guard

static bool HasValidImageHeader(byte[] bytes) =>
    bytes.Length >= 4 && (
        (bytes[0] == 0x89 && bytes[1] == 0x50) || // PNG
        (bytes[0] == 0xFF && bytes[1] == 0xD8) || // JPEG
        (bytes[0] == 0x47 && bytes[1] == 0x49));  // GIF

Try / catch

try { return DecodeImage(bytes); }
catch (InvalidDataException ex) { _logger.LogWarning(ex.Inner, "解码失败"); return Placeholder; }

Prevention

When it happens

Trigger: DecodeImage receives bytes that are corrupted, truncated, or in an unsupported format. BitmapDecoder.Create throws NotSupportedException/FileFormatException (WPF can't handle it), then Image.Load<Bgra32> also throws (ImageSharp can't handle it).

Common situations: Downloaded image was truncated by a network error or proxy, the file is not actually an image (e.g. an HTML 404 page saved as .png), the format is exotic (e.g. HEIC, AVIF) unsupported by both decoders, or the bytes are zero-length.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/eb5997630159aaca. Report an issue: GitHub.