Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
fix: a walk run while the picked source's request failed answers as the order's
My-Name-Is-Jeffe4c946c7f392
diff --git a/harness/lyrics-walk/README.md b/harness/lyrics-walk/README.mdnew file mode 100644index 00000000..40e740b1--- /dev/null+++ b/harness/lyrics-walk/README.md@@ -0,0 +1,15 @@+# Lyrics walk check++`./build.sh` checks, on the Mac as a Mac Catalyst binary, with no simulator and no network, the walk down+the sources' order (`Shared/LyricsSources/LyricsSources.m`) and a source picked for a track from the+redesigned player's ⋯ (`LyricsPick.m`, issue #67). Every source is answered by the check itself, by key:++- the walk stops at the order's first timed answer, which is nobody's pick;+- a pick keeps the source's answer as the track's, marked as the pick's, and shows it;+- a later walk, once the walk's answers have been forgotten (40 tracks), asks the pick first and keeps+ its lines as the pick's; with the pick's request failing, the pick stands and the walk's answer is kept+ as the order's, not the pick's, and the next walk asks the pick again; with the pick answering nothing,+ the pick is dropped.++It does not cover the sources' own requests, Spotify's own lines weighed against the walk's (LyricsHook.x),+or the menu.diff --git a/harness/lyrics-walk/build.sh b/harness/lyrics-walk/build.shnew file mode 100755index 00000000..92ce5e75--- /dev/null+++ b/harness/lyrics-walk/build.sh@@ -0,0 +1,12 @@+#!/bin/sh+# build.sh: builds check.m against this checkout's walk and pick (LyricsSources.m, LyricsPick.m) as a Mac+# Catalyst binary and runs it, so no simulator and no network is needed.+set -e+HERE=$(cd "$(dirname "$0")" && pwd)+SRC=$(cd "$HERE/../../tweak/Sources" && pwd)+LS="$SRC/Shared/LyricsSources"+mkdir -p "$HERE/build"+xcrun -sdk macosx clang -target arm64-apple-ios17.0-macabi -isysroot "$(xcrun --sdk macosx --show-sdk-path)" -iframework "$(xcrun --sdk macosx --show-sdk-path)/System/iOSSupport/System/Library/Frameworks" -fobjc-arc -g -Wall -I"$SRC" -DSG_VERSION=\"0.0.0\" \+ "$HERE/check.m" "$LS/LyricsSources.m" "$LS/LyricsPick.m" "$SRC/Shared/Lyrics/KaraokeTiming.m" "$SRC/Shared/AdBlock/Protobuf.m" "$SRC/Core/SGPrefs.m" \+ -framework UIKit -framework Foundation -o "$HERE/build/check"+"$HERE/build/check" "$@"diff --git a/harness/lyrics-walk/check.m b/harness/lyrics-walk/check.mnew file mode 100644index 00000000..490e874d--- /dev/null+++ b/harness/lyrics-walk/check.m@@ -0,0 +1,196 @@+// Checks the walk down the sources' order and a source picked from the player's ⋯ (LyricsSources.m,+// LyricsPick.m) without the phone or the network: every source is answered here, by key, so a walk, a pick,+// a later walk with the pick's request failing or answering nothing, a pick landing while the walk runs, and+// Automatic after each can be driven on the Mac. Exits non-zero on the first wrong answer.+#import <UIKit/UIKit.h>+#import "Shared/LyricsSources/LyricsSources.h"+#import "Shared/Lyrics/Lyrics.h"+#import "Shared/LocalFiles/LocalFiles.h"+#import "Settings/SGSourcesPage.h"+#import "Settings/SGPageStyle.h"+#import "Core/SGFlagForce.h"+#import "Headers/SPTPlayer.h"++#define CHECK(cond) do { if (!(cond)) { fprintf(stderr, "FAILED line %d: %s\n", __LINE__, #cond); exit(1); } } while (0)++// What the walk and the pick reach outside themselves for. Every track is a named local file, so the sources+// that search by name are asked at once.+NSString *const SGSpicyLyricsKey = @"spicylyrics";+NSString *const SGImportedLRCKey = @"importedLRC";+NSString *SGSpicyLyricsStatus(void) { return nil; }+BOOL SGLocalFileIs(NSString *uri) { return NO; }+NSDictionary<NSString *, id> *SGLocalFileInfo(NSString *uri) { return @{@"title": @"a", @"artist": @"b", @"seconds": @200}; }+SPTPlayerTrack *SGKaraokeTrackFor(NSString *trackID) { return nil; }+BOOL SGLyricsStandAsideForEevee(void) { return NO; }+void SGRegisterFlagForcer(BOOL beatsOverride, SGFlagForcer atLaunch, SGFlagForcer locked) {}+UIViewController *SGTopController(void) { return nil; }+UIViewController *SGSourcesPageMake(NSString *title, NSString *note, NSArray<SGSource *> *all,+ NSArray<NSString *> *(^order)(void), void (^setOrder)(NSArray<NSString *> *keys)) { return nil; }+@implementation SGSource++ (instancetype)sourceWithKey:(NSString *)key name:(NSString *)name detail:(NSString *)detail { return nil; }+@end++// The lyrics view: the lines kept for each track, NSNull once kept as none, and the tracks asked for again.+static NSMutableDictionary<NSString *, id> *sg_shown;+static NSMutableArray<NSString *> *sg_askedAgain;+void SGKaraokeKeepLines(NSString *track, NSArray<SGKaraokeLine *> *lines) { sg_shown[track] = lines ?: NSNull.null; }+NSArray<SGKaraokeLine *> *SGKaraokeLinesForTrack(NSString *track) {+ id lines = sg_shown[track];+ return lines == NSNull.null ? nil : lines;+}+void SGKaraokeAskAgain(NSString *track) {+ [sg_askedAgain addObject:track];+ [sg_shown removeObjectForKey:track];+}++// The sources, answered by key: a fresh result each ask, nil for none. One key may be held until release()+// runs, for what lands while the walk waits on it. Every ask is listed as "key:track".+typedef SGLyricsResult *(^SGAnswer)(void);+static NSMutableDictionary<NSString *, SGAnswer> *sg_answers;+static NSMutableArray<NSString *> *sg_asked;+static NSString *sg_held;+static void (^sg_release)(void);++static void answer(NSString *key, SGLyricsQuery *query, void (^done)(SGLyricsResult *result)) {+ [sg_asked addObject:[NSString stringWithFormat:@"%@:%@", key, query.trackID]];+ void (^now)(void) = ^{+ SGAnswer answer = sg_answers[key];+ done(answer ? answer() : nil);+ };+ if ([key isEqualToString:sg_held]) sg_release = now;+ else now();+}+#define ASK(name, key) SGLyricsAsk name = ^(SGLyricsQuery *query, void (^done)(SGLyricsResult *result)) { answer(key, query, done); };+ASK(SGBiniLyricsAsk, @"binilyrics")+ASK(SGMusixmatchAsk, @"musixmatch")+ASK(SGUnisonAsk, @"unison")+ASK(SGNetEaseAsk, @"netease")+ASK(SGLrcLibAsk, @"lrclib")+ASK(SGSpicyLyricsAsk, @"spicylyrics")+ASK(SGQQMusicAsk, @"qqmusic")+ASK(SGKuGouAsk, @"kugou")+ASK(SGImportedLRCAsk, @"importedLRC")+ASK(SGGeniusAsk, @"genius")++// Two lines, timed by the line or not at all, with the page's text made from them as a source would.+static SGAnswer lines(BOOL timed) {+ return ^SGLyricsResult *{+ SGLyricsResult *result = [SGLyricsResult new];+ result.karaokeLines = timed ? SGKaraokeEstimatedLines(@[@1000, @2000], @[@"a", @"b"]) : SGKaraokeStaticLines(@[@"a", @"b"]);+ result.synced = timed;+ NSArray<NSNumber *> *starts;+ NSArray<NSString *> *texts;+ SGLyricsPageLines(result.karaokeLines, &starts, &texts);+ result.starts = starts;+ result.texts = texts;+ return result;+ };+}++// A request lost on the way: counted as every source's are, so the walk knows nothing was not no lyrics.+static SGAnswer failing(void) {+ return ^SGLyricsResult *{+ SGLyricsNoteReply(nil, [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorTimedOut userInfo:nil]);+ return nil;+ };+}++static void pump(BOOL (^until)(void)) {+ NSDate *deadline = [NSDate dateWithTimeIntervalSinceNow:5];+ while (!until() && deadline.timeIntervalSinceNow > 0) [NSRunLoop.mainRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.02]];+ CHECK(until());+}++static SGLyricsResult *fetch(NSString *track) {+ __block BOOL answered = NO;+ __block SGLyricsResult *got = nil;+ SGLyricsFetch(track, ^(SGLyricsResult *result) {+ got = result;+ answered = YES;+ });+ pump(^BOOL { return answered; });+ return got;+}++static NSDictionary<NSString *, SGLyricsResult *> *offers(NSString *track) {+ __block NSDictionary<NSString *, SGLyricsResult *> *got = nil;+ SGLyricsOffers(track, 0, ^(NSDictionary<NSString *, SGLyricsResult *> *offered) { got = offered; });+ pump(^BOOL { return got != nil; });+ return got;+}++// The walk keeps its answers for 40 tracks and then forgets them all: 40 other tracks walked forget the ones+// before. The same 40 picked forget what Automatic puts back for the ones before.+static NSUInteger sg_others;+static void walkOthers(BOOL picked) {+ sg_answers[@"musixmatch"] = lines(YES);+ for (NSUInteger i = 0; i < 40; i++) {+ NSString *track = [NSString stringWithFormat:@"other%lu", (unsigned long)++sg_others];+ if (!picked) {+ fetch(track);+ continue;+ }+ offers(track);+ SGLyricsChooseSource(track, @"musixmatch");+ }+}++int main(int argc, char **argv) {+ @autoreleasepool {+ sg_shown = [NSMutableDictionary dictionary];+ sg_askedAgain = [NSMutableArray array];+ sg_answers = [NSMutableDictionary dictionary];+ sg_asked = [NSMutableArray array];+ // The order, read from the defaults, without writing the Mac's.+ [NSUserDefaults.standardUserDefaults setVolatileDomain:@{SGKeyLyricsProviders: @[@"musixmatch", @"lrclib", @"netease", @"genius"]}+ forName:NSArgumentDomain];+ sg_answers[@"musixmatch"] = lines(YES);+ sg_answers[@"lrclib"] = lines(YES);+ sg_answers[@"netease"] = lines(YES);+ sg_answers[@"genius"] = lines(NO);++ // The walk stops at the first timed answer, the order's first, and its answer is nobody's pick.+ SGLyricsResult *got = fetch(@"t1");+ CHECK([got.provider isEqualToString:@"Musixmatch"] && got.synced && !got.picked);+ CHECK([sg_asked isEqualToArray:@[@"musixmatch:t1"]]);++ // A pick from the player's ⋯: the source's answer is the track's, marked as the pick's, and shows.+ NSDictionary *offered = offers(@"t1");+ CHECK(offered[@"musixmatch"] && offered[@"lrclib"] && offered[@"netease"]);+ SGLyricsChooseSource(@"t1", @"lrclib");+ got = fetch(@"t1");+ CHECK([got.provider isEqualToString:@"LRCLIB"] && got.picked && [SGLyricsChosenSource(@"t1") isEqualToString:@"lrclib"]);+ CHECK(sg_shown[@"t1"] == got.karaokeLines && [SGLyricsCreditFor(@"t1") isEqualToString:@"LRCLIB"]);++ // A later walk asks the pick first, and its lines are the walk's answer, as the pick's.+ walkOthers(NO);+ [sg_asked removeAllObjects];+ got = fetch(@"t1");+ CHECK([sg_asked isEqualToArray:@[@"lrclib:t1"]]);+ CHECK([got.provider isEqualToString:@"LRCLIB"] && got.picked);++ // The pick's request fails: the pick stands, the walk goes on, and its answer is kept as the order's+ // rather than as the pick's, so Spotify's own lines weigh against it. The next walk asks the pick again.+ walkOthers(NO);+ sg_answers[@"lrclib"] = failing();+ [sg_asked removeAllObjects];+ got = fetch(@"t1");+ CHECK([sg_asked isEqualToArray:(@[@"lrclib:t1", @"musixmatch:t1"])]);+ CHECK([got.provider isEqualToString:@"Musixmatch"] && !got.picked && [SGLyricsChosenSource(@"t1") isEqualToString:@"lrclib"]);+ CHECK(fetch(@"t1") == got); // kept+ sg_answers[@"lrclib"] = lines(YES);+ walkOthers(NO);+ got = fetch(@"t1");+ CHECK([got.provider isEqualToString:@"LRCLIB"] && got.picked);++ // The pick has nothing: dropped, and the walk's answer is the order's.+ walkOthers(NO);+ sg_answers[@"lrclib"] = nil;+ got = fetch(@"t1");+ CHECK([got.provider isEqualToString:@"Musixmatch"] && !got.picked && !SGLyricsChosenSource(@"t1"));+ sg_answers[@"lrclib"] = lines(YES);++ printf("lyrics-walk: all checks passed\n");+ }+ return 0;+}diff --git a/tweak/Sources/Shared/LyricsSources/LyricsHook.x b/tweak/Sources/Shared/LyricsSources/LyricsHook.xindex f3219898..20d654d7 100644--- a/tweak/Sources/Shared/LyricsSources/LyricsHook.x+++ b/tweak/Sources/Shared/LyricsSources/LyricsHook.x@@ -168,8 +168,9 @@ static NSData *decide(NSString *track, SGLyricsResult *chain, NSData *spotifyBodNSArray<SGKaraokeLine *> *spotifyLines = spotifyBody ? SGKaraokeLinesFromBody(spotifyBody) : nil;if (!spotifyLines.count) spotifyLines = nil;SGKaraokeTiming spotifyTiming = SGKaraokeLinesTiming(spotifyLines);- // A source picked for the track from the player's ⋯ stands over Spotify's own, however either is timed.- BOOL picked = SGLyricsChosenSource(track) != nil;+ // A source picked for the track from the player's ⋯ stands over Spotify's own, however either is timed. Read+ // off the answer, not the pick: a walk run while the pick's request failed answers with another source's.+ BOOL picked = chain.picked;BOOL replace = chain.texts.count && (picked || chain.synced || spotifyTiming == SGKaraokeTimingNone);NSData *page = replace ? pageBody(chain, colours) : nil;diff --git a/tweak/Sources/Shared/LyricsSources/LyricsSources.h b/tweak/Sources/Shared/LyricsSources/LyricsSources.hindex 4d0ac36d..0f96846f 100644--- a/tweak/Sources/Shared/LyricsSources/LyricsSources.h+++ b/tweak/Sources/Shared/LyricsSources/LyricsSources.h@@ -50,6 +50,9 @@@property (nonatomic, copy) NSString *title, *artist, *album;@property (nonatomic) NSInteger seconds;@property (nonatomic) BOOL instrumental;+// The source picked for the track from the player's ⋯ answered with this (SGLyricsKeepChosen), so it stands+// over Spotify's own lines however either is timed. The walk's own answer never is, a pick standing or not.+@property (nonatomic) BOOL picked;@end// What is known about the track when a source is asked. Only trackID is always there; the rest isdiff --git a/tweak/Sources/Shared/LyricsSources/LyricsSources.m b/tweak/Sources/Shared/LyricsSources/LyricsSources.mindex f62e99ac..eed2d2e8 100644--- a/tweak/Sources/Shared/LyricsSources/LyricsSources.m+++ b/tweak/Sources/Shared/LyricsSources/LyricsSources.m@@ -418,6 +418,8 @@ static void finish(SGLyricsWalk *walk) {SGLyricsNoteOrderChoice(trackID, lyrics);lyrics = picked;} else if (lyrics || (everyoneAsked && !failed) || merged.instrumental) {+ // With a pick whose request failed, the walk's answer is kept, but not as the pick's: it weighs+ // against Spotify's own lines as any walk's does, and the next walk asks the pick first again.if (sg_kept.count >= kKeptTracks) [sg_kept removeAllObjects];sg_kept[trackID] = lyrics ?: NSNull.null;if (!lyrics) {@@ -722,6 +724,7 @@ void SGLyricsKeepChosen(NSString *trackID, SGLyricsResult *result) {result.synced = SGKaraokeLinesTiming(result.karaokeLines) != SGKaraokeTimingNone;}noteCredit(result);+ result.picked = YES;if (sg_kept.count >= kKeptTracks) [sg_kept removeAllObjects];sg_kept[trackID] = result;@synchronized (sg_missing) { [sg_missing removeObject:trackID]; }