Skip to content

Commit efd7486

Browse files
authored
Add backup memory import/export API to interface (#932)
1 parent a9df2b9 commit efd7486

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

desmume/src/frontend/interface/interface.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,22 @@ EXPORTED char* desmume_savestate_slot_date(int index)
287287
return savestates[index].date;
288288
}
289289

290+
EXPORTED BOOL desmume_backup_import_file(const char *filename, unsigned int force_size) {
291+
const auto success = MMU_new.backupDevice.importData(filename, force_size);
292+
293+
if (!success) {
294+
return FALSE;
295+
}
296+
297+
NDS_Reset();
298+
299+
return TRUE;
300+
}
301+
302+
EXPORTED BOOL desmume_backup_export_file(const char *filename) {
303+
return MMU_new.backupDevice.exportData(filename) ? TRUE : FALSE;
304+
}
305+
290306
EXPORTED BOOL desmume_gpu_get_layer_main_enable_state(int layer_index)
291307
{
292308
return GPU->GetEngineMain()->GetLayerEnableState(layer_index);

desmume/src/frontend/interface/interface.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,49 @@ EXPORTED void desmume_savestate_slot_save(int index);
106106
EXPORTED BOOL desmume_savestate_slot_exists(int index);
107107
EXPORTED char* desmume_savestate_slot_date(int index);
108108

109+
// Battery save (backup memory) import/export
110+
111+
/**
112+
* Import battery save file with optional size override.
113+
* Supports .sav (raw), .dsv (DeSmuME), .duc (Action Replay), .dss (DSOrganize).
114+
* The emulator will automatically reset after successful import.
115+
*
116+
* Prerequisites:
117+
* - MMU_new.backupDevice is a global object initialized at program startup
118+
* - A ROM should be loaded via desmume_open() first - the BackupDevice constructor
119+
* checks gameInfo.romsize and returns early if no ROM is loaded, preventing
120+
* proper initialization of the save file path
121+
*
122+
* Typical usage:
123+
* desmume_init(); // Initialize emulator
124+
* desmume_open("game.nds"); // Load ROM (initializes backup device filename)
125+
* desmume_backup_import_file("save.sav", 0); // Now safe to import
126+
*
127+
* @param filename Path to battery save file
128+
* @param force_size Backup size in bytes (0 = auto-detect, or explicit size like 524288 for 512KB)
129+
* @return TRUE on success, FALSE on failure
130+
*/
131+
EXPORTED BOOL desmume_backup_import_file(const char *filename, unsigned int force_size);
132+
133+
/**
134+
* Export current battery save to .dsv file.
135+
*
136+
* Prerequisites:
137+
* - MMU_new.backupDevice is a global object initialized at program startup
138+
* - A ROM must be loaded via desmume_open() first - the BackupDevice needs
139+
* the ROM to be loaded to have a valid save filename and data
140+
*
141+
* Typical usage:
142+
* desmume_init(); // Initialize emulator
143+
* desmume_open("game.nds"); // Load ROM
144+
* // ... play game, save data is created ...
145+
* desmume_backup_export_file("backup.dsv"); // Export save data
146+
*
147+
* @param filename Destination path for .dsv file
148+
* @return TRUE on success, FALSE on failure
149+
*/
150+
EXPORTED BOOL desmume_backup_export_file(const char *filename);
151+
109152
EXPORTED BOOL desmume_gpu_get_layer_main_enable_state(int layer_index);
110153
EXPORTED BOOL desmume_gpu_get_layer_sub_enable_state(int layer_index);
111154
EXPORTED void desmume_gpu_set_layer_main_enable_state(int layer_index, BOOL the_state);

0 commit comments

Comments
 (0)