diff --git a/Eu07.BlackBox.Views/EventList.cs b/Eu07.BlackBox.Views/EventList.cs index a47f486..b55cca9 100644 --- a/Eu07.BlackBox.Views/EventList.cs +++ b/Eu07.BlackBox.Views/EventList.cs @@ -302,8 +302,8 @@ public sealed class EventList : ListBox movingOpen = false; continue; } - // another cab swaps the sides, and tells nothing of the doors - bool sameCab = b.CabEnd == s.CabEnd; + // the other end swaps the sides, and tells nothing of the doors; no cab (0) sees them as the front one does + bool sameCab = (b.CabEnd < 0) == (s.CabEnd < 0); bool Came(CabDevices device) => !b.Has(device) && s.Has(device) && (sameCab || (device & DoorSides) == 0); bool Went(CabDevices device) => b.Has(device) && !s.Has(device) && (sameCab || (device & DoorSides) == 0); foreach (var (device, key) in Switched) diff --git a/StarterNG/Application/HaslerTapes.cs b/StarterNG/Application/HaslerTapes.cs index c183ebe..46ab209 100644 --- a/StarterNG/Application/HaslerTapes.cs +++ b/StarterNG/Application/HaslerTapes.cs @@ -458,12 +458,29 @@ public sealed class HaslerTapes public HaslerTape? Load(TapeFile file) => Load(file, follow: false); + /// No tape followed any more: what was kept of the last one is let go. + public void Forget() + { + lock (_followedLock) + _followed = null; + } + + // what the file has between the two: less, up to its end, when it was cut shorter since its length was taken private static byte[] Bytes(Stream stream, long from, long to) { stream.Seek(from, SeekOrigin.Begin); var bytes = new byte[to - from]; - stream.ReadExactly(bytes); - return bytes; + int read = stream.ReadAtLeast(bytes, bytes.Length, throwOnEndOfStream: false); + return read == bytes.Length ? bytes : bytes[..read]; + } + + // the end of what was read, after the part added to it + private static byte[] Tail(byte[] before, byte[] added) + { + if (added.Length >= FollowedTail) + return added[^FollowedTail..]; + var keptBefore = Math.Min(before.Length, FollowedTail - added.Length); + return [.. before[^keptBefore..], .. added]; } /// @@ -492,14 +509,17 @@ public sealed class HaslerTapes if (!follow || _followed is not { } followed || followed.Path != file.Path || length < followed.Read || !Bytes(stream, followed.Read - followed.Tail.Length, followed.Read).AsSpan().SequenceEqual(followed.Tail)) followed = (file.Path, 0, Array.Empty(), new HaslerTape.Reader()); - followed.Reader.Feed(Encoding.ASCII.GetString(Bytes(stream, followed.Read, length))); + // what was read, not the length taken before: the file may be cut shorter meanwhile + var added = Bytes(stream, followed.Read, length); + followed.Reader.Feed(Encoding.ASCII.GetString(added)); // the time went back in what was added: the log begun anew and already longer than was read if (followed.Read > 0 && followed.Reader.WentBack) { followed = (file.Path, 0, Array.Empty(), new HaslerTape.Reader()); - followed.Reader.Feed(Encoding.ASCII.GetString(Bytes(stream, 0, length))); + added = Bytes(stream, 0, length); + followed.Reader.Feed(Encoding.ASCII.GetString(added)); } - _followed = (file.Path, length, Bytes(stream, Math.Max(0, length - FollowedTail), length), followed.Reader); + _followed = (file.Path, followed.Read + added.Length, Tail(followed.Tail, added), followed.Reader); return followed.Reader.Tape(final: !follow); } catch (Exception ex) diff --git a/StarterNG/Views/Tapes/HaslerTapeWindow.cs b/StarterNG/Views/Tapes/HaslerTapeWindow.cs index e34c1d4..9cc1e3b 100644 --- a/StarterNG/Views/Tapes/HaslerTapeWindow.cs +++ b/StarterNG/Views/Tapes/HaslerTapeWindow.cs @@ -224,7 +224,11 @@ public sealed class HaslerTapeWindow : Window await RefreshListAsync(); }; Activated += (_, _) => ShowWhatIsKnown(); - Closed += (_, _) => _timer.Stop(); + Closed += (_, _) => + { + _timer.Stop(); + _tapes.Forget(); + }; } ///