forked from Tonwalter888/YouMod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYouModPerferences.x
More file actions
148 lines (133 loc) · 5.72 KB
/
YouModPerferences.x
File metadata and controls
148 lines (133 loc) · 5.72 KB
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
#import "Headers.h"
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h> // For import
#define Prefix @"YouMod"
static NSBundle *YouModBundle() {
static NSBundle *bundle = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *tweakBundlePath = [[NSBundle mainBundle] pathForResource:Prefix ofType:@"bundle"];
if (tweakBundlePath)
bundle = [NSBundle bundleWithPath:tweakBundlePath];
else
bundle = [NSBundle bundleWithPath:[NSString stringWithFormat:PS_ROOT_PATH_NS(@"/Library/Application Support/%@.bundle"), Prefix]];
});
return bundle;
}
#define LOC(x) [YouModBundle() localizedStringForKey:x value:nil table:nil]
@implementation YouModPrefsManager
+ (instancetype)sharedManager {
static YouModPrefsManager *shared = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shared = [[self alloc] init];
});
return shared;
}
// Import
- (void)importYouModSettingsFromVC:(UIViewController *)vc {
NSArray<UTType *> *types = @[UTTypePropertyList, UTTypeData];
// Modern constructor for iOS 14+
UIDocumentPickerViewController *picker = [[UIDocumentPickerViewController alloc] initForOpeningContentTypes:types asCopy:YES];
picker.delegate = self;
picker.modalPresentationStyle = UIModalPresentationFormSheet;
// Ensure it looks right on iPad
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
picker.popoverPresentationController.sourceView = vc.view;
picker.popoverPresentationController.sourceRect = CGRectMake(vc.view.bounds.size.width/2, vc.view.bounds.size.height/2, 0, 0);
picker.popoverPresentationController.permittedArrowDirections = 0;
}
[vc presentViewController:picker animated:YES completion:nil];
}
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
NSURL *selectedFileURL = urls.firstObject;
if (!selectedFileURL) return;
NSDictionary *importedData = [NSDictionary dictionaryWithContentsOfURL:selectedFileURL];
// Vaild plist check
if (!importedData || ![importedData isKindOfClass:[NSDictionary class]]) {
YTAlertView *alertView = [%c(YTAlertView) infoDialog];
alertView.title = LOC(@"ERROR");
alertView.subtitle = LOC(@"ERROR_INVALID_FILE");
[alertView show];
return;
}
BOOL foundKeys = NO;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// Remove old keys
for (NSString *key in [defaults dictionaryRepresentation]) {
if ([key hasPrefix:Prefix]) {
[defaults removeObjectForKey:key];
}
}
[defaults synchronize];
// Set new key from file
for (NSString *key in importedData) {
if ([key hasPrefix:Prefix]) {
[defaults setObject:importedData[key] forKey:key];
foundKeys = YES;
}
}
// Check if there's any YouMod key
if (!foundKeys) {
YTAlertView *alertView = [%c(YTAlertView) infoDialog];
alertView.title = LOC(@"ERROR");
alertView.subtitle = LOC(@"ERROR_NO_KEYS_IMPORT");
[alertView show];
return;
}
[defaults synchronize];
// Success Alert with Restart
YTAlertView *alertView = [%c(YTAlertView) confirmationDialogWithAction:^{
exit(0);
} actionTitle:LOC(@"YES")];
alertView.title = LOC(@"IMPORT_DONE");
alertView.subtitle = LOC(@"APPLY_DESC"); // "Restart required"
[alertView show];
}
// Export
- (void)exportYouModSettingsFromVC:(UIViewController *)vc {
NSDictionary *allSettings = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
NSMutableDictionary *youModOnly = [NSMutableDictionary dictionary];
for (NSString *key in allSettings) {
if ([key hasPrefix:Prefix]) {
youModOnly[key] = allSettings[key];
}
}
if (youModOnly.count == 0) {
YTAlertView *alertView = [%c(YTAlertView) infoDialog];
alertView.title = LOC(@"ERROR");
alertView.subtitle = LOC(@"ERROR_NO_KEYS_EXPORT");
[alertView show];
return;
}
NSString *tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"YouMod_Preferences.plist"];
NSURL *fileURL = [NSURL fileURLWithPath:tempPath];
[youModOnly writeToURL:fileURL atomically:YES];
UIDocumentPickerViewController *picker = [[UIDocumentPickerViewController alloc] initForExportingURLs:@[fileURL] asCopy:YES];
picker.modalPresentationStyle = UIModalPresentationFormSheet;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
picker.popoverPresentationController.sourceView = vc.view;
}
[vc presentViewController:picker animated:YES completion:nil];
}
// Reset
- (void)restoreYouModDefaults {
YTAlertView *alertView = [%c(YTAlertView) confirmationDialogWithAction:^{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
for (NSString *key in [defaults dictionaryRepresentation]) {
if ([key hasPrefix:Prefix]) {
[defaults removeObjectForKey:key];
}
}
[defaults setBool:YES forKey:AutoClearCache];
[defaults setBool:YES forKey:YTPremiumLogo];
[defaults setBool:YES forKey:HideCreateButton];
[defaults setBool:YES forKey:HideCastButtonNav];
[defaults setBool:YES forKey:HideCastButtonPlayer];
[defaults synchronize];
exit(0);
} actionTitle:LOC(@"YES")];
alertView.title = LOC(@"WARNING");
alertView.subtitle = LOC(@"RESETDEFAULT");
[alertView show];
}
@end