Skip to content

Commit e8eb6e0

Browse files
committed
Handle the size of the settings file needing to be adjusted
1 parent c891b3b commit e8eb6e0

File tree

8 files changed

+94
-40
lines changed

8 files changed

+94
-40
lines changed

ttyd-tools/rel/include/gc/card.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,12 @@ int32_t CARDGetResultCode(int32_t chn);
7373
int32_t CARDProbeEx(int32_t chn, int32_t *mem_size, int32_t *sect_size);
7474
int32_t CARDMountAsync(int32_t chn, void *workarea, cardcallback detach_cb, cardcallback attach_cb);
7575
int32_t CARDUnmount(int32_t chn);
76-
int32_t CARDOpen(int32_t chn, const char *fileName, card_file *file);
76+
int32_t CARDOpen(int32_t chn, char *fileName, card_file *file);
7777
int32_t CARDClose(card_file *file);
78-
int32_t CARDCreateAsync(int32_t chn, const char *fileName, uint32_t size, card_file *file, cardcallback callback);
78+
int32_t CARDCreateAsync(int32_t chn, char *fileName, uint32_t size, card_file *file, cardcallback callback);
7979
int32_t CARDReadAsync(card_file *file, void *buffer, int32_t len, int32_t offset, cardcallback callback);
8080
int32_t CARDWriteAsync(card_file *file, void *buffer, int32_t len, int32_t offset, cardcallback callback);
81+
int32_t CARDDelete(int32_t chn, char *fileName);
8182
int32_t CARDGetStatus(int32_t chn, int32_t fileNo, card_stat *stats);
8283
int32_t CARDSetStatusAsync(int32_t chn, int32_t fileNo, card_stat *stats, cardcallback callback);
8384

ttyd-tools/rel/include/global.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,8 @@ struct MemoryCardStruct
489489
// uint8_t CurrentAction;
490490
gc::card::card_file FileInfo[1];
491491
uint8_t *WorkArea;
492-
const char *FileName;
493-
const char *Description;
492+
char *FileName;
493+
char *Description;
494494
};
495495

496496
struct SettingsStruct

