babalae/better-genshin-impact · error · InvalidDataException

相对鼠标帧长度无效。

Error message

相对鼠标帧长度无效。

What it means

Thrown by ReadRelativeMouseBatch when the decoded sample count is 0 or greater than 64, or when the payload length does not equal headerLength + count * sampleSize (12 bytes each). This catches both impossible counts and length/count mismatches that would cause out-of-bounds reads.

Source

Thrown at BetterGenshinImpact/Service/Instance/InstanceIpcProtocol.cs:210

    internal static (ulong FirstSequence, RelativeMouseSample[] Samples) ReadRelativeMouseBatch(
        InstanceIpcFrame frame)
    {
        if (frame.PayloadType != InstanceIpcPayloadType.RelativeMouseBatch)
        {
            throw new InvalidDataException($"预期相对鼠标帧,实际为 {frame.PayloadType}。");
        }

        var span = frame.Payload.AsSpan();
        if (span.Length < RelativeMouseBatchHeaderLength)
        {
            throw new InvalidDataException("相对鼠标帧头不完整。");
        }

        var count = BinaryPrimitives.ReadUInt16LittleEndian(span);
        if (count is 0 or > 64
            || span.Length != RelativeMouseBatchHeaderLength + RelativeMouseSampleLength * count)
        {
            throw new InvalidDataException("相对鼠标帧长度无效。");
        }

        var firstSequence = BinaryPrimitives.ReadUInt64LittleEndian(span[sizeof(ushort)..]);
        var baseTicks = BinaryPrimitives.ReadInt64LittleEndian(span[(sizeof(ushort) + sizeof(ulong))..]);
        var samples = new RelativeMouseSample[count];
        var offset = RelativeMouseBatchHeaderLength;
        for (var index = 0; index < count; index++)
        {
            var deltaX = BinaryPrimitives.ReadInt32LittleEndian(span[offset..]);
            var deltaY = BinaryPrimitives.ReadInt32LittleEndian(span[(offset + sizeof(int))..]);
            var offsetMicroseconds =
                BinaryPrimitives.ReadInt32LittleEndian(span[(offset + sizeof(int) * 2)..]);
            long timestampTicks;
            try
            {
                timestampTicks = checked(baseTicks + offsetMicroseconds * 10L);
            }
            catch (OverflowException exception)

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Restart both instances.
  2. Ensure both instances are the same build so the binary mouse-batch layout matches.
  3. If reproducing, dump the raw header (count, declared length) to determine whether it is corruption or a layout mismatch.
Defensive patterns

Strategy: try-catch

Validate before calling

var count = BinaryPrimitives.ReadUInt16LittleEndian(span);
if (count is 0 or > 64
    || span.Length != RelativeMouseBatchHeaderLength + RelativeMouseSampleLength * count)
{
    throw new InvalidDataException("相对鼠标帧长度无效。");
}

Try / catch

try
{
    var (firstSeq, samples) = InstanceIpcProtocol.ReadRelativeMouseBatch(frame.Value);
}
catch (InvalidDataException ex) when (ex.Message.Contains("帧长度无效"))
{
    logger.LogError(ex, "Invalid mouse-batch length/count; restart instances");
}

Prevention

When it happens

Trigger: The peer wrote count=0 (WriteRelativeMouseBatchAsync rejects this on the writer side, so this implies a non-standard peer), count>64, or a length that disagrees with the count — corruption or a protocol/version mismatch in the binary layout.

Common situations: Mixed-version instances with different sample layouts; stream corruption flipping count or length bytes; a peer implementation that bypasses the writer validation.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/5b1cba5b26979796. Report an issue: GitHub.