babalae/better-genshin-impact · error · InvalidOperationException

请先打开合成界面。

Error message

请先打开合成界面。

What it means

Thrown as InvalidOperationException by CraftMaterialTask.EnsureInCraftingUi when IsInCraftingUi() returns false. The method checks two OCR text regions ('筛选' at bottom and '合成' at top-left) to confirm the game is on the crafting (alchemy) bench screen. If either text is missing, the task aborts before attempting any clicks.

Source

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

            var header = headers[i]?.Trim();
            if (candidates.Any(candidate => string.Equals(header, candidate, StringComparison.OrdinalIgnoreCase)))
            {
                return i;
            }
        }

        return -1;
    }

    /// <summary>
    /// 确认当前界面是合成界面。
    /// </summary>
    /// <exception cref="InvalidOperationException">当前不在合成界面时抛出。</exception>
    private void EnsureInCraftingUi()
    {
        if (!IsInCraftingUi())
        {
            throw new InvalidOperationException("请先打开合成界面。");
        }
    }

    /// <summary>
    /// 判断当前是否处于合成界面。
    /// </summary>
    /// <returns>处于合成界面时返回 true。</returns>
    private bool IsInCraftingUi()
    {
        return ContainsText(Rect1080(40, 960, 720, 105), "筛选")
               && ContainsText(Rect1080(0, 0, 260, 95), "合成");
    }

    /// <summary>
    /// 选择材料筛选类型,失败时抛出异常。
    /// </summary>
    /// <param name="materialType">材料筛选类型。</param>
    /// <returns>异步任务。</returns>

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Navigate the character to an alchemy bench and open the crafting screen before starting the task.
  2. Add a wait/retry loop to let the crafting UI fully render before calling Start.
  3. Ensure the game language is set to Chinese and the resolution is 1080p or scales correctly.

Example fix

// before
var task = new CraftMaterialTask("树脂", 5);
await task.Start(ct); // throws if not on crafting screen

// after
// ensure crafting screen is open first, optionally with a retry loop
await WaitForCraftingUiAsync(ct);
var task = new CraftMaterialTask("树脂", 5);
await task.Start(ct);
Defensive patterns

Strategy: validation

Validate before calling

// Before starting, confirm crafting UI via OCR
if (!ContainsText(craftingFilterRect, "筛选") || !ContainsText(craftingTitleRect, "合成"))
{
    // open crafting bench first
}

Type guard

null

Try / catch

try { await task.Start(ct); }
catch (InvalidOperationException ex) when (ex.Message.Contains("合成界面")) { /* navigate to alchemy bench, retry */ }

Prevention

When it happens

Trigger: Calling Start(ct) when the game is on a different screen (inventory, map, Paimon menu). The crafting bench was not opened, or it was closed between navigation and task start. The screenshot resolution is non-1080p and the Rect1080 scaling mis-crops the text regions.

Common situations: The user started the task from outside the crafting screen. A timing issue where the crafting UI was still loading. The game window resolution or scale differs from expected, causing OCR to miss the anchor text. Game UI language is not Chinese so '合成'/'筛选' are not found.

Related errors


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