babalae/better-genshin-impact · error · FormatException

未识别到全部恢复时间:{text}

Error message

未识别到全部恢复时间:{text}

What it means

ExtractFullRecoveryTime 先判断 '已完全恢复'(返回零),再用 '全部恢复H:MM:SS' 正则,最后兜底用任意 H:MM:SS 正则。三者都失败时说明弹窗 OCR 文本里没有任何可识别的恢复时间,抛 FormatException。

Source

Thrown at BetterGenshinImpact/GameTask/AutoBoss/AutoBossTask.cs:411

    }

    private static TimeSpan ExtractFullRecoveryTime(string text)
    {
        if (text.Contains("原粹树脂已完全恢复", StringComparison.Ordinal))
        {
            return TimeSpan.Zero;
        }

        var fullRecoveryMatch = Regex.Match(text, @"全部恢复(?<time>\d{1,3}:\d{2}:\d{2})");
        if (fullRecoveryMatch.Success)
        {
            return ParseRecoveryTime(fullRecoveryMatch.Groups["time"].Value);
        }

        var timeMatches = Regex.Matches(text, @"\d{1,3}:\d{2}:\d{2}");
        if (timeMatches.Count == 0)
        {
            throw new FormatException($"未识别到全部恢复时间:{text}");
        }

        return ParseRecoveryTime(timeMatches[timeMatches.Count - 1].Value);
    }

    private static TimeSpan ParseRecoveryTime(string timeText)
    {
        var parts = timeText.Split(':');
        if (parts.Length != 3
            || !int.TryParse(parts[0], NumberStyles.None, CultureInfo.InvariantCulture, out var hours)
            || !int.TryParse(parts[1], NumberStyles.None, CultureInfo.InvariantCulture, out var minutes)
            || !int.TryParse(parts[2], NumberStyles.None, CultureInfo.InvariantCulture, out var seconds)
            || minutes < 0
            || minutes > 59
            || seconds < 0
            || seconds > 59)
        {
            throw new FormatException($"树脂恢复时间格式无效:{timeText}");

View on GitHub (pinned to a7cb36712d)

Solutions

  1. 加大点击后等待并轮询弹窗出现再截图。
  2. 记录 normalizedText 原文(已有 debug 日志)确认 OCR 内容。
  3. 扩展正则容忍:允许半角/全角冒号、允许 'HH:MM' 缺秒、允许中文 '小时/分/秒' 描述。
  4. 校准 detailRect 偏移(resinIconLeft-13, resinIconBottom+29)是否随分辨率正确。

Example fix

// before
var timeMatches = Regex.Matches(text, @"\d{1,3}:\d{2}:\d{2}");
if (timeMatches.Count == 0)
{
    throw new FormatException($"未识别到全部恢复时间:{text}");
}

// after
var timeMatches = Regex.Matches(text, @"\d{1,3}[::]\d{2}[::]\d{2}");
if (timeMatches.Count == 0)
{
    throw new FormatException($"未识别到全部恢复时间:{text}");
}
Defensive patterns

Strategy: retry

Validate before calling

// RecognizeFullRecoveryTime 调用前确认 detailRegion 非空且 OCR 有文本
using var detailRegion = clickedCapture.DeriveCrop(detailRect);
var result = OcrFactory.Paddle.OcrResult(detailRegion.SrcMat);
if (result.Regions.Count == 0)
{
    throw new FormatException("树脂详情 OCR 区域无文本,弹窗可能未打开");
}

Try / catch

try { return ExtractFullRecoveryTime(normalizedText); }
catch (FormatException) { _logger.LogDebug("{Name}:恢复时间文本=[{Text}]", Name, normalizedText); throw; }

Prevention

When it happens

Trigger: 树脂详情弹窗未打开或内容被遮挡;OCR 文本里冒号是全角 ':' 未被 NormalizeRecoveryText 处理(注意代码已 Replace(':',':'),但若为其他分隔符则失败);时间格式被 OCR 拆断(如 '12:30:00' 被识别为 '12:30 OO')。

Common situations: 点击图标后 Delay(500) 不足,弹窗动画未完成;detailRect(220×150 固定像素)漂移;游戏语言/字体变化导致 '全部恢复' 文案变化;PaddleOCR 在小字号下漏识冒号。

Related errors


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