Play-/Source/ui_ios/CoverViewController.mm

259 lines
7.9 KiB
Text
Raw Permalink Normal View History

2017-10-31 07:35:57 -04:00
#import <SDWebImage/UIImageView+WebCache.h>
#import "CoverViewController.h"
#import "EmulatorViewController.h"
#import "SettingsViewController.h"
2017-10-31 07:35:57 -04:00
#import "../ui_shared/BootablesProcesses.h"
#import "../ui_shared/BootablesDbClient.h"
#import "PathUtils.h"
2015-08-17 12:48:29 -04:00
#import "BackgroundLayer.h"
#import "CoverViewCell.h"
#import "AltServerJitService.h"
static bool IsJitAvailable()
{
//If ppid != 1, it means we're being run in the debugger
if(getppid() != 1) return true;
if([[AltServerJitService sharedAltServerJitService] jitEnabled])
{
return true;
}
{
//Check if we can scan the mobile directory (only possible if jailbroken)
std::error_code errorCode;
fs::directory_iterator dirIterator("/private/var/mobile", errorCode);
if(!errorCode)
{
return true;
}
}
return false;
}
@interface CoverViewController ()
@end
@implementation CoverViewController
2017-10-31 07:35:57 -04:00
static NSString* const reuseIdentifier = @"coverCell";
2021-11-06 09:31:45 -04:00
- (void)buildCollectionWithForcedFullScan:(BOOL)forceFullDeviceScan
2017-10-31 07:35:57 -04:00
{
2021-11-06 09:31:45 -04:00
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Building collection" message:@"Please wait..." preferredStyle:UIAlertControllerStyleAlert];
CGRect aivRect = CGRectMake(0, 0, 40, 40);
2021-11-06 09:31:45 -04:00
UIActivityIndicatorView* aiv = [[UIActivityIndicatorView alloc] initWithFrame:aivRect];
[aiv startAnimating];
2021-11-06 09:31:45 -04:00
UIViewController* vc = [[UIViewController alloc] init];
vc.preferredContentSize = aivRect.size;
2021-11-06 09:31:45 -04:00
[vc.view addSubview:aiv];
[alert setValue:vc forKey:@"contentViewController"];
[self presentViewController:alert animated:YES completion:nil];
2021-11-06 09:31:45 -04:00
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
2021-11-06 09:31:45 -04:00
auto activeDirs = GetActiveBootableDirectories();
2023-12-22 17:47:41 -05:00
if(forceFullDeviceScan)
2021-11-06 09:31:45 -04:00
{
dispatch_async(dispatch_get_main_queue(), ^{
alert.message = @"Scanning games on filesystem...";
});
ScanBootables("/private/var/mobile");
}
2023-12-22 17:47:41 -05:00
else if(!activeDirs.empty())
2021-11-06 09:31:45 -04:00
{
dispatch_async(dispatch_get_main_queue(), ^{
alert.message = @"Scanning games in active directories...";
});
for(const auto& activeDir : activeDirs)
{
ScanBootables(activeDir, false);
}
}
//Always scan games in app storage. The app's path change when it's reinstalled,
//thus, games from the previous installation won't be found (will be deleted in PurgeInexistingFiles).
dispatch_async(dispatch_get_main_queue(), ^{
alert.message = @"Scanning games in app storage...";
});
ScanBootables(Framework::PathUtils::GetPersonalDataPath());
dispatch_async(dispatch_get_main_queue(), ^{
alert.message = @"Purging inexisting files...";
});
PurgeInexistingFiles();
dispatch_async(dispatch_get_main_queue(), ^{
alert.message = @"Fetching game titles...";
});
FetchGameTitles();
if(_bootables)
{
delete _bootables;
_bootables = nullptr;
}
_bootables = new BootableArray(BootablesDb::CClient::GetInstance().GetBootables());
//Done
dispatch_async(dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
[self.collectionView reloadData];
});
});
}
- (void)viewDidLoad
{
[super viewDidLoad];
CAGradientLayer* bgLayer = [BackgroundLayer blueGradient];
bgLayer.frame = self.view.bounds;
[self.view.layer insertSublayer:bgLayer atIndex:0];
self.collectionView.allowsMultipleSelection = NO;
if(@available(iOS 11.0, *))
{
self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
}
2021-11-06 09:31:45 -04:00
[[AltServerJitService sharedAltServerJitService] startProcess];
2021-11-06 09:31:45 -04:00
[self buildCollectionWithForcedFullScan:NO];
}
- (void)viewDidUnload
{
assert(_bootables != nullptr);
delete _bootables;
[super viewDidUnload];
}
2020-12-02 18:37:31 -05:00
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
2015-08-17 12:48:29 -04:00
{
// resize your layers based on the views new bounds
2020-12-02 18:37:31 -05:00
[[[self.view.layer sublayers] objectAtIndex:0] setFrame:self.view.bounds];
2015-08-17 12:48:29 -04:00
}
2020-12-02 18:37:31 -05:00
- (BOOL)shouldAutorotate
2017-10-31 07:35:57 -04:00
{
if([self isViewLoaded] && self.view.window)
{
2015-08-17 12:48:29 -04:00
return YES;
2017-10-31 07:35:57 -04:00
}
else
{
2015-08-17 12:48:29 -04:00
return NO;
}
}
#pragma mark <UICollectionViewDataSource>
2020-12-02 18:37:31 -05:00
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView
2017-10-31 07:35:57 -04:00
{
return 1;
}
2020-12-02 18:37:31 -05:00
- (NSString*)collectionView:(UICollectionView*)collectionView titleForHeaderInSection:(NSInteger)section
{
2017-10-31 07:35:57 -04:00
return @"";
}
2020-12-02 18:37:31 -05:00
- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section
2017-10-31 07:35:57 -04:00
{
return _bootables ? _bootables->size() : 0;
}
2020-12-02 18:37:31 -05:00
- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
2017-10-31 07:35:57 -04:00
{
2020-12-02 18:37:31 -05:00
CoverViewCell* cell = (CoverViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
2017-10-31 07:35:57 -04:00
auto bootable = (*_bootables)[indexPath.row];
2020-12-02 18:37:31 -05:00
UIImage* placeholder = [UIImage imageNamed:@"boxart.png"];
cell.nameLabel.text = [NSString stringWithUTF8String:bootable.title.c_str()];
cell.backgroundView = [[UIImageView alloc] initWithImage:placeholder];
2017-10-31 07:35:57 -04:00
if(!bootable.coverUrl.empty())
{
2020-12-02 18:37:31 -05:00
NSString* coverUrl = [NSString stringWithUTF8String:bootable.coverUrl.c_str()];
[(UIImageView*)cell.backgroundView sd_setImageWithURL:[NSURL URLWithString:coverUrl] placeholderImage:placeholder];
}
2020-12-02 18:37:31 -05:00
2017-10-31 07:35:57 -04:00
return cell;
}
#pragma mark <UICollectionViewDelegate>
- (BOOL)shouldPerformSegueWithIdentifier:(NSString*)identifier sender:(id)sender
{
if([identifier isEqualToString:@"showEmulator"] && !IsJitAvailable())
{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"JIT unavailable" message:@"JIT doesn't seem to be available at the moment. If JIT is not available, the emulator will crash. Do you wish to continue?" preferredStyle:UIAlertControllerStyleAlert];
{
UIAlertAction* continueAction = [UIAlertAction
actionWithTitle:@"Continue"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction*) {
[self performSegueWithIdentifier:@"showEmulator" sender:sender];
}];
[alert addAction:continueAction];
}
{
UIAlertAction* cancelAction = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction*){}];
[alert addAction:cancelAction];
}
{
UIAlertAction* helpAction = [UIAlertAction
actionWithTitle:@"Help"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction*) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://github.com/jpd002/Play-#running-on-ios"]];
}];
[alert addAction:helpAction];
}
[self presentViewController:alert animated:YES completion:nil];
return NO;
}
return YES;
}
2020-12-02 18:37:31 -05:00
- (void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender
{
2020-12-02 18:37:31 -05:00
if([segue.identifier isEqualToString:@"showEmulator"])
2017-10-31 07:35:57 -04:00
{
2020-12-02 18:37:31 -05:00
NSIndexPath* indexPath = [[self.collectionView indexPathsForSelectedItems] objectAtIndex:0];
2017-10-31 07:35:57 -04:00
auto bootable = (*_bootables)[indexPath.row];
BootablesDb::CClient::GetInstance().SetLastBootedTime(bootable.path, time(nullptr));
EmulatorViewController* emulatorViewController = segue.destinationViewController;
2020-12-02 18:37:31 -05:00
emulatorViewController.bootablePath = [NSString stringWithUTF8String:bootable.path.native().c_str()];
[self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
2017-10-31 07:35:57 -04:00
}
else if([segue.identifier isEqualToString:@"showSettings"])
{
UINavigationController* navViewController = segue.destinationViewController;
SettingsViewController* settingsViewController = (SettingsViewController*)navViewController.visibleViewController;
settingsViewController.allowFullDeviceScan = true;
settingsViewController.allowGsHandlerSelection = true;
settingsViewController.completionHandler = ^(bool fullScanRequested) {
[[AltServerJitService sharedAltServerJitService] startProcess];
2021-11-06 09:31:45 -04:00
if(fullScanRequested)
{
[self buildCollectionWithForcedFullScan:YES];
}
};
}
}
2020-12-02 18:37:31 -05:00
- (IBAction)onExit:(id)sender
2018-10-26 08:14:14 -04:00
{
exit(0);
}
@end