-
Notifications
You must be signed in to change notification settings - Fork 21
/
Settings.x
458 lines (409 loc) · 21 KB
/
Settings.x
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#import <YouTubeHeader/GOOHUDManagerInternal.h>
#import <YouTubeHeader/YTAlertView.h>
#import <YouTubeHeader/YTCommonUtils.h>
#import <YouTubeHeader/YTSearchableSettingsViewController.h>
#import <YouTubeHeader/YTSettingsGroupData.h>
#import <YouTubeHeader/YTSettingsPickerViewController.h>
#import <YouTubeHeader/YTSettingsSectionItem.h>
#import <YouTubeHeader/YTSettingsSectionItemManager.h>
#import <YouTubeHeader/YTSettingsViewController.h>
#import <YouTubeHeader/YTUIUtils.h>
#import <YouTubeHeader/YTVersionUtils.h>
#import <rootless.h>
#import <sys/utsname.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(b, x) [b localizedStringForKey:x value:nil table:nil]
#define LOC(x) _LOC(tweakBundle, x)
static const NSInteger YTABCSection = 404;
@interface YTSettingsSectionItemManager (YTABConfig)
- (void)updateYTABCSectionWithEntry:(id)entry;
@end
extern NSMutableDictionary <NSString *, NSMutableDictionary <NSString *, NSNumber *> *> *cache;
NSUserDefaults *defaults;
NSArray <NSString *> *allKeys;
BOOL tweakEnabled() {
return [defaults boolForKey:EnabledKey];
}
BOOL groupedSettings() {
return [defaults boolForKey:GroupedKey];
}
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:ROOT_PATH_NS(@"/Library/Application Support/YTABC.bundle")];
});
return bundle;
}
NSString *getKey(NSString *method, NSString *classKey) {
return [NSString stringWithFormat:@"%@.%@.%@", Prefix, classKey, method];
}
static NSString *getCacheKey(NSString *method, NSString *classKey) {
return [NSString stringWithFormat:@"%@.%@", classKey, method];
}
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 void setValueFromImport(NSString *settingKey, BOOL value) {
[cache setValue:@(value) forKeyPath:settingKey];
[defaults setBool:value forKey:[NSString stringWithFormat:@"%@.%@", Prefix, settingKey]];
}
void updateAllKeys() {
allKeys = [defaults dictionaryRepresentation].allKeys;
}
static NSString *getHardwareModel() {
struct utsname systemInfo;
uname(&systemInfo);
return [NSString stringWithUTF8String:systemInfo.machine];
}
%group Search
%hook YTSettingsViewController
- (void)loadWithModel:(id)model fromView:(UIView *)view {
%orig;
if ([[self valueForKey:@"_detailsCategoryID"] integerValue] == YTABCSection)
[self setValue:@(YES) forKey:@"_shouldShowSearchBar"];
}
- (void)setSectionControllers {
%orig;
if (![[self valueForKey:@"_shouldShowSearchBar"] boolValue]) return;
YTSettingsSectionController *settingsSectionController = [self settingsSectionControllers][[self valueForKey:@"_detailsCategoryID"]];
if (settingsSectionController == nil) return;
YTSearchableSettingsViewController *searchableVC = [self valueForKey:@"_searchableSettingsViewController"];
[searchableVC storeCollectionViewSections:@[settingsSectionController]];
}
%end
%end
%hook YTSettingsGroupData
- (NSArray <NSNumber *> *)orderedCategories {
if (self.type != 1 || class_getClassMethod(objc_getClass("YTSettingsGroupData"), @selector(tweaks)))
return %orig;
NSMutableArray *mutableCategories = %orig.mutableCopy;
[mutableCategories insertObject:@(YTABCSection) atIndex:0];
return mutableCategories.copy;
}
%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";
}
unichar uc = (unichar)c;
return [NSString stringWithCharacters:&uc length:1];;
}
%hook YTSettingsSectionItemManager
%new(v@:@)
- (void)updateYTABCSectionWithEntry:(id)entry {
NSMutableArray *sectionItems = [NSMutableArray array];
int totalSettings = 0;
NSBundle *tweakBundle = YTABCBundle();
BOOL isPhone = ![%c(YTCommonUtils) isIPad];
NSString *yesText = _LOC([NSBundle mainBundle], @"settings.yes");
NSString *cancelText = _LOC([NSBundle mainBundle], @"confirm.cancel");
NSString *deleteText = _LOC([NSBundle mainBundle], @"search.action.delete");
Class YTSettingsSectionItemClass = %c(YTSettingsSectionItem);
Class YTAlertViewClass = %c(YTAlertView);
if (tweakEnabled()) {
// AB flags
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];
updateAllKeys();
BOOL modified = [allKeys containsObject:getKey(method, classKey)];
NSString *modifiedTitle = modified ? [NSString stringWithFormat:@"%@ *", method] : method;
YTSettingsSectionItem *methodSwitch = [YTSettingsSectionItemClass switchItemWithTitle:modifiedTitle
titleDescription:isPhone && method.length > 26 ? modifiedTitle : 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) {
NSString *content = [NSString stringWithFormat:@"%@.%@", classKey, method];
YTAlertView *alertView = [YTAlertViewClass confirmationDialog];
alertView.title = method;
alertView.subtitle = content;
[alertView addTitle:LOC(@"COPY_TO_CLIPBOARD") withAction:^{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = content;
[[%c(GOOHUDManagerInternal) sharedInstance] showMessageMainThread:[%c(YTHUDMessage) messageWithText:LOC(@"COPIED_TO_CLIPBOARD")]];
}];
updateAllKeys();
NSString *key = getKey(method, classKey);
if ([allKeys containsObject:key]) {
[alertView addTitle:deleteText withAction:^{
[defaults removeObjectForKey:key];
updateAllKeys();
}];
}
[alertView addCancelButton:NULL];
[alertView show];
return NO;
}
settingItemId:0];
[properties[category] addObject:methodSwitch];
}
}
YTSettingsViewController *settingsViewController = [self valueForKey:@"_settingsViewControllerDelegate"];
BOOL grouped = groupedSettings();
for (NSString *category in properties) {
NSMutableArray <YTSettingsSectionItem *> *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 *headerItem = [YTSettingsSectionItemClass itemWithTitle:title accessibilityIdentifier:nil detailTextBlock:nil selectBlock:nil];
headerItem.enabled = NO;
[rows insertObject:headerItem atIndex:0];
YTSettingsSectionItem *sectionItem = [YTSettingsSectionItemClass itemWithTitle:title accessibilityIdentifier:nil detailTextBlock:nil selectBlock:^BOOL (YTSettingsCell *cell, NSUInteger arg1) {
YTSettingsPickerViewController *picker = [[%c(YTSettingsPickerViewController) alloc] initWithNavTitle:shortTitle pickerSectionTitle:nil rows:rows selectedItemIndex:0 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]];
// Import settings
YTSettingsSectionItem *import = [YTSettingsSectionItemClass itemWithTitle:LOC(@"IMPORT_SETTINGS")
titleDescription:[NSString stringWithFormat:LOC(@"IMPORT_SETTINGS_DESC"), @"YT(Cold|Hot|Global)Config.*: (0|1)"]
accessibilityIdentifier:nil
detailTextBlock:nil
selectBlock:^BOOL (YTSettingsCell *cell, NSUInteger arg1) {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSArray *lines = [pasteboard.string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
NSString *pattern = @"^(YT.*Config\\..*):\\s*(\\d)$";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
NSMutableDictionary *importedSettings = [NSMutableDictionary dictionary];
NSMutableArray *reportedSettings = [NSMutableArray array];
for (NSString *line in lines) {
NSTextCheckingResult *match = [regex firstMatchInString:line options:0 range:NSMakeRange(0, [line length])];
if (!match) continue;
NSString *key = [line substringWithRange:[match rangeAtIndex:1]];
id cacheValue = [cache valueForKeyPath:key];
if (cacheValue == nil) continue;
NSString *valueString = [line substringWithRange:[match rangeAtIndex:2]];
int integerValue = [valueString integerValue];
if (integerValue == 0 && ![cacheValue boolValue]) continue;
if (integerValue == 1 && [cacheValue boolValue]) continue;
importedSettings[key] = @(integerValue);
[reportedSettings addObject:[NSString stringWithFormat:@"%@: %d", key, integerValue]];
}
if (reportedSettings.count == 0) {
YTAlertView *alertView = [YTAlertViewClass infoDialog];
alertView.title = LOC(@"SETTINGS_TO_IMPORT");
alertView.subtitle = LOC(@"NOTHING_TO_IMPORT");
[alertView show];
return NO;
}
[reportedSettings insertObject:[NSString stringWithFormat:LOC(@"SETTINGS_TO_IMPORT_DESC"), reportedSettings.count] atIndex:0];
YTAlertView *alertView = [YTAlertViewClass confirmationDialogWithAction:^{
for (NSString *key in importedSettings) {
setValueFromImport(key, [importedSettings[key] boolValue]);
}
updateAllKeys();
} actionTitle:LOC(@"IMPORT")];
alertView.title = LOC(@"SETTINGS_TO_IMPORT");
alertView.subtitle = [reportedSettings componentsJoinedByString:@"\n"];
[alertView show];
return YES;
}];
[sectionItems insertObject:import atIndex:0];
// Copy current settings
YTSettingsSectionItem *copyAll = [YTSettingsSectionItemClass 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", classKey, key, [value boolValue]]];
}];
}
[content sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
[content insertObject:[NSString stringWithFormat:@"Device model: %@", getHardwareModel()] 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(GOOHUDManagerInternal) sharedInstance] showMessageMainThread:[%c(YTHUDMessage) messageWithText:LOC(@"COPIED_TO_CLIPBOARD")]];
return YES;
}];
[sectionItems insertObject:copyAll atIndex:0];
// View modified settings
YTSettingsSectionItem *modified = [YTSettingsSectionItemClass 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];
updateAllKeys();
for (NSString *key in 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 = [YTAlertViewClass confirmationDialogWithAction:^{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = content;
[[%c(GOOHUDManagerInternal) sharedInstance] showMessageMainThread:[%c(YTHUDMessage) messageWithText:LOC(@"COPIED_TO_CLIPBOARD")]];
} actionTitle:LOC(@"COPY_TO_CLIPBOARD")];
alertView.title = LOC(@"MODIFIED_SETTINGS_TITLE");
alertView.subtitle = content;
[alertView show];
return YES;
}];
[sectionItems insertObject:modified atIndex:0];
// Reset and kill
YTSettingsSectionItem *reset = [YTSettingsSectionItemClass itemWithTitle:LOC(@"RESET_KILL")
titleDescription:LOC(@"RESET_KILL_DESC")
accessibilityIdentifier:nil
detailTextBlock:nil
selectBlock:^BOOL (YTSettingsCell *cell, NSUInteger arg1) {
YTAlertView *alertView = [YTAlertViewClass confirmationDialogWithAction:^{
updateAllKeys();
for (NSString *key in allKeys) {
if ([key hasPrefix:Prefix])
[defaults removeObjectForKey:key];
}
exit(0);
} actionTitle:yesText];
alertView.title = LOC(@"WARNING");
alertView.subtitle = LOC(@"APPLY_DESC");
[alertView show];
return YES;
}];
[sectionItems insertObject:reset atIndex:0];
// Grouped settings
YTSettingsSectionItem *group = [YTSettingsSectionItemClass switchItemWithTitle:LOC(@"GROUPED")
titleDescription:nil
accessibilityIdentifier:nil
switchOn:groupedSettings()
switchBlock:^BOOL (YTSettingsCell *cell, BOOL enabled) {
YTAlertView *alertView = [YTAlertViewClass confirmationDialogWithAction:^{
[defaults setBool:enabled forKey:GroupedKey];
exit(0);
}
actionTitle:yesText
cancelAction:^{
[cell setSwitchOn:!enabled animated:YES];
[defaults setBool:!enabled forKey:GroupedKey];
}
cancelTitle:cancelText];
alertView.title = LOC(@"WARNING");
alertView.subtitle = LOC(@"APPLY_DESC");
[alertView show];
return YES;
}
settingItemId:0];
[sectionItems insertObject:group atIndex:0];
}
// Open megathread
YTSettingsSectionItem *thread = [YTSettingsSectionItemClass 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];
// Killswitch
YTSettingsSectionItem *master = [YTSettingsSectionItemClass switchItemWithTitle:LOC(@"ENABLED")
titleDescription:LOC(@"ENABLED_DESC")
accessibilityIdentifier:nil
switchOn:tweakEnabled()
switchBlock:^BOOL (YTSettingsCell *cell, BOOL enabled) {
[defaults setBool:enabled forKey:EnabledKey];
YTAlertView *alertView = [YTAlertViewClass confirmationDialogWithAction:^{ exit(0); }
actionTitle:yesText
cancelAction:^{
[cell setSwitchOn:!enabled animated:YES];
[defaults setBool:!enabled forKey:EnabledKey];
}
cancelTitle:cancelText];
alertView.title = LOC(@"WARNING");
alertView.subtitle = LOC(@"APPLY_DESC");
[alertView show];
return YES;
}
settingItemId:0];
[sectionItems insertObject:master atIndex:0];
YTSettingsViewController *delegate = [self valueForKey:@"_dataDelegate"];
NSString *title = @"A/B";
NSString *titleDescription = tweakEnabled() ? [NSString stringWithFormat:@"YTABConfig %@, %d feature flags.", @(OS_STRINGIFY(TWEAK_VERSION)), totalSettings] : nil;
if ([delegate respondsToSelector:@selector(setSectionItems:forCategory:title:icon:titleDescription:headerHidden:)]) {
YTIIcon *icon = [%c(YTIIcon) new];
icon.iconType = YT_EXPERIMENT;
[delegate setSectionItems:sectionItems
forCategory:YTABCSection
title:title
icon:icon
titleDescription:titleDescription
headerHidden:NO];
} else
[delegate setSectionItems:sectionItems
forCategory:YTABCSection
title:title
titleDescription:titleDescription
headerHidden:NO];
}
- (void)updateSectionForCategory:(NSUInteger)category withEntry:(id)entry {
if (category == YTABCSection) {
[self updateYTABCSectionWithEntry:entry];
return;
}
%orig;
}
%end
void SearchHook() {
%init(Search);
}
%ctor {
defaults = [NSUserDefaults standardUserDefaults];
%init;
}