babalae/better-genshin-impact · error · InvalidOperationException

未能识别最大可合成个数。

Error message

未能识别最大可合成个数。

What it means

Thrown as InvalidOperationException by CraftMaterialTask.SetCraftQuantity when ReadMaxCraftQuantity() returns a value <= 0. The method first tries OCR on the max-quantity region (1522,653), then falls back to parsing material fraction text (owned/required). If both approaches yield zero, the task cannot determine how many can be crafted and aborts before slider manipulation.

Source

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

    {
        return ContainsText(Rect1080(1139, 112, 401, 39), _materialName);
    }

    /// <summary>
    /// 设置本次合成个数并校验最终数量。
    /// </summary>
    /// <param name="targetQuantity">目标合成个数。</param>
    /// <returns>最终校验到的合成个数。</returns>
    /// <exception cref="InvalidOperationException">最大可合成个数、当前合成个数读取失败,材料不足或最终数量不匹配时抛出。</exception>
    private async Task<int> SetCraftQuantity(int targetQuantity)
    {
        var addButton = (X: 1614, Y: 672);
        var reduceButton = (X: 1074, Y: 672);

        var maxQuantity = ReadMaxCraftQuantity();
        if (maxQuantity <= 0)
        {
            throw new InvalidOperationException("未能识别最大可合成个数。");
        }

        if (targetQuantity > maxQuantity)
        {
            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);

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Add a delay after selecting the material to let the detail panel render fully before reading quantity.
  2. Verify the game resolution is 1080p or that the scale ratio is correct.
  3. If UI positions changed in a game update, update the Rect1080 coordinates for max-quantity and fraction text regions.

Example fix

null
Defensive patterns

Strategy: retry

Validate before calling

null

Type guard

null

Try / catch

for (int attempt = 0; attempt < 3; attempt++)
{
    try { await task.Start(ct); break; }
    catch (InvalidOperationException ex) when (ex.Message.Contains("最大可合成个数")) { /* wait for UI, retry */ } }

Prevention

When it happens

Trigger: The selected material detail panel does not show a readable max-quantity number or material fraction text. OCR failed on both the numeric region and the fraction region. The screenshot is blurry, wrong resolution, or the UI element was occluded.

Common situations: The crafting UI did not fully load the detail panel after material selection. Non-1080p resolution causes Rect1080 to crop the wrong area. OCR engine misread full-width digits or the text was rendered with unusual fonts. A game update changed the UI layout/positions.

Related errors


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