5050using System . Collections . Generic ;
5151using System . Diagnostics ;
5252using System . IO ;
53+ using System . Linq ;
5354using Event = Orts . Common . Event ;
5455
5556namespace Orts . Simulation . RollingStocks
@@ -1504,12 +1505,7 @@ public override void Restore(BinaryReader inf)
15041505 MassKG = inf . ReadSingle ( ) ;
15051506 MaxBrakeForceN = inf . ReadSingle ( ) ;
15061507 MaxHandbrakeForceN = inf . ReadSingle ( ) ;
1507- int n = inf . ReadInt32 ( ) ;
1508- for ( int i = 0 ; i < n ; i ++ )
1509- {
1510- Couplers . Add ( new MSTSCoupling ( ) ) ;
1511- Couplers [ i ] . Restore ( inf ) ;
1512- }
1508+ Couplers = ReadCouplersFromSave ( inf ) . ToList ( ) ;
15131509 Pantographs . Restore ( inf ) ;
15141510 if ( FreightAnimations != null )
15151511 {
@@ -1531,6 +1527,25 @@ public override void Restore(BinaryReader inf)
15311527 AuxPowerOn = true ;
15321528 }
15331529
1530+ /// <summary>
1531+ /// Read the coupler state(s) from a save stream.
1532+ /// </summary>
1533+ /// <remarks>
1534+ /// Has no side effects besides advancing the save stream, thus avoiding any shared-state pitfalls.
1535+ /// </remarks>
1536+ /// <param name="inf">The save stream.</param>
1537+ /// <returns>A list of newly restored <see cref="MSTSCoupling"/> instances.</returns>
1538+ private static IEnumerable < MSTSCoupling > ReadCouplersFromSave ( BinaryReader inf )
1539+ {
1540+ var n = inf . ReadInt32 ( ) ;
1541+ foreach ( int _ in Enumerable . Range ( 0 , n ) )
1542+ {
1543+ var coupler = new MSTSCoupling ( ) ;
1544+ coupler . Restore ( inf ) ;
1545+ yield return coupler ;
1546+ }
1547+ }
1548+
15341549 public override void Update ( float elapsedClockSeconds )
15351550 {
15361551 base . Update ( elapsedClockSeconds ) ;
0 commit comments