Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
refactor: the Live Activity queue, lyrics cache and retry, protobuf length check and telemetry rules are rewritten
Vojtěch Škopekfee72889ebfc
diff --git a/tweak/Sources/Shared/LiveActivity/LiveActivityBridge.swift b/tweak/Sources/Shared/LiveActivity/LiveActivityBridge.swiftindex ce7c982c..3ed7ac0c 100644--- a/tweak/Sources/Shared/LiveActivity/LiveActivityBridge.swift+++ b/tweak/Sources/Shared/LiveActivity/LiveActivityBridge.swift@@ -14,16 +14,50 @@ public final class SGLiveActivityBridge: NSObject {@objc public static var isShowing: Bool { current != nil }- // Updates and the end run one after another, in the order they were asked for: a Task apiece could- // land out of order, and a state one tick old would then be the one the activity is left showing.- // Only touched from the main thread, where the tweak's timer runs.- private static var pending: Task<Void, Never>?+ // Updates and ends go out one at a time in the order asked for, since ActivityKit applies+ // concurrent ones in whatever order they finish. An update still waiting gives way to a newer one.+ private enum Change: Sendable {+ case update(String, ActivityContent<SGLyricsAttributes.ContentState>)+ case end([String])+ }++ private struct Pending: Sendable {+ var changes: [Change] = []+ var sending = false+ }++ private static let pending = OSAllocatedUnfairLock(initialState: Pending())- private static func enqueue(_ work: @escaping @Sendable () async -> Void) {- let before = pending- pending = Task {- await before?.value- await work()+ private static func queue(_ change: Change) {+ let start = pending.withLock { pending in+ if case .update(let id, _) = change, case .update(let waiting, _)? = pending.changes.last, waiting == id {+ pending.changes.removeLast()+ }+ pending.changes.append(change)+ if pending.sending { return false }+ pending.sending = true+ return true+ }+ if start { Task { await send() } }+ }++ private static func send() async {+ while let change = pending.withLock({ pending -> Change? in+ if pending.changes.isEmpty {+ pending.sending = false+ return nil+ }+ return pending.changes.removeFirst()+ }) {+ let activities = Activity<SGLyricsAttributes>.activities+ switch change {+ case .update(let id, let content):+ await activities.first { $0.id == id }?.update(content)+ case .end(let ids):+ for activity in activities where ids.contains(activity.id) {+ await activity.end(nil, dismissalPolicy: .immediate)+ }+ }}}@@ -44,7 +78,7 @@ public final class SGLiveActivityBridge: NSObject {shuffle: shuffle, repeatMode: repeatMode, timerEnd: timerEnd, timerEndOfTrack: timerEndOfTrack)let content = ActivityContent(state: state, staleDate: nil)if let activity = current {- enqueue { await activity.update(content) }+ queue(.update(activity.id, content))return}guard ActivityAuthorizationInfo().areActivitiesEnabled else {@@ -59,9 +93,8 @@ public final class SGLiveActivityBridge: NSObject {}}+ // Named now, so an activity requested after this call is not ended with them.@objc public static func end() {- for activity in Activity<SGLyricsAttributes>.activities {- enqueue { await activity.end(nil, dismissalPolicy: .immediate) }- }+ queue(.end(Activity<SGLyricsAttributes>.activities.map(\.id)))}}diff --git a/tweak/Sources/Shared/Lyrics/KaraokeSource.x b/tweak/Sources/Shared/Lyrics/KaraokeSource.xindex 153589de..21c3db9d 100644--- a/tweak/Sources/Shared/Lyrics/KaraokeSource.x+++ b/tweak/Sources/Shared/Lyrics/KaraokeSource.x@@ -12,13 +12,18 @@static const NSUInteger kKeptTracks = 40;static const NSUInteger kSeenTracks = 200;-// Seconds before a lyrics request that failed is asked again.-static const NSTimeInterval kRetryAfter = 15;+// A track whose request was lost may be asked for again after this long, twice as long after each loss+// in a row up to the most: soon enough for the song still playing, not so soon that a busy server is pressed.+static const NSTimeInterval kRetryPause = 10, kRetryPauseMost = 60;// 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"};static NSMutableDictionary<NSString *, NSArray<SGKaraokeLine *> *> *sg_lyrics;static NSMutableSet<NSString *> *sg_requested;+// Of those, the ones with a request still out or waiting out its pause. A full cache spares their lines+// and leaves them asked for, so no second request runs beside the first.+static NSMutableSet<NSString *> *sg_asking;+static NSMutableDictionary<NSString *, NSNumber *> *sg_losses; // requests lost in a row, by trackstatic 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@@ -57,16 +62,29 @@ static void rememberHeaders(NSURLSession *session, NSURLRequest *request) {dispatch_async(dispatch_get_main_queue(), ^{ sg_spclientHeaders = headers; });}-// Main queue only. A full cache lets go of all but the track playing, which the page and the lock screen-// are reading. What goes is also forgotten as requested: kept there it could never be asked for again, and-// a track whose lyrics were dropped would stay without them until Spotify restarted.+static SPTPlayerState *playerState(void);+static NSString *idOf(SPTPlayerTrack *track);++// The track after this one, when the player knows it.+static SPTPlayerTrack *upNextIn(SPTPlayerState *state) {+ id future = [state respondsToSelector:@selector(future)] ? state.future : nil;+ id next = [future isKindOfClass:NSArray.class] ? [(NSArray *)future firstObject] : nil;+ return [next isKindOfClass:objc_getClass("SPTPlayerTrack")] ? next : nil;+}++// Main queue only. A full cache is emptied but for the track playing, the one up next and those still+// being asked for; what goes can be asked for again.static void keep(NSString *track, NSArray<SGKaraokeLine *> *lines) {if (sg_lyrics.count >= kKeptTracks && !sg_lyrics[track]) {- NSString *playing = SGKaraokePlayingTrack();- for (NSString *key in sg_lyrics.allKeys) {- if ([key isEqualToString:playing]) continue;- [sg_lyrics removeObjectForKey:key];- [sg_requested removeObject:key];+ NSMutableSet<NSString *> *spared = [sg_asking mutableCopy];+ SPTPlayerState *state = playerState();+ NSString *playing = idOf(state.track), *next = idOf(upNextIn(state));+ if (playing) [spared addObject:playing];+ if (next) [spared addObject:next];+ for (NSString *kept in sg_lyrics.allKeys) {+ if ([spared containsObject:kept]) continue;+ [sg_lyrics removeObjectForKey:kept];+ [sg_requested removeObject:kept];}}sg_lyrics[track] = lines;@@ -103,10 +121,24 @@ NSArray<SGKaraokeLine *> *SGKaraokeLinesForTrack(NSString *trackID) {return trackID ? sg_lyrics[trackID] : nil;}+// Main queue only. The track stays asked for through the pause, so the readers asking on every tick+// send nothing until it is over.+static void askAgainLater(NSString *trackID) {+ NSUInteger losses = sg_losses[trackID].unsignedIntegerValue;+ sg_losses[trackID] = @(losses + 1);+ NSTimeInterval pause = MIN(kRetryPause * pow(2, losses), kRetryPauseMost);+ SGLog(@"karaoke: lyrics for %@ not answered, may ask again in %.0fs", trackID, pause);+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(pause * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{+ [sg_asking removeObject:trackID];+ [sg_requested removeObject:trackID];+ });+}+static void requestFromSpotify(NSString *trackID) {NSDictionary<NSString *, NSString *> *headers = sg_spclientHeaders;if (!headers) return;[sg_requested addObject:trackID];+ [sg_asking addObject:trackID];NSString *address = [NSString stringWithFormat:@"https://spclient.wg.spotify.com/color-lyrics/v2/track/%@?format=json&vocalRemoval=false&market=from_token", trackID];NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:address]];[headers enumerateKeysAndObjectsUsingBlock:^(NSString *name, NSString *value, BOOL *stop) {@@ -117,22 +149,22 @@ static void requestFromSpotify(NSString *trackID) {[NSURLProtocol setProperty:@YES forKey:SGLyricsOwnRequestKey inRequest:request];[[NSURLSession.sharedSession dataTaskWithRequest:request completionHandler:^(NSData *body, NSURLResponse *response, NSError *error) {NSArray<SGKaraokeLine *> *lines = SGKaraokeLinesFromBody(body);+ NSInteger status = [response isKindOfClass:NSHTTPURLResponse.class] ? ((NSHTTPURLResponse *)response).statusCode : 0;SGLog(@"karaoke: fetched lyrics for %@: status %ld, %lu lines (%@), error %@", trackID,- (long)[(NSHTTPURLResponse *)response statusCode], (unsigned long)lines.count,+ (long)status, (unsigned long)lines.count,SGKaraokeLinesTiming(lines) == SGKaraokeTimingNone ? @"untimed" : @"line timed", error);- if (!lines) {- // A request lost to the network or to a busy server is not an answer of "no lyrics": it is asked- // again after a pause, where an answer with none stays kept as requested.- NSInteger status = [response isKindOfClass:NSHTTPURLResponse.class] ? ((NSHTTPURLResponse *)response).statusCode : 0;- if (error || status == 429 || status >= 500) {- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kRetryAfter * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{- [sg_requested removeObject:trackID];- });- }- return;- }- // Asked after the chain found plain text only: Spotify's replace it only when they are timed.+ // A refused authorization is lost too: the captured one may have expired, and Spotify's next+ // spclient request brings a fresh one.+ BOOL lost = SGLyricsReplyFailed(response, error) || status == 401 || status == 403;dispatch_async(dispatch_get_main_queue(), ^{+ if (lost) {+ askAgainLater(trackID);+ return;+ }+ [sg_asking removeObject:trackID];+ [sg_losses removeObjectForKey:trackID];+ if (!lines) return;+ // Asked after the chain found plain text only: Spotify's replace it only when they are timed.NSArray<SGKaraokeLine *> *kept = sg_lyrics[trackID];if (kept && SGKaraokeLinesTiming(kept) <= SGKaraokeLinesTiming(lines)) return;keep(trackID, lines);@@ -155,7 +187,9 @@ void SGKaraokeRequestLyrics(NSString *trackID) {return;}[sg_requested addObject:trackID];+ [sg_asking addObject:trackID];SGLyricsFetch(trackID, ^(SGLyricsResult *lyrics) {+ [sg_asking removeObject:trackID];if (lyrics.karaokeLines) {keep(trackID, lyrics.karaokeLines); // on the main queue, where the fetch answersSGLyricsSetCredit(trackID, lyrics.provider);@@ -232,9 +266,7 @@ void SGKaraokeRememberTrack(SPTPlayerTrack *track) {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;+ SPTPlayerTrack *next = upNextIn(state);NSString *nextID = idOf(next);if (!nextID || [nextID isEqualToString:trackID]) return;remember(next, nextID);@@ -288,6 +320,8 @@ static void prefetch(SPTPlayerTrack *track, NSString *trackID, SPTPlayerState *ssg_seenTracks = [NSMutableDictionary dictionary];sg_lyrics = [NSMutableDictionary dictionary];sg_requested = [NSMutableSet set];+ sg_asking = [NSMutableSet set];+ sg_losses = [NSMutableDictionary dictionary];sg_ownSources = SGLyricsEnabled();%init;SGLog(@"karaoke: on");diff --git a/tweak/Sources/Shared/Lyrics/Protobuf.m b/tweak/Sources/Shared/Lyrics/Protobuf.mindex 92588fa1..47d0fe97 100644--- a/tweak/Sources/Shared/Lyrics/Protobuf.m+++ b/tweak/Sources/Shared/Lyrics/Protobuf.m@@ -49,7 +49,7 @@ NSMutableArray<SGPBField *> *SGPBParse(NSData *data) {at += size;break;case 2:- // Against what is left rather than at + value, which a length near 2^64 wraps past the check.+ // Against what is left rather than added to `at`: a length near 2^64 wraps the sum.if (!readVarint(bytes, length, &at, &value) || value > length - at) return nil;field.payload = [NSData dataWithBytes:bytes + at length:(NSUInteger)value];at += value;diff --git a/tweak/Sources/Shared/LyricsSources/LyricsSources.h b/tweak/Sources/Shared/LyricsSources/LyricsSources.hindex ddf476e2..bdc5b676 100644--- a/tweak/Sources/Shared/LyricsSources/LyricsSources.h+++ b/tweak/Sources/Shared/LyricsSources/LyricsSources.h@@ -104,6 +104,8 @@ void SGLyricsPostJSON(NSURL *url, NSDictionary<NSString *, NSString *> *headers,// Every source's reply goes through this, so a walk that lost a request to the network or a busy// server is not kept as "no lyrics". The two above call it themselves.void SGLyricsNoteReply(NSURLResponse *response, NSError *error);+// What it counts as lost: the network failed, or the server answered 429 or 5xx.+BOOL SGLyricsReplyFailed(NSURLResponse *response, NSError *error);// SGTTML.m. Apple Music's TTML as timed lines, the voices already turned into alignments and each// line's translation and pronunciation added where the head has them; nil when the document holds nodiff --git a/tweak/Sources/Shared/LyricsSources/LyricsSources.m b/tweak/Sources/Shared/LyricsSources/LyricsSources.mindex ce415923..aeb4f952 100644--- a/tweak/Sources/Shared/LyricsSources/LyricsSources.m+++ b/tweak/Sources/Shared/LyricsSources/LyricsSources.m@@ -37,9 +37,13 @@ static NSString *const kLegacyNetEase = @"spotifyglass.neteaseWordTiming";// walk can tell "no source has lyrics" from "a source could not say". Only the first is kept.static _Atomic NSUInteger sg_failures;-void SGLyricsNoteReply(NSURLResponse *response, NSError *error) {+BOOL SGLyricsReplyFailed(NSURLResponse *response, NSError *error) {NSInteger status = [response isKindOfClass:NSHTTPURLResponse.class] ? ((NSHTTPURLResponse *)response).statusCode : 0;- if (error || status == 429 || status >= 500) atomic_fetch_add(&sg_failures, 1);+ return error || status == 429 || status >= 500;+}++void SGLyricsNoteReply(NSURLResponse *response, NSError *error) {+ if (SGLyricsReplyFailed(response, error)) atomic_fetch_add(&sg_failures, 1);}NSURL *SGLyricsURL(NSString *base, NSDictionary<NSString *, NSString *> *query) {diff --git a/tweak/Sources/Shared/Privacy/Privacy.x b/tweak/Sources/Shared/Privacy/Privacy.xindex f977d31d..cede5e84 100644--- a/tweak/Sources/Shared/Privacy/Privacy.x+++ b/tweak/Sources/Shared/Privacy/Privacy.x@@ -1,19 +1,15 @@// Telemetry blocking: an NSURLProtocol that answers the analytics endpoints itself instead of// letting them out, and keeps a count of what it stopped for the Privacy page.//-// Each SDK Spotify carries has somewhere of its own to send to: Firebase to app-measurement.com, the-// Facebook SDK to graph.facebook.com/<id>/activities, then Branch, Comscore, Segment. Nothing on the-// list carries playback, search, sign-in or deep links, which is why whole hosts are named where the-// host does nothing else and a path is named where it does.-//-// Spotify's own events, sent to spclient's /gabo-receiver-service/, are not on the list. They carry-// the playback reports Recents is built from: with them answered empty a track played never turned up-// in Recents, and it did with the block off (device 2026-09-21, issue #70).+// The SDKs Spotify ships each send somewhere of their own: Firebase to app-measurement.com, Facebook+// to graph.facebook.com/<id>/activities, then Branch, Comscore, Segment. None of it carries playback,+// search, sign-in or deep links, so a whole host is named where it does nothing else and a path where+// it does. Spotify's own events, sent to spclient's+// /gabo-receiver-service/, are left alone: Recents is built from the plays reported among them.//// A protocol handed to +registerClass: is only consulted by NSURLConnection and the shared// session, so both session configurations are given it as well: that is what the SDKs build their-// sessions from. What the core sends over its own sockets is out of reach either way, and the-// counters are what tell the two apart.+// sessions from. What the core sends over its own sockets is out of reach either way.#import "Core/SGCore.h"#import "Privacy.h"@@ -43,14 +39,14 @@ static NSMutableDictionary<NSString *, NSNumber *> *sg_counts;@end// The class object stands in for a lock: counting happens on whichever thread the request was made-// on, the Privacy page reads on the main one.+// on, the Privacy page reads on the main one. A count stored under a label no rule has any more is+// dropped as it loads, so the total adds up to the rows the page shows.static NSMutableDictionary<NSString *, NSNumber *> *countsLocked(void) {if (!sg_counts) {sg_counts = [[NSUserDefaults.standardUserDefaults dictionaryForKey:kCounts] mutableCopy] ?: [NSMutableDictionary dictionary];- // A count of a source that is not blocked any more (Spotify events, once) would stay in the total.- NSArray<NSString *> *labels = SGBlockedLabels();+ NSSet<NSString *> *current = [NSSet setWithArray:SGBlockedLabels()];for (NSString *label in sg_counts.allKeys) {- if (![labels containsObject:label]) [sg_counts removeObjectForKey:label];+ if (![current containsObject:label]) sg_counts[label] = nil;}}return sg_counts;