-
-
Notifications
You must be signed in to change notification settings - Fork 200
/
NSUserDefaults.m
232 lines (205 loc) · 8.51 KB
/
NSUserDefaults.m
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
//
// NSUserDefaults.m
// LiveContainer
//
// Created by s s on 2024/11/29.
//
#import <Foundation/Foundation.h>
#import "LCSharedUtils.h"
#import "UIKitPrivate.h"
#import "utils.h"
#import "LCSharedUtils.h"
void swizzle(Class class, SEL originalAction, SEL swizzledAction) {
method_exchangeImplementations(class_getInstanceMethod(class, originalAction), class_getInstanceMethod(class, swizzledAction));
}
@interface NSUserDefaults(LiveContainer)
+ (instancetype)lcSharedDefaults;
+ (instancetype)lcUserDefaults;
+ (NSString *)lcAppUrlScheme;
+ (NSString *)lcAppGroupPath;
@end
NSMutableDictionary* LCPreferences = 0;
void NUDGuestHooksInit() {
swizzle(NSUserDefaults.class, @selector(objectForKey:), @selector(hook_objectForKey:));
swizzle(NSUserDefaults.class, @selector(boolForKey:), @selector(hook_boolForKey:));
swizzle(NSUserDefaults.class, @selector(integerForKey:), @selector(hook_integerForKey:));
swizzle(NSUserDefaults.class, @selector(setObject:forKey:), @selector(hook_setObject:forKey:));
swizzle(NSUserDefaults.class, @selector(removeObjectForKey:), @selector(hook_removeObjectForKey:));
swizzle(NSUserDefaults.class, @selector(dictionaryRepresentation), @selector(hook_dictionaryRepresentation));
swizzle(NSUserDefaults.class, @selector(persistentDomainForName:), @selector(hook_persistentDomainForName:));
swizzle(NSUserDefaults.class, @selector(removePersistentDomainForName:), @selector(hook_removePersistentDomainForName:));
swizzle(NSUserDefaults.class, @selector(registerDefaults:), @selector(hook_registerDefaults:));
LCPreferences = [[NSMutableDictionary alloc] init];
NSFileManager* fm = NSFileManager.defaultManager;
NSURL* libraryPath = [fm URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask].lastObject;
NSURL* preferenceFolderPath = [libraryPath URLByAppendingPathComponent:@"Preferences"];
if(![fm fileExistsAtPath:preferenceFolderPath.path]) {
NSError* error;
[fm createDirectoryAtPath:preferenceFolderPath.path withIntermediateDirectories:YES attributes:@{} error:&error];
}
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillTerminateNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification * _Nonnull notification) {
// restore language if needed
NSArray* savedLaunguage = [NSUserDefaults.lcUserDefaults objectForKey:@"LCLastLanguages"];
if(savedLaunguage) {
[NSUserDefaults.lcUserDefaults setObject:savedLaunguage forKey:@"AppleLanguages"];
}
}];
}
NSURL* LCGetPreferencePath(NSString* identifier) {
NSFileManager* fm = NSFileManager.defaultManager;
NSURL* libraryPath = [fm URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask].lastObject;
NSURL* preferenceFilePath = [libraryPath URLByAppendingPathComponent:[NSString stringWithFormat: @"Preferences/%@.plist", identifier]];
return preferenceFilePath;
}
NSMutableDictionary* LCGetPreference(NSString* identifier) {
if(LCPreferences[identifier]) {
return LCPreferences[identifier];
}
NSURL* preferenceFilePath = LCGetPreferencePath(identifier);
if([NSFileManager.defaultManager fileExistsAtPath:preferenceFilePath.path]) {
LCPreferences[identifier] = [NSMutableDictionary dictionaryWithContentsOfFile:preferenceFilePath.path];
return LCPreferences[identifier];
} else {
return nil;
}
}
// save preference to livecontainer's user default
void LCSavePreference(void) {
NSString* containerId = [[NSString stringWithUTF8String:getenv("HOME")] lastPathComponent];
[NSUserDefaults.lcUserDefaults setObject:LCPreferences forKey:containerId];
}
@implementation NSUserDefaults(LiveContainerHooks)
- (id)hook_objectForKey:(NSString*)key {
// let LiveContainer itself bypass
NSString* identifier = [self _identifier];
if([identifier isEqualToString:(__bridge id)kCFPreferencesCurrentApplication]) {
return [self hook_objectForKey:key];
}
// priortize local preference file over values in native NSUserDefaults
NSMutableDictionary* preferenceDict = LCGetPreference(identifier);
if(preferenceDict && preferenceDict[key]) {
return preferenceDict[key];
} else {
return [self hook_objectForKey:key];
}
}
- (BOOL)hook_boolForKey:(NSString*)key {
id obj = [self objectForKey:key];
if(!obj) {
return NO;
} else if ([obj isKindOfClass:[NSNumber class]]) {
return [(NSNumber*)obj boolValue];
} else if([obj isKindOfClass:[NSString class]]) {
NSString* lowered = [(NSString*)obj lowercaseString];
if([lowered isEqualToString:@"yes"] || [lowered isEqualToString:@"true"] || [lowered boolValue]) {
return YES;
} else {
return NO;
}
} else {
return obj != 0;
}
}
- (NSInteger)hook_integerForKey:(NSString*)key {
id obj = [self objectForKey:key];
if(!obj) {
return 0;
} else if([obj isKindOfClass:[NSString class]]) {
return [(NSString*)obj integerValue];
} else if ([obj isKindOfClass:[NSNumber class]]) {
return [(NSNumber*)obj integerValue];
}
return 0;
}
- (void)hook_setObject:(id)obj forKey:(NSString*)key {
// let LiveContainer itself bypess
NSString* identifier = [self _identifier];
if([identifier isEqualToString:(__bridge id)kCFPreferencesCurrentApplication]) {
return [self hook_setObject:obj forKey:key];
}
@synchronized (LCPreferences) {
NSMutableDictionary* preferenceDict = LCGetPreference(identifier);
if(!preferenceDict) {
preferenceDict = [[NSMutableDictionary alloc] init];
LCPreferences[identifier] = preferenceDict;
}
preferenceDict[key] = obj;
LCSavePreference();
}
}
- (void)hook_removeObjectForKey:(NSString*)key {
NSString* identifier = [self _identifier];
if([self hook_objectForKey:key]) {
[self hook_removeObjectForKey:key];
return;
}
@synchronized (LCPreferences) {
NSMutableDictionary* preferenceDict = LCGetPreference(identifier);
if(!preferenceDict) {
return;
}
[preferenceDict removeObjectForKey:key];
LCSavePreference();
}
}
- (NSDictionary*) hook_dictionaryRepresentation {
NSString* identifier = [self _identifier];
NSMutableDictionary* ans = [[self hook_dictionaryRepresentation] mutableCopy];
if(ans) {
@synchronized (LCPreferences) {
[ans addEntriesFromDictionary:LCGetPreference(identifier)];
}
} else {
ans = LCGetPreference(identifier);
}
return ans;
}
- (NSDictionary*) hook_persistentDomainForName:(NSString*)domainName {
NSMutableDictionary* ans = [[self hook_persistentDomainForName:domainName] mutableCopy];
if(ans) {
@synchronized (LCPreferences) {
[ans addEntriesFromDictionary:LCGetPreference(domainName)];
}
} else {
ans = LCGetPreference(domainName);
}
return ans;
}
- (void) hook_removePersistentDomainForName:(NSString*)domainName {
NSMutableDictionary* ans = [[self hook_persistentDomainForName:domainName] mutableCopy];
@synchronized (LCPreferences) {
if(ans) {
[self hook_removePersistentDomainForName:domainName];
} else {
// empty dictionary means deletion
[LCPreferences setObject:[[NSMutableArray alloc] init] forKey:domainName];
LCSavePreference();
}
NSURL* preferenceFilePath = LCGetPreferencePath(domainName);
NSFileManager* fm = NSFileManager.defaultManager;
if([fm fileExistsAtPath:preferenceFilePath.path]) {
NSError* error;
[fm removeItemAtURL:preferenceFilePath error:&error];
}
}
}
- (void) hook_registerDefaults:(NSDictionary<NSString *,id> *)registrationDictionary {
NSString* identifier = [self _identifier];
@synchronized (LCPreferences) {
NSMutableDictionary* preferenceDict = LCGetPreference(identifier);
if(!preferenceDict) {
preferenceDict = [[NSMutableDictionary alloc] init];
LCPreferences[identifier] = preferenceDict;
}
for(NSString* key in registrationDictionary) {
if(![preferenceDict objectForKey:key]) {
preferenceDict[key] = registrationDictionary[key];
}
}
LCSavePreference();
}
}
@end