Skip to content

Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.

C

feat: lyrics from the sources for tracks Spotify has none for

Vojtěch Škopek11e6536a42f8
diff --git a/docs/tweaks.md b/docs/tweaks.md
index fb72c1c9..80c9881f 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -53,8 +53,15 @@ app:
BiniLyrics.m and Unison.m, read by SGTTML.m, which is the only shape carrying a second voice and the
backing vocals; Musixmatch.m, matched by Spotify's track id with an anonymous token, word timed where
it has richsync; NetEase.m, word timing from yrc for what the others only line time; LrcLib.m, open and
- keyless and timed by the line, the floor under the rest. color-lyrics is answered with whichever won and
- has_lyrics forced for every track (LyricsHook.x)
+ keyless and timed by the line, the floor under the rest. color-lyrics is answered with whichever won
+ (LyricsHook.x): Spotify's own 200 gets our lines swapped in; a track Spotify's metadata says has none has
+ its request sent to a donor track that does, so the reply is a real 200 (a 404 answered as a 200 in the
+ delegate alone never showed the card on 9.1.78); a 404 for a track not seen yet is held until the chain
+ answers. The card list the server sends per track (scrollsita) carries a lyrics section only for tracks
+ Spotify has lyrics for, so one is added to any list without it: that is what makes the player ask for the
+ lyrics and show the card. has_lyrics is forced on for every track, the walk starts at the track change
+ for it and the next, and the player's card-loading timeout flag is forced to its 5 s maximum while a
+ source is on
Onboarding/ the welcome tour over Home on the first launch (Onboarding.x, the pages in Tour.m), offered again from the Mod page
About/ the update check and the Mod page: the build, its updates, the links and the reset
diff --git a/scripts/dump-log.sh b/scripts/dump-log.sh
index 0b7930e3..c23b3ef4 100755
--- a/scripts/dump-log.sh
+++ b/scripts/dump-log.sh
@@ -5,4 +5,8 @@
# scripts/dump-log.sh -n # over Wi-Fi
#
# The hierarchy dumps arrive as numbered parts ("now playing hierarchy 3/9").
-exec idevicesyslog -p Spotify -m spotifyglass "$@"
+#
+# Matched on the tweak's own prefix alone. -p Spotify used to work and now lets nothing through,
+# not even the thousand lines a second Spotify(CoreFoundation) writes, once the app is signed under
+# a certificate's App ID; every line SGLog writes carries [spotifyglass], which is enough on its own.
+exec idevicesyslog -m "[spotifyglass]" "$@"
diff --git a/tweak/Sources/Features/Flags/Flags.x b/tweak/Sources/Features/Flags/Flags.x
index f781cb04..53ce9b41 100644
--- a/tweak/Sources/Features/Flags/Flags.x
+++ b/tweak/Sources/Features/Flags/Flags.x
@@ -4,6 +4,7 @@
#import "Core/SGCore.h"
#import "Flags.h"
#import "Features/AdBlock/AdBlock.h"
+#import "Features/LyricsSources/LyricsSources.h"
BOOL SGGlassOwnsFlag(NSString *key) {
static NSSet<NSString *> *owned;
@@ -27,6 +28,7 @@ static id forced(NSString *key) {
id value = SGFlagOverride(key);
if (!value && SGFlag(SGKeySpotifyGlass, NO) && SGGlassOwnsFlag(key)) value = @YES;
if (!value && SGAdBlockForcesFlagOff(key)) value = @NO;
+ if (!value) value = SGLyricsForcedFlag(key);
return value;
}
diff --git a/tweak/Sources/Features/Karaoke/Karaoke.h b/tweak/Sources/Features/Karaoke/Karaoke.h
index 4d1fe6e3..290a2d37 100644
--- a/tweak/Sources/Features/Karaoke/Karaoke.h
+++ b/tweak/Sources/Features/Karaoke/Karaoke.h
@@ -59,6 +59,11 @@ NSString *SGKaraokePlayingTrack(void); // the base62 id, nil before the player
NSInteger SGKaraokePositionMs(void); // negative when unknown
void SGKaraokeSeek(NSInteger ms);
id SGKaraokePlayer(void); // SPTEsperantoPlayer, nil before the app asked it for its state
+// A track the player has reported, by its base62 id; nil for one it has not played this session.
+@class SPTPlayerTrack;
+SPTPlayerTrack *SGKaraokeTrackFor(NSString *trackID);
+// Keeps a track seen elsewhere, so a source can name it before the player has reported it.
+void SGKaraokeRememberTrack(SPTPlayerTrack *track);
@interface SGKaraokeView : UIView
// Compact is the card under the player: Spotify's own type size, no seeking by tap and no margin of
diff --git a/tweak/Sources/Features/Karaoke/KaraokeSource.x b/tweak/Sources/Features/Karaoke/KaraokeSource.x
index dc513ac9..fb33f395 100644
--- a/tweak/Sources/Features/Karaoke/KaraokeSource.x
+++ b/tweak/Sources/Features/Karaoke/KaraokeSource.x
@@ -11,6 +11,7 @@
#import "Headers/SPTPlayer.h"
static const NSUInteger kKeptTracks = 40;
+static const NSUInteger kSeenTracks = 200;
// What spclient needs from a request to answer it as the signed-in app.
static NSString *const kSpclientHeaders[] = {@"authorization", @"client-token", @"app-platform", @"spotify-app-version", @"user-agent", @"accept-language"};
@@ -18,6 +19,13 @@ static NSMutableDictionary<NSString *, NSArray<SGKaraokeLine *> *> *sg_lyrics;
static NSMutableSet<NSString *> *sg_requested;
static NSDictionary<NSString *, NSString *> *sg_spclientHeaders;
static __weak id sg_player;
+// Every track the player has reported, by id, so a source can name a track that is not the one
+// playing at the moment it is asked: a lyrics request routinely lands a beat before the player
+// moves on to its track. The last object seen is kept by pointer so the check on each call is free,
+// and its id behind it, since the player hands out a fresh object with every state it reports.
+static NSMutableDictionary<NSString *, SPTPlayerTrack *> *sg_seenTracks;
+static __weak SPTPlayerTrack *sg_lastSeen;
+static NSString *sg_lastSeenID; // the player makes a new track object on every state it reports, so the id is what tells a change
static BOOL sg_ownSources; // a source of the mod's answers the color-lyrics request, not Spotify
static char kBodyKey;
@@ -91,6 +99,8 @@ static void requestFromSpotify(NSString *trackID) {
[request setValue:value forHTTPHeaderField:name];
}];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
+ // The mod's own, so LyricsHook's request hook does not send it to the donor.
+ [NSURLProtocol setProperty:@YES forKey:SGLyricsOwnRequestKey inRequest:request];
[[NSURLSession.sharedSession dataTaskWithRequest:request completionHandler:^(NSData *body, NSURLResponse *response, NSError *error) {
NSArray<SGKaraokeLine *> *lines = SGKaraokeLinesFromBody(body);
SGLog(@"karaoke: fetched lyrics for %@: status %ld, %lu synced lines, error %@", trackID,
@@ -147,10 +157,68 @@ void SGKaraokeSeek(NSInteger ms) {
[(id<SPTPlayer>)player seekTo:ms / 1000.0];
}
+static NSString *idOf(SPTPlayerTrack *track) {
+ id uri = track.URI;
+ NSString *text = [uri isKindOfClass:NSURL.class] ? ((NSURL *)uri).absoluteString : [uri description];
+ return [text hasPrefix:@"spotify:track:"] ? [text substringFromIndex:@"spotify:track:".length] : nil;
+}
+
+// Tracks come in from the player and from every list that reads their metadata, so when the table
+// is full it is emptied, all but the track playing, whose name the next lyrics request needs.
+static void remember(SPTPlayerTrack *track, NSString *trackID) {
+ @synchronized (sg_seenTracks) {
+ if (sg_seenTracks.count >= kSeenTracks) {
+ [sg_seenTracks removeAllObjects];
+ SPTPlayerTrack *playing = sg_lastSeen;
+ NSString *playingID = playing ? idOf(playing) : nil;
+ if (playingID) sg_seenTracks[playingID] = playing;
+ }
+ sg_seenTracks[trackID] = track;
+ }
+}
+
+SPTPlayerTrack *SGKaraokeTrackFor(NSString *trackID) {
+ if (!trackID) return nil;
+ @synchronized (sg_seenTracks) { return sg_seenTracks[trackID]; }
+}
+
+void SGKaraokeRememberTrack(SPTPlayerTrack *track) {
+ if (!sg_seenTracks) return;
+ NSString *trackID = idOf(track);
+ if (trackID) remember(track, trackID);
+}
+
+// With a source of the mod's on, the walk for a track starts the moment the player moves to it and,
+// for the track after it, while this one still plays: Spotify asks for a track's lyrics within a
+// beat of starting it and gives its card list about a second to load, so an answer that is already
+// in is what puts the card there. The track is named here, so no walk waits for a name.
+static void prefetch(SPTPlayerTrack *track, NSString *trackID, SPTPlayerState *state) {
+ if (!sg_ownSources) return;
+ SGLyricsPrefetch(trackID);
+ id future = [state respondsToSelector:@selector(future)] ? state.future : nil;
+ id next = [future isKindOfClass:NSArray.class] ? [(NSArray *)future firstObject] : nil;
+ if (![next isKindOfClass:objc_getClass("SPTPlayerTrack")]) return;
+ NSString *nextID = idOf(next);
+ if (!nextID || [nextID isEqualToString:trackID]) return;
+ remember(next, nextID);
+ SGLyricsPrefetch(nextID);
+}
+
%hook SPTEsperantoPlayer
- (id)state {
if (!sg_player) sg_player = self;
- return %orig;
+ SPTPlayerState *state = %orig;
+ SPTPlayerTrack *track = state.track;
+ if (track && track != sg_lastSeen) {
+ sg_lastSeen = track;
+ NSString *trackID = idOf(track);
+ if (trackID && ![trackID isEqualToString:sg_lastSeenID]) {
+ sg_lastSeenID = trackID;
+ remember(track, trackID);
+ prefetch(track, trackID, state);
+ }
+ }
+ return state;
}
%end
@@ -177,7 +245,10 @@ void SGKaraokeSeek(NSInteger ms) {
%end
%ctor {
- if (!SGFlag(SGKeyKaraokeLyrics, NO) && !SGFlag(SGKeyLockScreenLyrics, NO)) return;
+ // The sources that search by name learn the name from the player, so the player is caught
+ // whenever one is on, not only for the karaoke page and the lock screen.
+ if (!SGFlag(SGKeyKaraokeLyrics, NO) && !SGFlag(SGKeyLockScreenLyrics, NO) && !SGLyricsEnabled()) return;
+ sg_seenTracks = [NSMutableDictionary dictionary];
sg_lyrics = [NSMutableDictionary dictionary];
sg_requested = [NSMutableSet set];
sg_ownSources = SGLyricsEnabled();
diff --git a/tweak/Sources/Features/LyricsSources/LyricsHook.x b/tweak/Sources/Features/LyricsSources/LyricsHook.x
index 04d51ca5..6f1b2068 100644
--- a/tweak/Sources/Features/LyricsSources/LyricsHook.x
+++ b/tweak/Sources/Features/LyricsSources/LyricsHook.x
@@ -1,34 +1,74 @@
-// Spotify's /color-lyrics/v2/track/<id> answered with the chain's lyrics. An NSURLProtocol, put into
-// every session the app makes, takes the request over, sends it on itself and gives Spotify one reply:
-// timed lines from the chain first, then Spotify's timed ones, then untimed ones from the chain. A
-// request Spotify's server has no lyrics for (404) becomes a 200 when a source has some. Whichever
-// lines win go to the karaoke page as well, and the source that supplied them is credited there.
+// Spotify's /color-lyrics/v2/track/<id> answered with the chain's lyrics, in the URLSession delegates
+// Spotify reads the reply through, SPTDataLoaderService and HttpClientURLSession, the way
+// EeveeSpotify Reincarnated answers it on 9.1 (DataLoaderServiceHooks.x.swift). Three cases:
//
-// It is a protocol and not a rewrite in the URLSession delegates: a 200 handed to
-// Connectivity_HttpClientKit's delegate in place of a 404 still showed no lyrics, so the task's own
-// response has to be the 200.
+// Spotify has lyrics of its own (a 200). The body is read in whole and handed on with the chain's
+// lines in place of Spotify's when they are timed, on the main queue, which 9.1.60 and later require
+// of these callbacks. This is the path that is known to put the lyrics card under the player.
+//
+// Spotify's own metadata says the track has none. Its request is sent to a donor track instead, one
+// that has lyrics everywhere, and the 200 that comes back has its lines swapped for the chain's as
+// above: a 404 turned into a 200 in the delegate alone never showed a card on 9.1.78, however fast
+// it came, while a real 200 with our lines in it always did. The donor's own lines are never shown:
+// with nothing from the chain the request is failed as Spotify's would have been. The player-track
+// hook below notes what the metadata said, and forces has_lyrics on so the request is made at all.
+//
+// A 404 for a track the metadata has not been seen for. The reply is held where it arrives, its
+// completion handler with it, until the chain has answered; the delegate is then told of a 200, the
+// lyrics and the end, and the task's own response answers the same 200, since Spotify's client may
+// read the status from either. Spotify's real 404 body that follows is dropped, so the task is
+// answered exactly once.
+//
+// Whichever lines win go to the karaoke page as well, and the source that supplied them is credited
+// there.
+//
+// None of that shows a card on its own. The cards under the player are a list the server sends per
+// track, scrollsita's NpvScrollResponse, and it holds a lyrics section only for tracks Spotify has
+// lyrics for; the lyrics provider is asked for a track only when its section is in the list. So the
+// list is read on the way in and a lyrics section is put at its top for any track without one that
+// a source may have lyrics for. The section's shape is copied from the last list that carried one,
+// with the track's own URI inside; before any has passed, it is the URI alone. The player then asks
+// for the lyrics as it would for any track, and the reply is answered as above.
#import "Core/SGCore.h"
#import "LyricsSources.h"
#import "Features/AdBlock/Protobuf.h"
#import "Headers/SPTPlayer.h"
-static NSString *const kHandledKey = @"spotifyglass.lyricsHandled";
-
+// Blinding Lights, The Weeknd: lyrics in every market Spotify serves them in.
+static NSString *const kDonorTrack = @"0VjIjW4GlUZAMYd2vXMi3b";
+// On a request sent to the donor: the track it was made for.
+static NSString *const kTrackProperty = @"spotifyglass.lyricsTrack";
+static NSString *const kLyricsPath = @"/color-lyrics/v2/track/";
+// NpvScrollResponse { 1 structure: { 1 repeated section: Section { oneof section_type: 5 lyrics: { 1 entity_uri } } } }
+// The oneof's cases, in the order Spotify declares them: comments, creator_bio, discovery_feed,
+// credits, lyrics, merch and on, which is the order proto fields are numbered in.
+static const uint32_t kStructureField = 1, kSectionField = 1, kLyricsSectionField = 5, kEntityURIField = 1;
// The page's colours when Spotify sent none to keep, ARGB.
static const uint32_t kBackground = 0xFF535353, kLine = 0xFF000000, kActiveLine = 0xFFFFFFFF;
-// A protocol sees the request without its session's HTTPAdditionalHeaders, so the ones of Spotify's
-// sessions are kept and put back on the request it sends.
-static NSMutableDictionary<NSString *, NSString *> *sg_sessionHeaders;
-
-static NSString *lyricsTrack(NSURL *url) {
+static NSString *trackInURL(NSURL *url) {
NSString *path = url.path;
- NSRange marker = [path rangeOfString:@"/color-lyrics/v2/track/"];
+ NSRange marker = [path rangeOfString:kLyricsPath];
if (marker.location == NSNotFound) return nil;
NSString *track = [[path substringFromIndex:NSMaxRange(marker)] componentsSeparatedByString:@"/"].firstObject;
return track.length ? track : nil;
}
+// The track a request was made for: the one it was sent to the donor for, else the one in its URL.
+static NSString *trackFor(NSURLRequest *request) {
+ id kept = [NSURLProtocol propertyForKey:kTrackProperty inRequest:request];
+ return [kept isKindOfClass:NSString.class] ? kept : trackInURL(request.URL);
+}
+
+static NSString *trackOf(NSURLSessionTask *task) {
+ return trackFor(task.originalRequest) ?: trackFor(task.currentRequest);
+}
+
+static BOOL sentToDonor(NSURLSessionTask *task) {
+ return [NSURLProtocol propertyForKey:kTrackProperty inRequest:task.originalRequest] != nil
+ || [NSURLProtocol propertyForKey:kTrackProperty inRequest:task.currentRequest] != nil;
+}
+
static NSInteger statusOf(NSURLResponse *response) {
return [response isKindOfClass:NSHTTPURLResponse.class] ? ((NSHTTPURLResponse *)response).statusCode : 0;
}
@@ -48,137 +88,397 @@ static NSData *lyricsBody(SGLyricsResult *lyrics, NSData *spotify) {
return SGPBSerialize(@[SGPBBytes(1, SGPBSerialize(data)), colors]);
}
-// The chain's body, or nil to hand Spotify's reply on as it came.
-static NSData *chosenBody(NSString *track, SGLyricsResult *lyrics, NSData *spotify) {
- NSArray<SGKaraokeLine *> *spotifyLines = SGKaraokeLinesFromBody(spotify);
+// The chain's body, or nil to hand Spotify's reply on as it came. `spotify` is Spotify's own 200
+// body when it has one; a donor's carries no lines of the track's and only lends its colours, and
+// only when the server made them from the track's own artwork, which it names in the URL.
+static NSData *chosenBody(NSString *track, SGLyricsResult *lyrics, NSData *spotify, BOOL donor, BOOL donorColors) {
+ NSArray<SGKaraokeLine *> *spotifyLines = donor ? nil : SGKaraokeLinesFromBody(spotify);
BOOL ours = lyrics.texts.count && (lyrics.synced || !spotifyLines);
NSArray<SGKaraokeLine *> *karaoke = lyrics.wordTimed || ours ? lyrics.karaokeLines : spotifyLines;
if (karaoke) SGKaraokeKeepLines(track, karaoke);
SGLyricsSetCredit(track, karaoke == lyrics.karaokeLines ? lyrics.provider : @"Spotify");
- SGLog(@"lyrics: page of %@ gets %@, %@ word timing", track, ours ? [NSString stringWithFormat:@"%@'s lines", lyrics.provider] : spotify.length ? @"Spotify's own lines" : @"no lyrics",
+ SGLog(@"lyrics: page of %@ gets %@, %@ word timing", track, ours ? [NSString stringWithFormat:@"%@'s lines", lyrics.provider] : spotifyLines ? @"Spotify's own lines" : @"no lyrics",
lyrics.wordTimed ? @"real" : @"estimated");
- return ours ? lyricsBody(lyrics, spotify) : nil;
+ return ours ? lyricsBody(lyrics, donor && !donorColors ? nil : spotify) : nil;
}
-@interface SGLyricsURLProtocol : NSURLProtocol
-@end
+#pragma mark - the card list, given a lyrics section
-@implementation SGLyricsURLProtocol {
- NSURLSessionDataTask *_inner;
- NSThread *_thread;
- NSArray<NSString *> *_modes;
- BOOL _stopped;
+// The track a scrollsita request is for: .../scrollsita/v1/scroll/spotify:track:<id>, the URI
+// escaped or not.
+static NSString *scrollTrackOf(NSURLSessionTask *task) {
+ NSURL *url = task.currentRequest.URL ?: task.originalRequest.URL;
+ NSString *path = url.path;
+ if (![path containsString:@"/scrollsita/"]) return nil;
+ for (NSString *part in [path componentsSeparatedByString:@"/"]) {
+ NSString *piece = part.stringByRemovingPercentEncoding ?: part;
+ if ([piece hasPrefix:@"spotify:track:"]) {
+ NSString *track = [piece substringFromIndex:@"spotify:track:".length];
+ return track.length ? track : nil;
+ }
+ }
+ return nil;
}
-+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
- return lyricsTrack(request.URL) && ![NSURLProtocol propertyForKey:kHandledKey inRequest:request];
+// The last lyrics section the server sent, whole, so an added one has every field the client may
+// want beyond the URI. Any thread, under its lock.
+static NSData *sg_lyricsSectionTemplate;
+static NSObject *sg_templateLock;
+
+static NSData *lyricsSectionFor(NSString *track) {
+ NSString *uri = [@"spotify:track:" stringByAppendingString:track];
+ NSData *lyrics = SGPBSerialize(@[SGPBString(kEntityURIField, uri)]);
+ NSData *template;
+ @synchronized (sg_templateLock) { template = sg_lyricsSectionTemplate; }
+ NSMutableArray<SGPBField *> *fields = template ? SGPBParse(template) : nil;
+ if (!fields) return SGPBSerialize(@[SGPBBytes(kLyricsSectionField, lyrics)]);
+ for (SGPBField *field in fields) {
+ if (field.number == kLyricsSectionField) field.payload = lyrics;
+ }
+ return SGPBSerialize(fields);
+}
+
+// The list with a lyrics section at its top, or nil to hand it on as it came: it has one, or the
+// track is one no source has lyrics for, or the bytes are not what they should be.
+static NSData *listWithLyrics(NSData *body, NSString *track) {
+ NSMutableArray<SGPBField *> *fields = SGPBParse(body);
+ SGPBField *structure = SGPBFirst(fields, kStructureField);
+ if (structure.wire != 2) {
+ SGLog(@"scrollsita: list for %@ not read, %lu bytes", track, (unsigned long)body.length);
+ return nil;
+ }
+ NSMutableArray<SGPBField *> *sections = SGPBParse(structure.payload);
+ for (SGPBField *section in sections) {
+ if (section.number != kSectionField || section.wire != 2) continue;
+ SGPBField *kind = SGPBParse(section.payload).firstObject;
+ if (kind.number == kLyricsSectionField && kind.wire == 2) {
+ @synchronized (sg_templateLock) { sg_lyricsSectionTemplate = section.payload; }
+ return nil;
+ }
+ }
+ if (!SGLyricsMayHave(track)) return nil;
+ SGPBField *added = SGPBBytes(kSectionField, lyricsSectionFor(track));
+ NSMutableArray<SGPBField *> *withLyrics = [NSMutableArray arrayWithObject:added];
+ [withLyrics addObjectsFromArray:sections];
+ structure.payload = SGPBSerialize(withLyrics);
+ SGLog(@"scrollsita: lyrics section added to the card list of %@", track);
+ return SGPBSerialize(fields);
+}
+
+#pragma mark - the request, sent to the donor when Spotify has nothing
+
+// The same request for the donor track, or nil to let this one go as it is: Spotify has lyrics for
+// the track, or has not said yet, or every source has already said it has none, or the mod made it.
+static NSURLRequest *donorRequest(NSURLRequest *request) {
+ if (!request.URL || [NSURLProtocol propertyForKey:SGLyricsOwnRequestKey inRequest:request]) return nil;
+ if ([NSURLProtocol propertyForKey:kTrackProperty inRequest:request]) return nil;
+ NSString *track = trackInURL(request.URL);
+ if (!track || [track isEqualToString:kDonorTrack]) return nil;
+ if (SGLyricsSpotifyHas(track) != 0 || !SGLyricsMayHave(track)) return nil;
+ NSString *absolute = request.URL.absoluteString;
+ NSRange marker = [absolute rangeOfString:[kLyricsPath stringByAppendingString:track]];
+ if (marker.location == NSNotFound) return nil;
+ NSRange idRange = NSMakeRange(NSMaxRange(marker) - track.length, track.length);
+ NSURL *url = [NSURL URLWithString:[absolute stringByReplacingCharactersInRange:idRange withString:kDonorTrack]];
+ if (!url) return nil;
+ NSMutableURLRequest *donor = [request mutableCopy];
+ donor.URL = url;
+ [NSURLProtocol setProperty:track forKey:kTrackProperty inRequest:donor];
+ SGLog(@"lyrics: Spotify has none for %@, its request goes to the donor", track);
+ SGLyricsPrefetch(track);
+ return donor;
+}
+
+%group Sessions
+%hook NSURLSession
+- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request {
+ NSURLRequest *donor = donorRequest(request);
+ return %orig(donor ?: request);
+}
+%end
+%end
+
+// The sessions Spotify makes are __NSURLSessionLocal; hooked as well only when that class answers
+// dataTaskWithRequest: itself, since a hook on an inherited method would sit on NSURLSession's twice.
+%group LocalSessions
+%hook __NSURLSessionLocal
+- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request {
+ NSURLRequest *donor = donorRequest(request);
+ return %orig(donor ?: request);
+}
+%end
+%end
+
+#pragma mark - the reply
+
+// What the hook knows of one lyrics task it answers. The delegate hears of a task on Spotify's own
+// queue and of the answer on the main queue, so everything here is read and written under its lock.
+@interface SGLyricsTask : NSObject
+@property (nonatomic, copy) NSString *track;
+@property (nonatomic) BOOL scroll; // a card list, not lyrics: read in whole and given its section
+@property (nonatomic) BOOL donor; // the request went to the donor; the 200 is not the track's
+@property (nonatomic) BOOL donorColors; // and the server coloured it from the track's own artwork
+@property (nonatomic) BOOL held; // Spotify's server said no; the reply is the chain's
+@property (nonatomic) BOOL answered; // the 200 and the lyrics went out
+@property (nonatomic) BOOL gone; // the task ended before the lyrics were in
+@property (nonatomic, strong) NSHTTPURLResponse *fake; // the 200 the task itself answers with
+@property (nonatomic, strong) NSMutableData *buffer; // Spotify's own 200, read in whole
+@property (nonatomic, strong) NSData *ours; // the body handed on, let through by identity
+@end
+
+@implementation SGLyricsTask
+@end
+
+static char kTaskKey;
+
+static SGLyricsTask *stateOf(NSURLSessionTask *task) {
+ return objc_getAssociatedObject(task, &kTaskKey);
}
-+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
- return request;
+// A held task's own -response answers with the 200 the delegate was told of. The class that
+// implements -response is found from the first held task, so every task of it goes through here;
+// one not held answers as before.
+static IMP sg_origResponse;
+
+static id ownResponse(id self, SEL _cmd) {
+ SGLyricsTask *state = stateOf(self);
+ NSHTTPURLResponse *fake = nil;
+ if (state) {
+ @synchronized (state) { fake = state.fake; }
+ }
+ return fake ?: ((id (*)(id, SEL))sg_origResponse)(self, _cmd);
}
-+ (NSURLSession *)innerSession {
- static NSURLSession *session;
+static void answerResponseOf(NSURLSessionTask *task) {
static dispatch_once_t once;
dispatch_once(&once, ^{
- NSURLSessionConfiguration *configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration;
- configuration.URLCache = nil;
- session = [NSURLSession sessionWithConfiguration:configuration];
+ SEL selector = @selector(response);
+ Class owner = object_getClass(task);
+ while (owner) {
+ unsigned count = 0;
+ Method *methods = class_copyMethodList(owner, &count);
+ BOOL found = NO;
+ for (unsigned i = 0; i < count && !found; i++) found = method_getName(methods[i]) == selector;
+ free(methods);
+ if (found) break;
+ owner = class_getSuperclass(owner);
+ }
+ Method method = class_getInstanceMethod(owner ?: object_getClass(task), selector);
+ if (!method) return;
+ sg_origResponse = method_setImplementation(method, (IMP)ownResponse);
+ SGLog(@"lyrics: -response of %@ answers for held tasks", NSStringFromClass(owner ?: object_getClass(task)));
});
- return session;
}
-// The client is told things only on the thread that started the load, in its run loop mode.
-- (void)onLoadingThread:(void (^)(void))block {
- [self performSelector:@selector(runOnLoadingThread:) onThread:_thread withObject:[block copy] waitUntilDone:NO modes:_modes];
+// The body goes to the delegate as any data would, through every hook on the way, and the hook here
+// knows it by the very object it handed over.
+static void handOn(id delegate, NSURLSession *session, NSURLSessionTask *task, SGLyricsTask *state, NSData *body) {
+ @synchronized (state) { state.ours = body; }
+ [delegate URLSession:session dataTask:(NSURLSessionDataTask *)task didReceiveData:body];
}
-- (void)runOnLoadingThread:(void (^)(void))block {
- if (!_stopped) block();
+static NSHTTPURLResponse *okFor(NSURL *url, NSData *body) {
+ return [[NSHTTPURLResponse alloc] initWithURL:url statusCode:200 HTTPVersion:@"HTTP/2.0"
+ headerFields:@{@"Content-Type": @"application/protobuf", @"Content-Length": @(body.length).stringValue}];
}
-- (void)replyWith:(NSURLResponse *)response data:(NSData *)data {
- [self onLoadingThread:^{
- [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
- if (data.length) [self.client URLProtocol:self didLoadData:data];
- [self.client URLProtocolDidFinishLoading:self];
- }];
+static NSError *noLyricsError(void) {
+ return [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled
+ userInfo:@{NSLocalizedDescriptionKey: @"no lyrics for this track from any source"}];
}
-- (void)startLoading {
- _thread = NSThread.currentThread;
- NSString *mode = NSRunLoop.currentRunLoop.currentMode;
- _modes = mode && ![mode isEqualToString:NSDefaultRunLoopMode] ? @[NSDefaultRunLoopMode, mode] : @[NSDefaultRunLoopMode];
+typedef void (^SGDisposition)(NSURLSessionResponseDisposition);
- NSMutableURLRequest *request = [self.request mutableCopy];
- [NSURLProtocol setProperty:@YES forKey:kHandledKey inRequest:request];
- @synchronized (sg_sessionHeaders) {
- [sg_sessionHeaders enumerateKeysAndObjectsUsingBlock:^(NSString *name, NSString *value, BOOL *stop) {
- if (![request valueForHTTPHeaderField:name]) [request setValue:value forHTTPHeaderField:name];
- }];
+// The response callback. `orig` hands a response and a completion handler on to Spotify's delegate.
+static void onResponse(id delegate, NSURLSession *session, NSURLSessionDataTask *task, NSURLResponse *response,
+ SGDisposition handler, void (^orig)(NSURLResponse *, SGDisposition)) {
+ if (stateOf(task)) {
+ orig(response, handler);
+ return;
}
- NSString *track = lyricsTrack(request.URL);
- _inner = [[SGLyricsURLProtocol innerSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
- if (error) {
- [self onLoadingThread:^{ [self.client URLProtocol:self didFailWithError:error]; }];
- return;
- }
- NSInteger status = statusOf(response);
- BOOL json = data.length && ((const uint8_t *)data.bytes)[0] == '{';
- SGLog(@"lyrics: Spotify answered %ld with %lu bytes for %@", (long)status, (unsigned long)data.length, track);
- if ((status != 200 && status < 400) || (status == 200 && json)) {
- [self replyWith:response data:data];
- return;
+ NSString *scroll = scrollTrackOf(task);
+ if (scroll) {
+ if (statusOf(response) == 200) {
+ SGLyricsTask *state = [SGLyricsTask new];
+ state.track = scroll;
+ state.scroll = YES;
+ state.buffer = [NSMutableData data];
+ objc_setAssociatedObject(task, &kTaskKey, state, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
+ orig(response, handler);
+ return;
+ }
+ NSString *track = trackOf(task);
+ if (!track) {
+ orig(response, handler);
+ return;
+ }
+ SGLyricsTask *state = [SGLyricsTask new];
+ state.track = track;
+ state.donor = sentToDonor(task);
+ state.donorColors = state.donor && [task.currentRequest.URL.path containsString:@"/image/"];
+ objc_setAssociatedObject(task, &kTaskKey, state, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ NSInteger status = statusOf(response);
+ SGLog(@"lyrics: Spotify answered %ld for %@%@", (long)status, track, state.donor ? @" through the donor" : @"");
+ if (status == 200) {
+ @synchronized (state) { state.buffer = [NSMutableData data]; }
+ orig(response, handler);
+ return;
+ }
+ @synchronized (state) { state.held = YES; }
+ dispatch_async(dispatch_get_main_queue(), ^{
SGLyricsFetch(track, ^(SGLyricsResult *lyrics) {
- NSData *body = chosenBody(track, lyrics, status == 200 ? data : nil);
+ @synchronized (state) {
+ if (state.gone) return;
+ }
+ NSData *body = chosenBody(track, lyrics, nil, NO, NO);
if (!body) {
- [self replyWith:response data:data];
+ @synchronized (state) { state.held = NO; }
+ orig(response, handler);
return;
}
- NSHTTPURLResponse *ok = [[NSHTTPURLResponse alloc] initWithURL:request.URL statusCode:200 HTTPVersion:@"HTTP/1.1"
- headerFields:@{@"Content-Type": @"application/protobuf", @"Content-Length": @(body.length).stringValue}];
- [self replyWith:ok data:body];
+ // Spotify decides on the 200 with a handler of the hook's; the real one is only called once
+ // the lyrics are with the delegate, so the 404 body the server still sends comes after them
+ // and is dropped.
+ __block BOOL decided = NO, delivered = NO;
+ __block NSURLSessionResponseDisposition disposition = NSURLSessionResponseAllow;
+ SGDisposition decide = ^(NSURLSessionResponseDisposition chosen) {
+ BOOL now;
+ @synchronized (state) {
+ decided = YES;
+ disposition = chosen;
+ now = delivered;
+ }
+ if (now) handler(chosen);
+ };
+ NSHTTPURLResponse *ok = okFor(task.currentRequest.URL, body);
+ answerResponseOf(task);
+ @synchronized (state) {
+ state.fake = ok;
+ state.answered = YES;
+ }
+ orig(ok, decide);
+ handOn(delegate, session, task, state, body);
+ BOOL now;
+ @synchronized (state) {
+ delivered = YES;
+ now = decided;
+ }
+ if (now) handler(disposition);
});
- }];
- [_inner resume];
+ });
}
-- (void)stopLoading {
- _stopped = YES;
- [_inner cancel];
+// The data callback: YES when the data goes on to Spotify's delegate.
+static BOOL onData(NSURLSessionTask *task, NSData *data) {
+ SGLyricsTask *state = stateOf(task);
+ if (!state) return YES;
+ @synchronized (state) {
+ if (data == state.ours) {
+ state.ours = nil;
+ return YES;
+ }
+ if (state.held && state.answered) return NO; // Spotify's own 404 body, after the chain's lyrics
+ if (state.buffer) {
+ [state.buffer appendData:data];
+ return NO;
+ }
+ }
+ return YES;
}
-@end
-
-%hook NSURLSession
-+ (NSURLSession *)sessionWithConfiguration:(NSURLSessionConfiguration *)configuration delegate:(id)delegate delegateQueue:(NSOperationQueue *)queue {
- if (!configuration) return %orig;
- NSURLSessionConfiguration *withProtocol = [configuration copy];
- NSArray *classes = withProtocol.protocolClasses ?: @[];
- if (![classes containsObject:SGLyricsURLProtocol.class]) withProtocol.protocolClasses = [@[SGLyricsURLProtocol.class] arrayByAddingObjectsFromArray:classes];
- NSString *owner = delegate ? NSStringFromClass([delegate class]) : @"";
- if (configuration.HTTPAdditionalHeaders.count && ([owner isEqualToString:@"SPTDataLoaderService"] || [owner containsString:@"HttpClientURLSession"])) {
- @synchronized (sg_sessionHeaders) {
- [configuration.HTTPAdditionalHeaders enumerateKeysAndObjectsUsingBlock:^(id name, id value, BOOL *stop) {
- if ([name isKindOfClass:NSString.class] && [value isKindOfClass:NSString.class]) sg_sessionHeaders[name] = value;
- }];
+// The completion callback. `orig` ends the task at Spotify's delegate.
+static void onComplete(id delegate, NSURLSession *session, NSURLSessionTask *task, NSError *error, void (^orig)(NSError *)) {
+ SGLyricsTask *state = stateOf(task);
+ NSData *spotify = nil;
+ if (state) {
+ @synchronized (state) {
+ if (state.held && !state.answered) state.gone = YES;
+ if (state.buffer && !error) spotify = [state.buffer copy];
+ state.buffer = nil;
}
}
- return %orig(withProtocol, delegate, queue);
+ if (!spotify) {
+ orig(error);
+ return;
+ }
+ NSString *track = state.track;
+ if (state.scroll) {
+ handOn(delegate, session, task, state, listWithLyrics(spotify, track) ?: spotify);
+ orig(nil);
+ return;
+ }
+ BOOL json = spotify.length && ((const uint8_t *)spotify.bytes)[0] == '{';
+ dispatch_async(dispatch_get_main_queue(), ^{
+ void (^finish)(NSData *) = ^(NSData *body) {
+ handOn(delegate, session, task, state, body);
+ orig(nil);
+ };
+ if (json && !state.donor) {
+ finish(spotify);
+ return;
+ }
+ SGLyricsFetch(track, ^(SGLyricsResult *lyrics) {
+ NSData *body = chosenBody(track, lyrics, spotify, state.donor, state.donorColors);
+ if (body) {
+ finish(body);
+ return;
+ }
+ if (!state.donor) {
+ finish(spotify);
+ return;
+ }
+ // Nothing from anyone: the donor's lines must not show for the track, so the request
+ // fails as Spotify's own would have.
+ SGLog(@"lyrics: no lyrics for %@ from any source, its request fails as Spotify's did", track);
+ orig(noLyricsError());
+ });
+ });
+}
+
+%group Delegates
+%hook SPTDataLoaderService
+- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)task didReceiveResponse:(NSURLResponse *)response completionHandler:(SGDisposition)handler {
+ onResponse(self, session, task, response, handler, ^(NSURLResponse *passed, SGDisposition decide) { %orig(session, task, passed, decide); });
}
+- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)task didReceiveData:(NSData *)data {
+ if (onData(task, data)) %orig;
+}
+- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
+ onComplete(self, session, task, error, ^(NSError *passed) { %orig(session, task, passed); });
+}
+%end
+
+%hook _TtC26Connectivity_HttpClientKit20HttpClientURLSession
+- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)task didReceiveResponse:(NSURLResponse *)response completionHandler:(SGDisposition)handler {
+ onResponse(self, session, task, response, handler, ^(NSURLResponse *passed, SGDisposition decide) { %orig(session, task, passed, decide); });
+}
+- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)task didReceiveData:(NSData *)data {
+ if (onData(task, data)) %orig;
+}
+- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
+ onComplete(self, session, task, error, ^(NSError *passed) { %orig(session, task, passed); });
+}
+%end
%end
-// The player offers the lyrics card by the track's has_lyrics.
+#pragma mark - the track
+
+// The player offers the lyrics card by the track's has_lyrics. What Spotify's metadata said is
+// noted first, since it is what sends the request to the donor, and the track is remembered so the
+// chain has its name before the player reports it.
%group AllTracks
%hook SPTPlayerTrack
- (NSDictionary *)metadata {
NSDictionary *metadata = %orig;
- if ([metadata[@"has_lyrics"] isEqual:@"true"]) return metadata;
+ BOOL has = [metadata[@"has_lyrics"] isEqual:@"true"];
id uri = self.URI;
NSString *text = [uri isKindOfClass:NSURL.class] ? [(NSURL *)uri absoluteString] : [uri description];
- if (![text hasPrefix:@"spotify:track:"] || !SGLyricsMayHave([text substringFromIndex:@"spotify:track:".length])) return metadata;
+ if (![text hasPrefix:@"spotify:track:"]) return metadata;
+ NSString *track = [text substringFromIndex:@"spotify:track:".length];
+ SGLyricsNoteSpotifyHas(track, has);
+ SGKaraokeRememberTrack(self);
+ if (has || !SGLyricsMayHave(track)) return metadata;
static dispatch_once_t once;
dispatch_once(&once, ^{ SGLog(@"lyrics: marking tracks without Spotify lyrics as having some, first %@", text); });
NSMutableDictionary *marked = [metadata mutableCopy] ?: [NSMutableDictionary dictionary];
@@ -191,10 +491,17 @@ static NSData *chosenBody(NSString *track, SGLyricsResult *lyrics, NSData *spoti
%ctor {
SGLyricsMigrateLegacyKeys();
if (!SGLyricsEnabled()) return;
- sg_sessionHeaders = [NSMutableDictionary dictionary];
- %init;
+ sg_templateLock = [NSObject new];
+ %init(Delegates);
BOOL allTracks = SGFlag(SGKeyLyricsAllTracks, NO);
- if (allTracks) %init(AllTracks);
+ if (allTracks) {
+ %init(AllTracks);
+ %init(Sessions);
+ SEL selector = @selector(dataTaskWithRequest:);
+ Class local = objc_getClass("__NSURLSessionLocal");
+ Method own = local ? class_getInstanceMethod(local, selector) : NULL;
+ if (own && own != class_getInstanceMethod(NSURLSession.class, selector)) %init(LocalSessions);
+ }
SGLog(@"lyrics: sources %@, every track %@", [SGLyricsOrder() componentsJoinedByString:@", "], allTracks ? @"on" : @"off");
- SGRequireClasses(@[@"SPTPlayerTrack"]);
+ SGRequireClasses(@[@"SPTPlayerTrack", @"SPTDataLoaderService", @"_TtC26Connectivity_HttpClientKit20HttpClientURLSession"]);
}
diff --git a/tweak/Sources/Features/LyricsSources/LyricsSources.h b/tweak/Sources/Features/LyricsSources/LyricsSources.h
index 7c8b6c3a..3aa44b2e 100644
--- a/tweak/Sources/Features/LyricsSources/LyricsSources.h
+++ b/tweak/Sources/Features/LyricsSources/LyricsSources.h
@@ -52,6 +52,9 @@ typedef void (^SGLyricsAsk)(SGLyricsQuery *query, void (^done)(SGLyricsResult *r
@property (nonatomic, copy) NSString *key; // stored in the order, never shown
@property (nonatomic, copy) NSString *name; // "BiniLyrics", what the credit reads
@property (nonatomic, copy) NSString *detail; // one line under the name on the Lyrics page
+// Searches by title and artist, so it has nothing to ask with until someone has named the track.
+// Only Musixmatch matches by Spotify's id and can go without.
+@property (nonatomic) BOOL needsName;
@property (nonatomic, copy) SGLyricsAsk ask;
@end
@@ -68,6 +71,20 @@ BOOL SGLyricsEnabled(void); // any source at all is on
void SGLyricsFetch(NSString *trackID, void (^done)(SGLyricsResult *result));
// NO once every source has said it has nothing for the track; safe from any thread.
BOOL SGLyricsMayHave(NSString *trackID);
+// Starts the walk for a track before anyone has asked, so the answer is in when Spotify's request
+// comes; a walk already run or running is left alone. Safe from any thread.
+void SGLyricsPrefetch(NSString *trackID);
+// What Spotify's own metadata says of a track: 1 has lyrics, 0 has none, -1 not seen yet. The
+// player-track hook notes it; the request hook reads it to send a track Spotify has none for to
+// the donor. Safe from any thread.
+NSInteger SGLyricsSpotifyHas(NSString *trackID);
+void SGLyricsNoteSpotifyHas(NSString *trackID, BOOL has);
+// The remote-config values the lyrics feature forces while a source is on, nil for any other flag:
+// the player gives its cards this long to load before it shows the list without the slow ones, and
+// a source of the mod's can take longer than Spotify's default to answer.
+id SGLyricsForcedFlag(NSString *key);
+// Set on the requests the mod sends to spclient itself, so the request hook leaves them alone.
+extern NSString *const SGLyricsOwnRequestKey;
// The name of the source the lines shown for the track came from, nil until they arrive.
NSString *SGLyricsCreditFor(NSString *trackID);
void SGLyricsSetCredit(NSString *trackID, NSString *name);
diff --git a/tweak/Sources/Features/LyricsSources/LyricsSources.m b/tweak/Sources/Features/LyricsSources/LyricsSources.m
index 73a1ca97..028de9c3 100644
--- a/tweak/Sources/Features/LyricsSources/LyricsSources.m
+++ b/tweak/Sources/Features/LyricsSources/LyricsSources.m
@@ -1,12 +1,12 @@
-// The chain: every source in the order the Lyrics page puts them in, asked one after another until
-// there is nothing better left to learn.
+// The chain: every source in the order the Lyrics page puts them in, asked one after another, the
+// first with timed lyrics answering for the track.
//
-// It keeps the best answer rather than the first, because the two halves of an answer come apart.
-// A source can have the words of a song and no timing (Musixmatch, for a track it may only show as
-// plain text), or the timing of every word and no text worth putting on Spotify's own page
-// (NetEase). So the lines shown on the karaoke page and the lines handed to Spotify are chosen
-// separately, and the walk stops once both are as good as they can get: every word timed, and text
-// for the page.
+// Stopping at the first answer is what makes the lyrics card under the player appear at all. For a
+// track Spotify has no lyrics of its own for, the card is only offered when the answer to Spotify's
+// lyrics request arrives within about a second of the player building its list of cards; a walk that
+// went on through every source for word timing took three seconds and more, and the card never came.
+// So the order is the priority, as it reads on the Lyrics page: a source further down is only asked
+// when those above it had nothing timed, and then only for what they lacked.
#import "Core/SGCore.h"
#import "LyricsSources.h"
#import "Features/Karaoke/Karaoke.h"
@@ -109,6 +109,7 @@ NSArray<SGLyricsProvider *> *SGLyricsAllProviders(void) {
provider.key = key;
provider.name = name;
provider.detail = detail;
+ provider.needsName = ![key isEqualToString:@"musixmatch"];
provider.ask = ask;
return provider;
};
@@ -159,14 +160,16 @@ BOOL SGLyricsEnabled(void) {
#pragma mark - what is known about the track
-// The player knows the track it is playing by name, which is what every source but Musixmatch
-// searches by. A track that is not the one playing — a page opened for something else — starts with
-// nothing, and the first source that matches by id fills the rest in.
+// The player knows every track it has played by name, which is what every source but Musixmatch
+// searches by. The track is looked up by id rather than compared with the one playing now: a lyrics
+// request often lands a beat before the player moves on to its track, and comparing then left the
+// query nameless. A track the player has not reported starts with nothing, and the first source
+// that matches by id fills the rest in.
static SGLyricsQuery *queryFor(NSString *trackID) {
SGLyricsQuery *query = [SGLyricsQuery new];
query.trackID = trackID;
- if (![trackID isEqualToString:SGKaraokePlayingTrack()]) return query;
- SPTPlayerTrack *track = [(id<SPTPlayer>)SGKaraokePlayer() state].track;
+ SPTPlayerTrack *track = SGKaraokeTrackFor(trackID);
+ if (!track) return query;
query.title = track.trackTitle;
query.artist = track.artistName;
NSDictionary<NSString *, NSString *> *metadata = track.metadata;
@@ -191,6 +194,10 @@ static NSMutableDictionary<NSString *, id> *sg_kept;
static NSMutableDictionary<NSString *, NSMutableArray *> *sg_waiting;
static NSMutableSet<NSString *> *sg_missing;
static NSMutableDictionary<NSString *, NSString *> *sg_credits;
+// Spotify's own has_lyrics per track, as its metadata said. The player's metadata is read many
+// times a second while a list scrolls, so a value already noted costs one lookup and no write.
+static NSMutableDictionary<NSString *, NSNumber *> *sg_spotifyHas;
+static const NSUInteger kNotedTracks = 200;
static void setUp(void) {
static dispatch_once_t once;
@@ -199,6 +206,7 @@ static void setUp(void) {
sg_waiting = [NSMutableDictionary dictionary];
sg_missing = [NSMutableSet set];
sg_credits = [NSMutableDictionary dictionary];
+ sg_spotifyHas = [NSMutableDictionary dictionary];
});
}
@@ -215,34 +223,92 @@ static BOOL betterTexts(SGLyricsResult *merged, SGLyricsResult *fresh) {
return !merged.texts.count || (fresh.synced && !merged.synced);
}
-static void finish(NSString *trackID, SGLyricsResult *merged) {
+// How long a lyrics request waits for the player to name its track before the walk starts without a
+// name. The request routinely lands a few hundred milliseconds before the player reports the track
+// it belongs to, and a walk started in that gap passes over every source that searches by name.
+static const NSTimeInterval kNameWait = 1.5, kNamePoll = 0.1;
+
+static BOOL named(SGLyricsQuery *query) {
+ return query.title.length && query.artist.length;
+}
+
+// One walk down the order for one track.
+@interface SGLyricsWalk : NSObject
+@property (nonatomic, copy) NSArray<NSString *> *order;
+@property (nonatomic) NSUInteger index;
+@property (nonatomic, strong) SGLyricsQuery *query;
+@property (nonatomic, strong) SGLyricsResult *merged;
+// Sources that needed a name the query did not have when their turn came.
+@property (nonatomic, strong) NSMutableArray<NSString *> *passedOver;
+@end
+
+@implementation SGLyricsWalk
+@end
+
+// A walk that ends with nothing is only an answer when every source got to search. One that passed a
+// source over for want of a name asked it nothing, and keeping that as "no lyrics" would stick to the
+// track: every later request would get the kept nil, and the lyrics card would be taken off the track
+// for the rest of the session.
+static void finish(SGLyricsWalk *walk) {
+ SGLyricsQuery *query = walk.query;
+ SGLyricsResult *merged = walk.merged;
+ NSString *trackID = query.trackID;
SGLyricsResult *lyrics = merged.karaokeLines.count || merged.texts.count ? merged : nil;
- if (sg_kept.count >= kKeptTracks) [sg_kept removeAllObjects];
- sg_kept[trackID] = lyrics ?: NSNull.null;
- if (!lyrics) {
- @synchronized (sg_missing) { [sg_missing addObject:trackID]; }
+ BOOL everyoneAsked = !walk.passedOver.count;
+ if (lyrics || everyoneAsked || merged.instrumental) {
+ if (sg_kept.count >= kKeptTracks) [sg_kept removeAllObjects];
+ sg_kept[trackID] = lyrics ?: NSNull.null;
+ if (!lyrics) {
+ @synchronized (sg_missing) { [sg_missing addObject:trackID]; }
+ }
}
- SGLog(@"lyrics: %@ ends with %@", trackID, !lyrics ? @"nothing"
- : [NSString stringWithFormat:@"%lu %@ lines from %@, %lu page lines",
+ SGLog(@"lyrics: %@ ends with %@", trackID, lyrics
+ ? [NSString stringWithFormat:@"%lu %@ lines from %@, %lu page lines",
(unsigned long)lyrics.karaokeLines.count, lyrics.wordTimed ? @"word timed" : @"estimated",
- lyrics.provider, (unsigned long)lyrics.texts.count]);
+ lyrics.provider, (unsigned long)lyrics.texts.count]
+ : everyoneAsked ? @"nothing"
+ : [NSString stringWithFormat:@"nothing, %@ never knowing its name; not kept, so the next request asks again",
+ [walk.passedOver componentsJoinedByString:@", "]]);
NSArray *waiting = sg_waiting[trackID];
[sg_waiting removeObjectForKey:trackID];
for (void (^done)(SGLyricsResult *) in waiting) done(lyrics);
}
-static void askFrom(NSUInteger index, NSArray<NSString *> *order, SGLyricsQuery *query, SGLyricsResult *merged) {
- // Nothing left to gain: every word is timed and Spotify's page has its text.
- if (index >= order.count || (merged.wordTimed && merged.texts.count)) {
- finish(query.trackID, merged);
+static void step(SGLyricsWalk *walk) {
+ SGLyricsQuery *query = walk.query;
+ SGLyricsResult *merged = walk.merged;
+ // A source higher in the order has answered with timed lyrics: that is the answer.
+ if (merged.synced && merged.texts.count && merged.karaokeLines.count) {
+ finish(walk);
+ return;
+ }
+ if (walk.index >= walk.order.count) {
+ // A source that matches by id named the track partway down: the ones passed over ask now,
+ // in the order they came in. Once, since they cannot be passed over again with a name.
+ if (walk.passedOver.count && named(query)) {
+ SGLog(@"lyrics: %@ named partway as \"%@\" by \"%@\", asking %@ after all", query.trackID,
+ query.title, query.artist, [walk.passedOver componentsJoinedByString:@", "]);
+ walk.order = walk.passedOver;
+ walk.index = 0;
+ walk.passedOver = [NSMutableArray array];
+ step(walk);
+ return;
+ }
+ finish(walk);
+ return;
+ }
+ SGLyricsProvider *provider = SGLyricsProviderFor(walk.order[walk.index++]);
+ if (provider.needsName && !named(query)) {
+ [walk.passedOver addObject:provider.key];
+ step(walk);
return;
}
- SGLyricsProvider *provider = SGLyricsProviderFor(order[index]);
provider.ask(query, ^(SGLyricsResult *fresh) {
learnFrom(query, fresh);
if (fresh.instrumental) {
SGLog(@"lyrics: %@ is instrumental, by %@", query.trackID, provider.key);
- finish(query.trackID, merged);
+ merged.instrumental = YES;
+ finish(walk);
return;
}
if (betterLines(merged, fresh)) {
@@ -256,7 +322,32 @@ static void askFrom(NSUInteger index, NSArray<NSString *> *order, SGLyricsQuery
merged.synced = fresh.synced;
if (!merged.provider) merged.provider = provider.name;
}
- askFrom(index + 1, order, query, merged);
+ step(walk);
+ });
+}
+
+static void startWalk(NSString *trackID, SGLyricsQuery *query) {
+ SGLog(@"lyrics: asking %@ for %@ as \"%@\" by \"%@\", album \"%@\", %lds",
+ [SGLyricsOrder() componentsJoinedByString:@", "], trackID, query.title, query.artist, query.album, (long)query.seconds);
+ SGLyricsWalk *walk = [SGLyricsWalk new];
+ walk.order = SGLyricsOrder();
+ walk.query = query;
+ walk.merged = [SGLyricsResult new];
+ walk.passedOver = [NSMutableArray array];
+ step(walk);
+}
+
+// Starts the walk as soon as the player has named the track, or once it has waited long enough
+// that it is not going to: a track opened for something other than what is playing is never named.
+static void whenNamed(NSString *trackID, NSTimeInterval waited) {
+ SGLyricsQuery *query = queryFor(trackID);
+ if (named(query) || waited >= kNameWait) {
+ if (!named(query)) SGLog(@"lyrics: the player never named %@ in %.1fs", trackID, waited);
+ startWalk(trackID, query);
+ return;
+ }
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kNamePoll * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+ whenNamed(trackID, waited + kNamePoll);
});
}
@@ -278,7 +369,7 @@ void SGLyricsFetch(NSString *trackID, void (^done)(SGLyricsResult *result)) {
return;
}
sg_waiting[trackID] = [NSMutableArray arrayWithObject:[done copy]];
- askFrom(0, SGLyricsOrder(), queryFor(trackID), [SGLyricsResult new]);
+ whenNamed(trackID, 0);
});
}
@@ -287,6 +378,47 @@ BOOL SGLyricsMayHave(NSString *trackID) {
@synchronized (sg_missing) { return ![sg_missing containsObject:trackID]; }
}
+void SGLyricsPrefetch(NSString *trackID) {
+ if (!trackID.length) return;
+ SGLyricsFetch(trackID, ^(SGLyricsResult *result) {});
+}
+
+NSInteger SGLyricsSpotifyHas(NSString *trackID) {
+ setUp();
+ if (!trackID) return -1;
+ @synchronized (sg_spotifyHas) {
+ NSNumber *has = sg_spotifyHas[trackID];
+ return has ? has.integerValue : -1;
+ }
+}
+
+void SGLyricsNoteSpotifyHas(NSString *trackID, BOOL has) {
+ setUp();
+ if (!trackID.length) return;
+ @synchronized (sg_spotifyHas) {
+ NSNumber *noted = sg_spotifyHas[trackID];
+ if (noted && noted.boolValue == has) return;
+ if (sg_spotifyHas.count >= kNotedTracks) [sg_spotifyHas removeAllObjects];
+ sg_spotifyHas[trackID] = @(has);
+ }
+}
+
+NSString *const SGLyricsOwnRequestKey = @"spotifyglass.ownRequest";
+
+// The cards under the player load together, and the list is shown without any card still loading
+// once this many milliseconds have passed (NowPlaying_ScrollImpl's scrollCardsAsyncLoadingTimeoutMs,
+// 2 s unless the server says otherwise, 1 s at the least). The lyrics card is one of them and waits
+// for the color-lyrics reply, which with a source of the mod's on comes after the chain has answered;
+// so the wait is set to the most the flag allows.
+id SGLyricsForcedFlag(NSString *key) {
+ static BOOL on;
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{ on = SGLyricsEnabled(); });
+ if (!on) return nil;
+ if ([key isEqualToString:@"ios-nowplaying-scroll-impl.scroll_cards_async_loading_timeout_ms"]) return @5000;
+ return nil;
+}
+
NSString *SGLyricsCreditFor(NSString *trackID) {
setUp();
@synchronized (sg_credits) { return trackID ? sg_credits[trackID] : nil; }
diff --git a/tweak/Sources/Headers/SPTPlayer.h b/tweak/Sources/Headers/SPTPlayer.h
index 537d5477..e8fd214a 100644
--- a/tweak/Sources/Headers/SPTPlayer.h
+++ b/tweak/Sources/Headers/SPTPlayer.h
@@ -17,6 +17,9 @@
// Seconds. position runs on from positionAsOfTimestamp by the time elapsed since the state was made.
@property (nonatomic, readonly) double position;
@property (nonatomic, readonly) double positionAsOfTimestamp;
+// The tracks to come and the ones played, nearest first; SPTPlayerTrack each, read with a type check.
+@property (nonatomic, readonly) NSArray *future;
+@property (nonatomic, readonly) NSArray *reverse;
@end
@protocol SPTPlayer <NSObject>