babalae/better-genshin-impact · error · ArgumentOutOfRangeException

不支持的 OCR 引擎类型

Error message

不支持的 OCR 引擎类型

What it means

OcrFactory.Create switches on OcrEngineTypes; currently only Paddle is implemented, and the default arm throws ArgumentOutOfRangeException with the offending enum name as the param name. This guards against an unknown/unset engine type reaching the factory.

Source

Thrown at BetterGenshinImpact/Core/Recognition/OCR/OcrFactory.cs:43

    /// <summary>
    ///  OCR 工厂,不可以直接实例化,请使用 App.ServiceProvider获取实例
    /// </summary>
    /// <param name="logger"></param>
    public OcrFactory(ILogger<BgiOnnxFactory> logger)
    {
        _logger = logger;
        _config = GetConfig();
    }

    /// <summary>
    /// 创建
    /// </summary>
    private IOcrService Create(OcrEngineTypes type)
    {
        var result = type switch
        {
            OcrEngineTypes.Paddle => CreatePaddleOcrInstance(),
            _ => throw new ArgumentOutOfRangeException(Enum.GetName(type), type, "不支持的 OCR 引擎类型")
        };
        _logger.LogDebug("创建了类型为 {Type} 的 OCR服务", Enum.GetName(type));
        return result;
    }

    /// <summary>
    /// 获取 OCR 配置
    /// 为了单元测试
    /// </summary>
    /// <returns></returns>
    private OtherConfig.Ocr GetConfig()
    {
        try
        {
            // 直接使用配置
            return TaskContext.Instance().Config.OtherConfig.OcrConfig;
        }
        catch (Exception e)

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Set the OCR engine config to the supported value (Paddle).
  2. Validate the configured OcrEngineTypes at startup and reset to Paddle if invalid.
  3. When adding an engine type, implement its case here in the same commit.

Example fix

// before
var ocr = factory.Create(config.Engine); // unknown engine

// after
var engine = Enum.IsDefined(config.Engine) ? config.Engine : OcrEngineTypes.Paddle;
var ocr = factory.Create(engine);
Defensive patterns

Strategy: validation

Validate before calling

var engine = Enum.IsDefined(config.Engine) && config.Engine == OcrEngineTypes.Paddle
    ? config.Engine
    : OcrEngineTypes.Paddle;
var ocr = factory.Create(engine);

Type guard

static bool IsSupportedEngine(OcrEngineTypes e) => e == OcrEngineTypes.Paddle;

Prevention

When it happens

Trigger: Casting an integer to OcrEngineTypes that isn't Paddle; a config value set to an engine type not yet supported; default(OcrEngineTypes) when the enum's 0 member is not a valid engine.

Common situations: Future enum members added without a factory case; config file referencing a removed/renamed engine; serialized state from an older version.

Related errors


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