babalae/better-genshin-impact · error · FileNotFoundException

未找到{taskName}中的{assetName}文件

Error message

未找到{taskName}中的{assetName}文件

What it means

LoadTemplateImage found the task's Assets folder but the specific template image (assetName) referenced by Recognition.json is not present. FileNotFoundException identifies exactly which file is missing so the template match for that object cannot run.

Source

Thrown at BetterGenshinImpact/Core/Recognition/RecognitionAssets.cs:92

    }

    private static Mat LoadTemplateImage(string taskName, string assetName, int captureWidth, int captureHeight, ImreadModes flags)
    {
        var assetsFolder = Global.Absolute($@"GameTask\{taskName}\Assets\{captureWidth}x{captureHeight}");
        if (!Directory.Exists(assetsFolder))
        {
            assetsFolder = Global.Absolute($@"GameTask\{taskName}\Assets\1920x1080");
        }

        if (!Directory.Exists(assetsFolder))
        {
            throw new FileNotFoundException($"未找到{taskName}的素材文件夹");
        }

        var filePath = Path.Combine(assetsFolder, assetName);
        if (!File.Exists(filePath))
        {
            throw new FileNotFoundException($"未找到{taskName}中的{assetName}文件");
        }

        using var stream = File.OpenRead(filePath);
        var mat = Mat.FromStream(stream, flags);
        if (captureWidth < 1920)
        {
            using (mat)
            {
                return ResizeHelper.Resize(mat, captureWidth / 1920d);
            }
        }

        return mat;
    }

    private readonly record struct RecognitionAssetCacheKey(string TaskName, string ObjectName, int CaptureWidth, int CaptureHeight);
}

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Add the missing {assetName} file under GameTask\{taskName}\Assets\1920x1080\ (and the specific resolution folder if used).
  2. Fix the template filename in Recognition.json so it matches the on-disk name exactly, including case.
  3. Verify the file is included in the project and copied to the output directory.
  4. Clear the RecognitionAssets cache (RecognitionAssets.ClearTask) after correcting the file.

Example fix

// before: Recognition.json references "btn_start.png" that is not on disk -> throws
var ro = RecognitionAssets.Get(taskName, objectName);

// after: verify the template path the loader will use
var file = Path.Combine(Global.Absolute($@"GameTask\{taskName}\Assets\1920x1080"), "btn_start.png");
if (!File.Exists(file)) throw new InvalidOperationException($"Template missing: {file}");
var ro = RecognitionAssets.Get(taskName, objectName);
Defensive patterns

Strategy: validation

Validate before calling

bool TemplateExists(string taskName, string assetName)
{
    var folder = Global.Absolute($@"GameTask\{taskName}\Assets\1920x1080");
    return File.Exists(Path.Combine(folder, assetName));
}

if (!TemplateExists(taskName, assetName))
    throw new InvalidOperationException($"Template {assetName} missing for task {taskName}");

Try / catch

try { var ro = RecognitionAssets.Get(taskName, objectName); }
catch (FileNotFoundException ex) when (ex.Message.Contains("中的") && ex.Message.Contains("文件"))
{ Logger.LogError(ex, "Template file missing."); throw; }

Prevention

When it happens

Trigger: A RecognitionObject of type TemplateMatch references a template whose png exists in JSON but is absent on disk under the resolved Assets folder.

Common situations: Template png deleted but still referenced; filename typo or casing mismatch between JSON and disk; image added to source control but not committed/deployed; wrong resolution folder searched.

Related errors


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