babalae/better-genshin-impact · error · Exception

未知的定义字段:{stage}

Error message

未知的定义字段:{stage}

What it means

Thrown by ScriptParser.Parse when a script line is encountered under a stage header (a line ending with ':') that is neither '角色定义:' nor '策略定义:'. The parser uses a simple state machine: lines containing ':' set the current stage, and all subsequent non-empty, non-comment lines are dispatched based on that stage string. An unrecognized stage means the script contains a typo in a header, an unsupported section, or a stray colon in a data line that was misinterpreted as a header.

Source

Thrown at BetterGenshinImpact/GameTask/AutoGeniusInvokation/ScriptParser.cs:110

                        {
                            int delta = int.Parse(RegexHelper.ExcludeNumberRegex().Replace(actionParts[3], ""));
                            actionCommand.DiceDelta = delta;
                        }
                        else if (actionParts[3].StartsWith("骰子减少"))
                        {
                            int delta = int.Parse(RegexHelper.ExcludeNumberRegex().Replace(actionParts[3], ""));
                            actionCommand.DiceDelta = -delta;
                        }
                        else
                        {
                            MyAssert(false, $"策略中的行动命令解析错误:骰子增减参数格式不正确(应为 骰子增加N 或 骰子减少N ),实际:{actionParts[3]}");
                        }
                    }
                    duel.ActionCommandQueue.Add(actionCommand);
                }
                else
                {
                    throw new System.Exception($"未知的定义字段:{stage}");
                }
            }

            MyAssert(duel.Characters[3] != null, "角色未定义,请确认策略文本格式是否为UTF-8");
        }
        catch (System.Exception ex)
        {
            MyLogger.LogError($"解析脚本错误,行号:{i + 1},错误信息:{ex}");
            ThemedMessageBox.Error($"解析脚本错误,行号:{i + 1},错误信息:{ex}", "策略解析失败");
            return default!;
        }

        return duel;
    }

    /// <summary>
    /// 解析示例
    /// 角色1=刻晴|雷{技能3消耗=1雷骰子+2任意,技能2消耗=3雷骰子,技能1消耗=4雷骰子}

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Open the strategy script and verify every header line is exactly '角色定义:' or '策略定义:' with no extra spaces or characters.
  2. Check that no data lines (character or action definitions) contain a ':' character, which would be misinterpreted as a stage header.
  3. Ensure the file is saved as UTF-8 without BOM.

Example fix

// before — script with bad header
角色定义 :
角色1=刻晴|雷{...}

// after
角色定义:
角色1=刻晴|雷{...}
Defensive patterns

Strategy: validation

Validate before calling

// Before parsing, validate that all stage headers are recognized
var validStages = new HashSet<string> { "角色定义:", "策略定义:" };
foreach (var line in scriptLines)
{
    var trimmed = line.Trim();
    if (trimmed.Contains(':') && !trimmed.StartsWith("//"))
    {
        var stageHeader = trimmed.Contains(':') ? trimmed : null;
        // If a line looks like a header but is not recognized, warn early
    }
}

Type guard

public static bool IsValidStageHeader(string line)
{
    return line == "角色定义:" || line == "策略定义:";
}

Try / catch

catch (System.Exception ex) when (ex.Message.Contains("未知的定义字段"))
{
    MyLogger.LogError($"脚本包含未知的段落标题,请检查格式。错误: {ex.Message}");
    ThemedMessageBox.Error($"策略脚本格式错误:{ex.Message}", "解析失败");
    return default!;
}

Prevention

When it happens

Trigger: A user-authored TCG strategy script contains a header like '角色定义 :' (extra space), '角色definition:' (wrong language), '战斗策略:' (unsupported section), or a data line that contains a ':' making the parser treat it as a new stage. After the stage is set, the next data line hits the else branch and throws.

Common situations: Strategy script copied from a wiki with formatting artifacts, manual edits introducing typos in section headers, BOM or encoding issues causing the header string to not match exactly, or a script written for a newer/older parser version with different supported sections.

Related errors


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