ttyd-tools/rel/include/memcard.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ int32_t readFromCard(gc::card::card_file *file, void *buffer, int32_t len,
1515
int32_t writeToCard(gc::card::card_file *file, void *buffer, int32_t len,
1616
int32_t offset, gc::card::cardcallback callback);
1717

18-
int32_t createFile(int32_t chn, const char *fileName, uint32_t size,
18+
int32_t createFile(int32_t chn, char *fileName, uint32_t size,
1919
gc::card::card_file *file, gc::card::cardcallback callback);
2020

2121
int32_t setFileStatus(int32_t chn, int32_t fileNo, gc::card::card_stat *stats,
2222
gc::card::cardcallback callback);
2323

2424
int32_t loopUntilSynced();
2525

26-
int32_t loadSettings(const char *fileName, gc::card::card_file *fileInfo, uint8_t *workArea);
26+
int32_t loadSettings(char *fileName, gc::card::card_file *fileInfo, uint8_t *workArea);
2727

28-
int32_t writeSettings(const char *description, const char *fileName,
28+
int32_t writeSettings(char *description, char *fileName,
2929
gc::card::card_file *fileInfo, uint8_t *workArea);
3030

31-
int32_t createSettingsFile(const char *fileName, const char *description, gc::card::card_file *fileInfo);
31+
int32_t createSettingsFile(char *fileName, char *description, gc::card::card_file *fileInfo);
3232

3333
}

ttyd-tools/rel/include/ttyd.eu.lst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,7 @@
13861386
// CARDDelete.c
13871387
// 802B590C:DeleteCallback
13881388
// 802B59B0:CARDDeleteAsync
1389-
// 802B5AC0:CARDDelete
1389+
802B5AC0:CARDDelete
13901390

13911391
// CARDStat.c
13921392
// 802B5B08:UpdateIconOffsets

ttyd-tools/rel/include/ttyd.jp.lst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,7 @@
13781378
// CARDDelete.c
13791379
// 802ABA8C:DeleteCallback
13801380
// 802ABB30:CARDDeleteAsync
1381-
// 802ABC40:CARDDelete
1381+
802ABC40:CARDDelete
13821382

13831383
// CARDStat.c
13841384
// 802ABC88:UpdateIconOffsets

ttyd-tools/rel/include/ttyd.us.lst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1384,7 +1384,7 @@
13841384
// CARDDelete.c
13851385
// 802B1890:DeleteCallback
13861386
// 802B1934:CARDDeleteAsync
1387-
// 802B1A44:CARDDelete
1387+
802B1A44:CARDDelete
13881388

13891389
// CARDStat.c
13901390
// 802B1A8C:UpdateIconOffsets

ttyd-tools/rel/source/global.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace mod {
88

9-
const char *VersionNumber = "v3.0.13";
9+
const char *VersionNumber = "v3.0.14";
1010

1111
const char *RootLines[] =
1212
{
@@ -1026,14 +1026,14 @@ void initMenuVars()
10261026
*reinterpret_cast<uint32_t *>(
10271027
WorkAreaAddress) + 0x4));
10281028
#ifdef TTYD_US
1029-
MenuSettings.Description = "Practice Codes Settings (US)";
1029+
MenuSettings.Description = const_cast<char *>("Practice Codes Settings (US)");
10301030
#elif defined TTYD_JP
1031-
MenuSettings.Description = "Practice Codes Settings (JP)";
1031+
MenuSettings.Description = const_cast<char *>("Practice Codes Settings (JP)");
10321032
#elif defined TTYD_EU
1033-
MenuSettings.Description = "Practice Codes Settings (EU)";
1033+
MenuSettings.Description = const_cast<char *>("Practice Codes Settings (EU)");
10341034
#endif
10351035

1036-
MenuSettings.FileName = "rel_settings";
1036+
MenuSettings.FileName = const_cast<char *>("rel_settings");
10371037

10381038
// Try to open the settings file
10391039
int32_t ReturnCode = loadSettings(MenuSettings.FileName,

ttyd-tools/rel/source/memcard.cpp

Lines changed: 77 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ int32_t writeToCard(gc::card::card_file *file, void *buffer, int32_t len,
4646
return ReturnCode;
4747
}
4848

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,
5050
gc::card::card_file *file, gc::card::cardcallback callback)
5151
{
5252
int32_t ReturnCode = gc::card::CARDCreateAsync(chn, fileName, size, file, callback);
@@ -81,7 +81,7 @@ int32_t loopUntilSynced()
8181
return ReturnCode;
8282
}
8383

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)
8585
{
8686
// Load the settings file
8787
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_
156156
return CARD_ERROR_READY;
157157
}
158158

159-
int32_t writeSettings(const char *description, const char *fileName,
159+
int32_t writeSettings(char *description, char *fileName,
160160
gc::card::card_file *fileInfo, uint8_t *workArea)
161161
{
162162
// Load the settings file
@@ -173,12 +173,72 @@ int32_t writeSettings(const char *description, const char *fileName,
173173
return ReturnCode;
174174
}
175175

176+
uint32_t SettingsStructSize;
177+
uint32_t SettingsStructSizeAdjusted;
178+
176179
// Open the settings file if it exists
177180
ReturnCode = gc::card::CARDOpen(CARD_SLOTA, fileName, fileInfo);
178181
switch (ReturnCode)
179182
{
180183
case CARD_ERROR_READY:
181184
{
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+
}
182242
break;
183243
}
184244
case CARD_ERROR_NOFILE:
@@ -190,6 +250,12 @@ int32_t writeSettings(const char *description, const char *fileName,
190250
{
191251
return ReturnCode;
192252
}
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);
193259
break;
194260
}
195261
default:
@@ -198,25 +264,17 @@ int32_t writeSettings(const char *description, const char *fileName,
198264
}
199265
}
200266

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-
208267
// 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);
212270

213271
// Copy the name, description, and file size into the memory
214272
ttyd::string::strcpy(MiscData, "Paper Mario");
215273
ttyd::string::strcpy(&MiscData[0x20], description);
274+
*reinterpret_cast<uint32_t *>(&MiscData[0x40]) = SettingsStructSize + 0x200;
216275

217276
// 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]);
220278

221279
// Copy the desired variables to the struct
222280
uint32_t CheatsSize = sizeof(Cheat) / sizeof(Cheat[0]);
@@ -237,25 +295,20 @@ int32_t writeSettings(const char *description, const char *fileName,
237295
Settings->DisplaysButtonCombos[ONSCREEN_TIMER] = OnScreenTimer.ButtonCombo[0];
238296
Settings->DisplaysButtonCombos[ONSCREEN_TIMER + 1] = OnScreenTimer.ButtonCombo[1];
239297

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-
246298
// Write the data to the file
247-
ReturnCode = writeToCard(fileInfo, MiscData, MiscDataSize, 0x2000, nullptr);
299+
ReturnCode = writeToCard(fileInfo, MiscData, SettingsStructSizeAdjusted, 0x2000, nullptr);
248300

249301
delete[] (MiscData);
250302
gc::card::CARDClose(fileInfo);
251303
gc::card::CARDUnmount(CARD_SLOTA);
252304
return ReturnCode;
253305
}
254306

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)
256308
{
257309
// 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);
259312
if (ReturnCode != CARD_ERROR_READY)
260313
{
261314
gc::card::CARDUnmount(CARD_SLOTA);

0 commit comments

Comments
 (0)