枚举与单元格类型
基础插件负责把项目自己的枚举和单元格类型注册到内核。本页依次介绍基础插件、自定义单元格类型和包装类型。
注册枚举和解析器
实现 ISheetForgePlugin。这个接口给插件命名,并注册插件的枚举和单元格解析器。
using SheetForge.Core.Model;
using SheetForge.Core.Plugins;
public sealed class SkillsPlugin : ISheetForgePlugin
{
public string Name => "Skills"; // for diagnostics / duplicate-conflict reports
public void RegisterEnums(EnumRegistry enums)
{
// Any Enum<ActionType> / Enum<EffectType> cell in a sheet now resolves,
// and codegen emits the real CLR enum type on the generated field.
enums.Register<ActionType>();
enums.Register<EffectType>();
}
public void RegisterCellParsers(CellParserRegistry parsers)
{
// A custom cell type joins parsing, validation, codegen, bake and
// round-trip by registration alone (open-closed — zero pipeline edits).
parsers.Register(new ModifierCellParser());
}
}| 成员 | 作用 |
|---|---|
Name | 在诊断信息和重复冲突报告中标明是哪个插件 |
RegisterEnums | 让枚举单元格解析为插件里的 CLR 枚举 |
RegisterCellParsers | 添加自定义单元格类型 |
调用 enums.Register<ActionType>() 之后,工作表里所有 Enum<ActionType> 单元格都能解析。代码生成会在生成的字段上使用真正的 CLR 枚举类型。
单元格类型注册之后,就会自动加入解析、验证、代码生成、烘焙和往返。管线本身一行都不用改。
添加自定义单元格类型
自定义单元格类型负责把单元格里的文本转成自定义类型的值,最多涉及两个接口。
| 接口 | 转换内容 | 何时需要 |
|---|---|---|
ICellValueParser | 从字符串到值 | 所有自定义单元格类型 |
ICustomCellType | CLR 类型,以及从值到规范字符串 | 强类型烘焙,以及导出和推送的往返 |
示例实现的是 Modifier 迷你语法。单元格内容的格式为 stat:op:value,例如 attack:add:10。
using System;
using SheetForge.Core.Model;
using SheetForge.Core.Unparse;
public sealed class ModifierCellParser : ICellValueParser, ICustomCellType
{
// The @type cell text: a column declares "Modifier" or "List<Modifier>".
public string TypeName => "Modifier";
// ICustomCellType: the CLR value type codegen emits ([Serializable] struct).
public Type ValueType => typeof(Modifier);
public bool TryParse(CellParseContext context, string text, out object value)
{
value = null;
string[] parts = text.Split(':');
if (parts.Length != 3)
{
// Failure = collect a structured error and return false. Never throw.
context.Errors.Add(new ImportError(
ImportErrorCode.CustomTypeParseFailed, context.Coordinate,
text, "'stat:op:value' form (e.g. attack:add:10)", null));
return false;
}
// ... parse the three parts (InvariantCulture; reject NaN/Infinity) ...
value = new Modifier(parts[0].Trim(), /*op*/ default, /*value*/ 0f);
return true;
}
// ICustomCellType: value → canonical cell string (the exact inverse of TryParse).
public bool TryRender(object value, out string text, out string reason)
{
reason = null;
if (!(value is Modifier m)) { text = null; reason = "Not a Modifier."; return false; }
// Use CanonicalValueRenderer.RenderFloat for floats — round-trip-safe on Mono.
text = m.stat + ":" + "add" + ":" + CanonicalValueRenderer.RenderFloat(m.value);
return true;
}
}完整的正式版本是 Assets/SheetForge.PluginDemo/ModifierCellParser.cs,导入插件示例的包后会还原这个文件。正式版本还加入了 op 部分的验证和最接近的匹配建议。
解析器规则
TypeName就是@type单元格里的文本。列的类型写作Modifier或List<Modifier>。ValueType是代码生成输出的 CLR 值类型。示例里用的是[Serializable]结构体。- 解析失败时,向
context.Errors添加结构化错误,然后返回false。不要抛出异常。 - 示例用
InvariantCulture解析三个部分,并拒绝 NaN 和 Infinity。 TryRender返回规范的单元格字符串,与TryParse严格互逆。- 浮点数用
CanonicalValueRenderer.RenderFloat输出。这样在 Mono 上往返也不会走样。
带目标的自定义类型
自定义类型上的 @target 只靠注册就能用。把列声明为 Modifier@Stats,解析器读取 context.Type.TargetName 就会得到 "Stats"。
目标是否有效,由领域验证器负责检查。验证器判断标签页是否存在,以及 id 能否解析。RecordId@Tab 也是这样分工的。
带 @ 但没有注册的类型名仍会报错,并附带建议。拼错的类型名照样拦得住。
保留的类型名
内核会先于所有插件注册内置类型名,包括 int、float、bool、string、Enum、RecordId、AssetRef、ResourceRef、DirectRef、IntId、LocRef、Color、AnimationCurve 和 Gradient。
如果解析器重用了其中某个名称,注册会失败,并报告 PluginRegistrationConflict:
- 内置类型保持不变。
- 这次
RegisterCellParsers调用停在发生冲突的解析器上。 - 插件的其他槽位照常加载。
插件如果自带名为 Color 或 Gradient 的类型,必须重命名。详见更新日志里的升级说明。
颜色、曲线和渐变值
自定义类型如果要保存颜色、曲线或渐变,不必重新实现这些写法。内核已经提供了现成的部件。
| 部件 | 类型 | 提供的功能 |
|---|---|---|
| 值模型 | ColorValue、CurveValue、GradientValue | TryParse(text, out value, out error) 和 Render() |
| 求值器 | CurveEvaluator、GradientEvaluator | 与 Unity 完全一致的采样 |
| 编辑器提示 | StudioCellEditorHint | 在两个宿主中为这个类型打开原生编辑器 |
给 StudioCellEditorHint 指定 ColorPicker、CurveEditor 或 GradientEditor 原型即可。这个提示的用法见单元格编辑器原型。
添加包装类型
包装类型是写作 MyWrapper<T> 的泛型值形态,可以把多个内部的 T 值装进一个单元格。例如 Pair<int> 单元格的内容是 1~2。
插件只负责外层语法,也就是分隔符和元数。内部的 T 由内核递归解析。所以 Pair<RecordId@Effects>、Pair<Enum<DamageType>> 以及嵌套的 Box<Pair<int>> 不用写额外代码就能解析和验证,里面的引用也会得到完整验证。
实现 ICellWrapperType,并在同一个 RegisterCellParsers 钩子里通过 parsers.RegisterWrapper(...) 注册。
// A [Serializable] generic value type — codegen emits Pair<int>, Pair<RecordRef>, ...
[Serializable] public struct Pair<T> { public T First; public T Second; public Pair(T a, T b){First=a;Second=b;} }
public sealed class PairWrapper : ICellWrapperType
{
public string Name => "Pair"; // the @type token: Pair<Inner>
public Type OpenClrType => typeof(Pair<>); // generic open type — exactly one type parameter
// Outer syntax only: split "1~2" into ["1","2"]. Use a delimiter OTHER than ';'
// so List<Pair<T>> doesn't clash with the list separator.
public bool TrySplit(string cell, out IReadOnlyList<string> pieces, out string reason)
{
reason = null;
var parts = (cell ?? "").Split('~');
if (parts.Length != 2) { pieces = null; reason = "'a~b' form (two parts)."; return false; }
pieces = new[] { parts[0], parts[1] };
return true; // the Core parses each piece as the inner type
}
public string JoinCanonical(IReadOnlyList<string> inner) => inner[0] + "~" + inner[1]; // inverse of TrySplit
public object Assemble(IReadOnlyList<object> inner, Type closed) =>
Activator.CreateInstance(closed, inner[0], inner[1]); // bake: build Pair<TInner>
public bool TryDisassemble(object v, out IReadOnlyList<object> inner, out string reason)
{
reason = null;
var t = v.GetType();
inner = new[] { t.GetField("First").GetValue(v), t.GetField("Second").GetValue(v) };
return true; // Export: read the values back out (inverse of Assemble)
}
}
// In your ISheetForgePlugin.RegisterCellParsers:
public void RegisterCellParsers(CellParserRegistry parsers) => parsers.RegisterWrapper(new PairWrapper());包装类型的成员
| 成员 | 职责 |
|---|---|
Name | @type 里写的名称,如 Pair<Inner> |
OpenClrType | 开放泛型类型,只能有一个类型参数 |
TrySplit | 把单元格拆成若干片段,只处理外层语法 |
JoinCanonical | 把片段重新拼起来,是 TrySplit 的逆操作 |
Assemble | 烘焙时构造封闭类型,如 Pair<TInner> |
TryDisassemble | 导出时把值重新读出来,是 Assemble 的逆操作 |
内核会把 TrySplit 拆出的每个片段当作内部类型来解析。
分隔符不要用 ;。用了 ;,List<Pair<T>> 列就会和列表分隔符冲突。
一次注册带来的功能
只需这一次注册,就能得到:
- 递归的
@type解析。 - 强类型代码生成,例如
Pair<RecordRef> First;。 - 烘焙。
- 导出和推送的往返。
- 包装类型内部
RecordId@Tab的引用透传。
引用透传包含三点。内部的引用会做完整性检查,键重命名时随之传播,标签页重命名时也会相应改写。
包装类型的拒绝规则列出了不接受的写法,以及 ; 分隔符的注意事项。