mirror of https://github.com/LIJI32/SameBoy.git
More ROM management features
This commit is contained in:
parent
6ddc3b0f0a
commit
8ea5e8d74d
|
@ -42,7 +42,7 @@
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (nullable id)tableView:(NSTableView *)tableView objectValueForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row
|
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
|
||||||
{
|
{
|
||||||
size_t cheatCount;
|
size_t cheatCount;
|
||||||
GB_gameboy_t *gb = self.document.gameboy;
|
GB_gameboy_t *gb = self.document.gameboy;
|
||||||
|
|
|
@ -126,7 +126,7 @@
|
||||||
return self->_element == object;
|
return self->_element == object;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id)copyWithZone:(nullable NSZone *)zone;
|
- (id)copyWithZone:(NSZone *)zone;
|
||||||
{
|
{
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,7 +67,7 @@
|
||||||
return self.uniqueID == object.uniqueID;
|
return self.uniqueID == object.uniqueID;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id)copyWithZone:(nullable NSZone *)zone;
|
- (id)copyWithZone:(NSZone *)zone;
|
||||||
{
|
{
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,9 @@
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation GBLoadROMTableViewController
|
@implementation GBLoadROMTableViewController
|
||||||
|
{
|
||||||
|
NSIndexPath *_renamingPath;
|
||||||
|
}
|
||||||
|
|
||||||
- (instancetype)init
|
- (instancetype)init
|
||||||
{
|
{
|
||||||
|
@ -75,7 +78,7 @@
|
||||||
|
|
||||||
- (UIModalPresentationStyle)modalPresentationStyle
|
- (UIModalPresentationStyle)modalPresentationStyle
|
||||||
{
|
{
|
||||||
return UIModalPresentationFormSheet;
|
return UIModalPresentationOverFullScreen;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
|
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
|
||||||
|
@ -87,7 +90,7 @@
|
||||||
preferredStyle:UIAlertControllerStyleAlert];
|
preferredStyle:UIAlertControllerStyleAlert];
|
||||||
[alert addAction:[UIAlertAction actionWithTitle:@"Delete"
|
[alert addAction:[UIAlertAction actionWithTitle:@"Delete"
|
||||||
style:UIAlertActionStyleDestructive
|
style:UIAlertActionStyleDestructive
|
||||||
handler:^(UIAlertAction * _Nonnull action) {
|
handler:^(UIAlertAction *action) {
|
||||||
[[GBROMManager sharedManager] deleteROM:rom];
|
[[GBROMManager sharedManager] deleteROM:rom];
|
||||||
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
|
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
|
||||||
if ([[GBROMManager sharedManager].currentROM isEqualToString:rom]) {
|
if ([[GBROMManager sharedManager].currentROM isEqualToString:rom]) {
|
||||||
|
@ -101,4 +104,70 @@
|
||||||
[self presentViewController:alert animated:true completion:nil];
|
[self presentViewController:alert animated:true completion:nil];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)renameRow:(NSIndexPath *)indexPath
|
||||||
|
{
|
||||||
|
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
|
||||||
|
UITextField *field = [[UITextField alloc] initWithFrame:cell.textLabel.frame];
|
||||||
|
field.font = cell.textLabel.font;
|
||||||
|
field.text = cell.textLabel.text;
|
||||||
|
cell.textLabel.text = @"";
|
||||||
|
[[cell.textLabel superview] addSubview:field];
|
||||||
|
[field becomeFirstResponder];
|
||||||
|
[field selectAll:nil];
|
||||||
|
_renamingPath = indexPath;
|
||||||
|
[field addTarget:self action:@selector(doneRename:) forControlEvents:UIControlEventEditingDidEnd | UIControlEventEditingDidEndOnExit];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)doneRename:(UITextField *)sender
|
||||||
|
{
|
||||||
|
if (!_renamingPath) return;
|
||||||
|
NSString *newName = sender.text;
|
||||||
|
NSString *oldName = [GBROMManager sharedManager].allROMs[[_renamingPath indexAtPosition:1]];
|
||||||
|
_renamingPath = nil;
|
||||||
|
if ([newName isEqualToString:oldName]) {
|
||||||
|
[self.tableView reloadData];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if ([newName containsString:@"/"]) {
|
||||||
|
[self.tableView reloadData];
|
||||||
|
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"You can't use a name that contains “/”. Please choose another name."
|
||||||
|
message:nil
|
||||||
|
preferredStyle:UIAlertControllerStyleAlert];
|
||||||
|
[alert addAction:[UIAlertAction actionWithTitle:@"OK"
|
||||||
|
style:UIAlertActionStyleCancel
|
||||||
|
handler:nil]];
|
||||||
|
[self presentViewController:alert animated:true completion:nil];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
[[GBROMManager sharedManager] renameROM:oldName toName:newName];
|
||||||
|
[self.tableView reloadData];
|
||||||
|
_renamingPath = nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Leave these ROM management to iOS 13.0 and up for now
|
||||||
|
- (UIContextMenuConfiguration *)tableView:(UITableView *)tableView
|
||||||
|
contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
|
||||||
|
point:(CGPoint)point API_AVAILABLE(ios(13.0))
|
||||||
|
{
|
||||||
|
return [UIContextMenuConfiguration configurationWithIdentifier:nil
|
||||||
|
previewProvider:nil
|
||||||
|
actionProvider:^UIMenu *(NSArray<UIMenuElement *> *suggestedActions) {
|
||||||
|
return [UIMenu menuWithTitle:nil children:@[
|
||||||
|
[UIAction actionWithTitle:@"Rename"
|
||||||
|
image:[UIImage systemImageNamed:@"pencil"]
|
||||||
|
identifier:nil
|
||||||
|
handler:^(__kindof UIAction *action) {
|
||||||
|
[self renameRow:indexPath];
|
||||||
|
}],
|
||||||
|
[UIAction actionWithTitle:@"Duplicate"
|
||||||
|
image:[UIImage systemImageNamed:@"plus.rectangle.on.rectangle"]
|
||||||
|
identifier:nil
|
||||||
|
handler:^(__kindof UIAction *action) {
|
||||||
|
[[GBROMManager sharedManager] duplicateROM:[GBROMManager sharedManager].allROMs[[indexPath indexAtPosition:1]]];
|
||||||
|
[self.tableView reloadData];
|
||||||
|
}],
|
||||||
|
]];
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -16,5 +16,7 @@
|
||||||
- (NSString *)autosaveStateFileForROM:(NSString *)rom;
|
- (NSString *)autosaveStateFileForROM:(NSString *)rom;
|
||||||
- (NSString *)stateFile:(unsigned)index forROM:(NSString *)rom;
|
- (NSString *)stateFile:(unsigned)index forROM:(NSString *)rom;
|
||||||
- (NSString *)importROM:(NSString *)romFile keepOriginal:(bool)keep;
|
- (NSString *)importROM:(NSString *)romFile keepOriginal:(bool)keep;
|
||||||
|
- (NSString *)renameROM:(NSString *)rom toName:(NSString *)newName;
|
||||||
|
- (NSString *)duplicateROM:(NSString *)rom;
|
||||||
- (void)deleteROM:(NSString *)rom;
|
- (void)deleteROM:(NSString *)rom;
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -113,7 +113,23 @@
|
||||||
[ret addObject:romDirectory];
|
[ret addObject:romDirectory];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret;
|
return [ret sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSString *)makeNameUnique:(NSString *)name
|
||||||
|
{
|
||||||
|
NSString *root = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject;
|
||||||
|
if (![[NSFileManager defaultManager] fileExistsAtPath:[root stringByAppendingPathComponent:name]]) return name;
|
||||||
|
|
||||||
|
unsigned i = 2;
|
||||||
|
while (true) {
|
||||||
|
NSString *attempt = [name stringByAppendingFormat:@" %u", i];
|
||||||
|
if ([[NSFileManager defaultManager] fileExistsAtPath:[root stringByAppendingPathComponent:attempt]]) {
|
||||||
|
i++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
return attempt;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSString *)importROM:(NSString *)romFile keepOriginal:(bool)keep
|
- (NSString *)importROM:(NSString *)romFile keepOriginal:(bool)keep
|
||||||
|
@ -137,6 +153,12 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 *romFolder = [root stringByAppendingPathComponent:friendlyName];
|
NSString *romFolder = [root stringByAppendingPathComponent:friendlyName];
|
||||||
[[NSFileManager defaultManager] createDirectoryAtPath:romFolder
|
[[NSFileManager defaultManager] createDirectoryAtPath:romFolder
|
||||||
withIntermediateDirectories:false
|
withIntermediateDirectories:false
|
||||||
|
@ -167,6 +189,27 @@
|
||||||
return friendlyName;
|
return friendlyName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (NSString *)renameROM:(NSString *)rom toName:(NSString *)newName
|
||||||
|
{
|
||||||
|
newName = [self makeNameUnique:newName];
|
||||||
|
if ([rom isEqualToString:_currentROM]) {
|
||||||
|
_currentROM = newName;
|
||||||
|
}
|
||||||
|
NSString *root = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject;
|
||||||
|
|
||||||
|
[[NSFileManager defaultManager] moveItemAtPath:[root stringByAppendingPathComponent:rom]
|
||||||
|
toPath:[root stringByAppendingPathComponent:newName] error:nil];
|
||||||
|
return newName;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSString *)duplicateROM:(NSString *)rom
|
||||||
|
{
|
||||||
|
NSString *newName = [self makeNameUnique:rom];
|
||||||
|
return [self importROM:[self romFileForROM:rom]
|
||||||
|
withName:newName
|
||||||
|
keepOriginal:true];
|
||||||
|
}
|
||||||
|
|
||||||
- (void)deleteROM:(NSString *)rom
|
- (void)deleteROM:(NSString *)rom
|
||||||
{
|
{
|
||||||
NSString *root = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject;
|
NSString *root = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#import <UIKit/UIKit.h>
|
#import <UIKit/UIKit.h>
|
||||||
|
|
||||||
@interface GBViewController : UIViewController <UIApplicationDelegate>
|
@interface GBViewController : UIViewController <UIApplicationDelegate>
|
||||||
@property (nullable, nonatomic, strong) UIWindow *window;
|
@property (nonatomic, strong) UIWindow *window;
|
||||||
- (void)reset;
|
- (void)reset;
|
||||||
- (void)openLibrary;
|
- (void)openLibrary;
|
||||||
- (void)start;
|
- (void)start;
|
||||||
|
|
Loading…
Reference in New Issue