Skip to content
SheetForge

Enums and cell types

A base plugin registers your enums and your own cell types with the Core. This page covers the base plugin, a custom cell type, and a wrapper type.

Register enums and parsers

Implement ISheetForgePlugin. It names the plugin and registers its enums and cell parsers.

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());
    }
}
MemberWhat it does
NameIdentifies the plugin in diagnostics and duplicate-conflict reports
RegisterEnumsMakes enum cells resolve to your CLR enums
RegisterCellParsersAdds your custom cell types

After enums.Register<ActionType>(), any Enum<ActionType> cell in a sheet resolves. Codegen emits the real CLR enum type on the generated field.

A registered cell type joins parsing, validation, codegen, bake and round trip by registration alone. You edit nothing in the pipeline.

Add a custom cell type

A custom cell type turns the text of a cell into a value of your own type. It takes up to two interfaces.

InterfaceConvertsNeeded for
ICellValueParserString to valueEvery custom cell type
ICustomCellTypeCLR type, and value to canonical stringStrong-typed baking and the Export/Push round trip

The sample implements the Modifier mini-grammar. A cell holds stat:op:value, for example 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;
    }
}

The complete production version is in Assets/SheetForge.PluginDemo/ModifierCellParser.cs, which the Plugin demo package restores. It adds op-token validation and nearest-match suggestions.

Parser rules

  • TypeName is the text of the @type cell. A column declares Modifier or List<Modifier>.
  • ValueType is the CLR value type that codegen emits. In the sample it is a [Serializable] struct.
  • On failure, add a structured error to context.Errors and return false. Never throw.
  • The sample parses the three parts with InvariantCulture and rejects NaN and Infinity.
  • TryRender returns the canonical cell string. It is the exact inverse of TryParse.
  • Render floats with CanonicalValueRenderer.RenderFloat. It is round-trip-safe on Mono.

Custom types with a target

@target on a custom type works by registration alone. Declare a column as Modifier@Stats, and your parser reads context.Type.TargetName, which is "Stats".

Integrity checking of that target belongs to a domain validator. The validator answers whether the tab exists and whether the id resolves. That is the same division of labor as RecordId@Tab.

An unregistered type name with @ is still an error with a suggestion. Typo safety is preserved.

Reserved type names

The Core registers the built-in type names before any plugin. The names are int, float, bool, string, Enum, RecordId, AssetRef, ResourceRef, DirectRef, IntId, LocRef, Color, AnimationCurve and Gradient.

A parser that reuses one of them fails registration with PluginRegistrationConflict:

  • The built-in stays.
  • That RegisterCellParsers call stops at the conflicting parser.
  • The plugin's other slots still load.

A plugin that shipped its own Color or Gradient type must rename it. See the upgrade notes in the changelog.

Color, curve and gradient values

If your type stores a color, a curve or a gradient, you do not have to re-implement the notation. The Core provides the pieces.

PieceTypesWhat you get
Value modelsColorValue, CurveValue, GradientValueTryParse(text, out value, out error) and Render()
EvaluatorsCurveEvaluator, GradientEvaluatorSampling exactly as Unity does
Editor hintStudioCellEditorHintThe native editor for your type, in both hosts

Give the StudioCellEditorHint the ColorPicker, CurveEditor or GradientEditor archetype. Cell-editor archetypes describes the hint.

Add a wrapper type

A wrapper is a generic value shape, written MyWrapper<T>, that packs several inner T values into one cell. For example, a Pair<int> cell holds 1~2.

You own only the outer syntax: the delimiter and the arity. The Core parses the inner T recursively. So Pair<RecordId@Effects>, Pair<Enum<DamageType>> and nested Box<Pair<int>> parse and validate with no additional code. References inside are fully validated.

Implement ICellWrapperType. Register it in the same RegisterCellParsers hook through 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());

Wrapper members

MemberRole
NameThe @type token, as in Pair<Inner>
OpenClrTypeThe generic open type, with exactly one type parameter
TrySplitSplits the cell into pieces. Outer syntax only
JoinCanonicalJoins the pieces back. The inverse of TrySplit
AssembleBuilds the closed type, such as Pair<TInner>, at bake
TryDisassembleReads the values back out at Export. The inverse of Assemble

The Core parses each piece from TrySplit as the inner type.

Use a delimiter other than ;. With ;, a List<Pair<T>> column clashes with the list separator.

What one registration provides

That single registration gets you:

  • Recursive @type resolution.
  • Strong-typed codegen, such as Pair<RecordRef> First;.
  • Baking.
  • The Export/Push round trip.
  • Reference pass-through for a RecordId@Tab inside the wrapper.

Reference pass-through means three things. The inner reference is integrity-checked, propagated on key rename, and rewritten on tab rename.

Wrapper rejection rules lists the notations that are rejected and the ;-delimiter caveat.

Next steps