Mod API: Registering controls in OnLoad removes default controls

Merii shared this bug 16 hours ago
Submitted

Build tested: Space Engineers 1.211.017. Mod API only, no vanilla gameplay repro.


1. Registering a terminal control for a block type that has not been placed yet permanently removes that type's own vanilla controls (per session)


MyTerminalControlFactory.AreControlsCreated<T>() / AreControlsCreated(Type) return only whether the type is a key in the factory's control dictionary, nothing about whether the entry actually contains controls.


Every mod API call that names a block type resolves it through MyTerminalControlFactory.GetList(Type), and GetList creates a new BlockData when the key is missing; InitializeControls then fills it only from base-class entries that already exist. The affected entry points are MyTerminalControls.AddControl / AddAction / RemoveControl / RemoveAction and, notably, the read-only GetControls<TBlock>(out ...) / GetActions<TBlock>(out ...), so merely querying a type has the same effect as registering for it.


Once that empty entry exists, the block's own control creation bails permanently: MyTerminalBlock.CreateTerminalControls() and every override (MyAssembler, MySmallMissileLauncher, MyShipConnector, ...) is gated on that check, so the type's vanilla controls are never added for the rest of the session. All 74 overrides in the shipped assemblies check AreControlsCreated, in two forms: an early return, and the inverted if (!AreControlsCreated<T>()) { ... } that wraps the base call and the type's own controls (MyFunctionalBlock, MyMotorStator, MyBeacon, ...). The semantics are the same either way; the base-class call inside the inverted form also bails, so the loss cascades down the inheritance chain. One override (MyScenarioBuildingBlock) checks MyTerminalBlock rather than its own type.


The worst case is a world in which no terminal block has been initialized yet. Because AddBaseClass only copies from entries that already exist and never creates the base entry itself, the entry the mod just created is completely empty rather than base-populated: the first block of that type shows no vanilla controls at all, and every other terminal in the world keeps its own type-specific controls but never gets the base ones.


Repro

  1. From a mod session component, in BeforeStart(), call either MyAPIGateway.TerminalControls.AddControl<IMyConveyorSorter>(myButton) or MyAPIGateway.TerminalControls.GetControls<IMyConveyorSorter>(out list).
  2. Start a world with the mod that contains no Conveyor Sorter.
  3. Place the first Conveyor Sorter. Its own vanilla terminal controls are missing; only the base controls (Name, CustomData, Show in terminal / inventory / toolbar / HUD) appear. Re-enter the world and they are present again.


The same applies one level up: AddControl<IMyTerminalBlock> before any terminal block exists in the session removes those base controls from every terminal in the world.


Component-provided terminal controls are suppressed by the same key-existence check. MyTerminalBlock.CreateTerminalsComponentControls skips any IMyTerminalControlsProviderComponent whose component type already has a dictionary key, and it is invoked from inside the block-type guard (MyFunctionalBlock, MyRemoteControl, MyEventControllerBlock, MyPathRecorderBlock, MyOffensiveCombatBlock, MyDefensiveCombatBlock, MyBroadcastController, MyEmotionControllerBlock, MyFlightMovementBlock), so pre-registering a block type also permanently drops that block's component controls for the session. The component overload MyTerminalControlFactory.AddControl<TComponent> has the same key-existence semantics on the component side.


A mod cannot distinguish "entry exists and is populated" from "entry exists but is empty" without calling the same GetList path that creates it, so the destructive call is the only probe available.


Expected: "controls created" should mean controls were actually created for the type, not that a dictionary key exists. Currently a call intended to add a control can silently delete the game's controls for that type.


2. MyTerminalControlFactory.EnsureControlsAreCreated never invokes anything


It looks up the creation method with GetMethod("CreateTerminalControls", BindingFlags.Static | BindingFlags.NonPublic) and invokes it if found. Terminal classes define CreateTerminalControls() as a protected virtual instance method (the doc comment on MyTerminalBlock.CreateTerminalControls records it being moved out of the static constructor), so GetMethod returns null for every terminal type and the call is a no-op.


This matters because it is the intended safety net for issue 1: MyTerminalControls.CreateControl<TControl, TBlock> calls it when AreControlsCreated is false, so that vanilla controls exist before a mod control is added. It never runs. MyTerminalControlFactory.AddBaseClass performs the same static-only lookup, so base-class control creation is skipped there too. It works only when the base entry happens to have been created already.


For scope: the dead net is harmless in itself. EnsureControlsAreCreated never inserts into the dictionary, and CreateControl / CreateProperty / CreateAction only instantiate objects, so a mod that just creates controls without adding or querying them by type does not create the empty entry. The damage comes exclusively from the type-naming query and mutate calls listed in issue 1, which are necessary to use the created controls.

Leave a Comment
 
Attach a file