babalae/better-genshin-impact · error · InvalidOperationException

当前不在主界面

Error message

当前不在主界面

What it means

InvalidOperationException in AutoTrackPathTask.Track's main loop when GetMiniMapMat returns null on the first capture of the iteration. GetMiniMapMat returns null when ElementRecognition cannot find the PaimonMenu element (top-left Paimon face icon), which is the standard sentinel for 'currently on the main in-world UI'. Absence means the game is in a menu/dialogue/loading screen.

Source

Thrown at BetterGenshinImpact/GameTask/AutoTrackPath/AutoTrackPathTask.cs:210

                // Debug.WriteLine($"当前人物图像坐标系角度:{angle}");

                // var moveAngle = (int)(_targetAngle - angle);
                // Debug.WriteLine($"旋转到目标角度:{_targetAngle},鼠标平移{moveAngle}单位");
                // Simulation.SendInput.Mouse.MoveMouseBy(moveAngle, 0);
                Sleep(60);
            }
        });
    }

    public Task Track(List<GiPathPoint> pList, int angleOffsetUnit, CancellationTokenSource trackCts)
    {
        return new Task(() =>
        {
            var currIndex = 0;
            while (!_ct.IsCancellationRequested)
            {
                var ra = CaptureToRectArea();
                var miniMapMat = GetMiniMapMat(ra) ?? throw new InvalidOperationException("当前不在主界面");

                // 注意游戏坐标系的角度是顺时针的
                var matchingMethod = TaskContext.Instance().Config.PathingConditionConfig.MapMatchingMethod;
                var currMapImageAvatarPos = MapManager.GetMap(MapTypes.Teyvat, matchingMethod).GetMiniMapPosition(miniMapMat);
                if (currMapImageAvatarPos.IsEmpty())
                {
                    Debug.WriteLine("识别小地图位置失败");
                    continue;
                }
                var (nextMapImagePathPoint, nextPointIndex) = GetNextPoint(currMapImageAvatarPos, pList, currIndex); // 动态计算下个点位
                var nextMapImagePathPos = nextMapImagePathPoint.MatchPt;
                Logger.LogInformation("下个点位[{Index}]:{nextMapImagePathPos}", nextPointIndex, nextMapImagePathPos);

                var angle = CharacterOrientation.Compute(miniMapMat);
                CameraOrientation.DrawDirection(ra, angle, "avatar", new Pen(Color.Blue, 1));
                Debug.WriteLine($"当前人物图像坐标系角度:{angle},位置:{currMapImageAvatarPos}");

                var nextAngle = Math.Round(Math.Atan2(nextMapImagePathPos.Y - currMapImageAvatarPos.Y, nextMapImagePathPos.X - currMapImageAvatarPos.X) * 180 / Math.PI);

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Do not interact with the game during an active AutoTrackPath run.
  2. Run at 1920x1080 so the PaimonMenu template matches reliably.
  3. Treat this as abort: re-run the path from a teleport point after returning to the main UI.
  4. If frequent, wrap the GetMiniMapMat call in a short retry before throwing so a single bad frame does not abort.

Example fix

// before
var miniMapMat = GetMiniMapMat(ra) ?? throw new InvalidOperationException("当前不在主界面");

// after: tolerate a few transient misses
var miniMapMat = GetMiniMapMat(ra);
if (miniMapMat == null)
{
    Logger.LogDebug("未检测到主界面,跳过本帧");
    Sleep(100);
    continue;
}
Defensive patterns

Strategy: try-catch

Try / catch

try { await trackTask; }
catch (InvalidOperationException e) when (e.Message == "当前不在主界面")
{ /* return to main UI and restart the route from nearest TP */ }

Prevention

When it happens

Trigger: Track loop iterating while the player opened the inventory/map/menu, entered dialogue, hit a loading screen, or the PaimonMenu template failed to match due to resolution/HDR/capture desync. Because the Track loop has no retry around this first GetMiniMapMat, the first missed frame aborts tracking.

Common situations: User interacted with the game mid-track; cutscene triggered; capture pipeline delivered a stale/blank frame; resolution mismatch broke PaimonMenu template; PaimonMenu hidden by a notification toast at that instant.

Related errors


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