-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTweak.x
More file actions
169 lines (141 loc) · 4.12 KB
/
Tweak.x
File metadata and controls
169 lines (141 loc) · 4.12 KB
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
#import <notify.h>
#import <substrate.h>
#import <libcolorpicker.h>
#import <objc/runtime.h>
enum FPSMode{
kModeAverage=1,
kModePerSecond
};
static BOOL enabled;
static enum FPSMode fpsMode;
static dispatch_source_t _timer;
static UILabel *fpsLabel;
static void loadPref(){
NSLog(@"loadPref..........");
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:kPrefPath];
enabled=prefs[@"enabled"]?[prefs[@"enabled"] boolValue]:YES;
fpsMode=prefs[@"fpsMode"]?[prefs[@"fpsMode"] intValue]:0;
if(fpsMode==0) fpsMode++; //0.0.2 compatibility
NSString *colorString = prefs[@"color"]?:@"#ffff00";
UIColor *color = LCPParseColorString(colorString, nil);
[fpsLabel setHidden:!enabled];
[fpsLabel setTextColor:color];
}
static BOOL isEnabledApp(){
NSString* bundleIdentifier=[[NSBundle mainBundle] bundleIdentifier];
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:kPrefPath];
return [prefs[@"apps"] containsObject:bundleIdentifier];
}
double FPSavg = 0;
double FPSPerSecond = 0;
static void startRefreshTimer(){
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), (1.0/5.0) * NSEC_PER_SEC, 0);
dispatch_source_set_event_handler(_timer, ^{
switch(fpsMode){
case kModeAverage:
[fpsLabel setText:[NSString stringWithFormat:@"%.1lf",FPSavg]];
break;
case kModePerSecond:
[fpsLabel setText:[NSString stringWithFormat:@"%.1lf",FPSPerSecond]];
break;
default:
break;
}
NSLog(@"%.1lf %.1lf",FPSavg,FPSPerSecond);
});
dispatch_resume(_timer);
}
#pragma mark ui
#define kFPSLabelWidth 50
#define kFPSLabelHeight 20
%group ui
%hook UIWindow
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
CGRect bounds=[self bounds];
CGFloat safeOffsetY=0;
CGFloat safeOffsetX=0;
if(@available(iOS 11.0,*)) {
if(self.frame.size.width<self.frame.size.height){
safeOffsetY=self.safeAreaInsets.top;
}
else{
safeOffsetX=self.safeAreaInsets.right;
}
}
fpsLabel= [[UILabel alloc] initWithFrame:CGRectMake(bounds.size.width-kFPSLabelWidth-5.-safeOffsetX, safeOffsetY, kFPSLabelWidth, kFPSLabelHeight)];
fpsLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size:16];
fpsLabel.textAlignment=NSTextAlignmentRight;
fpsLabel.userInteractionEnabled=NO;
[self addSubview:fpsLabel];
loadPref();
startRefreshTimer();
});
return %orig;
}
%end
%end//ui
// credits to https://github.com/masagrator/NX-FPS/blob/master/source/main.cpp#L64
void frameTick(){
static double FPS_temp = 0;
static double starttick = 0;
static double endtick = 0;
static double deltatick = 0;
static double frameend = 0;
static double framedelta = 0;
static double frameavg = 0;
if (starttick == 0) starttick = CACurrentMediaTime()*1000.0;
endtick = CACurrentMediaTime()*1000.0;
framedelta = endtick - frameend;
frameavg = ((9*frameavg) + framedelta) / 10;
FPSavg = 1000.0f / (double)frameavg;
frameend = endtick;
FPS_temp++;
deltatick = endtick - starttick;
if (deltatick >= 1000.0f) {
starttick = CACurrentMediaTime()*1000.0;
FPSPerSecond = FPS_temp - 1;
FPS_temp = 0;
}
return;
}
#pragma mark gl
%group gl
%hook EAGLContext
- (BOOL)presentRenderbuffer:(NSUInteger)target{
BOOL ret=%orig;
frameTick();
return ret;
}
%end
%end//gl
#pragma mark metal
%group metal
%hook CAMetalDrawable
- (void)present{
%orig;
frameTick();
}
- (void)presentAfterMinimumDuration:(CFTimeInterval)duration{
%orig;
frameTick();
}
- (void)presentAtTime:(CFTimeInterval)presentationTime{
%orig;
frameTick();
}
%end //CAMetalDrawable
%end//metal
%ctor{
if(!isEnabledApp()) return;
NSLog(@"ctor: FPSIndicator");
%init(ui);
%init(gl);
%init(metal);
int token = 0;
notify_register_dispatch("com.brend0n.fpsindicator/loadPref", &token, dispatch_get_main_queue(), ^(int token) {
loadPref();
});
}