Gum codegen

From vchelaru/FlatRedBall · View interactive version

Gum codegen is an AI agent skill from the GitHub repository vchelaru/FlatRedBall. Use when working on Glue's Gum code generation — adding/removing/version-gating generated properties on Gum standard runtime types (NineSlice, Text, Container, Sprite, etc.), or debugging "why is/isn't this Gum variable being generated.

How to use this skill

Skill file

Gum Code Generation in Glue

Glue does not rely on Gum's own runtime code generation for the standard element runtimes. Instead, the Gum plugin regenerates these classes itself from the Gum project data. The upside is tight control over what gets emitted; the downside is that whenever Gum adds, removes, or renames a variable on a standard type, Glue's generator has to be updated to match — otherwise the new variable is silently emitted (causing build/runtime errors on older projects) or silently omitted (causing missing properties on newer projects).

This skill is about the omission side: how Glue decides which Gum variables to skip when generating a standard runtime, and how to gate that decision on a GluxVersions value. For the version-numbering rules themselves, see the gluj-versions skill.

There are THREE independent decisions, not one

Generating a standard runtime like NineSliceRuntime.Generated.cs involves three independent code paths. All three must agree on whether a member exists in a given version, and each disagreement has its own compiler error:

  1. Property generation — emits the public T Foo { get; set; } (or backing-field) property on the runtime class. Driven by StandardsCodeGenerator and per-type generators (NineSliceCodeGenerator, etc.).
  2. State generation — emits the case VariableState.Default: Foo = …; assignments inside the state-switch. Driven by StateCodeGenerator.
  3. Inheritance — emits the : global::Gum.Wireframe.IFooRuntime on the class declaration, via each per-type generator's AddAdditionalInheritance (wired up in StandardsCodeGenerator.GenerateStandardElementSaveCodeFor).

