@@ -244,36 +244,38 @@ static int pt_usleep(useconds_t time) {
244244 [thread_sleep_lock lock ];
245245 });
246246
247- int thread_id = pthread_mach_thread_np (pthread_self ());
248- NSNumber *threadKey = @(thread_id);
249-
250- int thread_sleep_counter = [thread_sleep_counters[threadKey] intValue ];
251- int last_sleep_attempt = [last_sleep_attempts[threadKey] intValue ];
252-
253- if (time == 100000 ) {
254- int timestamp = (int )[[NSDate date ] timeIntervalSince1970 ];
255- // If it sleeps too fast, increase counter
256- if (timestamp - last_sleep_attempt < 2 ) {
257- thread_sleep_counter++;
258- } else {
259- thread_sleep_counter = 1 ;
260- }
261- last_sleep_attempt = timestamp;
262- thread_sleep_counters[threadKey] = @(thread_sleep_counter);
263- last_sleep_attempts[threadKey] = @(last_sleep_attempt);
247+ if ([[PlaySettings shared ] blockSleepSpamming ]) {
248+ int thread_id = pthread_mach_thread_np (pthread_self ());
249+ NSNumber *threadKey = @(thread_id);
264250
265- }
266-
267- if (thread_sleep_counter > 100 ) {
268- // Stop this thread from spamming usleep calls
269- NSLog (@" [PC] Thread %i exceeded usleep limit. Seem sus, stopping this "
270- @" thread FOREVER" ,
271- thread_id);
272-
273- [thread_sleep_lock lock ];
274- [thread_sleep_lock unlock ];
251+ int thread_sleep_counter = [thread_sleep_counters[threadKey] intValue ];
252+ int last_sleep_attempt = [last_sleep_attempts[threadKey] intValue ];
275253
276- return 0 ;
254+ if (time == 100000 ) {
255+ int timestamp = (int )[[NSDate date ] timeIntervalSince1970 ];
256+ // If it sleeps too fast, increase counter
257+ if (timestamp - last_sleep_attempt < 2 ) {
258+ thread_sleep_counter++;
259+ } else {
260+ thread_sleep_counter = 1 ;
261+ }
262+ last_sleep_attempt = timestamp;
263+ thread_sleep_counters[threadKey] = @(thread_sleep_counter);
264+ last_sleep_attempts[threadKey] = @(last_sleep_attempt);
265+
266+ }
267+
268+ if (thread_sleep_counter > 100 ) {
269+ // Stop this thread from spamming usleep calls
270+ NSLog (@" [PC] Thread %i exceeded usleep limit. Seem sus, stopping this "
271+ @" thread FOREVER" ,
272+ thread_id);
273+
274+ [thread_sleep_lock lock ];
275+ [thread_sleep_lock unlock ];
276+
277+ return 0 ;
278+ }
277279 }
278280
279281 return usleep (time);
@@ -301,13 +303,15 @@ static void __attribute__((constructor)) initialize(void) {
301303 [PlayKeychain debugLogger: [NSString stringWithFormat: @" UnrealEngine Hooked" ]];
302304 }
303305
304- // Add an observer so we can unlock threads on app termination
305- [[NSNotificationCenter defaultCenter ] addObserverForName: UIApplicationWillTerminateNotification
306- object: nil
307- queue: [NSOperationQueue mainQueue ]
308- usingBlock: ^(NSNotification * _Nonnull note) {
309- [thread_sleep_lock unlock ];
310- }];
306+ if ([[PlaySettings shared ] blockSleepSpamming ]) {
307+ // Add an observer so we can unlock threads on app termination
308+ [[NSNotificationCenter defaultCenter ] addObserverForName: UIApplicationWillTerminateNotification
309+ object: nil
310+ queue: [NSOperationQueue mainQueue ]
311+ usingBlock: ^(NSNotification * _Nonnull note) {
312+ [thread_sleep_lock unlock ];
313+ }];
314+ }
311315}
312316
313317@end
0 commit comments