From 21f21f6bc7789b12d2951323679d8ac0c9e39ab2 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 18 Jan 2023 23:08:17 +0200 Subject: [PATCH] Allow importing ROMs and Files app interaction --- iOS/GBROMManager.h | 2 +- iOS/GBROMManager.m | 46 ++++++++++++++++++ iOS/GBViewController.m | 19 +++++++- iOS/Info.plist | 105 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 170 insertions(+), 2 deletions(-) diff --git a/iOS/GBROMManager.h b/iOS/GBROMManager.h index 09cacfa..ed12821 100644 --- a/iOS/GBROMManager.h +++ b/iOS/GBROMManager.h @@ -15,5 +15,5 @@ - (NSString *)batterySaveFileForROM:(NSString *)rom; - (NSString *)autosaveStateFileForROM:(NSString *)rom; - (NSString *)stateFile:(unsigned)index forROM:(NSString *)rom; - +- (NSString *)importROM:(NSString *)romFile keepOriginal:(bool)keep; @end diff --git a/iOS/GBROMManager.m b/iOS/GBROMManager.m index f31a27c..0b7e38b 100644 --- a/iOS/GBROMManager.m +++ b/iOS/GBROMManager.m @@ -114,4 +114,50 @@ return ret; } +- (NSString *)importROM:(NSString *)romFile keepOriginal:(bool)keep +{ + NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\([^)]+\\)|\\[[^\\]]+\\]" options:0 error:nil]; + NSString *friendlyName = [[romFile lastPathComponent] stringByDeletingPathExtension]; + friendlyName = [regex stringByReplacingMatchesInString:friendlyName options:0 range:NSMakeRange(0, [friendlyName length]) withTemplate:@""]; + friendlyName = [friendlyName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; + + NSString *root = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject; + if ([[NSFileManager defaultManager] fileExistsAtPath:[root stringByAppendingPathComponent:friendlyName]]) { + unsigned i = 2; + while (true) { + NSString *attempt = [friendlyName stringByAppendingFormat:@" %u", i]; + if ([[NSFileManager defaultManager] fileExistsAtPath:[root stringByAppendingPathComponent:attempt]]) { + i++; + continue; + } + friendlyName = attempt; + break; + } + } + + NSString *romFolder = [root stringByAppendingPathComponent:friendlyName]; + [[NSFileManager defaultManager] createDirectoryAtPath:romFolder + withIntermediateDirectories:false + attributes:nil + error:nil]; + + NSString *newROMPath = [romFolder stringByAppendingPathComponent:romFile.lastPathComponent]; + + NSError *error = nil; + + if (keep) { + [[NSFileManager defaultManager] linkItemAtPath:romFile + toPath:newROMPath + error:&error]; + } + else { + [[NSFileManager defaultManager] moveItemAtPath:romFile + toPath:newROMPath + error:&error]; + } + NSLog(@"%@", error); + + return friendlyName; +} + @end diff --git a/iOS/GBViewController.m b/iOS/GBViewController.m index 928c14e..e511702 100644 --- a/iOS/GBViewController.m +++ b/iOS/GBViewController.m @@ -316,7 +316,7 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) /* Convert the screenshot to a CGImageRef */ CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data.bytes, data.length, NULL); CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); - CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; + CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaNoneSkipLast; CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; CGImageRef iref = CGImageCreate(width, @@ -473,4 +473,21 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) { GB_set_palette(&_gb, [self userPalette]); } + +- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options +{ + [self stop]; + NSString *potentialROM = [[url.path stringByDeletingLastPathComponent] lastPathComponent]; + if ([[[GBROMManager sharedManager] romFileForROM:potentialROM].stringByStandardizingPath isEqualToString:url.path.stringByStandardizingPath]) { + [GBROMManager sharedManager].currentROM = potentialROM; + } + else { + [GBROMManager sharedManager].currentROM = + [[GBROMManager sharedManager] importROM:url.path + keepOriginal:[options[UIApplicationOpenURLOptionsOpenInPlaceKey] boolValue]]; + } + [self loadROM]; + [self start]; + return false; +} @end diff --git a/iOS/Info.plist b/iOS/Info.plist index 9d7889a..3783240 100644 --- a/iOS/Info.plist +++ b/iOS/Info.plist @@ -48,5 +48,110 @@ LSApplicationCategoryType public.app-category.games + LSSupportsOpeningDocumentsInPlace + + UIFileSharingEnabled + + UISupportsDocumentBrowser + + CFBundleDocumentTypes + + + CFBundleTypeExtensions + + gb + + CFBundleTypeName + Game Boy Game + LSItemContentTypes + + com.github.liji32.sameboy.gb + + LSHandlerRank + Owner + + + CFBundleTypeExtensions + + gbc + + CFBundleTypeName + Game Boy Color Game + LSItemContentTypes + + com.github.liji32.sameboy.gbc + + LSHandlerRank + Owner + + + CFBundleTypeExtensions + + isx + + CFBundleTypeName + Game Boy ISX File + LSItemContentTypes + + com.github.liji32.sameboy.isx + + LSHandlerRank + Owner + + + UTExportedTypeDeclarations + + + UTTypeConformsTo + + public.data + + UTTypeDescription + Game Boy Game + UTTypeIdentifier + com.github.liji32.sameboy.gb + UTTypeTagSpecification + + public.filename-extension + + gb + + + + + UTTypeConformsTo + + public.data + + UTTypeDescription + Game Boy Color Game + UTTypeIdentifier + com.github.liji32.sameboy.gbc + UTTypeTagSpecification + + public.filename-extension + + gbc + + + + + UTTypeConformsTo + + public.data + + UTTypeDescription + Game Boy ISX File + UTTypeIdentifier + com.github.liji32.sameboy.isx + UTTypeTagSpecification + + public.filename-extension + + isx + + + +