@@ -46,7 +46,7 @@ int32_t writeToCard(gc::card::card_file *file, void *buffer, int32_t len,
46
46
return ReturnCode;
47
47
}
48
48
49
- int32_t createFile (int32_t chn, const char *fileName, uint32_t size,
49
+ int32_t createFile (int32_t chn, char *fileName, uint32_t size,
50
50
gc::card::card_file *file, gc::card::cardcallback callback)
51
51
{
52
52
int32_t ReturnCode = gc::card::CARDCreateAsync (chn, fileName, size, file, callback);
@@ -81,7 +81,7 @@ int32_t loopUntilSynced()
81
81
return ReturnCode;
82
82
}
83
83
84
- int32_t loadSettings (const char *fileName, gc::card::card_file *fileInfo, uint8_t *workArea)
84
+ int32_t loadSettings (char *fileName, gc::card::card_file *fileInfo, uint8_t *workArea)
85
85
{
86
86
// Load the settings file
87
87
int32_t ReturnCode = gc::card::CARDProbeEx (CARD_SLOTA, nullptr , nullptr );
@@ -156,7 +156,7 @@ int32_t loadSettings(const char *fileName, gc::card::card_file *fileInfo, uint8_
156
156
return CARD_ERROR_READY;
157
157
}
158
158
159
- int32_t writeSettings (const char *description, const char *fileName,
159
+ int32_t writeSettings (char *description, char *fileName,
160
160
gc::card::card_file *fileInfo, uint8_t *workArea)
161
161
{
162
162
// Load the settings file
@@ -173,12 +173,72 @@ int32_t writeSettings(const char *description, const char *fileName,
173
173
return ReturnCode;
174
174
}
175
175
176
+ uint32_t SettingsStructSize;
177
+ uint32_t SettingsStructSizeAdjusted;
178
+
176
179
// Open the settings file if it exists
177
180
ReturnCode = gc::card::CARDOpen (CARD_SLOTA, fileName, fileInfo);
178
181
switch (ReturnCode)
179
182
{
180
183
case CARD_ERROR_READY:
181
184
{
185
+ // Set up the array to hold the area of the file that contains the size
186
+ char *tempFileData = new char [0x200 ];
187
+ clearMemory (tempFileData, 0x200 );
188
+
189
+ // Get the data from the area that holds the size
190
+ ReturnCode = readFromCard (fileInfo, tempFileData, 0x200 , 0x2000 , nullptr );
191
+ if (ReturnCode != CARD_ERROR_READY)
192
+ {
193
+ delete[] (tempFileData);
194
+ gc::card::CARDClose (fileInfo);
195
+ gc::card::CARDUnmount (CARD_SLOTA);
196
+ return ReturnCode;
197
+ }
198
+
199
+ // Get the size of the file
200
+ uint32_t CurrentFileSize = *reinterpret_cast <uint32_t *>(&tempFileData[0x40 ]);
201
+
202
+ // Delete the data that holds the size, as it's not needed anymore
203
+ delete[] (tempFileData);
204
+
205
+ // Adjust the file size to be in multiples of 0x2000, rounding up
206
+ uint32_t CurrentFileSizeAdjusted = (CurrentFileSize + 0x2000 - 1 ) & ~(0x2000 - 1 );
207
+
208
+ // Get the size thats going to be written
209
+ SettingsStructSize = sizeof (struct SettingsStruct );
210
+
211
+ // Adjust the file size to be in multiples of 0x2000, rounding up
212
+ SettingsStructSizeAdjusted = (SettingsStructSize + 0x2000 - 1 ) & ~(0x2000 - 1 );
213
+
214
+ // Make sure the size being written does not exceed the current size
215
+ if (SettingsStructSizeAdjusted > CurrentFileSizeAdjusted)
216
+ {
217
+ // The new size exceeds the current size, so a new file must be made created
218
+ // Close the file
219
+ ReturnCode = gc::card::CARDClose (fileInfo);
220
+ if (ReturnCode != CARD_ERROR_READY)
221
+ {
222
+ gc::card::CARDUnmount (CARD_SLOTA);
223
+ return ReturnCode;
224
+ }
225
+
226
+ // Delete the current file
227
+ ReturnCode = gc::card::CARDDelete (CARD_SLOTA, fileName);
228
+ if (ReturnCode != CARD_ERROR_READY)
229
+ {
230
+ gc::card::CARDUnmount (CARD_SLOTA);
231
+ return ReturnCode;
232
+ }
233
+
234
+ // Make the new file
235
+ // createSettingsFile keeps the file open, but closes and unmounts if it fails to create the file
236
+ ReturnCode = createSettingsFile (fileName, description, fileInfo);
237
+ if (ReturnCode != CARD_ERROR_READY)
238
+ {
239
+ return ReturnCode;
240
+ }
241
+ }
182
242
break ;
183
243
}
184
244
case CARD_ERROR_NOFILE:
@@ -190,6 +250,12 @@ int32_t writeSettings(const char *description, const char *fileName,
190
250
{
191
251
return ReturnCode;
192
252
}
253
+
254
+ // Get the size thats going to be written
255
+ SettingsStructSize = sizeof (struct SettingsStruct );
256
+
257
+ // Adjust the file size to be in multiples of 0x2000, rounding up
258
+ SettingsStructSizeAdjusted = (SettingsStructSize + 0x2000 - 1 ) & ~(0x2000 - 1 );
193
259
break ;
194
260
}
195
261
default :
@@ -198,25 +264,17 @@ int32_t writeSettings(const char *description, const char *fileName,
198
264
}
199
265
}
200
266
201
- // Get the size of the file
202
- uint32_t SettingsStructSize = sizeof (struct SettingsStruct );
203
- uint32_t FileSize = 0x2000 + SettingsStructSize + 0x200 ;
204
-
205
- // Adjust the file size to be in multiples of 0x2000, rounding up
206
- uint32_t FileSizeAdjusted = (FileSize + 0x2000 - 1 ) & ~(0x2000 - 1 );
207
-
208
267
// Set up the memory to be written to the file
209
- uint32_t MiscDataSize = FileSizeAdjusted - 0x2000 ; // Remove the extra 0x2000 from the banner and icon
210
- char *MiscData = new char [MiscDataSize];
211
- clearMemory (MiscData, MiscDataSize);
268
+ char *MiscData = new char [SettingsStructSizeAdjusted];
269
+ clearMemory (MiscData, SettingsStructSizeAdjusted);
212
270
213
271
// Copy the name, description, and file size into the memory
214
272
ttyd::string::strcpy (MiscData, " Paper Mario" );
215
273
ttyd::string::strcpy (&MiscData[0x20 ], description);
274
+ *reinterpret_cast <uint32_t *>(&MiscData[0x40 ]) = SettingsStructSize + 0x200 ;
216
275
217
276
// Set up the struct to hold the variables to store
218
- SettingsStruct *Settings = new SettingsStruct;
219
- clearMemory (Settings, SettingsStructSize);
277
+ SettingsStruct *Settings = reinterpret_cast <SettingsStruct *>(&MiscData[0x200 ]);
220
278
221
279
// Copy the desired variables to the struct
222
280
uint32_t CheatsSize = sizeof (Cheat) / sizeof (Cheat[0 ]);
@@ -237,25 +295,20 @@ int32_t writeSettings(const char *description, const char *fileName,
237
295
Settings->DisplaysButtonCombos [ONSCREEN_TIMER] = OnScreenTimer.ButtonCombo [0 ];
238
296
Settings->DisplaysButtonCombos [ONSCREEN_TIMER + 1 ] = OnScreenTimer.ButtonCombo [1 ];
239
297
240
- // Copy the struct contents into the memory
241
- ttyd::__mem::memcpy (&MiscData[0x200 ], Settings, SettingsStructSize);
242
-
243
- // Delete the struct holding the variables, as they're no longer needed
244
- delete[] (Settings);
245
-
246
298
// Write the data to the file
247
- ReturnCode = writeToCard (fileInfo, MiscData, MiscDataSize , 0x2000 , nullptr );
299
+ ReturnCode = writeToCard (fileInfo, MiscData, SettingsStructSizeAdjusted , 0x2000 , nullptr );
248
300
249
301
delete[] (MiscData);
250
302
gc::card::CARDClose (fileInfo);
251
303
gc::card::CARDUnmount (CARD_SLOTA);
252
304
return ReturnCode;
253
305
}
254
306
255
- int32_t createSettingsFile (const char *fileName, const char *description, gc::card::card_file *fileInfo)
307
+ int32_t createSettingsFile (char *fileName, char *description, gc::card::card_file *fileInfo)
256
308
{
257
309
// Get the banner and icon data from the current REL file
258
- int32_t ReturnCode = gc::card::CARDOpen (CARD_SLOTA, " rel" , fileInfo);
310
+ char *relFileName = const_cast <char *>(" rel" );
311
+ int32_t ReturnCode = gc::card::CARDOpen (CARD_SLOTA, relFileName, fileInfo);
259
312
if (ReturnCode != CARD_ERROR_READY)
260
313
{
261
314
gc::card::CARDUnmount (CARD_SLOTA);
0 commit comments