Skip to content

Commit

Permalink
Syncing with the App Store branch (iCloud support)
Browse files Browse the repository at this point in the history
  • Loading branch information
LIJI32 committed Sep 14, 2024
1 parent 8cb94e7 commit 1931c28
Show file tree
Hide file tree
Showing 14 changed files with 218 additions and 152 deletions.
1 change: 1 addition & 0 deletions iOS/GBCheatsController.m
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ - (void)viewWillAppear:(BOOL)animated
self.navigationController.toolbar.disableCompactLayout = true;
}


- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url
{
[url startAccessingSecurityScopedResource];
Expand Down
7 changes: 3 additions & 4 deletions iOS/GBHubGameViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ - (void)rightButtonPressed
if ([GBROMManager.sharedManager romFileForROM:_game.title]) {
[GBROMManager sharedManager].currentROM = _game.title;
[self.navigationController dismissViewControllerAnimated:true completion:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"GBROMChanged" object:nil];
}
else {
UIActivityIndicatorViewStyle style = UIActivityIndicatorViewStyleWhite;
Expand All @@ -237,9 +236,9 @@ - (void)rightButtonPressed
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Could not download ROM"
message:@"Could not download this ROM from Homebrew Hub. Please try again later."
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Close"
style:UIAlertActionStyleCancel
handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"Close"
style:UIAlertActionStyleCancel
handler:nil]];
[self presentViewController:alert animated:true completion:nil];
self.navigationItem.rightBarButtonItem.customView = nil;
});
Expand Down
2 changes: 1 addition & 1 deletion iOS/GBLibraryViewController.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import <UIKit/UIKit.h>

@interface GBLibraryViewController : UITabBarController

@end


10 changes: 7 additions & 3 deletions iOS/GBLibraryViewController.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#import "GBLibraryViewController.h"
#import "GBLoadROMTableViewController.h"
#import "GBROMViewController.h"
#import "GBHubViewController.h"
#import "GBViewController.h"
#import "GBROMManager.h"


@implementation GBLibraryViewController

Expand All @@ -19,8 +21,9 @@ + (UIViewController *)wrapViewController:(UIViewController *)controller
- (void)viewDidLoad
{
[super viewDidLoad];

self.viewControllers = @[
[self.class wrapViewController:[[GBLoadROMTableViewController alloc] init]],
[self.class wrapViewController:[[GBROMViewController alloc] init]],
[self.class wrapViewController:[[GBHubViewController alloc] init]],
];
if (@available(iOS 13.0, *)) {
Expand All @@ -42,12 +45,13 @@ - (void)viewDidLoad
}
}
self.viewControllers[0].tabBarItem.image = [UIImage systemImageNamed:symbol] ?: [UIImage systemImageNamed:@"folder.fill"];
self.viewControllers[1].tabBarItem.image = [UIImage systemImageNamed:@"globe"];
self.viewControllers.lastObject.tabBarItem.image = [UIImage systemImageNamed:@"globe"];
}
else {
self.viewControllers[0].tabBarItem.image = [UIImage imageNamed:@"FolderTemplate"];
self.viewControllers[1].tabBarItem.image = [UIImage imageNamed:@"GlobeTemplate"];
}
}


@end
5 changes: 0 additions & 5 deletions iOS/GBLoadROMTableViewController.h

This file was deleted.

3 changes: 3 additions & 0 deletions iOS/GBROMManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
@property (readonly) NSString *batterySaveFile;
@property (readonly) NSString *autosaveStateFile;
@property (readonly) NSString *cheatsFile;

@property (readonly) NSString *localRoot;
- (NSString *)stateFile:(unsigned)index;

- (NSString *)romFileForROM:(NSString *)rom;
Expand All @@ -21,4 +23,5 @@
- (NSString *)renameROM:(NSString *)rom toName:(NSString *)newName;
- (NSString *)duplicateROM:(NSString *)rom;
- (void)deleteROM:(NSString *)rom;

@end
58 changes: 31 additions & 27 deletions iOS/GBROMManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
@implementation GBROMManager
{
NSString *_romFile;
NSMutableDictionary<NSString *,NSString *> *_cloudNameToFile;
bool _doneInitializing;
}

+ (instancetype)sharedManager
Expand All @@ -21,18 +23,24 @@ - (instancetype)init
self = [super init];
if (!self) return nil;
self.currentROM = [[NSUserDefaults standardUserDefaults] stringForKey:@"GBLastROM"];
_doneInitializing = true;
return self;
}

- (void)setCurrentROM:(NSString *)currentROM
{
_romFile = nil;
_currentROM = currentROM;
if (currentROM && !self.romFile) {
bool foundROM = self.romFile;

if (currentROM && !foundROM) {
_currentROM = nil;
}

[[NSUserDefaults standardUserDefaults] setObject:_currentROM forKey:@"GBLastROM"];
if (_doneInitializing) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"GBROMChanged" object:nil];
}
}

