forked from PoomSmart/YTABConfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tweak.xm
378 lines (342 loc) · 16.4 KB
/
Tweak.xm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#import "../YouTubeHeader/YTAlertView.h"
#import "../YouTubeHeader/YTAppDelegate.h"
#import "../YouTubeHeader/YTCommonUtils.h"
#import "../YouTubeHeader/YTUIUtils.h"
#import "../YouTubeHeader/YTVersionUtils.h"
#import "../YouTubeHeader/YTGlobalConfig.h"
#import "../YouTubeHeader/YTColdConfig.h"
#import "../YouTubeHeader/YTHotConfig.h"
#import "../YouTubeHeader/YTSettingsSectionItem.h"
#import "../YouTubeHeader/YTSettingsSectionItemManager.h"
#import "../YouTubeHeader/YTSettingsPickerViewController.h"
#import "../YouTubeHeader/YTSettingsViewController.h"
#import "../YouTubeHeader/YTSearchableSettingsViewController.h"
#import "../YouTubeHeader/YTToastResponderEvent.h"
#define Prefix @"YTABC"
#define EnabledKey @"EnabledYTABC"
#define GroupedKey @"GroupedYTABC"
#define INCLUDED_CLASSES @"Included classes: YTGlobalConfig, YTColdConfig, YTHotConfig"
#define EXCLUDED_METHODS @"Excluded settings: android*, amsterdam*, musicClient* and unplugged*"
#define LOC(x) [tweakBundle localizedStringForKey:x value:nil table:nil]
@interface YTSettingsSectionItemManager (YTABConfig)
- (void)updateYTABCSectionWithEntry:(id)entry;
@end
static const NSInteger YTABCSection = 404;
NSMutableDictionary <NSString *, NSMutableDictionary <NSString *, NSNumber *> *> *cache;
NSUserDefaults *defaults;
NSArray <NSString *> *allKeys;
static BOOL tweakEnabled() {
return [defaults boolForKey:EnabledKey];
}
static BOOL groupedSettings() {
return [defaults boolForKey:GroupedKey];
}
static NSString *getKey(NSString *method, NSString *classKey) {
return [NSString stringWithFormat:@"%@.%@.%@", Prefix, classKey, method];
}
static NSString *getCacheKey(NSString *method, NSString *classKey) {
return [NSString stringWithFormat:@"%@.%@", classKey, method];
}
static BOOL getValue(NSString *methodKey) {
if (![allKeys containsObject:methodKey])
return [[cache valueForKeyPath:[methodKey substringFromIndex:Prefix.length + 1]] boolValue];
return [defaults boolForKey:methodKey];
}
static void setValue(NSString *method, NSString *classKey, BOOL value) {
[cache setValue:@(value) forKeyPath:getCacheKey(method, classKey)];
[defaults setBool:value forKey:getKey(method, classKey)];
}
static BOOL returnFunction(id const self, SEL _cmd) {
NSString *method = NSStringFromSelector(_cmd);
NSString *methodKey = getKey(method, NSStringFromClass([self class]));
return getValue(methodKey);
}
static BOOL getValueFromInvocation(id target, SEL selector) {
NSInvocationOperation *i = [[NSInvocationOperation alloc] initWithTarget:target selector:selector object:nil];
[i start];
BOOL result = NO;
[i.result getValue:&result];
return result;
}
NSBundle *YTABCBundle() {
static NSBundle *bundle = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *tweakBundlePath = [[NSBundle mainBundle] pathForResource:@"YTABC" ofType:@"bundle"];
if (tweakBundlePath)
bundle = [NSBundle bundleWithPath:tweakBundlePath];
else {
bundle = [NSBundle bundleWithPath:@"/Library/Application Support/YTABC.bundle"];
if (!bundle)
bundle = [NSBundle bundleWithPath:@"/var/jb/Library/Application Support/YTABC.bundle"];
}
});
return bundle;
}
%hook YTSettingsSectionController
- (void)setSelectedItem:(NSUInteger)selectedItem {
if (selectedItem != NSNotFound) %orig;
}
%end
%hook YTSettingsViewController
- (void)loadWithModel:(id)model fromView:(UIView *)view {
%orig;
if ([[self valueForKey:@"_detailsCategoryID"] integerValue] == YTABCSection)
MSHookIvar<BOOL>(self, "_shouldShowSearchBar") = YES;
}
- (void)setSectionControllers {
%orig;
if (MSHookIvar<BOOL>(self, "_shouldShowSearchBar")) {
YTSettingsSectionController *settingsSectionController = [self settingsSectionControllers][[self valueForKey:@"_detailsCategoryID"]];
if (settingsSectionController) {
YTSearchableSettingsViewController *searchableVC = [self valueForKey:@"_searchableSettingsViewController"];
[searchableVC storeCollectionViewSections:@[settingsSectionController]];
}
}
}
%end
%hook YTAppSettingsPresentationData
+ (NSArray *)settingsCategoryOrder {
NSArray *order = %orig;
NSMutableArray *mutableOrder = [order mutableCopy];
[mutableOrder insertObject:@(YTABCSection) atIndex:0];
return mutableOrder;
}
%end
static NSString *getCategory(char c, NSString *method) {
if (c == 'e') {
if ([method hasPrefix:@"elements"]) return @"elements";
if ([method hasPrefix:@"enable"]) return @"enable";
}
if (c == 'i') {
if ([method hasPrefix:@"ios"]) return @"ios";
if ([method hasPrefix:@"is"]) return @"is";
}
if (c == 's') {
if ([method hasPrefix:@"shorts"]) return @"shorts";
if ([method hasPrefix:@"should"]) return @"should";
}
return [NSString stringWithFormat:@"%c", c]; // FIXME: Yike -stringWithFormat:
}
%hook YTSettingsSectionItemManager
%new(v@:@)
- (void)updateYTABCSectionWithEntry:(id)entry {
NSMutableArray *sectionItems = [NSMutableArray array];
int totalSettings = 0;
NSBundle *tweakBundle = YTABCBundle();
BOOL isPhone = ![%c(YTCommonUtils) isIPad];
if (tweakEnabled()) {
NSMutableDictionary <NSString *, NSMutableArray <YTSettingsSectionItem *> *> *properties = [NSMutableDictionary dictionary];
for (NSString *classKey in cache) {
for (NSString *method in cache[classKey]) {
char c = tolower([method characterAtIndex:0]);
NSString *category = getCategory(c, method);
if (![properties objectForKey:category]) properties[category] = [NSMutableArray array];
YTSettingsSectionItem *methodSwitch = [%c(YTSettingsSectionItem) switchItemWithTitle:method
titleDescription:isPhone && method.length > 26 ? method : nil
accessibilityIdentifier:nil
switchOn:getValue(getKey(method, classKey))
switchBlock:^BOOL (YTSettingsCell *cell, BOOL enabled) {
setValue(method, classKey, enabled);
return YES;
}
// selectBlock:^BOOL (YTSettingsCell *cell, NSUInteger arg1) {
// YTAlertView *alertView = [%c(YTAlertView) infoDialog];
// alertView.title = method;
// alertView.subtitle = [NSString stringWithFormat:@"-[%@ %@]", classKey, method];
// [alertView show];
// return YES;
// }
settingItemId:0];
[properties[category] addObject:methodSwitch];
}
}
YTSettingsViewController *settingsViewController = [self valueForKey:@"_settingsViewControllerDelegate"];
BOOL grouped = groupedSettings();
for (NSString *category in properties) {
NSMutableArray *rows = properties[category];
totalSettings += rows.count;
if (grouped) {
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
[rows sortUsingDescriptors:@[sort]];
NSString *shortTitle = [NSString stringWithFormat:@"\"%@\" (%ld)", category, rows.count];
NSString *title = [NSString stringWithFormat:@"%@ %@", LOC(@"SETTINGS_START_WITH"), shortTitle];
YTSettingsSectionItem *sectionItem = [%c(YTSettingsSectionItem) itemWithTitle:title accessibilityIdentifier:nil detailTextBlock:nil selectBlock:^BOOL (YTSettingsCell *cell, NSUInteger arg1) {
YTSettingsPickerViewController *picker = [[%c(YTSettingsPickerViewController) alloc] initWithNavTitle:shortTitle pickerSectionTitle:nil rows:rows selectedItemIndex:NSNotFound parentResponder:[self parentResponder]];
[settingsViewController pushViewController:picker];
return YES;
}];
[sectionItems addObject:sectionItem];
} else {
[sectionItems addObjectsFromArray:rows];
}
}
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
[sectionItems sortUsingDescriptors:@[sort]];
YTSettingsSectionItem *copyAll = [%c(YTSettingsSectionItem)
itemWithTitle:LOC(@"COPY_CURRENT_SETTINGS")
titleDescription:LOC(@"COPY_CURRENT_SETTINGS_DESC")
accessibilityIdentifier:nil
detailTextBlock:nil
selectBlock:^BOOL (YTSettingsCell *cell, NSUInteger arg1) {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSMutableArray *content = [NSMutableArray array];
for (NSString *classKey in cache) {
[cache[classKey] enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSNumber *value, BOOL* stop) {
[content addObject:[NSString stringWithFormat:@"%@: %d", key, [value boolValue]]];
}];
}
[content sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
[content insertObject:[NSString stringWithFormat:@"Device model: %@", [%c(YTCommonUtils) hardwareModel]] atIndex:0];
[content insertObject:[NSString stringWithFormat:@"App version: %@", [%c(YTVersionUtils) appVersion]] atIndex:0];
[content insertObject:EXCLUDED_METHODS atIndex:0];
[content insertObject:INCLUDED_CLASSES atIndex:0];
[content insertObject:[NSString stringWithFormat:@"YTABConfig version: %@", @(OS_STRINGIFY(TWEAK_VERSION))] atIndex:0];
pasteboard.string = [content componentsJoinedByString:@"\n"];
[[%c(YTToastResponderEvent) eventWithMessage:LOC(@"COPIED_TO_CLIPBOARD") firstResponder:[self parentResponder]] send];
return YES;
}];
[sectionItems insertObject:copyAll atIndex:0];
YTSettingsSectionItem *modified = [%c(YTSettingsSectionItem)
itemWithTitle:LOC(@"VIEW_MODIFIED_SETTINGS")
titleDescription:LOC(@"VIEW_MODIFIED_SETTINGS_DESC")
accessibilityIdentifier:nil
detailTextBlock:nil
selectBlock:^BOOL (YTSettingsCell *cell, NSUInteger arg1) {
NSMutableArray *features = [NSMutableArray array];
for (NSString *key in [defaults dictionaryRepresentation].allKeys) {
if ([key hasPrefix:Prefix]) {
NSString *displayKey = [key substringFromIndex:Prefix.length + 1];
[features addObject:[NSString stringWithFormat:@"%@: %d", displayKey, [defaults boolForKey:key]]];
}
}
[features sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
[features insertObject:[NSString stringWithFormat:LOC(@"TOTAL_MODIFIED_SETTINGS"), features.count] atIndex:0];
NSString *content = [features componentsJoinedByString:@"\n"];
YTAlertView *alertView = [%c(YTAlertView) confirmationDialogWithAction:^{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = content;
[[%c(YTToastResponderEvent) eventWithMessage:LOC(@"COPIED_TO_CLIPBOARD") firstResponder:[self parentResponder]] send];
} actionTitle:LOC(@"COPY_TO_CLIPBOARD")];
alertView.title = LOC(@"MODIFIED_SETTINGS_TITLE");
alertView.subtitle = content;
[alertView show];
return YES;
}];
[sectionItems insertObject:modified atIndex:0];
YTSettingsSectionItem *reset = [%c(YTSettingsSectionItem)
itemWithTitle:LOC(@"RESET_KILL")
titleDescription:LOC(@"RESET_KILL_DESC")
accessibilityIdentifier:nil
detailTextBlock:nil
selectBlock:^BOOL (YTSettingsCell *cell, NSUInteger arg1) {
for (NSString *key in [defaults dictionaryRepresentation].allKeys) {
if ([key hasPrefix:Prefix])
[defaults removeObjectForKey:key];
}
exit(0);
}];
[sectionItems insertObject:reset atIndex:0];
YTSettingsSectionItem *group = [%c(YTSettingsSectionItem)
switchItemWithTitle:LOC(@"GROUPED")
titleDescription:nil
accessibilityIdentifier:nil
switchOn:groupedSettings()
switchBlock:^BOOL (YTSettingsCell *cell, BOOL enabled) {
[defaults setBool:enabled forKey:GroupedKey];
exit(0);
return YES;
}
settingItemId:0];
[sectionItems insertObject:group atIndex:0];
}
YTSettingsSectionItem *thread = [%c(YTSettingsSectionItem)
itemWithTitle:LOC(@"OPEN_MEGATHREAD")
titleDescription:LOC(@"OPEN_MEGATHREAD_DESC")
accessibilityIdentifier:nil
detailTextBlock:nil
selectBlock:^BOOL (YTSettingsCell *cell, NSUInteger arg1) {
return [%c(YTUIUtils) openURL:[NSURL URLWithString:@"https://github.com/PoomSmart/YTABConfig/discussions"]];
}];
[sectionItems insertObject:thread atIndex:0];
YTSettingsSectionItem *master = [%c(YTSettingsSectionItem)
switchItemWithTitle:LOC(@"ENABLED")
titleDescription:LOC(@"ENABLED_DESC")
accessibilityIdentifier:nil
switchOn:tweakEnabled()
switchBlock:^BOOL (YTSettingsCell *cell, BOOL enabled) {
[defaults setBool:enabled forKey:EnabledKey];
exit(0);
return YES;
}
settingItemId:0];
[sectionItems insertObject:master atIndex:0];
YTSettingsViewController *delegate = [self valueForKey:@"_dataDelegate"];
[delegate
setSectionItems:sectionItems
forCategory:YTABCSection
title:@"A/B"
titleDescription:tweakEnabled() ? [NSString stringWithFormat:@"YTABConfig %@, %d feature flags.", @(OS_STRINGIFY(TWEAK_VERSION)), totalSettings] : nil
headerHidden:NO];
}
- (void)updateSectionForCategory:(NSUInteger)category withEntry:(id)entry {
if (category == YTABCSection) {
[self updateYTABCSectionWithEntry:entry];
return;
}
%orig;
}
%end
static NSMutableArray <NSString *> *getBooleanMethods(Class clz) {
NSMutableArray *allMethods = [NSMutableArray array];
unsigned int methodCount = 0;
Method *methods = class_copyMethodList(clz, &methodCount);
for (unsigned int i = 0; i < methodCount; ++i) {
Method method = methods[i];
const char *name = sel_getName(method_getName(method));
if (strstr(name, "ndroid") || strstr(name, "musicClient") || strstr(name, "amsterdam") || strstr(name, "unplugged")) continue;
const char *encoding = method_getTypeEncoding(method);
if (strcmp(encoding, "B16@0:8")) continue;
NSString *selector = [NSString stringWithUTF8String:name];
if (![allMethods containsObject:selector])
[allMethods addObject:selector];
}
free(methods);
return allMethods;
}
static void hookClass(NSObject *instance) {
Class instanceClass = [instance class];
NSMutableArray <NSString *> *methods = getBooleanMethods(instanceClass);
NSString *classKey = NSStringFromClass(instanceClass);
NSMutableDictionary *classCache = cache[classKey] = [NSMutableDictionary new];
for (NSString *method in methods) {
SEL selector = NSSelectorFromString(method);
BOOL result = getValueFromInvocation(instance, selector);
classCache[method] = @(result);
MSHookMessageEx(instanceClass, selector, (IMP)returnFunction, NULL);
}
}
%hook YTAppDelegate
- (BOOL)application:(id)arg1 didFinishLaunchingWithOptions:(id)arg2 {
defaults = [NSUserDefaults standardUserDefaults];
allKeys = [defaults dictionaryRepresentation].allKeys;
if (tweakEnabled()) {
YTGlobalConfig *globalConfig = [self valueForKey:@"_globalConfig"];
YTColdConfig *coldConfig = [self valueForKey:@"_coldConfig"];
YTHotConfig *hotConfig = [self valueForKey:@"_hotConfig"];
hookClass(globalConfig);
hookClass(coldConfig);
hookClass(hotConfig);
}
return %orig;
}
%end
%ctor {
NSBundle *bundle = [NSBundle bundleWithPath:[NSString stringWithFormat:@"%@/Frameworks/Module_Framework.framework", [[NSBundle mainBundle] bundlePath]]];
if (!bundle.loaded) [bundle load];
cache = [NSMutableDictionary new];
%init;
}
%dtor {
[cache removeAllObjects];
}