-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Tweak.xm
247 lines (225 loc) · 7.53 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
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
#import <UIKit/UIKit.h>
#import <Goose/MGGooseView.h>
#import <Goose/MGViewController.h>
#import <Goose/MGGooseController.h>
#import <rootless.h>
@interface MGWindow : UIWindow
@end
@interface SpringBoard : NSObject
- (BOOL)isLocked;
- (NSSet<UIWindowScene *> *)connectedScenes;
@end
static CGAffineTransform transform;
static UIWindow *gooseWindow;
static NSArray *honks;
static NSPointerArray *containers;
static MGViewController *viewController;
static NSDictionary<NSString *, NSBundle *> *modBundles;
static NSDictionary<NSString *, NSArray<NSObject<MGMod> *> *> *allModObjects;
CGAffineTransform MGGetTransform(void) {
return transform;
}
%subclass MGWindow : UIWindow
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
UIView *viewAtPoint = [self.rootViewController.view hitTest:point withEvent:event];
if (!viewAtPoint || (viewAtPoint == self.rootViewController.view)) return NO;
else return YES;
}
%end
%hook MGViewController
- (void)activeInterfaceOrientationDidChangeToOrientation:(long long)arg1 willAnimateWithDuration:(double)arg2 fromOrientation:(long long)arg3 {
transform = [self transformForOrientation:arg1];
for (UIView *view in viewController.view.subviews) {
view.transform = transform;
}
[UIView animateWithDuration:0.5 animations:^{
self.view.alpha = 1.0;
}];
}
- (void)activeInterfaceOrientationWillChangeToOrientation:(long long)arg1 {
[UIView animateWithDuration:0.5 animations:^{
self.view.alpha = 0.0;
}];
}
%end
%hook SpringBoard
- (void)applicationDidFinishLaunching:(id)application {
%orig;
viewController = [MGViewController new];
transform = [viewController transformForOrientation:UIInterfaceOrientationPortrait];
gooseWindow = (UIWindow *)[[%c(MGWindow) alloc] initWithFrame:UIScreen.mainScreen.bounds];
if (@available(iOS 13.0, *)) {
UIWindowScene *scene = self.connectedScenes.allObjects[0];
gooseWindow.windowScene = scene;
}
else {
gooseWindow.screen = [UIScreen mainScreen];
}
gooseWindow.rootViewController = viewController;
gooseWindow.userInteractionEnabled = YES;
gooseWindow.opaque = NO;
gooseWindow.hidden = NO;
gooseWindow.backgroundColor = [UIColor clearColor];
gooseWindow.windowLevel = CGFLOAT_MAX / 2.0;
[gooseWindow makeKeyAndVisible];
containers = [NSPointerArray weakObjectsPointerArray];
NSMutableArray *mHonks = [NSMutableArray new];
const NSInteger gooseCount = @(floor(((NSNumber *)(PrefValue(@"GooseCount") ?: @1)).doubleValue)).integerValue;
NSMutableDictionary *modObjects = [NSMutableDictionary new];
for (NSInteger i=0; i<gooseCount; i++) {
CGRect frame = CGRectMake(0, 0, 0, 0);
MGGooseView *honk = [[MGGooseView alloc] initWithFrame:frame];
MGGooseController *controller = [[MGGooseController alloc] initWithGoose:honk];
frame.size = [honk sizeThatFits:frame.size];
frame.origin = CGPointMake(
arc4random_uniform(gooseWindow.frame.size.width - frame.size.width),
arc4random_uniform(gooseWindow.frame.size.height - frame.size.height)
);
honk.frame = frame;
honk.shouldRenderFrameBlock = ^BOOL(MGGooseView *_gooseView){
BOOL disabled = (
[(SpringBoard *)UIApplication.sharedApplication isLocked] ||
![(PrefValue(@"Enabled") ?: @YES) boolValue]
);
gooseWindow.userInteractionEnabled = !disabled;
gooseWindow.hidden = disabled;
return !disabled;
};
[gooseWindow.rootViewController.view addSubview:honk];
honk.layer.zPosition = 100;
[mHonks addObject:honk];
honk.facingTo = 0.0;
[controller startLooping];
// Initialize mods
NSArray *mods;
NSMutableArray *mMods = [NSMutableArray new];
for (NSBundle *bundle in modBundles.allValues) {
__kindof NSObject<MGMod> *mod = [bundle.principalClass alloc];
if ([mod respondsToSelector:@selector(initWithGoose:bundle:)]) {
mod = [mod initWithGoose:honk bundle:bundle];
}
else if ([mod respondsToSelector:@selector(initWithGoose:)]) {
mod = [mod initWithGoose:honk];
}
else {
mod = [mod init];
}
if (mod) {
[mMods addObject:mod];
if ([mod respondsToSelector:@selector(enabled)]) {
mod.enabled = [(NSNumber *)([NSUserDefaults.standardUserDefaults
objectForKey:@"Enabled"
inDomain:bundle.bundleIdentifier
] ?: @YES) boolValue];
}
}
if (!modObjects[bundle.bundlePath]) {
modObjects[bundle.bundlePath] = [NSMutableArray new];
}
NSMutableArray *array = modObjects[bundle.bundlePath];
[array addObject:mod];
}
mods = mMods.copy;
mMods = nil;
objc_setAssociatedObject(honk, @selector(mods), mods, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
// Make the goose notify its mods
[honk addFrameHandler:^(MGGooseView *sender, MGGooseFrameState state){
NSArray *mods = objc_getAssociatedObject(sender, @selector(mods));
for (__kindof NSObject<MGMod> *mod in mods) {
if ([mod respondsToSelector:@selector(enabled)] && !mod.enabled) continue;
if ([mod respondsToSelector:@selector(handleFrameInState:)]) {
[mod handleFrameInState:state];
}
}
}];
// Causes a retain cycle, but MGGooseViews are never deallocated so it's fine
objc_setAssociatedObject(honk, @selector(controller), controller, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
// Add preference observers
for (NSString *key in modObjects.allKeys.copy) {
modObjects[key] = [(NSObject *)modObjects[key] copy];
}
allModObjects = modObjects.copy;
[[%c(NSDistributedNotificationCenter) defaultCenter]
addObserverForName:@"com.pixelomer.mobilegoose/ModPreferencesChanged"
object:nil
queue:nil
usingBlock:^(NSNotification *notification){
NSArray *allRelatedObjects = allModObjects[notification.object];
NSString *key = notification.userInfo[@"key"];
id newValue = notification.userInfo[@"newValue"];
for (NSObject<MGMod> *mod in allRelatedObjects) {
if ([key isEqualToString:@"Enabled"]) {
if ([mod respondsToSelector:@selector(enabled)]) {
mod.enabled = [(NSNumber *)newValue boolValue];
}
}
else if ([mod respondsToSelector:@selector(preferenceWithKey:didChangeToValue:)]) {
[mod preferenceWithKey:key didChangeToValue:newValue];
}
}
}
];
// Finalization
honks = mHonks.copy;
mHonks = nil;
for (MGGooseView *honk in honks) {
NSArray *mods = objc_getAssociatedObject(honk, @selector(mods));
for (__kindof NSObject<MGMod> *mod in mods) {
if ([mod respondsToSelector:@selector(springboardDidFinishLaunching::)]) {
[mod springboardDidFinishLaunching:self];
}
}
}
}
%end
static void MGResetPreferences(
CFNotificationCenterRef center,
void *observer,
CFNotificationName name,
const void *object,
CFDictionaryRef userInfo
) {
[NSUserDefaults.standardUserDefaults synchronize];
}
static void MGHandleExitNotification(
CFNotificationCenterRef center,
void *observer,
CFNotificationName name,
const void *object,
CFDictionaryRef userInfo
) {
exit(0);
}
%ctor {
CFNotificationCenterAddObserver(
CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
&MGResetPreferences,
CFSTR("com.pixelomer.mobilegoose/PreferenceChange"),
NULL,
0
);
CFNotificationCenterAddObserver(
CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
&MGHandleExitNotification,
CFSTR("com.pixelomer.mobilegoose/Exit"),
NULL,
0
);
NSString *dir = ROOT_PATH_NS(@"/Library/MobileGoose/Mods");
NSArray *mods = [NSFileManager.defaultManager
contentsOfDirectoryAtPath:dir
error:nil
];
NSMutableDictionary *mModBundles = [NSMutableDictionary new];
for (NSString *filename in mods) {
NSString *fullFilePath = [dir stringByAppendingPathComponent:filename];
NSBundle *bundle = [NSBundle bundleWithPath:fullFilePath];
if ([bundle load]) {
mModBundles[bundle.bundlePath] = bundle;
}
}
modBundles = mModBundles.copy;
}