forked from slinga-homebrew/Save-Game-Copier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup-satiator.c
370 lines (298 loc) · 7.92 KB
/
backup-satiator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include "backup-satiator.h"
#include "libsatiator/satiator.h"
// returns true if the backup device is found
bool satiatorIsBackupDeviceAvailable(int backupDevice)
{
int result;
if(backupDevice != SatiatorBackup)
{
return false;
}
result = satiatorEnter();
if(result != 0)
{
// failed to find Satiator
return false;
}
// found Satiator
return true;
}
// queries the saves on the Satiator device and fills out the saves array
// BUGBUG: code copied from satiator-menu/main.c and needs to be under the MPL
int satiatorListSaveFiles(int backupDevice, PSAVES saves, unsigned int numSaves)
{
int result = 0;
unsigned int count = 0;
char statbuf[280] = {0};
s_stat_t *st = (s_stat_t*)statbuf;
int len = 0;
if(backupDevice != SatiatorBackup)
{
return -1;
}
result = satiatorEnter();
if(result != 0)
{
sgc_core_error("Failed to detect satiator");
return -1;
}
result = s_opendir(".");
if(result != 0)
{
sgc_core_error("readSatiatorSaveFiles: Failed to open SATSAVES directory");
return -2;
}
// loop through the files in the directory
while ((len = s_stat(NULL, st, sizeof(statbuf)-1)) > 0)
{
st->name[len] = 0;
//skip directories
if(st->attrib & AM_DIR)
{
continue;
}
// skip . and .. (and anything beginning with .)
if (st->name[0] == '.')
{
continue;
}
result = isFileBUPExt(st->name);
if(result == false)
{
// not a .BUP file, skip
continue;
}
strncpy((char*)saves[count].filename, st->name, MAX_FILENAME);
saves[count].filename[MAX_FILENAME - 1] = '\0';
saves[count].datasize = st->size - sizeof(BUP_HEADER);
saves[count].blocksize = 0; // blocksize on the Satiator doesn't matter
count++;
if(count >= numSaves)
{
break;
}
}
// for each file found, read the BUP header and parse the metadata
for(unsigned int i = 0; i < count; i++)
{
BUP_HEADER bupHeader = {0};
result = satiatorReadBUPHeader(saves[i].filename, &bupHeader);
if(result != 0)
{
sgc_core_error("bup header %s", saves[i].filename);
continue;
}
result = parseBupHeaderValues(&bupHeader, saves[count].datasize + sizeof(BUP_HEADER), saves[i].name, saves[i].comment, &saves[i].language, &saves[i].date, &saves[i].datasize, &saves[i].blocksize);
if(result != 0)
{
sgc_core_error("Failed with %d", result);
// BUGBUG: handle error conditions gracefully
strncpy(saves[i].name, "Error", MAX_SAVE_FILENAME);
continue;
}
}
// BUGBUG: close the directory??
return count;
}
// copies the specified Satiator save game to the saveFileData buffer
int satiatorReadSaveFile(int backupDevice, char* filename, unsigned char* outBuffer, unsigned int outSize)
{
int result = 0;
int fd = 0;
if(backupDevice != SatiatorBackup)
{
return -1;
}
result = satiatorEnter();
if(result == 0)
{
// why is it failing to detect now??
//sgc_core_error("Failed to detect satiator %d", result);
//return -1;
}
if(outBuffer == NULL || filename == NULL)
{
sgc_core_error("readSatiatorSaveFile: Save file data buffer is NULL!!");
return -1;
}
if(outSize == 0 || outSize > MAX_SAVE_SIZE)
{
sgc_core_error("readSatiatorSaveFile: Save file size is invalid %d!!", outSize);
return -2;
}
fd = s_open(filename, FA_READ);
if(fd < 0)
{
sgc_core_error("readSatiatorSaveFile: Failed to open satiator file!!");
return -2;
}
for(unsigned int bytesRead = 0; bytesRead < outSize; )
{
unsigned int count;
count = MIN(outSize - bytesRead, S_MAXBUF);
// BUGBUG: fix this
result = s_read(fd, outBuffer + bytesRead, count);
if(result <= 0)
{
sgc_core_error("Bad read result: %x", result);
s_close(fd);
return result;
}
bytesRead += count;
}
s_close(fd);
if(result < 0)
{
sgc_core_error("readSatiatorSaveFile: Failed to read satiator file!!");
return -3;
}
return 0;
}
// write the save game to the Satiator
int satiatorWriteSaveFile(int backupDevice, char* filename, unsigned char* inBuffer, unsigned int inSize)
{
int result = 0;
int fd = 0;
if(backupDevice != SatiatorBackup)
{
return -1;
}
result = satiatorEnter();
if(result != 0)
{
sgc_core_error("Failed to detect satiator");
return -1;
}
if(filename == NULL)
{
sgc_core_error("writeSatiatorSaveData: Save file data buffer is NULL!!");
return -1;
}
if(inBuffer == NULL || filename == NULL)
{
sgc_core_error("writeSatiatorSaveData: Save file size is invalid %d!!", inSize);
return -2;
}
fd = s_open(filename, FA_WRITE|FA_CREATE_ALWAYS);
if(fd < 0)
{
sgc_core_error("writeSatiatorSaveData: Failed to open satiator file!!");
return -2;
}
for(unsigned int bytesWritten = 0; bytesWritten < inSize; )
{
unsigned int count;
count = MIN(inSize - bytesWritten, S_MAXBUF);
// BUGBUG: fix this
result = s_write(fd, inBuffer + bytesWritten, count);
s_sync(fd);
if(result <= 0)
{
sgc_core_error("Bad write result: %x", result);
s_close(fd);
return result;
break;
}
bytesWritten += count;
}
s_close(fd);
if(result < 0)
{
sgc_core_error("writeSatiatorSaveData: Failed to read satiator file!!");
return -3;
}
return 0;
}
// delete the save
int satiatorDeleteSaveFile(int backupDevice, char* filename)
{
int result = 0;
if(backupDevice != SatiatorBackup)
{
return -1;
}
result = satiatorEnter();
if(result != 0)
{
sgc_core_error("Failed to detect satiator");
//return -1;
}
if(filename == NULL)
{
sgc_core_error("deleteSatiatorSaveData: Filename is NULL!!");
return -1;
}
result = s_unlink(filename);
if(result < 0)
{
sgc_core_error("deleteSatiatorSaveData: Failed to read satiator file!!");
return -3;
}
return 0;
}
// enable satiator extra mode
// without this you cannot access the filesystem
int satiatorEnter(void)
{
int result;
result = s_mode(s_api);
if(result != 0)
{
return -1;
}
s_chdir("/SATSAVES");
return 0;
}
// exit satiator extra mode
// BUGBUG: this does not appear to work and ends up with the ISO no longer being accessible
int satiatorExit(void)
{
// BUGBUG: this doesn't quite work correctly
//s_chdir("..");
//s_mode(S_MODE_CDROM);
return 0;
}
// read the bup header
int satiatorReadBUPHeader(char* filename, PBUP_HEADER bupHeader)
{
int result = 0;
int fd = 0;
bool openedFile = false;
if(!filename || !bupHeader)
{
return -1;
}
fd = s_open(filename, FA_READ);
if(fd < 0)
{
sgc_core_error("readSatiatorSaveFile: Failed to open satiator file!!");
return -2;
}
// read the .BUP header
result = s_read(fd, (unsigned char*)bupHeader, sizeof(BUP_HEADER));
if(result <= 0)
{
sgc_core_error("Bad read result: %x", result);
result = -3;
goto exit;
}
openedFile = true;
if(result < (int)sizeof(BUP_HEADER))
{
sgc_core_error("bup header is too small");
result = -4;
goto exit;
}
result = 0;
exit:
if(openedFile == true)
{
s_close(fd);
}
return result;
}
// relaunch the satiator menu
void satiatorReboot(void)
{
s_reset_to_satiator();
}