列舉與儲存格型別
基礎外掛負責向 Core 註冊你的列舉與自訂儲存格型別。本頁依序說明基礎外掛、自訂儲存格型別與包裝型別。
註冊列舉與剖析器
實作 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,匯入 Plugin Demo 套件就會還原這個檔案。正式版本另外檢查 op 的寫法,並提供最接近名稱的建議。
剖析器的規則
TypeName是@type儲存格的文字。欄要宣告為Modifier或List<Modifier>。ValueType是程式碼產生輸出的 CLR 值型別。範例裡是[Serializable]struct。- 剖析失敗時,把結構化錯誤加進
context.Errors,再回傳false。不要擲出例外。 - 範例用
InvariantCulture剖析三個部分,並拒絕 NaN 與 Infinity。 TryRender回傳標準的儲存格字串,做的事正好是TryParse的反向。- 浮點數用
CanonicalValueRenderer.RenderFloat輸出。這樣在 Mono 上往返也不會走樣。
指定目標的自訂型別
自訂型別上的 @target 只要註冊就能用。把欄宣告為 Modifier@Stats,剖析器讀取 context.Type.TargetName 就會得到 "Stats"。
這個目標的完整性檢查交給領域驗證器。分頁是否存在、id 能否解析,由驗證器回答。這和 RecordId@Tab 的分工方式相同。
帶 @ 的型別名稱如果沒有註冊,仍然是錯誤,並會提供建議。打錯字一樣抓得到。
保留的型別名稱
Core 會在所有外掛之前註冊內建的型別名稱,包括 int、float、bool、string、Enum、RecordId、AssetRef、ResourceRef、DirectRef、IntId、LocRef、Color、AnimationCurve 與 Gradient。
如果剖析器沿用了其中任何一個名稱,註冊會以 PluginRegistrationConflict 失敗,結果如下:
- 內建型別保持不變。
- 那次
RegisterCellParsers呼叫在發生衝突的剖析器停下。 - 外掛的其他插槽照常載入。
如果外掛自己帶了 Color 或 Gradient 型別,必須重新命名。詳見變更記錄裡的升級說明。
色彩、曲線與漸層值
如果型別要存放色彩、曲線或漸層,不必自己重寫一套寫法。Core 已經備好下列部分。
| 部分 | 型別 | 提供的功能 |
|---|---|---|
| 值模型 | 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 由 Core 遞迴剖析。所以 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 拆出的每一段,Core 都當成內部型別剖析。
分隔符號不要用 ;。用了 ;,List<Pair<T>> 欄就會和清單的分隔符號衝突。
一次註冊提供的功能
只要註冊一次,就能得到下列功能:
- 遞迴解析
@type。 - 強型別的程式碼產生,例如
Pair<RecordRef> First;。 - 烘焙。
- 匯出與推送的往返。
- 包裝型別裡
RecordId@Tab的參照穿透。
參照穿透包含三件事。內部的參照會經過完整性檢查,鍵值重新命名時連動更新,分頁重新命名時也會改寫。
包裝型別的拒絕規則列出會遭到拒絕的寫法,以及 ; 分隔符號的注意事項。