diff --git a/Natives/LauncherNavigationController.m b/Natives/LauncherNavigationController.m index 6c401421..2ec9c162 100644 --- a/Natives/LauncherNavigationController.m +++ b/Natives/LauncherNavigationController.m @@ -11,8 +11,9 @@ #import "PickTextField.h" #import "PLPickerView.h" #import "PLProfiles.h" -#import "ios_uikit_bridge.h" +#import "UIKit+AFNetworking.h" #import "UIKit+hook.h" +#import "ios_uikit_bridge.h" #import "utils.h" #define AUTORESIZE_MASKS UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin @@ -313,12 +314,7 @@ #pragma mark - UIPickerView stuff - (void)pickerView:(PLPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { self.profileSelectedAt = row; -/* - if (self.versionList.count == 0) { - self.versionTextField.text = @""; - return; - } -*/ + //((UIImageView *)self.versionTextField.leftView).image = [pickerView imageAtRow:row column:component]; ((UIImageView *)self.versionTextField.leftView).image = [pickerView imageAtRow:row column:component]; self.versionTextField.text = [self pickerView:pickerView titleForRow:row forComponent:component]; PLProfiles.current.selectedProfileName = self.versionTextField.text; @@ -336,14 +332,10 @@ return PLProfiles.current.profiles.allValues[row][@"name"]; } -- (UIImage *)pickerView:(UIPickerView *)pickerView imageForRow:(NSInteger)row forComponent:(NSInteger)component { +- (void)pickerView:(UIPickerView *)pickerView enumerateImageView:(UIImageView *)imageView forRow:(NSInteger)row forComponent:(NSInteger)component { + UIImage *fallbackImage = [[UIImage imageNamed:@"DefaultProfile"] _imageWithSize:CGSizeMake(40, 40)]; NSString *urlString = PLProfiles.current.profiles.allValues[row][@"icon"]; - if (!urlString) { - return [[UIImage imageNamed:@"DefaultProfile"] _imageWithSize:CGSizeMake(40, 40)]; - } - NSString *iconStr = [urlString stringByReplacingOccurrencesOfString:@"data:image/png;base64," withString:@""]; - NSData *iconData = [[NSData alloc] initWithBase64EncodedString:iconStr options:NSDataBase64DecodingIgnoreUnknownCharacters]; - return [[UIImage imageWithData:iconData] _imageWithSize:CGSizeMake(40, 40)]; + [imageView setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:fallbackImage]; } - (void)versionClosePicker { diff --git a/Natives/LauncherProfilesViewController.m b/Natives/LauncherProfilesViewController.m index 59250787..4461fe43 100644 --- a/Natives/LauncherProfilesViewController.m +++ b/Natives/LauncherProfilesViewController.m @@ -7,11 +7,17 @@ #import "LauncherProfilesViewController.h" //#import "NSFileManager+NRFileManager.h" #import "PLProfiles.h" +#import "UIKit+AFNetworking.h" #import "UIKit+hook.h" #import "installer/FabricInstallViewController.h" #import "ios_uikit_bridge.h" #import "utils.h" +typedef NS_ENUM(NSUInteger, LauncherProfilesTableSection) { + kInstances, + kProfiles +}; + @interface LauncherProfilesViewController () // @property(nonatomic) UIBarButtonItem *createButtonItem; @@ -154,30 +160,30 @@ cell.detailTextLabel.text = profile[@"lastVersionId"]; cell.imageView.layer.magnificationFilter = kCAFilterNearest; - if (profile[@"icon"]) { - NSString *iconStr = [profile[@"icon"] stringByReplacingOccurrencesOfString:@"data:image/png;base64," withString:@""]; - NSData *iconData = [[NSData alloc] initWithBase64EncodedString:iconStr options:NSDataBase64DecodingIgnoreUnknownCharacters]; - cell.imageView.image = [[UIImage imageWithData:iconData] _imageWithSize:CGSizeMake(40, 40)]; - } else { - cell.imageView.image = [[UIImage imageNamed:@"DefaultProfile"] _imageWithSize:CGSizeMake(40, 40)]; - } + UIImage *fallbackImage = [[UIImage imageNamed:@"DefaultProfile"] _imageWithSize:CGSizeMake(40, 40)]; + [cell.imageView setImageWithURL:[NSURL URLWithString:profile[@"icon"]] placeholderImage:fallbackImage]; } - (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DefaultCell"]; + NSString *cellID = indexPath.section == kInstances ? @"InstanceCell" : @"ProfileCell"; + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (cell == nil) { - cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"DefaultCell"]; + cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.detailTextLabel.numberOfLines = 0; cell.detailTextLabel.lineBreakMode = NSLineBreakByWordWrapping; + if (indexPath.section == kProfiles) { + cell.imageView.frame = CGRectMake(0, 0, 40, 40); + cell.imageView.isSizeFixed = YES; + } } else { cell.imageView.image = nil; cell.userInteractionEnabled = YES; cell.accessoryView = nil; } - if (indexPath.section == 0) { + if (indexPath.section == kInstances) { [self setupInstanceCell:cell atRow:indexPath.row]; } else { [self setupProfileCell:cell atRow:indexPath.row]; @@ -190,7 +196,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:NO]; - if (indexPath.section == 0) { + if (indexPath.section == kInstances) { if (indexPath.row == 0) { [self.navigationController pushViewController:[LauncherPrefGameDirViewController new] animated:YES]; } @@ -232,7 +238,7 @@ - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { - if (indexPath.section == 0 || PLProfiles.current.profiles.count==1) { + if (indexPath.section == kInstances || PLProfiles.current.profiles.count==1) { return UITableViewCellEditingStyleNone; } return UITableViewCellEditingStyleDelete; diff --git a/Natives/PLPickerView.h b/Natives/PLPickerView.h index 4b622573..1179b8b2 100644 --- a/Natives/PLPickerView.h +++ b/Natives/PLPickerView.h @@ -11,5 +11,7 @@ @protocol PLPickerViewDelegate @required -- (UIImage * _Nullable)pickerView:(PLPickerView * _Nonnull)pickerView imageForRow:(NSInteger)row forComponent:(NSInteger)component; +- (void)pickerView:(PLPickerView * _Nonnull)pickerView enumerateImageView:(UIImageView * _Nonnull)imageView forRow:(NSInteger)row forComponent:(NSInteger)component; + +//- (UIImage * _Nullable)pickerView:(PLPickerView * _Nonnull)pickerView imageForRow:(NSInteger)row forComponent:(NSInteger)component; @end diff --git a/Natives/PLPickerView.m b/Natives/PLPickerView.m index 11ed8b5d..db61d4d8 100644 --- a/Natives/PLPickerView.m +++ b/Natives/PLPickerView.m @@ -1,10 +1,15 @@ #import "PLPickerView.h" +#import "UIKit+hook.h" @implementation PLPickerView - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath]; - cell.imageView.image = [(id)self.delegate pickerView:self imageForRow:indexPath.row forComponent:indexPath.section]; + //cell.imageView.image = [(id)self.delegate pickerView:self imageForRow:indexPath.row forComponent:indexPath.section]; + cell.imageView.frame = CGRectMake(0, 0, 40, 40); + cell.imageView.isSizeFixed = YES; + cell.imageView.layer.magnificationFilter = kCAFilterNearest; + [(id)self.delegate pickerView:self enumerateImageView:cell.imageView forRow:indexPath.row forComponent:indexPath.section]; return cell; } diff --git a/Natives/UIKit+hook.h b/Natives/UIKit+hook.h index c485c884..14576a0b 100644 --- a/Natives/UIKit+hook.h +++ b/Natives/UIKit+hook.h @@ -6,6 +6,10 @@ - (UIUserInterfaceIdiom)hook_userInterfaceIdiom; @end +@interface UIImageView(hook) +@property(nonatomic) BOOL isSizeFixed; +@end + // private functions @interface UIContextMenuInteraction(private) - (void)_presentMenuAtLocation:(CGPoint)location; diff --git a/Natives/UIKit+hook.m b/Natives/UIKit+hook.m index 4abd8dab..ad2c327e 100644 --- a/Natives/UIKit+hook.m +++ b/Natives/UIKit+hook.m @@ -14,6 +14,7 @@ void swizzleClass(Class class, SEL originalAction, SEL swizzledAction) { void init_hookUIKitConstructor(void) { swizzle(UIDevice.class, @selector(userInterfaceIdiom), @selector(hook_userInterfaceIdiom)); + swizzle(UIImageView.class, @selector(setImage:), @selector(hook_setImage:)); swizzle(UIView.class, @selector(didMoveToSuperview), @selector(hook_didMoveToSuperview)); swizzle(UIView.class, @selector(setFrame:), @selector(hook_setFrame:)); @@ -45,26 +46,47 @@ void init_hookUIKitConstructor(void) { @end +// Patch: emulate scaleToFill for table views +@implementation UIImageView(hook) + +- (BOOL)isSizeFixed { + return [objc_getAssociatedObject(self, @selector(isSizeFixed)) boolValue]; +} + +- (void)setIsSizeFixed:(BOOL)fixed { + objc_setAssociatedObject(self, @selector(isSizeFixed), @(fixed), OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + +- (void)hook_setImage:(UIImage *)image { + if (self.isSizeFixed) { + UIImage *resizedImage = [image _imageWithSize:self.frame.size]; + [self hook_setImage:resizedImage]; + } else { + [self hook_setImage:image]; + } +} + +@end + // Patch: unimplemented get/set UIToolbar functions on tvOS @implementation UINavigationController(hook) -const NSString *toolbarKey = @"toolbar"; - (UIToolbar *)hook_toolbar { - UIToolbar *toolbar = objc_getAssociatedObject(self, &toolbarKey); + UIToolbar *toolbar = objc_getAssociatedObject(self, @selector(toolbar)); if (toolbar == nil) { toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(self.view.bounds.origin.x, self.view.bounds.size.height - 100, self.view.bounds.size.width, 100)]; toolbar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth; toolbar.backgroundColor = UIColor.systemBackgroundColor; - objc_setAssociatedObject(self, &toolbarKey, toolbar, OBJC_ASSOCIATION_RETAIN_NONATOMIC); + objc_setAssociatedObject(self, @selector(toolbar), toolbar, OBJC_ASSOCIATION_RETAIN_NONATOMIC); [self performSelector:@selector(_configureToolbar)]; } return toolbar; } - (void)hook_setToolbar:(UIToolbar *)toolbar { - objc_setAssociatedObject(self, &toolbarKey, toolbar, OBJC_ASSOCIATION_RETAIN_NONATOMIC); + objc_setAssociatedObject(self, @selector(toolbar), toolbar, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } @end diff --git a/Natives/installer/FabricInstallViewController.m b/Natives/installer/FabricInstallViewController.m index 27601024..04afd397 100644 --- a/Natives/installer/FabricInstallViewController.m +++ b/Natives/installer/FabricInstallViewController.m @@ -1,5 +1,6 @@ #import "AFNetworking.h" #import "FabricInstallViewController.h" +#import "LauncherNavigationController.h" #import "LauncherPreferences.h" #import "LauncherProfileEditorViewController.h" #import "PickTextField.h" @@ -24,14 +25,14 @@ - (void)viewDidLoad { // Setup navigation bar self.title = localize(@"profile.title.install_fabric_quilt", nil); - self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(actionDone)]; + self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(actionDone:)]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemClose target:self action:@selector(actionClose)]; // Setup appearance self.prefSectionsVisible = YES; // Setup preference getter and setter - __weak FabricInstallViewController *weakSelf = self; + __weak __typeof(self) weakSelf = self; self.localKVO = @{ @"gameVersion": @"1.20.1", @"loaderVendor": @"Fabric", @@ -100,11 +101,13 @@ @"Fabric": @{ @"game": @"https://meta.fabricmc.net/v2/versions/game", @"loader": @"https://meta.fabricmc.net/v2/versions/loader", + @"icon": @"https://avatars.githubusercontent.com/u/21025855?s=64", @"json": @"https://meta.fabricmc.net/v2/versions/loader/%@/%@/profile/json" }, @"Quilt": @{ @"game": @"https://meta.quiltmc.org/v3/versions/game", @"loader": @"https://meta.quiltmc.org/v3/versions/loader", + @"icon": @"https://raw.githubusercontent.com/QuiltMC/art/master/brand/64png/quilt_logo_transparent.png", @"json": @"https://meta.quiltmc.org/v3/versions/loader/%@/%@/profile/json" } }; @@ -117,7 +120,7 @@ id errorCallback = ^(NSURLSessionTask *operation, NSError *error) { if (!errorShown) { errorShown = YES; - NSLog(@"Error: %@", error); + NSDebugLog(@"Error: %@", error); showDialog(localize(@"Error", nil), error.localizedDescription); [self actionClose]; } @@ -125,12 +128,12 @@ AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; NSDictionary *endpoint = self.endpoints[self.localKVO[@"loaderVendor"]]; [manager GET:endpoint[@"game"] parameters:nil headers:nil progress:nil success:^(NSURLSessionTask *task, NSArray *response) { - NSLog(@"[%@ Installer] Got %d game versions", self.localKVO[@"loaderVendor"], response.count); + NSDebugLog(@"[%@ Installer] Got %d game versions", self.localKVO[@"loaderVendor"], response.count); self.versionMetadata = response; [self changeVersionTypeTo:[self.localKVO[@"gameType_index"] intValue]]; } failure:errorCallback]; [manager GET:endpoint[@"loader"] parameters:nil headers:nil progress:nil success:^(NSURLSessionTask *task, NSArray *response) { - NSLog(@"[%@ Installer] Got %d loader versions", self.localKVO[@"loaderVendor"], response.count); + NSDebugLog(@"[%@ Installer] Got %d loader versions", self.localKVO[@"loaderVendor"], response.count); self.loaderMetadata = response; [self.loaderList removeAllObjects]; [self.loaderList addObjectsFromArray:[response valueForKey:@"version"]]; @@ -141,29 +144,38 @@ [self.navigationController dismissViewControllerAnimated:YES completion:nil]; } -- (void)actionDone { +- (void)actionDone:(UIBarButtonItem *)sender { + sender.enabled = NO; + NSDictionary *endpoint = self.endpoints[self.localKVO[@"loaderVendor"]]; NSString *path = [NSString stringWithFormat:endpoint[@"json"], self.localKVO[@"gameVersion"], self.localKVO[@"loaderVersion"]]; - NSLog(@"[%@ Installer] Downloading %@", self.localKVO[@"loaderVendor"], path); + NSDebugLog(@"[%@ Installer] Downloading %@", self.localKVO[@"loaderVendor"], path); AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; [manager GET:path parameters:nil headers:nil progress:nil success:^(NSURLSessionTask *task, NSDictionary *response) { + sender.enabled = YES; + NSString *jsonPath = [NSString stringWithFormat:@"%1$s/versions/%2$@/%2$@.json", getenv("POJAV_GAME_DIR"), response[@"id"]]; [NSFileManager.defaultManager createDirectoryAtPath:jsonPath.stringByDeletingLastPathComponent withIntermediateDirectories:YES attributes:nil error:nil]; NSError *error = saveJSONToFile(response, jsonPath); if (error) { showDialog(localize(@"Error", nil), error.localizedDescription); } else { + [localVersionList addObject:@{ + @"id": response[@"id"], + @"type": @"custom"}]; // Jump to the profile editor LauncherProfileEditorViewController *vc = [LauncherProfileEditorViewController new]; vc.profile = @{ + @"icon": endpoint[@"icon"], @"name": response[@"id"], @"lastVersionId": response[@"id"] }.mutableCopy; [self.navigationController pushViewController:vc animated:YES]; } } failure:^(NSURLSessionTask *operation, NSError *error) { - NSLog(@"Error: %@", error); + sender.enabled = YES; + NSDebugLog(@"Error: %@", error); showDialog(localize(@"Error", nil), error.localizedDescription); }]; }