Skip to content

Commit 9671d98

Browse files
authored
Merge pull request #259 from YoRyan/fix-resume-couplers
Fix duplicate couplers upon resuming from save: https://bugs.launchpad.net/or/+bug/1892403
2 parents 98edaea + a37e1d6 commit 9671d98

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

Source/Orts.Simulation/Simulation/RollingStocks/MSTSWagon.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
using System.Collections.Generic;
5151
using System.Diagnostics;
5252
using System.IO;
53+
using System.Linq;
5354
using Event = Orts.Common.Event;
5455

5556
namespace 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

Comments
 (0)