-
Notifications
You must be signed in to change notification settings - Fork 3
/
Tweak.xm
156 lines (133 loc) · 6.26 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
//Needed to play audio
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#import <UIKit/UIKit.h>
//Definitions for usage
#define kIdentifier @"com.6ilent.pokefullcharge"
#define kSettingsChangedNotification (CFStringRef)@"com.6ilent.pokefullcharge/ReloadPrefs"
#define kSettingsPath @"/var/mobile/Library/Preferences/com.6ilent.pokefullcharge.plist"
//Variables from the preference file
static NSString *bundlePrefix = @"/Library/MobileSubstrate/DynamicLibraries/com.6ilent.pokefullcharge.bundle";
static BOOL enabled;
static BOOL lowenabled;
static NSInteger batPercent;
static NSInteger lowbatPercent;
static NSInteger sound;
static NSInteger lowsound;
//Creates the audio player
AVAudioPlayer *player;
AVAudioPlayer *lowplayer;
//Hooks the needed method
%hook UIDevice
-(void)_setBatteryLevel:(float)arg1 {
//Runs the original code
%orig;
//Creates a UIDevice object which is used to get the battery level and store it in 'batLeft'
UIDevice *dev = [UIDevice currentDevice];
float batLeft = [dev batteryLevel]*100;
//Formats the string with the remaining battery so it can be used
NSString *batString = [NSString stringWithFormat:@"%0.0f", batLeft];
//Formats our preference integer into a string for comparison
// //We have to cast our integer as a long because the compiler likes it like that
NSString *batPercent_String = [NSString stringWithFormat:@"%ld", (long)batPercent];
NSString *lowbatPercent_String = [NSString stringWithFormat:@"%ld", (long)lowbatPercent];
//Sound player path variables
NSString *soundFilePath = nil;
NSURL *soundFileURL;
//Runs the code IF the tweak is ENABLED
if (enabled) {
//If its our percent value and hasn't played yet
if ([batString isEqualToString:batPercent_String]) {
// // YES: Play the healing sound
if (player) {
return;
}
//Sets the sound paths sound
if (sound == 0)
soundFilePath = [NSString stringWithFormat:@"%@/heal.mp3", bundlePrefix];
else if (sound == 1)
soundFilePath = [NSString stringWithFormat:@"%@/lowhealth.mp3", bundlePrefix];
else if (sound == 2)
soundFilePath = [NSString stringWithFormat:@"%@/solidalert.mp3", bundlePrefix];
else if (sound == 3)
soundFilePath = [NSString stringWithFormat:@"%@/switch_bing.wav", bundlePrefix];
else if (sound == 4)
soundFilePath = [NSString stringWithFormat:@"%@/switch_enter.wav", bundlePrefix];
else if (sound == 5)
soundFilePath = [NSString stringWithFormat:@"%@/switch_user.wav", bundlePrefix];
//Grabs the sound file from the bundle path and makes the NSURL equal to it
soundFileURL = [NSURL fileURLWithPath:soundFilePath];
//Makes the audio player object and plays the sound 0(actually 1) time(s)
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = 0;
[player play];
} else {
if (player) {
player = nil;
}
}
}
//Same as above but with the low variables instead
if (lowenabled) {
if ([batString isEqualToString:lowbatPercent_String]) {
if (lowplayer) {
return;
}
if (lowsound == 0)
soundFilePath = [NSString stringWithFormat:@"%@/heal.mp3", bundlePrefix];
else if (lowsound == 1)
soundFilePath = [NSString stringWithFormat:@"%@/lowhealth.mp3", bundlePrefix];
else if (lowsound == 2)
soundFilePath = [NSString stringWithFormat:@"%@/solidalert.mp3", bundlePrefix];
else if (lowsound == 3)
soundFilePath = [NSString stringWithFormat:@"%@/switch_bing.wav", bundlePrefix];
else if (lowsound == 4)
soundFilePath = [NSString stringWithFormat:@"%@/switch_enter.wav", bundlePrefix];
else if (lowsound == 5)
soundFilePath = [NSString stringWithFormat:@"%@/switch_user.wav", bundlePrefix];
else
soundFilePath = [NSString stringWithFormat:@"%@/lowhealth.mp3", bundlePrefix];
soundFileURL = [NSURL fileURLWithPath:soundFilePath];
lowplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
lowplayer.numberOfLoops = 0;
[lowplayer play];
} else {
if (lowplayer) {
lowplayer = nil;
}
}
}
}
%end
//Reloads the Preferences
static void reloadPrefs() {
//Identifies our tweak
CFPreferencesAppSynchronize((CFStringRef)kIdentifier);
//Defines our dictionary and adds the values
NSDictionary *prefs = nil;
if ([NSHomeDirectory() isEqualToString:@"/var/mobile"]) {
CFArrayRef keys = CFPreferencesCopyKeyList((CFStringRef)kIdentifier, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
if (keys != nil) {
prefs = (NSDictionary *)CFBridgingRelease(CFPreferencesCopyMultiple(keys, (CFStringRef)kIdentifier, kCFPreferencesCurrentUser, kCFPreferencesAnyHost));
if (prefs == nil) {
prefs = [NSDictionary dictionary];
}
CFRelease(keys);
}
} else {
prefs = [NSDictionary dictionaryWithContentsOfFile:kSettingsPath];
}
//Assigns the values that are in the prefs Dictionary into our variables
enabled = [prefs objectForKey:@"enabled"] ? [(NSNumber *)[prefs objectForKey:@"enabled"] boolValue] : false;
batPercent = [prefs objectForKey:@"batPercent"] ? [[prefs objectForKey:@"batPercent"] integerValue] : 100;
lowenabled = [prefs objectForKey:@"lowenabled"] ? [(NSNumber *)[prefs objectForKey:@"lowenabled"] boolValue] : false;
lowbatPercent = [prefs objectForKey:@"lowbatPercent"] ? [[prefs objectForKey:@"lowbatPercent"] integerValue] : 20;
sound = [prefs objectForKey:@"sound"] ? [[prefs objectForKey:@"sound"] integerValue] : 0;
lowsound = [prefs objectForKey:@"lowsound"] ? [[prefs objectForKey:@"lowsound"] integerValue] : 1;
}
//The constructor of our tweak (What gets loaded before anything else, in a sense the "entry point" of our tweak)
// // Make sure it's at the end so everything gets loaded into memory first and then can construct
%ctor {
reloadPrefs();
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)reloadPrefs, kSettingsChangedNotification, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
}