1
0
mirror of https://github.com/opa334/TrollStore.git synced 2025-02-17 11:05:40 +08:00
TrollStore/TrollStore/TSApplicationsManager.m
2023-11-26 17:56:14 +01:00

165 lines
5.0 KiB
Objective-C

#import "TSApplicationsManager.h"
#import <TSUtil.h>
extern NSUserDefaults* trollStoreUserDefaults();
@implementation TSApplicationsManager
+ (instancetype)sharedInstance
{
static TSApplicationsManager *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[TSApplicationsManager alloc] init];
});
return sharedInstance;
}
- (NSArray*)installedAppPaths
{
return trollStoreInstalledAppBundlePaths();
}
- (NSError*)errorForCode:(int)code
{
NSString* errorDescription = @"Unknown Error";
switch(code)
{
// IPA install errors
case 166:
errorDescription = @"The IPA file does not exist or is not accessible.";
break;
case 167:
errorDescription = @"The IPA file does not appear to contain an app.";
break;
case 168:
errorDescription = @"Failed to extract IPA file.";
break;
case 169:
errorDescription = @"Failed to extract update tar file.";
break;
// App install errors
case 170:
errorDescription = @"Failed to create container for app bundle.";
break;
case 171:
errorDescription = @"A non-TrollStore app with the same identifier is already installed. If you are absolutely sure it is not, you can force install it.";
break;
case 172:
errorDescription = @"The app does not contain an Info.plist file.";
break;
case 174:
errorDescription = @"The app's main executable does not exist.";
break;
case 175:
errorDescription = @"Failed to sign the app.";
break;
case 176:
errorDescription = @"The app's Info.plist is missing required values.";
break;
case 177:
errorDescription = @"Failed to mark app as TrollStore app.";
break;
case 178:
errorDescription = @"Failed to copy app bundle.";
break;
case 179:
errorDescription = @"The app you tried to install has the same identifier as a system app already installed on the device. The installation has been prevented to protect you from possible bootloops or other issues.";
break;
}
NSError* error = [NSError errorWithDomain:TrollStoreErrorDomain code:code userInfo:@{NSLocalizedDescriptionKey : errorDescription}];
return error;
}
- (int)installIpa:(NSString*)pathToIpa force:(BOOL)force log:(NSString**)logOut
{
NSMutableArray* args = [NSMutableArray new];
[args addObject:@"install"];
if(force)
{
[args addObject:@"force"];
}
NSNumber* installationMethodToUseNum = [trollStoreUserDefaults() objectForKey:@"installationMethod"];
int installationMethodToUse = installationMethodToUseNum ? installationMethodToUseNum.intValue : 1;
if(installationMethodToUse == 1)
{
[args addObject:@"custom"];
}
else
{
[args addObject:@"installd"];
}
[args addObject:pathToIpa];
int ret = spawnRoot(rootHelperPath(), args, nil, logOut);
[[NSNotificationCenter defaultCenter] postNotificationName:@"ApplicationsChanged" object:nil];
return ret;
}
- (int)installIpa:(NSString*)pathToIpa
{
return [self installIpa:pathToIpa force:NO log:nil];
}
- (int)uninstallApp:(NSString*)appId
{
if(!appId) return -200;
NSMutableArray* args = [NSMutableArray new];
[args addObject:@"uninstall"];
NSNumber* uninstallationMethodToUseNum = [trollStoreUserDefaults() objectForKey:@"uninstallationMethod"];
int uninstallationMethodToUse = uninstallationMethodToUseNum ? uninstallationMethodToUseNum.intValue : 0;
if(uninstallationMethodToUse == 1)
{
[args addObject:@"custom"];
}
else
{
[args addObject:@"installd"];
}
[args addObject:appId];
int ret = spawnRoot(rootHelperPath(), args, nil, nil);
[[NSNotificationCenter defaultCenter] postNotificationName:@"ApplicationsChanged" object:nil];
return ret;
}
- (int)uninstallAppByPath:(NSString*)path
{
if(!path) return -200;
NSMutableArray* args = [NSMutableArray new];
[args addObject:@"uninstall-path"];
NSNumber* uninstallationMethodToUseNum = [trollStoreUserDefaults() objectForKey:@"uninstallationMethod"];
int uninstallationMethodToUse = uninstallationMethodToUseNum ? uninstallationMethodToUseNum.intValue : 0;
if(uninstallationMethodToUse == 1)
{
[args addObject:@"custom"];
}
else
{
[args addObject:@"installd"];
}
[args addObject:path];
int ret = spawnRoot(rootHelperPath(), args, nil, nil);
[[NSNotificationCenter defaultCenter] postNotificationName:@"ApplicationsChanged" object:nil];
return ret;
}
- (BOOL)openApplicationWithBundleID:(NSString *)appId
{
return [[LSApplicationWorkspace defaultWorkspace] openApplicationWithBundleID:appId];
}
- (int)changeAppRegistration:(NSString*)appPath toState:(NSString*)newState
{
if(!appPath || !newState) return -200;
return spawnRoot(rootHelperPath(), @[@"modify-registration", appPath, newState], nil, nil);
}
@end