-
Notifications
You must be signed in to change notification settings - Fork 15
/
Bundle.xm
executable file
·294 lines (264 loc) · 11.3 KB
/
Bundle.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
#import "ANEMSettingsManager.h"
#import "Bundle.h"
#import <objc/runtime.h>
#import <dlfcn.h>
//sysctl needed for legacy support (check iPad vs iPhone)
#include <sys/types.h>
#include <sys/sysctl.h>
#ifndef NO_OPTITHEME
#import "../common/CPDistributedMessagingCenter.h"
#import <rocketbootstrap/rocketbootstrap.h>
#endif
static NSMutableDictionary *cachedBundles = nil;
@implementation NSBundle (Anemone)
+ (NSBundle *) anemoneBundleWithFile:(NSString *)path {
path = [path stringByDeletingLastPathComponent];
if (path == nil || [path length] == 0 || [path isEqualToString:@"/"])
return nil;
NSBundle *bundle = nil;
if (!cachedBundles)
cachedBundles = [[NSMutableDictionary alloc] initWithCapacity:5];
bundle = [cachedBundles objectForKey:path];
if ((NSNull *)bundle == [NSNull null])
return nil;
else if (bundle == nil){
if ([[NSFileManager defaultManager] fileExistsAtPath:[path stringByAppendingPathComponent:@"Info.plist"]])
bundle = [NSBundle bundleWithPath:path];
if (bundle == nil)
bundle = [NSBundle anemoneBundleWithFile:path];
[cachedBundles setObject:(bundle == nil ? [NSNull null] : bundle) forKey:path];
}
return bundle;
}
@end
@implementation NSString (Anemone)
- (NSString *) anemoneThemedPath {
return [self anemoneThemedPath:NO];
}
- (NSString *) anemoneThemedPath:(BOOL)enableLegacy {
if (kCFCoreFoundationVersionNumber > MaxSupportedCFVersion)
return self;
NSString *themesDir = [[ANEMSettingsManager sharedManager] themesDir];
if ([self hasPrefix:@"/var/stash/anemonecache"])
return self;
if ([self hasPrefix:themesDir])
return self;
if ([self hasPrefix:@"/var/mobile/Library/Caches"])
return self;
if ([self hasSuffix:@".artwork"])
return self;
if ([self hasSuffix:@".car"])
return self;
self = [self stringByResolvingSymlinksInPath];
NSString *fileName = [self lastPathComponent];
NSArray *themes = [[ANEMSettingsManager sharedManager] themeSettings];
NSBundle *bundle = [NSBundle anemoneBundleWithFile:self];
if ([[ANEMSettingsManager sharedManager] masksOnly]){
if (![[bundle bundleIdentifier] isEqualToString:@"com.apple.mobileicons.framework"]){
return self;
}
}
NSString *fileEnding = fileName;
if (bundle){
NSString *prefix = [[bundle bundlePath] stringByResolvingSymlinksInPath];
if ([self hasPrefix:prefix])
fileEnding = [self substringFromIndex:[prefix length]+1];
}
BOOL useOptithemeInstead = NO;
#ifndef NO_OPTITHEME
if ([[ANEMSettingsManager sharedManager] optithemeEnabled]){
if (bundle){
NSString *cacheFolder = [@"/var/stash/anemonecache/" stringByAppendingPathComponent:[bundle bundleIdentifier]];
NSString *completeName = [cacheFolder stringByAppendingPathComponent:@".complete"];
if ([[NSFileManager defaultManager] fileExistsAtPath:completeName]){
useOptithemeInstead = YES;
NSString *path = [NSString stringWithFormat:@"%@/%@",cacheFolder,fileEnding];
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
return path;
if (enableLegacy){
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = (char *)malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithUTF8String:machine];
free(machine);
//this is slow and should be avoided
NSString *suffixToStrip = @"~iphone";
if ([platform rangeOfString:@"iPad"].location != NSNotFound)
suffixToStrip = @"~ipad";
if ([path rangeOfString:suffixToStrip].location != NSNotFound){
NSString *noSuffixPath = [path stringByReplacingOccurrencesOfString:suffixToStrip withString:@""];
if ([[NSFileManager defaultManager] fileExistsAtPath:noSuffixPath])
return noSuffixPath;
} else {
NSString *suffixToAdd = [suffixToStrip stringByAppendingString:@".png"];
NSString *suffixedPath = [path stringByReplacingOccurrencesOfString:@".png" withString:suffixToAdd];
if ([[NSFileManager defaultManager] fileExistsAtPath:suffixedPath])
return suffixedPath;
}
}
} else {
if ([bundle bundlePath] != nil){
dlopen("/System/Library/PrivateFrameworks/AppSupport.framework/AppSupport", RTLD_NOW);
CPDistributedMessagingCenter *client = [objc_getClass("CPDistributedMessagingCenter") centerNamed:@"com.anemonetheming.anemone.optitheme"];
rocketbootstrap_distributedmessagingcenter_apply(client);
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[bundle bundlePath],@"Path",nil];
[client sendMessageName:@"cacheCarWithOptions" userInfo:dict];
}
}
}
}
#endif
NSString *folderName = [[self stringByDeletingLastPathComponent] lastPathComponent];
for (NSString *theme in themes)
{
if (bundle && !useOptithemeInstead){
NSString *path = [NSString stringWithFormat:@"%@/%@.theme/Bundles/%@/%@",themesDir,theme,bundle.bundleIdentifier,fileEnding];
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
return path;
if (enableLegacy){
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = (char *)malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithUTF8String:machine];
free(machine);
//this is slow and should be avoided
NSString *suffixToStrip = @"~iphone";
if ([platform rangeOfString:@"iPad"].location != NSNotFound)
suffixToStrip = @"~ipad";
if ([path rangeOfString:suffixToStrip].location != NSNotFound){
NSString *noSuffixPath = [path stringByReplacingOccurrencesOfString:suffixToStrip withString:@""];
if ([[NSFileManager defaultManager] fileExistsAtPath:noSuffixPath])
return noSuffixPath;
} else {
NSString *suffixToAdd = [suffixToStrip stringByAppendingString:@".png"];
NSString *suffixedPath = [path stringByReplacingOccurrencesOfString:@".png" withString:suffixToAdd];
if ([[NSFileManager defaultManager] fileExistsAtPath:suffixedPath])
return suffixedPath;
}
}
if (SupportsNoExtensionDir){
path = [NSString stringWithFormat:@"%@/%@/Bundles/%@/%@",themesDir,theme,bundle.bundleIdentifier,fileEnding];
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
return path;
}
if (enableLegacy){
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = (char *)malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithUTF8String:machine];
free(machine);
//this is slow and should be avoided
NSString *suffixToStrip = @"~iphone";
if ([platform rangeOfString:@"iPad"].location != NSNotFound)
suffixToStrip = @"~ipad";
if ([path rangeOfString:suffixToStrip].location != NSNotFound){
NSString *noSuffixPath = [path stringByReplacingOccurrencesOfString:suffixToStrip withString:@""];
if ([[NSFileManager defaultManager] fileExistsAtPath:noSuffixPath])
return noSuffixPath;
} else {
NSString *suffixToAdd = [suffixToStrip stringByAppendingString:@".png"];
NSString *suffixedPath = [path stringByReplacingOccurrencesOfString:@".png" withString:suffixToAdd];
if ([[NSFileManager defaultManager] fileExistsAtPath:suffixedPath])
return suffixedPath;
}
}
}
NSString *pathFolders = [NSString stringWithFormat:@"%@/%@.theme/Folders/%@/%@",themesDir,theme,folderName,fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:pathFolders])
return pathFolders;
if (SupportsNoExtensionDir){
pathFolders = [NSString stringWithFormat:@"%@/%@/Folders/%@/%@",themesDir,theme,folderName,fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:pathFolders])
return pathFolders;
}
NSString *pathFallback = [NSString stringWithFormat:@"%@/%@.theme/Fallback/%@",themesDir,theme,fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:pathFallback])
return pathFallback;
if (SupportsNoExtensionDir){
pathFallback = [NSString stringWithFormat:@"%@/%@/Fallback/%@",themesDir,theme,fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:pathFallback])
return pathFallback;
}
}
return self;
}
@end
%group BundleHook
%hook NSBundle
/*- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName {
//XXX: This is broken and needs to be fixed before it can be re-enabled
// There hasn't been a huge demand to support theming locale strings, so keep this disabled until there is one.
NSLocale *locale = [NSLocale currentLocale];
NSString *language = [locale objectForKey:NSLocaleLanguageCode];
if (!tableName)
tableName = @"Localizable";
NSArray *themes = [[ANEMSettingsManager sharedManager] themeSettings];
for (NSString *theme in themes)
{
NSString *path = [NSString stringWithFormat:@"/Library/Themes/%@.theme/Bundles/%@/%@.lproj/%@.strings",theme,self.bundleIdentifier,language,tableName];
if (![[NSFileManager defaultManager] fileExistsAtPath:path]){
if (SupportsNoExtensionDir){
path = [NSString stringWithFormat:@"/Library/Themes/%@/Bundles/%@/%@.lproj/%@.strings",theme,self.bundleIdentifier,language,tableName];
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
continue;
}
}
NSDictionary *localizationFile = [NSDictionary dictionaryWithContentsOfFile:path];
if ([localizationFile objectForKey:key])
return [localizationFile objectForKey:key];
}
return %orig;
}*/
- (NSString *)pathForResource:(NSString *)resource ofType:(NSString *)type {
if (kCFCoreFoundationVersionNumber > MaxSupportedCFVersion)
return %orig;
NSString *themesDir = [[ANEMSettingsManager sharedManager] themesDir];
NSString *fileName;
if (type != nil && [type length] != 0 && ![type isEqualToString:@""])
fileName = [resource stringByAppendingFormat:@".%@",type];
else
fileName = resource;
NSArray *themes = [[ANEMSettingsManager sharedManager] themeSettings];
#ifndef NO_OPTITHEME
NSString *cacheFolder = [@"/var/stash/anemonecache/" stringByAppendingPathComponent:[self bundleIdentifier]];
NSString *completeName = [cacheFolder stringByAppendingPathComponent:@".complete"];
if ([[ANEMSettingsManager sharedManager] optithemeEnabled]){
if ([[NSFileManager defaultManager] fileExistsAtPath:completeName]){
//load optithemed image
NSString *path = [NSString stringWithFormat:@"%@/%@",cacheFolder,fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
return path;
} else {
if ([self bundlePath] != nil){
dlopen("/System/Library/PrivateFrameworks/AppSupport.framework/AppSupport", RTLD_NOW);
CPDistributedMessagingCenter *client = [objc_getClass("CPDistributedMessagingCenter") centerNamed:@"com.anemonetheming.anemone.optitheme"];
rocketbootstrap_distributedmessagingcenter_apply(client);
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[self bundlePath],@"Path",nil];
[client sendMessageName:@"cacheCarWithOptions" userInfo:dict];
}
}
}
#endif
for (NSString *theme in themes)
{
NSString *path = [NSString stringWithFormat:@"%@/%@.theme/Bundles/%@/%@",themesDir,theme,self.bundleIdentifier,fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
return path;
if (SupportsNoExtensionDir){
path = [NSString stringWithFormat:@"%@/%@/Bundles/%@/%@",themesDir,theme,self.bundleIdentifier,fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
return path;
}
}
return %orig;
}
%end
%end
%ctor {
if (![[[NSBundle mainBundle] bundleIdentifier] isEqualToString:@"org.coolstar.anemone"]){
%init(BundleHook);
}
}