/// <summary>
    /// Reads a ToEE object id from the reader.
    /// </summary>
    /// <param name="reader"></param>
    /// <returns></returns>
    public static ObjectId ReadObjectId(this BinaryReader reader)
    {
        Span <byte> buffer = stackalloc byte[24];

        if (reader.Read(buffer) != buffer.Length)
        {
            throw new Exception("Failed to read 24-byte ObjectId");
        }

        var type = BitConverter.ToUInt16(buffer.Slice(0, sizeof(ushort)));

        var payload = buffer.Slice(8); // ObjectId seems to use 8-byte packed

        switch (type)
        {
        case 0:
            return(ObjectId.CreateNull());

        case 1:
            var protoId = BitConverter.ToUInt16(payload.Slice(0, sizeof(ushort)));
            return(ObjectId.CreatePrototype(protoId));

        case 2:
            var guid = MemoryMarshal.Read <Guid>(payload);
            return(ObjectId.CreatePermanent(guid));

        case 3:
            var positionalId = MemoryMarshal.Read <PositionalId>(payload);
            return(ObjectId.CreatePositional(positionalId));

        case 0xFFFE:
            throw new Exception("Cannot load 'handle' ObjectIDs because the pointers are stale!");

        case 0xFFFF:
            return(ObjectId.CreateBlocked());

        default:
            throw new Exception("Unknown ObjectId type: " + type);
        }
    }