babalae/better-genshin-impact · critical · Exception

分层地图数据文件夹中中存在无法解析的文件名: {fileName}

Error message

分层地图数据文件夹中中存在无法解析的文件名: {fileName}

What it means

Thrown while grouping map-feature files in BaseMapLayer.LoadLayers when a filename in the map data directory, after stripping extension and splitting on '_', yields fewer than 3 parts. The expected naming convention is '<MapType>_<floor>_<size>_<algo>', e.g. 'Teyvat_0_2048_SIFT'. Fewer than 3 segments means the file does not follow the contract and cannot be assigned a floor.

Source

Thrown at BetterGenshinImpact/GameTask/Common/Map/Maps/Base/BaseMapLayer.cs:78

        var layers = new List<BaseMapLayer>();
        var layerDir = Path.Combine(Global.Absolute(@"Assets\Map\"), baseMap.Type.ToString());
        if (!Directory.Exists(layerDir))
        {
            return layers;
        }

        var files = Directory.GetFiles(layerDir);
        var validFiles = files.Where(f => (f.EndsWith(".kp.bin") || f.EndsWith(".mat.png"))
                                          && !f.EndsWith("Teyvat_0_256_SIFT.kp.bin")
                                          && !f.EndsWith("Teyvat_0_256_SIFT.mat.png"));
        // 解析后按 floor 分组,然后按 floor 创建BaseMapLayer
        var groupedFiles = validFiles.GroupBy(file =>
        {
            var fileName = Path.GetFileNameWithoutExtension(file);
            var parts = fileName.Split('_');
            if (parts.Length < 3)
            {
                throw new Exception($"分层地图数据文件夹中中存在无法解析的文件名: {fileName}");
            }

            return int.TryParse(parts[1], out var floor) ? floor : throw new Exception($"分层地图数据文件夹中中存在无法解析的文件名: {fileName}");
        });

        foreach (var group in groupedFiles)
        {
            var floor = group.Key;
            var layer = new BaseMapLayer(baseMap) { Floor = floor };

            // 查找特征文件路径
            var kpFilePath = group.First(f => f.EndsWith(".kp.bin"));
            var matFilePath = group.First(f => f.EndsWith(".mat.png"));

            SpeedTimer speedTimer = new($"加载 {Path.GetFileNameWithoutExtension(kpFilePath)} 地图特征");
            // 加载特征数据
            layer.TrainKeyPoints = FeatureStorageHelper.LoadKeyPointArray(kpFilePath) ?? throw new Exception($"地图数据加载失败,文件: {kpFilePath}");
            speedTimer.Record("特征点");

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Inspect the contents of Assets\Map\<MapType> for any *.kp.bin / *.mat.png file whose name has fewer than 3 underscore-separated segments and remove or rename it.
  2. Ensure all feature files follow the '<Type>_<floor>_<size>_<algo>' naming convention.
  3. If the directory may legitimately contain non-conforming files, pre-filter validFiles with a regex like ^[^_]+_\d+_[^_]+_[^_]+$ before grouping.
  4. Re-run the map-data generation/export tool to regenerate a clean dataset.

Example fix

// before
var validFiles = files.Where(f => (f.EndsWith(".kp.bin") || f.EndsWith(".mat.png"))
                                  && !f.EndsWith("Teyvat_0_256_SIFT.kp.bin")
                                  && !f.EndsWith("Teyvat_0_256_SIFT.mat.png"));

// after — also enforce the naming convention so stray files are skipped, not fatal
var namePattern = new System.Text.RegularExpressions.Regex(@"^[^_]+_(\-?\d+)_\d+_[^_]+$");
var validFiles = files.Where(f => (f.EndsWith(".kp.bin") || f.EndsWith(".mat.png"))
                                  && !f.EndsWith("Teyvat_0_256_SIFT.kp.bin")
                                  && !f.EndsWith("Teyvat_0_256_SIFT.mat.png")
                                  && namePattern.IsMatch(Path.GetFileNameWithoutExtension(f)));
Defensive patterns

Strategy: validation

Validate before calling

// Pre-validate filenames before grouping
var namePattern = new System.Text.RegularExpressions.Regex(@"^[^_]+_\-?\d+_\d+_[^_]+$");
var badFiles = files.Where(f => (f.EndsWith(".kp.bin") || f.EndsWith(".mat.png"))
    && !namePattern.IsMatch(Path.GetFileNameWithoutExtension(f))).ToList();
if (badFiles.Count > 0) throw new Exception($"存在不符合命名规范的地图文件: {string.Join(", ", badFiles)}");

Try / catch

try { var layers = BaseMapLayer.LoadLayers(baseMap); }
catch (Exception ex) when (ex.Message.Contains("无法解析的文件名"))
{
    Logger.LogError("地图特征文件命名不规范,请检查 Assets\\Map 目录: {Msg}", ex.Message);
    throw;
}

Prevention

When it happens

Trigger: LoadLayers enumerates Directory.GetFiles(layerDir), filters to *.kp.bin / *.mat.png (excluding the special 256 SIFT files), then GroupBy parses each name. Any stray file matching the extension filter but not the naming convention — e.g. a backup 'Teyvat.kp.bin', a README renamed, a temp download, or a hand-placed file — triggers this on the first GroupBy evaluation (deferred, so it fires when groupedFiles is first enumerated in the foreach).

Common situations: User or a tool dropped an unexpectedly named *.kp.bin or *.mat.png into Assets\Map\<Type>; a partial/corrupted download left a truncated filename; the map-generation pipeline changed its naming and old files remain; a backup file like 'Teyvat_0_2048_SIFT.kp.bin.bak' was renamed to '.kp.bin'.

Related errors


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