public virtual void UnregisterFromSystems(MyCubeBlock block) { // Note: ResourceDistributor, WeaponSystem and TemrminalSystem can be null on closing (they are not in the ship but in the logical group). That's why they are null-checked if (ResourceDistributor != null) { ProfilerShort.Begin("Unregister Power producer"); var powerProducer = block.Components.Get <MyResourceSourceComponent>(); if (powerProducer != null) { ResourceDistributor.RemoveSource(powerProducer); } ProfilerShort.BeginNextBlock("Unregister Power consumer"); var powerConsumer = block.Components.Get <MyResourceSinkComponent>(); if (!(block is MyThrust) && powerConsumer != null) { ResourceDistributor.RemoveSink(powerConsumer); } ProfilerShort.End(); var socketOwner = block as IMyRechargeSocketOwner; if (socketOwner != null) { socketOwner.RechargeSocket.ResourceDistributor = null; } } ProfilerShort.Begin("Unregister gun object"); if (WeaponSystem != null) { var weapon = block as IMyGunObject <MyDeviceBase>; if (weapon != null) { WeaponSystem.Unregister(weapon); } } ProfilerShort.BeginNextBlock("Unregister functional block"); if (TerminalSystem != null) { var functionalBlock = block as MyTerminalBlock; if (functionalBlock != null) { TerminalSystem.Remove(functionalBlock); } } // CH: We probably don't need to unregister controller blocks here. It's done in ShipController's OnUnregisteredFromGridSystems /*ProfilerShort.BeginNextBlock("Unregister controller block"); * if (ControlSystem != null) * { * var controllableBlock = block as MyShipController; * if (controllableBlock != null && controllableBlock.ControllerInfo.Controller != null) * ControlSystem.RemoveControllerBlock(controllableBlock); * }*/ ProfilerShort.BeginNextBlock("Unregister inventory block"); var inventoryBlock = (block != null && block.HasInventory) ? block : null; if (inventoryBlock != null && inventoryBlock.HasInventory) { ConveyorSystem.Remove(inventoryBlock); } ProfilerShort.BeginNextBlock("Unregister conveyor block"); var conveyorBlock = block as IMyConveyorEndpointBlock; if (conveyorBlock != null) { ConveyorSystem.RemoveConveyorBlock(conveyorBlock); } ProfilerShort.BeginNextBlock("Unregister segment block"); var segmentBlock = block as IMyConveyorSegmentBlock; if (segmentBlock != null) { ConveyorSystem.RemoveSegmentBlock(segmentBlock); } ProfilerShort.BeginNextBlock("Unregister Reflector light"); var reflectorLight = block as MyReflectorLight; if (reflectorLight != null) { ReflectorLightSystem.Unregister(reflectorLight); } if (MyFakes.ENABLE_WHEEL_CONTROLS_IN_COCKPIT) { ProfilerShort.BeginNextBlock("Unregister wheel"); var wheel = block as MyMotorSuspension; if (wheel != null) { WheelSystem.Unregister(wheel); } } ProfilerShort.BeginNextBlock("Unregister landing gear"); var gear = block as IMyLandingGear; if (gear != null) { LandingSystem.Unregister(gear); } ProfilerShort.BeginNextBlock("Unregister gyro"); var gyro = block as MyGyro; if (gyro != null) { GyroSystem.Unregister(gyro); } ProfilerShort.BeginNextBlock("Unregister camera"); var camera = block as MyCameraBlock; if (camera != null) { CameraSystem.Unregister(camera); } ProfilerShort.BeginNextBlock("block.OnUnregisteredFromGridSystems()"); block.OnUnregisteredFromGridSystems(); ProfilerShort.End(); }