Skip to content

Commit 37a9bc3

Browse files
committed
Fix duplicate couplers upon resuming from save: https://bugs.launchpad.net/or/+bug/1892403
1 parent 984b198 commit 37a9bc3

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

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

Lines changed: 13 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,17 @@ public override void Restore(BinaryReader inf)
15311527
AuxPowerOn = true;
15321528
}
15331529

1530+
private static IEnumerable<MSTSCoupling> ReadCouplersFromSave(BinaryReader inf)
1531+
{
1532+
var n = inf.ReadInt32();
1533+
foreach (int _ in Enumerable.Range(0, n))
1534+
{
1535+
var coupler = new MSTSCoupling();
1536+
coupler.Restore(inf);
1537+
yield return coupler;
1538+
}
1539+
}
1540+
15341541
public override void Update(float elapsedClockSeconds)
15351542
{
15361543
base.Update(elapsedClockSeconds);

0 commit comments

Comments
 (0)