Property vs. state disagreement is CS0103: The name 'IsTilingMiddleSections' does not exist in the current context — a state-init assignment for a property nobody generated. Inheritance vs. property disagreement is CS0535: does not implement interface member (issue #1979).

Pipelines 1 and 2 are driven by the project's variables; pipeline 3 is driven by GluxVersions alone, so it is the one that most easily drifts. The Gum.Wireframe.I*Runtime interfaces it names are hand-written in the sibling Gum repo under #if FRB (Gum/Wireframe/CustomSetPropertyOnRenderable.cs) and mirror what Glue generates; they grow as Gum routes more of its property dispatch through the runtime instead of the renderable. Glue holds no reference to GumCore, so nothing in Glue can read one — NineSliceCodeGenerator.InterfaceMemberNames restates the contract by hand, and GumGeneratedCodeCompilesTests is what detects drift. ContainerCodeGenerator/PolygonCodeGenerator avoid the problem differently, by emitting their interface members from GenerateAdditionalMethods rather than relying on the project's variables at all.

When you add a version gate, update every pipeline that touches the member. Searching for an existing skipped variable name (e.g. "IgnoredByParentSize", "StackSpacing") is the fastest way to spot every place that needs a parallel change — they typically appear in FrbBackedGumVariables plus a gate in StandardsCodeGenerator.RefreshVariableNamesToSkipForProperties or StateCodeGenerator.RefreshVariableNamesToSkipBasedOnGlueVersion.

Glue never back-fills a loaded project's standard elements (landmine)

Codegen emits from the variables in the project's own .gutx, and Glue never reconciles that against Gum's canonical schema. Gum's tool does (GumProjectSaveExtensionMethods.Initialize back-fills each standard element from StandardElementsManager, gated by VariableSave.MinimumGumxVersion); Glue calls GumProjectSave.Load and never that. What it does call — FileReferenceTracker.InitializeElements — passes each element its own default state, which resolves variable types but back-fills nothing.

Consequences worth knowing before debugging "why isn't this variable generated":

The compiler can never catch a runtime mismatch (landmine)

Glue has no compile-time knowledge of the runtime types it generates against. mStandardElementToQualifiedTypes holds fully-qualified type names as plain strings; RenderingLibrary.Graphics.Text is not a referenced assembly in any Glue project, and it is not among GumPlugin's embedded LibraryFiles resources either (LineRectangle.cs is embedded, Text.cs is not). So emitting ContainedText.DropshadowBlur for a member that doesn't exist compiles Glue perfectly and only fails in the user's game project, as CS1061.

A Gum variable whose Type is an enum name (GradientType, ColorOperation) is emitted verbatim as the property's type, so it needs an entry in GueDerivingClassCodeGenerator's mTypeToQualifiedTypes or the generated file fails with CS0246 in the user's game.

This is why the bug class recurs: Rectangle/Circle's fill/stroke family (#1907), the Arc/ColoredCircle gradient CS0266, and Text's dropshadow-channel + LocalizeText family were each found by a user's build breaking, not by CI.

GlueUnitTests/GumPlugin/GumRuntimeMemberContractTests.cs is the guard: it builds the engine's Forms solution, reflects over the real GumCore/SkiaInGum assemblies, and asserts every ContainedXxx.Member the generator emits, and every FrbBackedGumVariables entry, exists on the runtime type. When you add an entry to the allowlist, that test (not the compiler) tells you whether FRB's runtime can back it.

Note the fix is per-member, not per-type: Text legitimately backs HasDropshadow/DropshadowOffsetX/DropshadowOffsetY (it has a single DropshadowColor) while having no per-channel color, no blur, and no LocalizeText. Skip only what's actually unbacked.

CI goes red after Gum adds a variable

A variable Gum adds is not generated until it is listed in FrbBackedGumVariables (same folder), so a new Gum variable no longer breaks CI by itself. GumSchemaVariablesNotGeneratedReport lists the schema variables left out. To opt one in, add it to that class (PropertyVariables for a forwarded property, GraphicalUiElementVariables for a GraphicalUiElement member, RuntimeOnlyVariables for one a per-type generator emits), then version-gate it as below if older shipped runtimes lack it. Default to leaving out anything that only adds visual functionality (shaders, effects, new rendering options). GumRuntimeMemberContractTests checks every entry against the real runtime types. Text assertions on generated code must match a whole member name, not a substring, because CustomBorderScaleBottom contains BorderScale.

Where it lives

Property pipeline:

State pipeline:

Allowlist first, then version-gate skip lists

FrbBackedGumVariables decides which schema variables exist at all. The property loop in StandardsCodeGenerator.GenerateVariableProperties checks IsGeneratedAsProperty; StateCodeGenerator.IsBackedIfStandardElementVariable checks IsAssignableInState against the target's root standard element, and lets custom, exposed and state variables through. The skip lists that remain hold only version gates: mVariableNamesToSkipForProperties (every element) and _typedVariableNamesToSkipForProperties (keyed by element name), both consumed by GetIfShouldGenerateProperty.

Version-gating an omission

There are two patterns depending on whether the variable is type-specific.

Global (any standard element): use the local ExcludeIfVersionLessThan helper inside RefreshVariableNamesToSkipForProperties:

ExcludeIfVersionLessThan("CustomFrameTextureCoordinateWidth", GluxVersions.GumUsesSystemTypes);

This adds the name to mVariableNamesToSkipForProperties only when the loaded project is below the gating version.

Type-specific: put the gate on the per-type generator as a bool property, then conditionally add to its contribution. NineSliceCodeGenerator is the canonical small example:

bool HasNineSliceAnimate =>
    _glueState.CurrentGlueProject?.FileVersion >= (int)GluxVersions.GumNineSliceHasAnimate ||
    _glueState.CurrentMainProject?.IsFrbSourceLinked() == true;

internal void AddTypeSpecificVariableNamesToSkipForProperties(
    Dictionary<string, List<string>> typedVariableNamesToSkipForProperties)
{
    var variablesToIgnore = new List<string>();
    typedVariableNamesToSkipForProperties.Add("NineSlice", variablesToIgnore);

    if (!HasNineSliceAnimate)
    {
        variablesToIgnore.Add("Animate");
    }
}

Two things to note in that pattern:

Live GumxVersions gating — a different axis than the GluxVersions skip-lists above

Everything above gates on GluxVersions (Glue's own file format) at skip-list-refresh time. A standard element's variable family — and even which qualified runtime type it maps to — can instead need gating on Gum's own project version (GumxVersions, e.g. ShapeVariableExpansion), read live off Gum.Managers.ObjectFinder.Self.GumProjectSave.Version at generation time rather than cached in a skip-list refresh. This matters when one generator instance/process must serve whatever gumx version happens to be loaded (old and new projects both use the same Glue build), so the decision can't be baked in once. See StandardsCodeGenerator.IsRectangleFillStrokeSupported/GetQualifiedTypeFor for the pattern: a live bool checked both when choosing the contained type (mStandardElementToQualifiedTypes lookup vs. an override) and when deciding per-property generation, instead of a static skip list.

State-pipeline gate

StateCodeGenerator.RefreshVariableNamesToSkipBasedOnGlueVersion uses a paired Include / Skip helper pattern. To gate a new variable, add a block alongside the existing ones:

if (version >= (int)GluxVersions.NineSliceHasTilingMiddleSections)
{
    Include("IsTilingMiddleSections");
}
else
{
    Skip("IsTilingMiddleSections");
}

For a variable that only exists on a single standard type (like IsTilingMiddleSections on NineSlice), a global skip is fine — older projects don't have that type with that variable, so a global gate is harmless and matches existing precedent (StackSpacing, IgnoredByParentSize, IsBold, etc., are all gated globally here).

There's also a per-type variant analogous to the property pipeline (AddTypeSpecificVariableNamesToSkipForStates on per-type generators, dictionary-keyed by element name). Use it only when a variable name might collide across types and the gate must apply to one type.

When you're adding a new gate

  1. Decide if the variable is global or type-specific.
  2. Add a GluxVersions entry per the gluj-versions skill (including LatestVersion, SyntaxVersionAttribute, docs).
  3. Update every pipeline that touches it:
  1. The gate's polarity is "skip when below version", i.e. older projects don't see the new variable. The new variable becomes visible at and above the gating version.

A pre-flight check that's saved time more than once: pick one already-gated variable (IgnoredByParentSize is a good one) and grep for it. Every place it appears is a place your new variable probably also needs to appear.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.