babalae/better-genshin-impact · error · InvalidOperationException

未能读取当前合成个数。

Error message

未能读取当前合成个数。

What it means

Thrown after the craft-quantity slider is dragged to the target ratio when ReadCurrentCraftQuantity() returns a non-positive value. ReadCurrentCraftQuantity OCRs the current-quantity text at Rect1080(1248,615,221,34) and parses the first positive integer. A zero return means the OCR region produced no parseable digits.

Source

Thrown at BetterGenshinImpact/GameTask/Common/Job/CraftMaterialTask.cs:410

            throw new InvalidOperationException($"材料不足以合成指定个数,目标 {targetQuantity},当前最多可合成 {maxQuantity}。");
        }

        if (maxQuantity == 1)
        {
            return 1;
        }

        await DragSliderToRatio(0);
        await Delay(250, _ct);

        var ratio = Math.Clamp((targetQuantity - 1d) / (maxQuantity - 1d), 0d, 1d);
        await DragSliderToRatio(ratio);
        await Delay(350, _ct);

        var actualQuantity = ReadCurrentCraftQuantity();
        if (actualQuantity <= 0)
        {
            throw new InvalidOperationException("未能读取当前合成个数。");
        }

        if (Math.Abs(actualQuantity - targetQuantity) > 30)
        {
            await DragSliderToRatio(ratio);
            await Delay(350, _ct);
            actualQuantity = ReadCurrentCraftQuantity();
            if (actualQuantity <= 0 || Math.Abs(actualQuantity - targetQuantity) > 30)
            {
                throw new InvalidOperationException($"滑块调整合成个数后校验失败,目标 {targetQuantity},当前 {actualQuantity}。");
            }
        }

        while (actualQuantity < targetQuantity)
        {
            GameCaptureRegion.GameRegion1080PPosClick(addButton.X, addButton.Y);
            await Delay(60, _ct);
            actualQuantity++;

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Confirm the capture rect Rect1080(1248,615,221,34) aligns with the current-quantity field in the running game.
  2. Verify DragSliderToRatio actually moved the handle (check the mouse-down/up path and the 12-step interpolation).
  3. Add one extra re-read with a delay before failing, since OCR misses are often transient.
  4. Log the raw OCR text (via ReadOcrText) when the parse returns 0 to diagnose which character is unreadable.

Example fix

// before
var actualQuantity = ReadCurrentCraftQuantity();
if (actualQuantity <= 0)
{
    throw new InvalidOperationException("未能读取当前合成个数。");
}

// after: re-read once and log raw text on failure
var actualQuantity = ReadCurrentCraftQuantity();
if (actualQuantity <= 0)
{
    await Delay(300, _ct);
    actualQuantity = ReadCurrentCraftQuantity();
}
if (actualQuantity <= 0)
{
    Logger.LogWarning("当前合成个数 OCR 原文:{Raw}", ReadOcrText(Rect1080(1248, 615, 221, 34)));
    throw new InvalidOperationException("未能读取当前合成个数。");
}
Defensive patterns

Strategy: retry

Validate before calling

// Pre-validate the OCR region is on a populated field
var raw = ReadOcrText(Rect1080(1248, 615, 221, 34));
if (string.IsNullOrWhiteSpace(StringUtils.RemoveAllSpace(raw)))
    Logger.LogWarning("合成个数区域为空,可能未对齐");

Type guard

null

Try / catch

try { await SetCraftQuantity(target); }
catch (InvalidOperationException e) when (e.Message.Contains("未能读取当前合成个数"))
{ /* re-orient / re-drag, then retry once */ }

Prevention

When it happens

Trigger: DragSliderToRatio(ratio) completes but the number at the quantity field is not recognized — text region misaligned, the field is obscured, the slider did not actually move, or OCR (Paddle) returned garbage/full-width characters that StringUtils.ConvertFullWidthNumToHalfWidth + ReadFirstPositiveInt cannot parse.

Common situations: Game resolution or capture scale changed so the 1080p-derived Rect no longer sits on the number; the slider drag was dropped early and the field still shows a placeholder; a localization/font change makes digits unrecognizable; transient capture where the field is mid-animation.

Related errors


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