-#define SubstrateLaunchDaemons_ "/Library/LaunchDaemons"
-#define SubstrateVariable_ "DYLD_INSERT_LIBRARIES"
-#define SubstrateLibrary_ "/usr/lib/libuicache.dylib"
-
-static bool HookInstall() {
- NSString *file([NSString stringWithFormat:@"%@/%s.plist", @ SubstrateLaunchDaemons_, "com.apple.mobile.installd"]);
- if (file == nil)
- return false;
-
- NSMutableDictionary *root([NSMutableDictionary dictionaryWithContentsOfFile:file]);
- if (root == nil)
- return false;
-
- NSMutableDictionary *environment([root objectForKey:@"EnvironmentVariables"]);
- if (environment == nil) {
- environment = [NSMutableDictionary dictionaryWithCapacity:1];
- if (environment == nil)
- return false;
-
- [root setObject:environment forKey:@"EnvironmentVariables"];
- }
-
- NSString *variable([environment objectForKey:@ SubstrateVariable_]);
- if (variable == nil || [variable length] == 0)
- [environment setObject:@ SubstrateLibrary_ forKey:@ SubstrateVariable_];
- else {
- NSArray *dylibs([variable componentsSeparatedByString:@":"]);
- if (dylibs == nil)
- return false;
-
- NSUInteger index([dylibs indexOfObject:@ SubstrateLibrary_]);
- if (index != NSNotFound)
- return false;
-
- [environment setObject:[NSString stringWithFormat:@"%@:%@", variable, @ SubstrateLibrary_] forKey:@ SubstrateVariable_];
- }
-
- NSString *error;
- NSData *data([NSPropertyListSerialization dataFromPropertyList:root format:NSPropertyListBinaryFormat_v1_0 errorDescription:&error]);
- if (data == nil)
- return false;
-
- if (![data writeToFile:file atomically:YES])
- return false;
-
- system("launchctl unload /Library/LaunchDaemons/com.apple.mobile.installd.plist");
- system("launchctl load /Library/LaunchDaemons/com.apple.mobile.installd.plist");
- return true;
-}
-