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

fix: shuffle and repeat work in radio and autoplay with Spoof Premium on

My-Name-Is-Jeff49e462268c75
diff --git a/docs/tweaks.md b/docs/tweaks.md
index abdf442f..32f97664 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -80,7 +80,14 @@ Shared:
AdBlock/ EeveeSpotify's ad blocking: the ad and upsell services silenced (AdServices.x), ad components out of the
Hub JSON (AdHubs.x) and the feeds (Feeds.m), Premium pop-ups dropped (AdPopups.x), and the responses
rewritten on the way in (AdNetwork.x, Premium.m over the protobuf walker in Protobuf.m), with crossfade
- and automix switched on in the player core and crossfade's switch kept in step with its slider (Crossfade.x)
+ and automix switched on in the player core and crossfade's switch kept in step with its slider (Crossfade.x).
+ With Spoof Premium on, RadioModes.m enables shuffle and repeat for radio/autoplay in either look:
+ only restrictions made entirely of radio, endless_context and autoplay are allowed.
+ Raw reason sets remain intact when Spotify copies its state. Positive radio evidence survives
+ empty optimistic updates only for the same playbackId/contextURI; explicit non-radio reasons revoke it. The matching
+ command's ESP request gets overrideRestrictions before the core receives it; other restrictions
+ and commands stay as Spotify supplied them. The hooks install together only when the expected
+ runtime signatures match. Checked by harness/radio-modes/; its README records device evidence.
Privacy/ telemetry blocking and its counters, and the tracking taken off shared open.spotify.com links
(CleanLinks.x over the cleaner in CleanLinks.m)
ArtistBlock/ tracks by blocked artists skipped as they start (ArtistSkip.x), the list and the Blocked artists page under Player
diff --git a/harness/radio-modes/README.md b/harness/radio-modes/README.md
new file mode 100644
index 00000000..54b84f23
--- /dev/null
+++ b/harness/radio-modes/README.md
@@ -0,0 +1,67 @@
+# Radio shuffle and repeat
+
+Run `harness/radio-modes/build.sh` on macOS. It compiles the production
+`Shared/AdBlock/RadioModes.m` against Foundation mocks of the observed Spotify APIs.
+Spoof Premium on and off each pass 435 checks.
+
+Coverage includes allowlisted/mixed/unknown restrictions, exact command mode and
+boolean values, preserving copied command options, unmodified normal requests,
+combined repeat requests, rejecting incidental speed/mode changes, nested calls,
+exceptions, thread-local isolation, raw restriction copies/serialization, transient
+empty state, revocation by explicit restrictions, playback/context changes, and
+missing state or identity. The mocks model the combined request's nested optional
+boolean getters: Spotify does not call its `setOptions:` setter when constructing
+that request. These tests verify hook behavior, not Spotify core behavior.
+
+## Device evidence (2026-10-09)
+
+Tested on the paired iPhone 15 Pro, Spotify 9.1.88, Spoof Premium enabled, native
+player. An auxiliary dylib containing this production source was added to the
+existing development app to preserve its separate lossless experiment. The
+complete main-checkout tweak also builds successfully.
+
+The failing session was radio/autoplay reached after single-track playback and
+skipping. Raw restrictions were `{radio, endless_context, autoplay}` for shuffle
+and repeat-context, and `{autoplay}` for repeat-track. A normal playlist accepted
+shuffle/repeat before these hooks, so this evidence does not establish a general
+9.1.78-to-9.1.88 regression.
+
+A temporary device probe read the player's actual `SPTPlayerOptions`. With the final
+command handling, consecutive single taps produced:
+
+| Action | Shuffle | Repeat context | Repeat track |
+|---|---|---|---|
+| Initial | on | on | off |
+| Shuffle 1 | off | on | off |
+| Shuffle 2 | on | on | off |
+| Shuffle 3 | off | on | off |
+| Shuffle 4 | on | on | off |
+| Repeat 1 | on | on | on |
+| Repeat 2 | on | off | off |
+| Repeat 3 | on | on | off |
+
+This establishes accepted mode changes, rather than merely undimmed buttons. The
+user also reported repeat-one working. End-of-track testing overlapped user input
+and is not an isolated automated playback proof. Repeat-all in an endless context
+has no finite end to loop back to; these hooks do not create a finite queue or prove
+that radio recommendations are reordered. Physical-device behavior on 9.1.78,
+Connect receivers, and the redesigned player was not retested here.
+
+## Important implementation details
+
+- Enable only with Spoof Premium; require the complete expected runtime API before
+ installing any hooks. Unrecognized restriction reasons remain restricted.
+- Filter the three reason getters for presentation. Preserve their original values
+ during copying and serialization and read original implementations for commands.
+- The core needs `ESPCommandOptions.overrideRestrictions` for these radio commands.
+ Direct mode setters fill boolean request fields; repeat's combined options path
+ obtains nested optional-boolean messages and fills their values instead.
+- Spotify publishes temporary empty restriction sets between core updates. Remember
+ positive radio reasons per player and exact playback ID/context URI so subsequent
+ taps still carry the override. Missing identity, changed identity, malformed
+ reasons, or explicit non-radio restrictions discard that eligibility. Empty
+ reasons on a playback never observed as radio do not grant an override.
+- Scope command decisions to the synchronous request construction on that thread;
+ never leave a global override active for unrelated commands or async responses.
+
+Temporary tracing is not part of the tweak or harness.
diff --git a/harness/radio-modes/build.sh b/harness/radio-modes/build.sh
new file mode 100755
index 00000000..fc0ba22c
--- /dev/null
+++ b/harness/radio-modes/build.sh
@@ -0,0 +1,8 @@
+#!/bin/sh
+set -eu
+cd "$(dirname "$0")"
+mkdir -p build
+xcrun clang -fobjc-arc -O2 -Wall -Wextra -Werror -Wno-unused-parameter -I stub -I ../../tweak/Sources \
+ -framework Foundation check.m ../../tweak/Sources/Shared/AdBlock/RadioModes.m -o build/check
+build/check
+SG_RADIO_TEST_OFF=1 build/check
diff --git a/harness/radio-modes/check.m b/harness/radio-modes/check.m
new file mode 100644
index 00000000..2d96e99f
--- /dev/null
+++ b/harness/radio-modes/check.m
@@ -0,0 +1,254 @@
+#import <Foundation/Foundation.h>
+#import <objc/runtime.h>
+#import "Shared/AdBlock/RadioModes.h"
+
+BOOL SGHidden(NSString *key) { return getenv("SG_RADIO_TEST_OFF") == NULL; }
+@interface SPTPlayerRestrictions : NSObject <NSCopying>
+@property NSSet *disallowTogglingShuffleReasons, *disallowTogglingRepeatContextReasons, *disallowTogglingRepeatTrackReasons;
+- (NSDictionary *)serializedDictionary;
+- (BOOL)disallowTogglingShuffle;
+- (BOOL)disallowTogglingRepeatContext;
+- (BOOL)disallowTogglingRepeatTrack;
+@end
+@implementation SPTPlayerRestrictions
+- (NSDictionary *)serializedDictionary {
+ return @{@"shuffle":self.disallowTogglingShuffleReasons, @"context":self.disallowTogglingRepeatContextReasons,
+ @"track":self.disallowTogglingRepeatTrackReasons};
+}
+- (id)copyWithZone:(NSZone *)zone {
+ SPTPlayerRestrictions *copy = [SPTPlayerRestrictions new];
+ copy.disallowTogglingShuffleReasons = self.disallowTogglingShuffleReasons;
+ copy.disallowTogglingRepeatContextReasons = self.disallowTogglingRepeatContextReasons;
+ copy.disallowTogglingRepeatTrackReasons = self.disallowTogglingRepeatTrackReasons;
+ return copy;
+}
+- (BOOL)disallowTogglingShuffle { return self.disallowTogglingShuffleReasons.count > 0; }
+- (BOOL)disallowTogglingRepeatContext { return self.disallowTogglingRepeatContextReasons.count > 0; }
+- (BOOL)disallowTogglingRepeatTrack { return self.disallowTogglingRepeatTrackReasons.count > 0; }
+@end
+@interface SPTPlayerState : NSObject
+@property SPTPlayerRestrictions *restrictions;
+@property NSString *playbackId, *contextURI;
+@end
+@implementation SPTPlayerState
+@end
+@interface ESPCommandOptions : NSObject <NSCopying>
+@property BOOL overrideRestrictions, onlyForLocalDevice;
+@property NSString *onlyForPlaybackId;
+@end
+@implementation ESPCommandOptions
+- (id)copyWithZone:(NSZone *)zone {
+ ESPCommandOptions *copy = [ESPCommandOptions new];
+ copy.overrideRestrictions = self.overrideRestrictions;
+ copy.onlyForLocalDevice = self.onlyForLocalDevice;
+ copy.onlyForPlaybackId = self.onlyForPlaybackId;
+ return copy;
+}
+@end
+#define REQUEST(CLASS, PROP) \
+@interface CLASS : NSObject \
+@property BOOL PROP; \
+@property ESPCommandOptions *options; \
+@end \
+@implementation CLASS \
+@end
+REQUEST(ESPSetShufflingContextRequest, shufflingContext)
+REQUEST(ESPSetRepeatingContextRequest, repeatingContext)
+REQUEST(ESPSetRepeatingTrackRequest, repeatingTrack)
+@interface SPTPlayerOptionOverrides : NSObject
+@property NSNumber *shufflingContext, *repeatingContext, *repeatingTrack, *playbackSpeed;
+@property NSDictionary *modes;
+@end
+@implementation SPTPlayerOptionOverrides
+@end
+static ESPCommandOptions *originalOptions;
+@interface ESPSetOptionsRequest : NSObject
+@property ESPCommandOptions *options;
+@property SPTPlayerOptionOverrides *changes;
+@property id shufflingContext, repeatingContext, repeatingTrack;
+@end
+@implementation ESPSetOptionsRequest
+- (instancetype)init {
+ if ((self = [super init])) self.options = originalOptions;
+ return self;
+}
+@end
+
+static BOOL shouldThrow, reconstructState;
+static void (^nested)(void);
+@interface SPTEsperantoPlayer : NSObject
+@property (nonatomic) SPTPlayerState *state;
+- (id)setShufflingContext:(BOOL)on;
+- (id)setRepeatingContext:(BOOL)on;
+- (id)setRepeatingTrack:(BOOL)on;
+- (id)setOptions:(id)changes loggingParams:(id)logging;
+@end
+@implementation SPTEsperantoPlayer
+@synthesize state = _state;
+- (SPTPlayerState *)state {
+ if (!reconstructState || !_state) return _state;
+ SPTPlayerState *rebuilt = [SPTPlayerState new];
+ rebuilt.playbackId = _state.playbackId; rebuilt.contextURI = _state.contextURI;
+ SPTPlayerRestrictions *raw = _state.restrictions;
+ rebuilt.restrictions = [SPTPlayerRestrictions new];
+ rebuilt.restrictions.disallowTogglingShuffleReasons = raw.disallowTogglingShuffleReasons;
+ rebuilt.restrictions.disallowTogglingRepeatContextReasons = raw.disallowTogglingRepeatContextReasons;
+ rebuilt.restrictions.disallowTogglingRepeatTrackReasons = raw.disallowTogglingRepeatTrackReasons;
+ return rebuilt;
+}
+- (id)setOptions:(id)changes loggingParams:(id)logging {
+ ESPSetOptionsRequest *r = [ESPSetOptionsRequest new]; r.changes = changes;
+ SPTPlayerOptionOverrides *bits = [changes isKindOfClass:SPTPlayerOptionOverrides.class] ? changes : nil;
+ if ([bits shufflingContext]) { (void)r.shufflingContext; r.shufflingContext = [bits shufflingContext]; }
+ if ([bits repeatingContext]) { (void)r.repeatingContext; r.repeatingContext = [bits repeatingContext]; }
+ if ([bits repeatingTrack]) { (void)r.repeatingTrack; r.repeatingTrack = [bits repeatingTrack]; }
+ return r;
+}
+- (id)setShufflingContext:(BOOL)on {
+ if (shouldThrow) @throw [NSException exceptionWithName:@"test" reason:nil userInfo:nil];
+ if (nested) { void (^call)(void) = nested; nested = nil; call(); }
+ ESPSetShufflingContextRequest *r = [ESPSetShufflingContextRequest new];
+ r.options = originalOptions;
+ r.shufflingContext = on;
+ return r;
+}
+- (id)setRepeatingContext:(BOOL)on {
+ ESPSetRepeatingContextRequest *r = [ESPSetRepeatingContextRequest new];
+ r.options = originalOptions;
+ r.repeatingContext = on;
+ return r;
+}
+- (id)setRepeatingTrack:(BOOL)on {
+ ESPSetRepeatingTrackRequest *r = [ESPSetRepeatingTrackRequest new];
+ r.options = originalOptions;
+ r.repeatingTrack = on;
+ return r;
+}
+@end
+static int failures, checks;
+#define CHECK(c) do { checks++; if (!(c)) { fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #c); failures++; } } while (0)
+static void setReasons(SPTPlayerRestrictions *r, NSSet *v) {
+ r.disallowTogglingShuffleReasons = v;
+ r.disallowTogglingRepeatContextReasons = v;
+ r.disallowTogglingRepeatTrackReasons = v;
+}
+int main(void) { @autoreleasepool {
+ BOOL active = SGHidden(nil);
+ CHECK(!SGRadioModeReasonsOnly(nil));
+ CHECK(!SGRadioModeReasonsOnly((id)@[@"radio"]));
+ CHECK(!SGRadioModeReasonsOnly([NSSet set]));
+ CHECK(!SGRadioModeReasonsOnly([NSSet setWithObject:@42]));
+ SPTPlayerRestrictions *r = [SPTPlayerRestrictions new];
+ SPTEsperantoPlayer *player = [SPTEsperantoPlayer new];
+ player.state = [SPTPlayerState new]; player.state.restrictions = r;
+ player.state.playbackId = @"playback-123"; player.state.contextURI = @"context-A";
+ originalOptions = [ESPCommandOptions new];
+ originalOptions.onlyForLocalDevice = YES; originalOptions.onlyForPlaybackId = @"playback-123";
+ NSArray *cases = @[@[], @[@"radio"], @[@"autoplay"], @[@"endless_context"],
+ @[@"radio", @"autoplay", @"endless_context"], @[@"ad_disallow"], @[@"radio", @"ad_disallow"],
+ @[@"autoplay", @"mft_disallow"], @[@"autoplay", @"future_unknown_reason"], @[@"jam_is_active"]];
+ for (NSArray *values in cases) {
+ NSSet *reasons = [NSSet setWithArray:values]; setReasons(r, reasons);
+ BOOL overridden = active && SGRadioModeReasonsOnly(reasons);
+ CHECK([r.disallowTogglingShuffleReasons isEqual:overridden ? [NSSet set] : reasons]);
+ CHECK(r.disallowTogglingShuffle == (!overridden && reasons.count > 0));
+ CHECK([r.disallowTogglingRepeatContextReasons isEqual:overridden ? [NSSet set] : reasons]);
+ CHECK(r.disallowTogglingRepeatContext == (!overridden && reasons.count > 0));
+ CHECK([r.disallowTogglingRepeatTrackReasons isEqual:overridden ? [NSSet set] : reasons]);
+ CHECK(r.disallowTogglingRepeatTrack == (!overridden && reasons.count > 0));
+ CHECK([[r serializedDictionary][@"shuffle"] isEqual:reasons]);
+ player.state.restrictions = [[r copy] copy]; // Repeated copies must preserve raw restrictions.
+ CHECK([[player.state.restrictions serializedDictionary][@"context"] isEqual:reasons]);
+ for (NSNumber *toggle in @[@NO, @YES]) {
+ NSArray *requests = @[[player setShufflingContext:toggle.boolValue], [player setRepeatingContext:toggle.boolValue], [player setRepeatingTrack:toggle.boolValue]];
+ for (id request in requests) {
+ ESPCommandOptions *options = [(ESPSetShufflingContextRequest *)request options];
+ CHECK(options.overrideRestrictions == overridden);
+ CHECK(options.onlyForLocalDevice);
+ CHECK([options.onlyForPlaybackId isEqual:@"playback-123"]);
+ CHECK((options == originalOptions) == !overridden);
+ }
+ CHECK([[requests[0] valueForKey:@"shufflingContext"] boolValue] == toggle.boolValue);
+ CHECK([[requests[1] valueForKey:@"repeatingContext"] boolValue] == toggle.boolValue);
+ CHECK([[requests[2] valueForKey:@"repeatingTrack"] boolValue] == toggle.boolValue);
+ CHECK(!originalOptions.overrideRestrictions);
+ }
+ }
+ player.state.restrictions = r;
+ // Scope is per mode, and a direct ESP request never inherits a completed call's decision.
+ setReasons(r, [NSSet setWithObject:@"radio"]);
+ r.disallowTogglingRepeatTrackReasons = [NSSet setWithObject:@"ad_disallow"];
+ CHECK(((ESPSetRepeatingTrackRequest *)[player setRepeatingTrack:YES]).options == originalOptions);
+ ESPSetShufflingContextRequest *direct = [ESPSetShufflingContextRequest new]; direct.shufflingContext = YES;
+ CHECK(!direct.options.overrideRestrictions);
+ // An exception must restore thread-local state.
+ shouldThrow = YES;
+ @try { [player setShufflingContext:YES]; } @catch (NSException *e) {}
+ shouldThrow = NO; direct.shufflingContext = NO;
+ CHECK(!direct.options.overrideRestrictions);
+ // A blocked nested command must not lose its caller's permitted decision.
+ nested = ^{ CHECK(((ESPSetRepeatingTrackRequest *)[player setRepeatingTrack:YES]).options == originalOptions); };
+ CHECK(((ESPSetShufflingContextRequest *)[player setShufflingContext:YES]).options.overrideRestrictions == active);
+ // The UI's combined request: off -> context -> track -> off, no incidental speed/mode bypass.
+ setReasons(r, [NSSet setWithObject:@"autoplay"]);
+ SPTPlayerOptionOverrides *changes = [SPTPlayerOptionOverrides new];
+ for (NSArray *bits in @[@[@YES, @NO], @[@NO, @YES], @[@NO, @NO]]) {
+ changes.repeatingContext = bits[0]; changes.repeatingTrack = bits[1];
+ ESPSetOptionsRequest *request = [player setOptions:changes loggingParams:nil];
+ CHECK(request.options.overrideRestrictions == active);
+ CHECK(request.options.onlyForLocalDevice);
+ CHECK(request.changes == changes);
+ }
+ player.state.restrictions = r;
+ (void)player.state; // Observe positive radio evidence before an optimistic empty update.
+ reconstructState = YES;
+ CHECK(((ESPSetShufflingContextRequest *)[player setShufflingContext:YES]).options.overrideRestrictions == active);
+ CHECK(((ESPSetOptionsRequest *)[player setOptions:changes loggingParams:nil]).options.overrideRestrictions == active);
+ reconstructState = NO;
+ changes.playbackSpeed = @1.5;
+ CHECK(((ESPSetOptionsRequest *)[player setOptions:changes loggingParams:nil]).options == originalOptions);
+ changes.playbackSpeed = nil; changes.modes = @{@"media": @"VIDEO"};
+ CHECK(((ESPSetOptionsRequest *)[player setOptions:changes loggingParams:nil]).options == originalOptions);
+ changes.modes = nil;
+ r.disallowTogglingRepeatTrackReasons = [NSSet setWithObjects:@"autoplay", @"ad_disallow", nil];
+ CHECK(((ESPSetOptionsRequest *)[player setOptions:changes loggingParams:nil]).options == originalOptions);
+ changes.repeatingTrack = nil; // Unchanged, so its restriction cannot block repeat-context.
+ CHECK(((ESPSetOptionsRequest *)[player setOptions:changes loggingParams:nil]).options.overrideRestrictions == active);
+ changes.repeatingContext = nil;
+ CHECK(((ESPSetOptionsRequest *)[player setOptions:changes loggingParams:nil]).options == originalOptions);
+ nested = ^{
+ ESPSetRepeatingContextRequest *other = [ESPSetRepeatingContextRequest new]; other.repeatingContext = YES;
+ CHECK(!other.options.overrideRestrictions);
+ dispatch_semaphore_t done = dispatch_semaphore_create(0);
+ [NSThread detachNewThreadWithBlock:^{
+ ESPSetShufflingContextRequest *otherThread = [ESPSetShufflingContextRequest new]; otherThread.shufflingContext = YES;
+ CHECK(!otherThread.options.overrideRestrictions);
+ dispatch_semaphore_signal(done);
+ }];
+ dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER);
+ };
+ CHECK(((ESPSetShufflingContextRequest *)[player setShufflingContext:YES]).options.overrideRestrictions == active);
+ // Empty optimistic updates retain only positive evidence for this playback.
+ setReasons(r, [NSSet setWithObject:@"radio"]); (void)player.state;
+ setReasons(r, [NSSet set]);
+ CHECK(((ESPSetShufflingContextRequest *)[player setShufflingContext:NO]).options.overrideRestrictions == active);
+ // An explicit new restriction revokes the evidence, even if followed by emptiness.
+ setReasons(r, [NSSet setWithObject:@"ad_disallow"]); (void)player.state;
+ setReasons(r, [NSSet set]);
+ CHECK(((ESPSetShufflingContextRequest *)[player setShufflingContext:YES]).options == originalOptions);
+ setReasons(r, [NSSet setWithObject:@"radio"]); (void)player.state;
+ setReasons(r, [NSSet set]); player.state.playbackId = @"new-playback";
+ CHECK(((ESPSetShufflingContextRequest *)[player setShufflingContext:YES]).options == originalOptions);
+ setReasons(r, [NSSet setWithObject:@"radio"]); (void)player.state;
+ setReasons(r, [NSSet set]); player.state.contextURI = @"new-context";
+ CHECK(((ESPSetShufflingContextRequest *)[player setShufflingContext:YES]).options == originalOptions);
+ setReasons(r, [NSSet setWithObject:@"radio"]); player.state.playbackId = nil;
+ CHECK(((ESPSetShufflingContextRequest *)[player setShufflingContext:YES]).options == originalOptions);
+ // Missing state leaves the request alone.
+ player.state = nil;
+ CHECK(((ESPSetShufflingContextRequest *)[player setShufflingContext:YES]).options == originalOptions);
+ originalOptions.overrideRestrictions = YES;
+ CHECK(((ESPSetRepeatingContextRequest *)[player setRepeatingContext:YES]).options == originalOptions);
+ printf("radio modes (%s): %d checks, %d failures\n", active ? "on" : "off", checks, failures);
+ return failures != 0;
+} }
diff --git a/harness/radio-modes/stub/UIKit/UIKit.h b/harness/radio-modes/stub/UIKit/UIKit.h
new file mode 100644
index 00000000..8524ba4c
--- /dev/null
+++ b/harness/radio-modes/stub/UIKit/UIKit.h
@@ -0,0 +1,2 @@
+#import <Foundation/Foundation.h>
+@class UIViewController;
diff --git a/tweak/Sources/Shared/AdBlock/AdBlock.h b/tweak/Sources/Shared/AdBlock/AdBlock.h
index 66d17a8d..d189db73 100644
--- a/tweak/Sources/Shared/AdBlock/AdBlock.h
+++ b/tweak/Sources/Shared/AdBlock/AdBlock.h
@@ -3,7 +3,8 @@
// before they render (AdHubs.x, Feeds.m) and answers the requests behind them empty (AdNetwork.x).
// Hide upsells drops the Premium prompts (AdPopups.x, AdServices.x) and forces the flags that show
// them off (AdBlock.m, through Flags.x). Spoof Premium rewrites the product state and remote
-// config as they arrive (Premium.m), crossfade and automix included (Crossfade.x), and keeps the
+// config as they arrive (Premium.m), crossfade and automix included (Crossfade.x), enables shuffle
+// and repeat in radio/autoplay (RadioModes.m), and keeps the
// session alive when the server disagrees (AdNetwork.x). Hide the video carousel in Search forces its
// flag off (AdBlock.m, through Flags.x).
#import <UIKit/UIKit.h>
diff --git a/tweak/Sources/Shared/AdBlock/RadioModes.h b/tweak/Sources/Shared/AdBlock/RadioModes.h
new file mode 100644
index 00000000..6b6e7430
--- /dev/null
+++ b/tweak/Sources/Shared/AdBlock/RadioModes.h
@@ -0,0 +1,14 @@
+#import <Foundation/Foundation.h>
+
+// Only these context restrictions may be overridden. An empty, malformed or mixed set
+// must not turn a normal command into one that bypasses the player's other restrictions.
+static inline BOOL SGRadioModeReasonsOnly(NSSet *reasons) {
+ if (![reasons isKindOfClass:NSSet.class] || !reasons.count) return NO;
+ for (id reason in reasons) {
+ if (![reason isKindOfClass:NSString.class] ||
+ (![(NSString *)reason isEqualToString:@"radio"] &&
+ ![(NSString *)reason isEqualToString:@"endless_context"] &&
+ ![(NSString *)reason isEqualToString:@"autoplay"])) return NO;
+ }
+ return YES;
+}
diff --git a/tweak/Sources/Shared/AdBlock/RadioModes.m b/tweak/Sources/Shared/AdBlock/RadioModes.m
new file mode 100644
index 00000000..e83e1b9f
--- /dev/null
+++ b/tweak/Sources/Shared/AdBlock/RadioModes.m
@@ -0,0 +1,232 @@
+// Radio/autoplay publishes shuffle and repeat restrictions even with Spoof Premium.
+// Allow only those restrictions for the UI/MediaRemote, and opt the corresponding command
+// into the core's override. Merely clearing the displayed restrictions does not make
+// the core accept the command. The command APIs were inspected in 9.1.78/9.1.88;
+// the full path was tested on 9.1.88. See harness/radio-modes/.
+#import "Core/SGPrefs.h"
+#import "Core/SGLog.h"
+#import "AdBlock.h"
+#import "RadioModes.h"
+#import <objc/message.h>
+#import <objc/runtime.h>
+#import <string.h>
+
+// The player creates and fills the ESP request synchronously, then sends it later.
+// Carry the decision only while filling that request, never to an unrelated command
+// or to the async response. Nested calls restore their caller's decision.
+static _Thread_local NSInteger sg_radioCommand = -1;
+static _Thread_local BOOL sg_preserveReasons;
+typedef id (*SGRadioCopyIMP)(id, SEL, NSZone *) __attribute__((ns_returns_retained));
+static SGRadioCopyIMP sg_copyRestrictions;
+static id SGRadioCopyRestrictions(id value, SEL selector, NSZone *zone) __attribute__((ns_returns_retained));
+static id SGRadioCopyRestrictions(id value, SEL selector, NSZone *zone) {
+ BOOL previous = sg_preserveReasons;
+ sg_preserveReasons = YES;
+ @try { return sg_copyRestrictions(value, selector, zone); }
+ @finally { sg_preserveReasons = previous; }
+}
+static dispatch_once_t sg_radioReasonsReported[3];
+static id (*sg_rawReasons[3])(id, SEL);
+static SEL sg_reasonSelectors[3];
+
+static id object(id receiver, SEL selector) {
+ return ((id (*)(id, SEL))objc_msgSend)(receiver, selector);
+}
+
+static char sg_radioEvidenceKey;
+// Some optimistic state updates have empty restrictions until the next core update.
+// Keep positive radio evidence only for the same playback and context. An explicit
+// non-radio reason replaces it immediately; a different playback starts afresh.
+static NSArray *commandReasons(id player, id state) {
+ id playback = object(state, sel_registerName("playbackId"));
+ id context = object(state, sel_registerName("contextURI"));
+ id limits = object(state, sel_registerName("restrictions"));
+ if (!playback || !context || ![limits isKindOfClass:objc_getClass("SPTPlayerRestrictions")]) {
+ @synchronized (player) {
+ objc_setAssociatedObject(player, &sg_radioEvidenceKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ }
+ return nil;
+ }
+ NSArray *identity = @[playback, context];
+ @synchronized (player) {
+ NSDictionary *prior = objc_getAssociatedObject(player, &sg_radioEvidenceKey);
+ NSArray *old = [prior[@"identity"] isEqual:identity] ? prior[@"reasons"] : nil;
+ NSMutableArray *result = [NSMutableArray new];
+ for (NSUInteger i = 0; i < 3; i++) {
+ id reasons = sg_rawReasons[i](limits, sg_reasonSelectors[i]);
+ if ([reasons isKindOfClass:NSSet.class] && ![reasons count] && old && SGRadioModeReasonsOnly(old[i]))
+ reasons = old[i];
+ [result addObject:reasons ?: NSNull.null];
+ }
+ NSArray *snapshot = [result copy];
+ objc_setAssociatedObject(player, &sg_radioEvidenceKey, @{@"identity":identity, @"reasons":snapshot}, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ return snapshot;
+ }
+}
+
+static BOOL signature(Method method, const char *result, const char *argument) {
+ if (!method || method_getNumberOfArguments(method) != (argument ? 3 : 2)) return NO;
+ char type[16];
+ method_getReturnType(method, type, sizeof(type));
+ if (strcmp(type, result)) return NO;
+ if (argument) {
+ method_getArgumentType(method, 2, type, sizeof(type));
+ if (strcmp(type, argument)) return NO;
+ }
+ return YES;
+}
+
+static void allowRadioRequest(id request, Class options) {
+ id commandOptions = [object(request, sel_registerName("options")) copy] ?: [options new];
+ ((void (*)(id, SEL, BOOL))objc_msgSend)(commandOptions, sel_registerName("setOverrideRestrictions:"), YES);
+ ((void (*)(id, SEL, id))objc_msgSend)(request, sel_registerName("setOptions:"), commandOptions);
+}
+
+__attribute__((constructor)) static void SGRadioModesInstall(void) {
+ if (!SGHidden(SGKeyFakePremium)) return;
+ Class restrictions = objc_getClass("SPTPlayerRestrictions");
+ Class player = objc_getClass("SPTEsperantoPlayer");
+ Class options = objc_getClass("ESPCommandOptions");
+ const char *reasonNames[] = {"disallowTogglingShuffleReasons", "disallowTogglingRepeatContextReasons", "disallowTogglingRepeatTrackReasons"};
+ const char *commandNames[] = {"setShufflingContext:", "setRepeatingContext:", "setRepeatingTrack:"};
+ const char *requestNames[] = {"ESPSetShufflingContextRequest", "ESPSetRepeatingContextRequest", "ESPSetRepeatingTrackRequest"};
+ Method reasons[3], commands[3], setters[3];
+ Class requests[3];
+ SEL getOptions = sel_registerName("options"), setOptions = sel_registerName("setOptions:");
+ SEL setOverride = sel_registerName("setOverrideRestrictions:");
+ SEL stateSelector = sel_registerName("state"), restrictionsSelector = sel_registerName("restrictions");
+ SEL combinedSelector = sel_registerName("setOptions:loggingParams:");
+ Method combined = class_getInstanceMethod(player, combinedSelector);
+ Class combinedRequest = objc_getClass("ESPSetOptionsRequest");
+ Method combinedGetters[3];
+ Class overrides = objc_getClass("SPTPlayerOptionOverrides");
+ const char *optionNames[] = {"shufflingContext", "repeatingContext", "repeatingTrack", "playbackSpeed", "modes"};
+ BOOL compatible = signature(class_getInstanceMethod(options, setOverride), @encode(void), @encode(BOOL)) &&
+ signature(class_getInstanceMethod(player, stateSelector), @encode(id), NULL) &&
+ signature(class_getInstanceMethod(objc_getClass("SPTPlayerState"), restrictionsSelector), @encode(id), NULL);
+ for (NSString *name in @[@"playbackId", @"contextURI"])
+ compatible &= signature(class_getInstanceMethod(objc_getClass("SPTPlayerState"), NSSelectorFromString(name)), @encode(id), NULL);
+ char encoding[16];
+ compatible &= combined && method_getNumberOfArguments(combined) == 4;
+ if (combined) {
+ method_getReturnType(combined, encoding, sizeof(encoding)); compatible &= !strcmp(encoding, @encode(id));
+ for (unsigned i = 2; i < 4; i++) {
+ method_getArgumentType(combined, i, encoding, sizeof(encoding)); compatible &= !strcmp(encoding, @encode(id));
+ }
+ }
+ compatible &= signature(class_getInstanceMethod(combinedRequest, getOptions), @encode(id), NULL) &&
+ signature(class_getInstanceMethod(combinedRequest, setOptions), @encode(void), @encode(id));
+ for (NSUInteger i = 0; i < 5; i++)
+ compatible &= signature(class_getInstanceMethod(overrides, sel_registerName(optionNames[i])), @encode(id), NULL);
+ for (NSUInteger i = 0; i < 3; i++) {
+ combinedGetters[i] = class_getInstanceMethod(combinedRequest, sel_registerName(optionNames[i]));
+ compatible &= signature(combinedGetters[i], @encode(id), NULL);
+ reasons[i] = class_getInstanceMethod(restrictions, sel_registerName(reasonNames[i]));
+ commands[i] = class_getInstanceMethod(player, sel_registerName(commandNames[i]));
+ requests[i] = objc_getClass(requestNames[i]);
+ setters[i] = class_getInstanceMethod(requests[i], sel_registerName(commandNames[i]));
+ compatible &= signature(reasons[i], @encode(id), NULL) &&
+ signature(commands[i], @encode(id), @encode(BOOL)) &&
+ signature(setters[i], @encode(void), @encode(BOOL)) &&
+ signature(class_getInstanceMethod(requests[i], getOptions), @encode(id), NULL) &&
+ signature(class_getInstanceMethod(requests[i], setOptions), @encode(void), @encode(id));
+ }
+ SEL copySelector = @selector(copyWithZone:), serializeSelector = sel_registerName("serializedDictionary");
+ Method copyMethod = class_getInstanceMethod(restrictions, copySelector);
+ Method serializeMethod = class_getInstanceMethod(restrictions, serializeSelector);
+ compatible &= signature(copyMethod, @encode(id), @encode(NSZone *)) &&
+ signature(serializeMethod, @encode(id), NULL);
+ // Install all three parts together, or leave Spotify alone.
+ if (!compatible) { SGLog(@"radio modes: unsupported player API; hooks inactive"); return; }
+ // Filtering getters must not erase the raw reasons in Spotify's next state copy.
+ sg_copyRestrictions = (SGRadioCopyIMP)method_getImplementation(copyMethod);
+ class_replaceMethod(restrictions, copySelector, (IMP)SGRadioCopyRestrictions, method_getTypeEncoding(copyMethod));
+ id (*serialize)(id, SEL) = (void *)method_getImplementation(serializeMethod);
+ IMP serializeHook = imp_implementationWithBlock(^id(id value) {
+ BOOL previous = sg_preserveReasons;
+ sg_preserveReasons = YES;
+ @try { return serialize(value, serializeSelector); }
+ @finally { sg_preserveReasons = previous; }
+ });
+ class_replaceMethod(restrictions, serializeSelector, serializeHook, method_getTypeEncoding(serializeMethod));
+ for (NSUInteger i = 0; i < 3; i++) {
+ NSInteger mode = (NSInteger)i;
+ SEL reasonSelector = sel_registerName(reasonNames[i]);
+ SEL commandSelector = sel_registerName(commandNames[i]);
+ id (*readReasons)(id, SEL) = (void *)method_getImplementation(reasons[i]);
+ sg_rawReasons[i] = readReasons;
+ sg_reasonSelectors[i] = reasonSelector;
+ id (*sendCommand)(id, SEL, BOOL) = (void *)method_getImplementation(commands[i]);
+ void (*fillRequest)(id, SEL, BOOL) = (void *)method_getImplementation(setters[i]);
+ IMP reasonHook = imp_implementationWithBlock(^id(id value) {
+ id original = readReasons(value, reasonSelector);
+ if (sg_preserveReasons || !SGRadioModeReasonsOnly(original)) return original;
+ dispatch_once(&sg_radioReasonsReported[mode], ^{
+ SGLog(@"radio modes: %@ allows %@", NSStringFromSelector(reasonSelector), original);
+ });
+ return [NSSet set];
+ });
+ IMP commandHook = imp_implementationWithBlock(^id(id value, BOOL on) {
+ NSArray *evidence = commandReasons(value, object(value, stateSelector));
+ NSInteger previous = sg_radioCommand;
+ // Reason sets remain intact, including when Spotify copies its state.
+ sg_radioCommand = evidence && SGRadioModeReasonsOnly(evidence[mode]) ? mode : -1;
+ @try { return sendCommand(value, commandSelector, on); }
+ @finally { sg_radioCommand = previous; }
+ });
+ IMP requestHook = imp_implementationWithBlock(^(id value, BOOL on) {
+ fillRequest(value, commandSelector, on);
+ if (sg_radioCommand != mode) return;
+ allowRadioRequest(value, options);
+ SGLog(@"radio modes: %@ overrides radio/autoplay restrictions", NSStringFromSelector(commandSelector));
+ });
+ class_replaceMethod(restrictions, reasonSelector, reasonHook, method_getTypeEncoding(reasons[i]));
+ class_replaceMethod(player, commandSelector, commandHook, method_getTypeEncoding(commands[i]));
+ class_replaceMethod(requests[i], commandSelector, requestHook, method_getTypeEncoding(setters[i]));
+ }
+ Method stateMethod = class_getInstanceMethod(player, stateSelector);
+ id (*readState)(id, SEL) = (void *)method_getImplementation(stateMethod);
+ IMP stateHook = imp_implementationWithBlock(^id(id value) {
+ id state = readState(value, stateSelector);
+ commandReasons(value, state);
+ return state;
+ });
+ class_replaceMethod(player, stateSelector, stateHook, method_getTypeEncoding(stateMethod));
+ // The on-screen repeat button uses the combined options API (not setRepeatingTrack:).
+ // It may change both repeat bits in one request. Reject an override for speed/modes,
+ // for any non-radio restriction on a requested bit, or when no bit needs our help.
+ id (*sendCombined)(id, SEL, id, id) = (void *)method_getImplementation(combined);
+ IMP combinedHook = imp_implementationWithBlock(^id(id value, id changes, id logging) {
+ NSInteger previous = sg_radioCommand;
+ sg_radioCommand = -1;
+ if ([changes isKindOfClass:overrides] && !object(changes, sel_registerName("playbackSpeed")) &&
+ ![object(changes, sel_registerName("modes")) count]) {
+ NSArray *evidence = commandReasons(value, object(value, stateSelector));
+ BOOL allowed = evidence != nil, needed = NO;
+ NSArray *names = @[@"shufflingContext", @"repeatingContext", @"repeatingTrack"];
+ for (NSUInteger i = 0; allowed && i < 3; i++) {
+ if (!object(changes, NSSelectorFromString(names[i]))) continue;
+ id reasons = evidence[i];
+ if (SGRadioModeReasonsOnly(reasons)) needed = YES;
+ else if (![reasons isKindOfClass:NSSet.class] || [reasons count]) allowed = NO;
+ }
+ if (allowed && needed) sg_radioCommand = 3;
+ }
+ @try { return sendCombined(value, combinedSelector, changes, logging); }
+ @finally { sg_radioCommand = previous; }
+ });
+ class_replaceMethod(player, combinedSelector, combinedHook, method_getTypeEncoding(combined));
+ // Spotify obtains each optional-boolean child and fills its value, without
+ // calling the request's boolean setters or setOptions:. Attach command options
+ // at those synchronous child accesses, before the request is dispatched.
+ for (NSUInteger i = 0; i < 3; i++) {
+ SEL selector = sel_registerName(optionNames[i]);
+ id (*read)(id, SEL) = (void *)method_getImplementation(combinedGetters[i]);
+ IMP hook = imp_implementationWithBlock(^id(id request) {
+ if (sg_radioCommand == 3) allowRadioRequest(request, options);
+ return read(request, selector);
+ });
+ class_replaceMethod(combinedRequest, selector, hook, method_getTypeEncoding(combinedGetters[i]));
+ }
+ SGLog(@"radio modes: shuffle and repeat enabled for radio/autoplay");
+}