@@ -120,6 +120,8 @@ @implementation Document
120120 __weak NSThread *_emulationThread;
121121
122122 GBCheatSearchController *_cheatSearchController;
123+
124+ bool _romModified;
123125}
124126
125127static void boot_rom_load (GB_gameboy_t *gb, GB_boot_rom_t type)
@@ -1095,11 +1097,6 @@ - (void)initMemoryView
10951097 self.memoryBankItem .enabled = false ;
10961098}
10971099
1098- + (BOOL )autosavesInPlace
1099- {
1100- return true ;
1101- }
1102-
11031100- (NSString *)windowNibName
11041101{
11051102 // Override returning the nib file name of the document
@@ -1295,20 +1292,22 @@ - (int)loadROM
12951292 }
12961293
12971294 NSString *rom_warnings = [self captureOutputForBlock: ^{
1298- GB_debugger_clear_symbols (&_gb);
1299- if ([[[fileName pathExtension ] lowercaseString ] isEqualToString: @" isx" ]) {
1300- ret = GB_load_isx (&_gb, fileName.UTF8String );
1301- if (!self.isCartContainer ) {
1302- GB_load_battery (&_gb, [[self .fileURL URLByDeletingPathExtension ] URLByAppendingPathExtension: @" ram" ].path .UTF8String );
1295+ if (!_romModified) {
1296+ GB_debugger_clear_symbols (&_gb);
1297+ if ([[[fileName pathExtension ] lowercaseString ] isEqualToString: @" isx" ]) {
1298+ ret = GB_load_isx (&_gb, fileName.UTF8String );
1299+ if (!self.isCartContainer ) {
1300+ GB_load_battery (&_gb, [[self .fileURL URLByDeletingPathExtension ] URLByAppendingPathExtension: @" ram" ].path .UTF8String );
1301+ }
1302+ }
1303+ else if ([[[fileName pathExtension ] lowercaseString ] isEqualToString: @" gbs" ]) {
1304+ __block GB_gbs_info_t info;
1305+ ret = GB_load_gbs (&_gb, fileName.UTF8String , &info);
1306+ [self prepareGBSInterface: &info];
1307+ }
1308+ else {
1309+ ret = GB_load_rom (&_gb, [fileName UTF8String ]);
13031310 }
1304- }
1305- else if ([[[fileName pathExtension ] lowercaseString ] isEqualToString: @" gbs" ]) {
1306- __block GB_gbs_info_t info;
1307- ret = GB_load_gbs (&_gb, fileName.UTF8String , &info);
1308- [self prepareGBSInterface: &info];
1309- }
1310- else {
1311- ret = GB_load_rom (&_gb, [fileName UTF8String ]);
13121311 }
13131312 if (GB_save_battery_size (&_gb)) {
13141313 if (!is_path_writeable (self.savPath .UTF8String )) {
@@ -1468,6 +1467,12 @@ - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)anItem
14681467 else if ([anItem action ] == @selector (reloadROM: )) {
14691468 return !_gbsTracks;
14701469 }
1470+ else if ([anItem action ] == @selector (saveDocument: )) {
1471+ return _romModified;
1472+ }
1473+ else if ([anItem action ] == @selector (saveDocumentAs: )) {
1474+ return _romModified && !self.isCartContainer ;
1475+ }
14711476
14721477 return [super validateUserInterfaceItem: anItem];
14731478}
@@ -2915,6 +2920,8 @@ - (IBAction)reloadROM:(id)sender
29152920 [self stop ];
29162921 }
29172922
2923+ _romModified = false ;
2924+ [self updateChangeCount: NSChangeCleared];
29182925 [self loadROM ];
29192926
29202927 if (wasRunning) {
@@ -2969,6 +2976,24 @@ - (IBAction)debuggerButtonPressed:(NSButton *)sender
29692976 [self queueDebuggerCommand: sender.alternateTitle];
29702977}
29712978
2979+ - (void )setROMModified
2980+ {
2981+ _romModified = true ;
2982+ [self updateChangeCount: NSChangeDone];
2983+ }
2984+
2985+ - (BOOL )writeToFile : (NSString *)path ofType : (NSString *)type
2986+ {
2987+ if ([type isEqualToString: @" Game Boy Cartridge" ]) {
2988+ if (![[NSFileManager defaultManager ] copyItemAtPath: self .fileName toPath: path error: nil ]) return false ;
2989+ path = self.romPath ;
2990+ if (!path) return false ;
2991+ }
2992+ size_t size;
2993+ uint8_t *data = GB_get_direct_access (&_gb, GB_DIRECT_ACCESS_ROM, &size, NULL );
2994+ return [[NSData dataWithBytesNoCopy: data length: size freeWhenDone: false ] writeToFile: path atomically: true ];
2995+ }
2996+
29722997+ (NSArray <NSString *> *)readableTypes
29732998{
29742999 NSMutableSet *set = [NSMutableSet setWithArray: [super readableTypes ]];
0 commit comments