Glue project codegen

From vchelaru/FlatRedBall · View interactive version

Glue project codegen is an AI agent skill from the GitHub repository vchelaru/FlatRedBall. Glue's .gluj/.glsj/.glej project JSON format and how it drives Screen/Entity .Generated.cs code generation. Triggers: GlueProjectSave, ScreenSave, EntitySave, NamedObjectSave, CustomVariable, CodeWriter, ElementComponentCodeGenerator, .gluj, .glsj, .glej, .glux.

How to use this skill

Skill file

Glue Project JSON → Generated Code

File formats

| Extension | Contents | Format |

|---|---|---|

| .glux | Legacy single-file project (whole project inline) | XML (System.Xml.Serialization) |

| .gluj | Current root project file (Screens/Entities referenced by name once split) | JSON (Newtonsoft.Json) |

| .glsj | One file per Screen | JSON |

| .glej | One file per Entity | JSON |

| *.generated.glux/*.generated.gluj | Plugin-owned partial fragments merged into the main file at load | XML/JSON |

.glux→.gluj and the later per-element split into .glsj/.glej are both GluxVersions-gated migrations — see the gluj-versions skill for the version enum and gating workflow; don't duplicate that history here.

Load entry point: GlueProjectSaveExtensions.Load in FRBDK/Glue/Glue/SaveClasses/GlueProjectSaveExtensions.cs — prefers .gluj over .glux if both exist, falls back to .glux if no .gluj (mid-migration tolerance), then loads per-element .glsj/.glej files individually if FileVersion >= SeparateJsonFilesForElements. Save entry point: SaveMainAndElementsToFile (same file) → RemoveAndSaveElements for per-element files. Both driven from FRBDK/Glue/Glue/IO/ProjectLoader.cs and GluxCommands.SaveProjectAndElements*.

Schema is a plain POCO graph, not a .schema.json — the canonical source is FRBDK/Glue/GlueCommon/SaveClasses/ (GlueProjectSave.cs, ScreenSave.cs, EntitySave.cs, GlueElement.cs, NamedObjectSave.cs, CustomVariable.cs, StateSave.cs, ReferencedFileSave.cs, PropertySave.cs, VariableDefinition.cs). A second, trimmed copy of GlueProjectSave/GluxVersions lives at FRBDK/Glue/GameCommunicationPlugin/GlueControl/Embedded/Models/GlueProjectSave.cs, compiled as source into the user's game for live in-editor editing — it's frozen at an old version and drifts from the editor's own enum; don't assume version parity between the two when touching live-edit/hot-reload code.

Serialization gotchas (landmines)

JSON → generated code pipeline

Entry points: CodeWriter.GenerateCode(GlueElement element) (FRBDK/Glue/Glue/CodeGeneration/CodeWriter.cs) builds one Screen/Entity's <ElementName>.Generated.cs; CodeGeneratorIElement.cs wraps it (GenerateElementAndDerivedCode also regenerates inheriting elements); GenerateCodeCommands (Glue/Plugins/ExportedImplementations/CommandInterfaces/GenerateCodeCommands.cs) is the public command surface, including GenerateAllCode for full-project regen.

Generation is a plugin pipeline, not one big method: each contributor subclasses ElementComponentCodeGenerator (CodeGeneration/ElementComponentCodeGenerator.cs), overriding phase methods (GenerateFields, GenerateConstructor, GenerateInitialize, GenerateAddToManagers, GenerateActivity, GenerateDestroy, GenerateEvent, HandlesVariable, …). CodeWriter's static ctor lists the built-ins in order (fields → static ctor → ctor → Initialize → AddToManagers → Activity → Destroy → additional methods): ErrorCheckingCodeGenerator, ScrollableListCodeGenerator, StateCodeGenerator, FactoryElementGeneratorEarly, FactoryElementCodeGenerator, ReferencedFileSaveCodeGenerator, NamedObjectSaveCodeGenerator, CustomVariableCodeGenerator, EventCodeGenerator, PooledCodeGenerator, IVisibleCodeGenerator, IWindowCodeGenerator, ITiledTileMetadataCodeGenerator, PauseCodeGenerator, LoadingScreenCodeGenerator. External plugins (Gum, TopDown, Platformer, RedGrin, …) contribute their own generators via ICodeGeneratorPlugin (Glue/Plugins/Interfaces/ICodeGeneratorPlugin.cs) — currently only its GenerateActivity/GenerateAdditionalMethods hooks are wired up (there's a //TODO: Add more generation types here marking the gap).

The two save-class types that map most directly to generated members:

Regeneration is synchronous-on-edit, not file-watch or debounce driven: nearly every mutating Glue command (add/remove/rename object, variable, screen, entity, event) calls GlueCommands.Self.GenerateCodeCommands.GenerateElementCode(element) directly, queued onto TaskManager.Self to run off the UI thread in order. GenerateAllCode additionally fires on project load and via an explicit "Regenerate All Code" command.

Other gotchas:

Runtime-testing generated code

Compiling generated code proves it resolves against the engine, not that its logic is correct. To pin

runtime behavior, build a gold project, Assembly.LoadFrom its output DLL, and reflect into the generated

type — see GoldProjectCompileTests.FormsSampleProject_WithAddedPlatformerEntity_LoadInGlue_ThenBuild_ShouldSucceed.

Landmines: constructing an entity needs FlatRedBall.FlatRedBallServices.InitializeCommandLine() first

(reflectively) or LoadStaticContent NREs; the parameterless ctor reads ScreenManager.CurrentScreen

(null headless) — use the (contentManagerName, addToManagers: false) overload; a CSV-generated custom

class emits public fields, not properties.

A tile map's data needs no GraphicsDevice: TiledMapSave.FromFile plus

ReducedTileMapInfo.FromTiledMapSave yield layer names, map dimensions, per-layer tileset image paths and

every tile position headlessly. Only LayeredTileMap's per-layer texture load

(TextureContentLoader.LoadTexture2D, which throws when Renderer.Graphics is null) needs one — assert on

the reduced info when a test cares which map loaded rather than how it rasterizes.

Related skills

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.