- (NSString *)romFileForDirectory:(NSString *)romDirectory
Expand All @@ -47,13 +55,17 @@ - (NSString *)romFileForDirectory:(NSString *)romDirectory
return nil;
}

- (NSString *)romDirectoryForROM:(NSString *)romFile
{

return [self.localRoot stringByAppendingPathComponent:romFile];
}

- (NSString *)romFile
{
if (_romFile) return _romFile;
if (!_currentROM) return nil;
NSString *root = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject;
NSString *romDirectory = [root stringByAppendingPathComponent:_currentROM];
return _romFile = [self romFileForDirectory:romDirectory];
return _romFile = [self romFileForDirectory:[self romDirectoryForROM:_currentROM]];
}

- (NSString *)romFileForROM:(NSString *)rom
Expand All @@ -63,9 +75,9 @@ - (NSString *)romFileForROM:(NSString *)rom
if (rom == _currentROM) {
return self.romFile;
}
NSString *root = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject;
NSString *romDirectory = [root stringByAppendingPathComponent:rom];
return [self romFileForDirectory:romDirectory];


return [self romFileForDirectory:[self romDirectoryForROM:rom]];
}

- (NSString *)auxilaryFileForROM:(NSString *)rom withExtension:(NSString *)extension
Expand Down Expand Up @@ -117,7 +129,7 @@ - (NSString *)cheatsFile
- (NSArray<NSString *> *)allROMs
{
NSMutableArray<NSString *> *ret = [NSMutableArray array];
NSString *root = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject;
NSString *root = self.localRoot;
for (NSString *romDirectory in [NSFileManager.defaultManager contentsOfDirectoryAtPath:root
error:nil]) {
if ([romDirectory hasPrefix:@"."] || [romDirectory isEqualToString:@"Inbox"]) continue;
Expand All @@ -130,7 +142,7 @@ - (NSString *)cheatsFile

- (NSString *)makeNameUnique:(NSString *)name
{
NSString *root = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject;
NSString *root = self.localRoot;
if (![[NSFileManager defaultManager] fileExistsAtPath:[root stringByAppendingPathComponent:name]]) return name;

unsigned i = 2;
Expand All @@ -150,27 +162,14 @@ - (NSString *)importROM:(NSString *)romFile keepOriginal:(bool)keep
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;
}
}
friendlyName = [self makeNameUnique:friendlyName];

return [self importROM:romFile withName:friendlyName keepOriginal:keep];
}

- (NSString *)importROM:(NSString *)romFile withName:(NSString *)friendlyName keepOriginal:(bool)keep
{
NSString *root = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject;
NSString *root = self.localRoot;
NSString *romFolder = [root stringByAppendingPathComponent:friendlyName];
[[NSFileManager defaultManager] createDirectoryAtPath:romFolder
withIntermediateDirectories:false
Expand Down Expand Up @@ -205,9 +204,9 @@ - (NSString *)renameROM:(NSString *)rom toName:(NSString *)newName
{
newName = [self makeNameUnique:newName];
if ([rom isEqualToString:_currentROM]) {
_currentROM = newName;
self.currentROM = newName;
}
NSString *root = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject;
NSString *root = self.localRoot;

[[NSFileManager defaultManager] moveItemAtPath:[root stringByAppendingPathComponent:rom]
toPath:[root stringByAppendingPathComponent:newName] error:nil];
Expand All @@ -224,9 +223,14 @@ - (NSString *)duplicateROM:(NSString *)rom

- (void)deleteROM:(NSString *)rom
{
NSString *root = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject;
NSString *root = self.localRoot;
NSString *romDirectory = [root stringByAppendingPathComponent:rom];
[[NSFileManager defaultManager] removeItemAtPath:romDirectory error:nil];
}


- (NSString *)localRoot
{
return NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject;
}
@end
14 changes: 14 additions & 0 deletions iOS/GBROMViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#import <UIKit/UIKit.h>

@interface GBROMViewController : UITableViewController<UIDocumentPickerDelegate>

/* For inheritance */
- (void)romSelectedAtIndex:(unsigned)index;
- (void)deleteROMAtIndex:(unsigned)index;
- (void)renameROM:(NSString *)oldName toName:(NSString *)newName;
- (void)duplicateROMAtIndex:(unsigned)index;
- (NSString *)rootPath;

/* To be used by subclasses */
- (UITableViewCell *)cellForROM:(NSString *)rom;
@end
Loading

0 comments on commit 1931c28

Please sign in to comment.