-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTweak.xm
164 lines (145 loc) · 5.59 KB
/
Tweak.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
#import "Tweak.h"
#import "Headers.h"
NSBundle *bundle = [NSBundle mainBundle];
NSString *bunlde_id = [bundle bundleIdentifier];
NSString *plist_path = [NSString stringWithFormat:@"%@/Library/Preferences/%@.plist", NSHomeDirectory(), bunlde_id]; \
inline NSMutableDictionary* getPref(NSString* plugin_name){
// NSLog(@"%@", plist_path)
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:plist_path];
if ([prefs objectForKey:@"enmity"] && [[prefs objectForKey:@"enmity"] objectForKey:plugin_name]){
return [[prefs objectForKey:@"enmity"] objectForKey:plugin_name];
}
return nil;
}
/* Enmity comannd handling: https://github.com/enmity-mod/tweak/blob/main/src/Commands.x */
// Create a response to a command
NSDictionary* createResponse(NSString *uuid, NSString *data) {
NSDictionary *response = @{
@"id": uuid,
@"data": data
};
return response;
}
// Send a response back
void sendResponse(NSDictionary *response) {
NSError *err;
NSData *data = [NSJSONSerialization
dataWithJSONObject:response
options:0
error:&err];
if (err) {
return;
}
NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *responseString = [NSString stringWithFormat: @"%@%@", ENMITY_PROTOCOL, [json stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]];
NSURL *url = [NSURL URLWithString:responseString];
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
}
// Validate that a command is using the Enmity scheme
BOOL validateCommand(NSString *command) {
BOOL valid = [command containsString:@"enmity"];
return valid;
}
// Clean the received command
NSString* cleanCommand(NSString *command) {
NSString *json = [[command stringByReplacingOccurrencesOfString:ENMITY_PROTOCOL withString:@""] stringByRemovingPercentEncoding];
return json;
}
// Parse the command
NSDictionary* parseCommand(NSString *json) {
NSURLComponents* components = [[NSURLComponents alloc] initWithString:json];
NSArray *queryItems = components.queryItems;
NSMutableDictionary *command = [[NSMutableDictionary alloc] init];
for (NSURLQueryItem *item in queryItems) {
if ([item.name isEqualToString:@"id"]) {
command[@"id"] = item.value;
}
if ([item.name isEqualToString:@"command"]) {
command[@"command"] = item.value;
}
if ([item.name isEqualToString:@"params"]) {
command[@"params"] = [item.value componentsSeparatedByString:@","];
}
}
return [command copy];
}
BOOL hasBiometricsPerm(){
// [K2geLocker] check biometrics perm
NSMutableDictionary *infoPlistDict = [NSMutableDictionary dictionaryWithDictionary:[[NSBundle mainBundle] infoDictionary]];
return [infoPlistDict objectForKey:@"NSFaceIDUsageDescription"] != nil ? true : false;
}
void handleAuthenticate(NSString *uuid) {
// [K2geLocker] handle biometrics authentication
LAContext *context = [[LAContext alloc] init];
if (hasBiometricsPerm()) {
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"K2geLocker" reply:^(BOOL success, NSError * _Nullable error) {
if (success){ // on authentication success
sendResponse(createResponse(uuid, @"success"));
} else {
// NSString* errorStr = [NSString stringWithFormat:@"%@", error];
// sendResponse(createResponse(uuid, errorStr));
sendResponse(createResponse(uuid, @"fail"));
}
}];
} else {
sendResponse(createResponse(uuid, @"fail"));
}
}
// Handle the command
void handleCommand(NSDictionary *command) {
NSString *name = [command objectForKey:@"command"];
if (name == nil) {
return;
}
NSString *uuid = [command objectForKey:@"id"];
NSArray *params = [command objectForKey:@"params"];
// [K2geLocker] respond to check
if ([name isEqualToString:@"K2geLocker"]) {
if ([params[0] isEqualToString:@"check"]){ // check installed and has perms
sendResponse(createResponse(uuid, hasBiometricsPerm() ? @"yes" : @"no"));
} else if ([params[0] isEqualToString:@"authentication"]){ // do authentication
handleAuthenticate(uuid);
}
}
}
%hook GULAppDelegateSwizzler // handle it before Enmity hooks it
- (BOOL)application: (UIApplication *)app openURL: (NSURL *)url options: (NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
// NSLog(@"K2genmity | openURL: %@", url.absoluteString);
NSString *input = url.absoluteString;
if (!validateCommand(input)) {
%orig;
return true;
}
NSString *json = cleanCommand(input);
NSDictionary *command = parseCommand(json);
handleCommand(command);
return %orig(app, url, options); // pass to Enmity
}
%end
// [HighlightCode] change the font & size
%hook YYLabel
- (void)setAttributedText: (NSAttributedString *)attributedText {
/* filter code block by content */
if ([[attributedText string] hasSuffix:@"-- By CodeHighlight"]){
CGFloat fontSize = 10;
BOOL changeFont = false;
NSMutableDictionary* pref = getPref(@"HighlightCode");
if (pref){
changeFont = (BOOL)[pref objectForKey:@"change_font"];
NSNumber* num = [pref objectForKey:@"font_size"];
fontSize = [num doubleValue];
}
/* create editable NSMutableAttributeString with the original attributedText */
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:attributedText];
__block NSString* fontName = [[self font] fontName];
if (changeFont){
fontName = @"Courier";
}
/* change the font */
[attributedString addAttribute: NSFontAttributeName value:[UIFont fontWithName:fontName size:fontSize] range: NSMakeRange(0, [attributedText length])];
%orig(attributedString);
} else {
%orig;
}
}
%end