-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathUtilities.h
87 lines (69 loc) · 3.34 KB
/
Utilities.h
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
#import <CydiaSubstrate.h>
#import <UIKit/UIKit.h>
#import <RemoteLog.h> // For debugging
#import <rootless.h>
#import <version.h>
#define PREF_CHANGED_NOTIF "SNMessenger/prefChanged"
extern BOOL isDarkMode;
extern NSBundle *tweakBundle;
static inline MSImageRef getImageRef(NSString *framework) {
NSString *frameworkPath = [NSString stringWithFormat:@"%@/Frameworks/%@", [[NSBundle mainBundle] bundlePath], framework];
NSBundle *bundle = [NSBundle bundleWithPath:frameworkPath];
if (!bundle.loaded) [bundle load];
return MSGetImageByName([frameworkPath UTF8String]);
}
static inline CGFloat MessengerVersion() {
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
return [[version substringToIndex:5] floatValue];
}
static inline NSString *localizedStringForKey(NSString *key) {
return [tweakBundle localizedStringForKey:key value:nil table:nil];
}
static inline NSString *getSettingsPlistPath() {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"SNMessenger.plist"];
return plistPath;
}
static inline NSMutableDictionary *getCurrentSettings() {
return [[NSMutableDictionary alloc] initWithContentsOfFile:getSettingsPlistPath()] ?: [@{} mutableCopy];
}
static inline NSMutableDictionary *compareDictionaries(NSDictionary *oldDict, NSDictionary *newDict) {
NSMutableDictionary *result = [NSMutableDictionary dictionary];
[newDict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id newDictObj, BOOL *stop) {
id oldDictObj = oldDict[key];
if (!oldDictObj || ![newDictObj isEqual:oldDictObj]) {
result[key] = newDictObj;
}
}];
return result;
}
static inline UIImage *getImage(NSString *name) {
NSString *path = [tweakBundle pathForResource:name ofType:@"png"];
return [UIImage imageWithContentsOfFile:path];
}
static inline UIImage *getTemplateImage(NSString *name) {
return [getImage(name) imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
}
static inline UIImage *scaleImageWithSize(UIImage *image, CGSize size) {
if (!image) return nil;
UIGraphicsBeginImageContextWithOptions(size, NO, UIScreen.mainScreen.scale);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
static inline CGFloat colorComponentFrom(NSString *string, NSUInteger start, NSUInteger length) {
NSString *substring = [string substringWithRange:NSMakeRange(start, length)];
NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
unsigned int hexComponent;
[[NSScanner scannerWithString:fullHex] scanHexInt:&hexComponent];
return hexComponent / 255.0f;
}
static inline UIColor *colorWithHexString(NSString *hexString) {
CGFloat Red = colorComponentFrom(hexString, 1, 2);
CGFloat Green = colorComponentFrom(hexString, 3, 2);
CGFloat Blue = colorComponentFrom(hexString, 5, 2);
CGFloat Alpha = [hexString length] == 9 ? colorComponentFrom(hexString, 7, 2) : 1.0f;
return [UIColor colorWithRed:Red green:Green blue:Blue alpha:Alpha];
}