]> git.saurik.com Git - patcyh.git/blobdiff - patcyh.mm
For some reason, lsd crashes on iOS 8.3 with this.
[patcyh.git] / patcyh.mm
index a2c5039165b5634b1f81f33e0f26e5f4e116497f..3000a5099d9be6a65b36f1e27f4a681a3682f808 100644 (file)
--- a/patcyh.mm
+++ b/patcyh.mm
 **/
 /* }}} */
 
+#include <pthread.h>
 #include <objc/runtime.h>
 #include <Foundation/Foundation.h>
 
+@class LSDXPCServer;
+
 @interface MIFileManager
 + (MIFileManager *) defaultManager;
 - (NSURL *) destinationOfSymbolicLinkAtURL:(NSURL *)url error:(NSError *)error;
@@ -29,6 +32,8 @@
 
 static Class $MIFileManager;
 
+static pthread_key_t key_;
+
 static NSArray *(*_MIFileManager$urlsForItemsInDirectoryAtURL$ignoringSymlinks$error$)(MIFileManager *self, SEL _cmd, NSURL *url, BOOL ignoring, NSError *error);
 
 static NSArray *$MIFileManager$urlsForItemsInDirectoryAtURL$ignoringSymlinks$error$(MIFileManager *self, SEL _cmd, NSURL *url, BOOL ignoring, NSError *error) {
@@ -45,20 +50,27 @@ static NSArray *$MIFileManager$urlsForItemsInDirectoryAtURL$ignoringSymlinks$err
         [items addObject:[NSURL fileURLWithPathComponents:[prefix arrayByAddingObjectsFromArray:[components subarrayWithRange:NSMakeRange(skip, [components count] - skip)]]]];
     }
 
-    NSLog(@"items = %@", items);
     return items;
 }
 
 static NSString *(*_NSURL$path)(NSURL *self, SEL _cmd);
 
 static NSString *$NSURL$path(NSURL *self, SEL _cmd) {
-    return [[_NSURL$path(self, _cmd) mutableCopy] autorelease];
+    NSString *path(_NSURL$path(self, _cmd));
+    if (pthread_getspecific(key_) != NULL)
+        path = [[path mutableCopy] autorelease];
+    return path;
 }
 
 static NSRange (*_NSString$rangeOfString$options$)(NSString *self, SEL _cmd, NSString *value, NSStringCompareOptions options);
 
 static NSRange $NSString$rangeOfString$options$(NSString *self, SEL _cmd, NSString *value, NSStringCompareOptions options) {
-    if ([value isEqualToString:@".app/"]) do {
+    do {
+        if (pthread_getspecific(key_) == NULL)
+            break;
+        if (![value isEqualToString:@".app/"])
+            break;
+
         char *real(realpath("/Applications", NULL));
         NSString *destiny([NSString stringWithUTF8String:real]);
         free(real);
@@ -70,16 +82,12 @@ static NSRange $NSString$rangeOfString$options$(NSString *self, SEL _cmd, NSStri
         if (![self hasPrefix:destiny])
             break;
 
-        if (![self hasSuffix:@".app"])
-            break;
-
         BOOL directory;
         if (![[NSFileManager defaultManager] fileExistsAtPath:self isDirectory:&directory])
             break;
         if (!directory)
             break;
 
-        NSLog(@"path = %@", self);
         // the trailing / allows lsd to "restart" its verification attempt
         [(NSMutableString *) self setString:[NSString stringWithFormat:@"/Applications/%@/", [self substringFromIndex:[destiny length]]]];
     } while (false);
@@ -87,8 +95,21 @@ static NSRange $NSString$rangeOfString$options$(NSString *self, SEL _cmd, NSStri
     return _NSString$rangeOfString$options$(self, _cmd, value, options);
 }
 
+static id (*_LSDXPCServer$canOpenURL$connection$)(LSDXPCServer *self, SEL _cmd, id url, id connection);
+
+static id $LSDXPCServer$canOpenURL$connection$(LSDXPCServer *self, SEL _cmd, id url, id connection) {
+    pthread_setspecific(key_, connection);
+    @try {
+        return _LSDXPCServer$canOpenURL$connection$(self, _cmd, url, connection);
+    } @finally {
+        pthread_setspecific(key_, NULL);
+    }
+}
+
 __attribute__((__constructor__))
 static void initialize() {
+    pthread_key_create(&key_, NULL);
+
     $MIFileManager = objc_getClass("MIFileManager");
 
     if ($MIFileManager != Nil) {
@@ -114,4 +135,12 @@ static void initialize() {
             method_setImplementation(method, reinterpret_cast<IMP>(&$NSString$rangeOfString$options$));
         }
     }
+
+    if (Class $LSDXPCServer = objc_getClass("LSDXPCServer")) {
+        SEL sel(@selector(canOpenURL:connection:));
+        if (Method method = class_getInstanceMethod($LSDXPCServer, sel)) {
+            _LSDXPCServer$canOpenURL$connection$ = reinterpret_cast<id (*)(LSDXPCServer *, SEL, id, id)>(method_getImplementation(method));
+            method_setImplementation(method, reinterpret_cast<IMP>(&$LSDXPCServer$canOpenURL$connection$));
+        }
+    }
 }