{"record":{"id":"b98b7de93d291b56","repo":"babalae/better-genshin-impact","slug":"error-b98b7d","errorCode":null,"errorMessage":"鼠标坐标超出游戏窗口范围","messagePattern":"鼠标坐标超出游戏窗口范围","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/Core/Script/Dependence/GlobalMethod.cs","lineNumber":187,"sourceCode":"\n    public static double[] GetGameMetrics()\n    {\n        return [_gameWidth, _gameHeight, _dpi];\n    }\n\n    public static void MoveMouseBy(int x, int y)\n    {\n        var realDpi = TaskContext.Instance().DpiScale;\n        x = (int)(x * realDpi / _dpi);\n        y = (int)(y * realDpi / _dpi);\n        Simulation.SendInput.Mouse.MoveMouseBy(x, y);\n    }\n\n    public static void MoveMouseTo(int x, int y)\n    {\n        if (x < 0 || x > _gameWidth || y < 0 || y > _gameHeight)\n        {\n            throw new ArgumentException(\"鼠标坐标超出游戏窗口范围\");\n        }\n\n        GameCaptureRegion.GameRegionMove((size, s2) =>\n        {\n            var scale = 1920.0 / _gameWidth;\n            return (x * scale * s2, y * scale * s2);\n        });\n    }\n\n    public static void Click(int x, int y)\n    {\n        MoveMouseTo(x, y);\n        LeftButtonClick();\n    }\n\n    public static void LeftButtonClick()\n    {\n        Simulation.SendInput.Mouse.LeftButtonDown().Sleep(60).LeftButtonUp();","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/Core/Script/Dependence/GlobalMethod.cs#L169-L205","documentation":"Thrown by GlobalMethod.MoveMouseTo when either x or y coordinate is negative or exceeds the configured game width/height (_gameWidth, _gameHeight, defaulting to 1920x1080). The method validates against the game's logical pixel space before computing the scaled mouse position.","triggerScenarios":"Calling GlobalMethod.MoveMouseTo(x, y) or Click(x, y) with coordinates outside [0, _gameWidth] × [0, _gameHeight]. This includes negative values, values larger than the game resolution, or coordinates computed from an incorrect resolution assumption.","commonSituations":"Script hardcodes 1920x1080 coordinates but SetGameMetrics was called with a different resolution (e.g., 2560x1440). Off-by-one boundary (x = _gameWidth exactly triggers the error since the check is >). Coordinate computed from a percentage that rounds above the max.","solutions":["Clamp coordinates to [0, width-1] × [0, height-1] before calling MoveMouseTo.","Retrieve the current game metrics via GlobalMethod.GetGameMetrics() and use the returned width/height for bounds checking.","Ensure SetGameMetrics has been called with the correct resolution matching the actual game capture.","When computing coordinates from ratios, clamp: x = Math.Clamp((int)(ratio * width), 0, width - 1)."],"exampleFix":"// before\nGlobalMethod.Click(targetX, targetY);\n\n// after\nvar metrics = GlobalMethod.GetGameMetrics();\nvar w = (int)metrics[0];\nvar h = (int)metrics[1];\nGlobalMethod.Click(\n    Math.Clamp(targetX, 0, w),\n    Math.Clamp(targetY, 0, h)\n);","handlingStrategy":"validation","validationCode":"// Validate mouse coordinates before calling MoveMouseTo/Click\nvar metrics = GlobalMethod.GetGameMetrics();\nvar gameW = (int)metrics[0];\nvar gameH = (int)metrics[1];\nif (x < 0 || x > gameW || y < 0 || y > gameH)\n    throw new ArgumentOutOfRangeException($\"Coordinates ({x},{y}) out of bounds [0-{gameW}, 0-{gameH}]\");\nGlobalMethod.MoveMouseTo(x, y);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always check against current game metrics via GetGameMetrics().","Clamp computed coordinates: Math.Clamp(x, 0, gameWidth).","Remember the check is > (strictly greater), so x == gameWidth triggers the error.","Derive coordinates from the actual game resolution, not hardcoded 1920x1080."],"tags":["validation","mouse","coordinates","bounds-check","game-capture"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}