fix(tapes): a followed tape cut shorter meanwhile is read as it is, let go when the window closes

- what is read up to the file's end counts, not the length taken before it; the end kept is
  made of the bytes read, with no second read
- the window of the tapes forgets the followed one when closed
- the door sides change only with the end of the cab, not when it is left (0)
This commit is contained in:
maj00r
2026-11-15 23:56:25 +01:00
parent 192f9fa499
commit f1d77addae
3 changed files with 32 additions and 8 deletions

View File

@@ -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)

View File

@@ -458,12 +458,29 @@ public sealed class HaslerTapes
public HaslerTape? Load(TapeFile file) => Load(file, follow: false);
/// <summary>No tape followed any more: what was kept of the last one is let go.</summary>
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];
}
/// <summary>
@@ -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<byte>(), 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<byte>(), 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)

View File

@@ -224,7 +224,11 @@ public sealed class HaslerTapeWindow : Window
await RefreshListAsync();
};
Activated += (_, _) => ShowWhatIsKnown();
Closed += (_, _) => _timer.Stop();
Closed += (_, _) =>
{
_timer.Stop();
_tapes.Forget();
};
}
/// <summary>