-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwizzleUtils.m
53 lines (43 loc) · 1.33 KB
/
SwizzleUtils.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
#import "SwizzleUtils.h"
static NSBundle *SULoadBundle (NSString *bundlePath, NSString **error) {
NSBundle *bundle = [NSBundle bundleWithPath: bundlePath];
if (!bundle) {
if (error)
*error = @"Bundle could not be found/opened.";
return nil;
}
NSError *frameworkError;
if (![bundle loadAndReturnError: &frameworkError]) {
if (error)
*error = [frameworkError localizedFailureReason];
return nil;
}
return bundle;
}
void SUNSLogvWithHeader (NSString *header, NSString *format, va_list args) {
NSLog (@"%@: %@", header, [[NSString alloc] initWithFormat: format arguments: args]);
}
BOOL SUWrapBundle (NSString *bundlePath, NSBundle **wrappedBundle, SULogger logger) {
if (*wrappedBundle)
return YES;
NSBundle *bundle;
NSString *error;
if (!(bundle = SULoadBundle (bundlePath, &error))) {
logger (@"Failed to load the wrapped bundle - %@: %@", bundlePath, error);
return NO;
}
// the casts are effectively a retain
*wrappedBundle = (__bridge NSBundle *) (__bridge_retained void *) bundle;
return YES;
}
BOOL SUUnwrapBundle (NSBundle **wrappedBundle, SULogger logger) {
NSBundle *bundle = *wrappedBundle;
if (!bundle)
return YES;
if (![bundle unload])
return NO;
// the casts are effectively a release
bundle = (__bridge_transfer NSBundle *) (__bridge void *) *wrappedBundle;
*wrappedBundle = nil;
return YES;
}