Paper Mario DX
Paper Mario (N64) modding
 
Loading...
Searching...
No Matches
fio.c File Reference

Go to the source code of this file.

Data Structures

struct  SaveInfo
 

Macros

#define GLOBALS_PAGE_1   6
 
#define GLOBALS_PAGE_2   7
 

Functions

b32 fio_read_flash (s32 pageNum, void *readBuffer, u32 numBytes)
 
b32 fio_write_flash (s32 pageNum, s8 *readBuffer, u32 numBytes)
 
void fio_erase_flash (s32 pageNum)
 
s32 get_spirits_rescued (void)
 
s32 fio_calc_globals_checksum (void)
 
b32 fio_validate_globals_checksums (void)
 
b32 fio_load_globals (void)
 
b32 fio_save_globals (void)
 
s32 fio_calc_file_checksum (SaveData *saveData)
 
b32 fio_validate_file_checksum (SaveData *saveData)
 
b32 fio_fetch_saved_file_info (void)
 
b32 fio_load_game (s32 saveSlot)
 
void fio_save_game (s32 saveSlot)
 
void fio_erase_game (s32 saveSlot)
 
void fio_init_flash (void)
 

Variables

BSS SaveData FetchSaveBuffer
 
BSS SaveInfo LogicalSaveInfo [4]
 
BSS SaveInfo PhysicalSaveInfo [6]
 
BSS s32 NextAvailablePhysicalSave
 
SaveGlobals gSaveGlobals
 
SaveData gCurrentSaveFile
 
char MagicSaveString [] = "Mario Story 006"
 

Data Structure Documentation

◆ SaveInfo

struct SaveInfo
Data Fields
s32 slot
s32 count

Macro Definition Documentation

◆ GLOBALS_PAGE_1

#define GLOBALS_PAGE_1   6

Definition at line 12 of file fio.c.

Referenced by fio_load_globals(), and fio_save_globals().

◆ GLOBALS_PAGE_2

#define GLOBALS_PAGE_2   7

Definition at line 13 of file fio.c.

Referenced by fio_load_globals(), and fio_save_globals().

Function Documentation

◆ fio_read_flash()

b32 fio_read_flash ( s32 pageNum,
void * readBuffer,
u32 numBytes )

Definition at line 236 of file fio.c.

236 {
237 OSIoMesg mb;
238 OSMesgQueue mesgQueue;
239 OSMesg mesg;
240 s8* buf = (s8*)readBuffer;
241 s32 amt;
242 u16 i;
243
244 osInvalDCache(buf, numBytes);
245 osCreateMesgQueue(&mesgQueue, &mesg, 1);
246
247 i = 0;
248 while (numBytes != 0) {
249 if (numBytes > sizeof(SaveGlobals)) {
250 amt = sizeof(SaveGlobals);
251 } else {
252 amt = numBytes;
253 }
254
255 osFlashReadArray(&mb, 0, pageNum * sizeof(SaveGlobals) + i, buf, 1, &mesgQueue);
256 osRecvMesg(&mesgQueue, NULL, 1);
257 i++;
258 numBytes -= amt;
259 buf += amt;
260 }
261 return TRUE;
262}

Referenced by fio_fetch_saved_file_info(), fio_load_game(), and fio_load_globals().

◆ fio_write_flash()

b32 fio_write_flash ( s32 pageNum,
s8 * readBuffer,
u32 numBytes )

Definition at line 264 of file fio.c.

264 {
265 OSIoMesg mb;
266 OSMesgQueue mesgQueue;
267 OSMesg mesg;
268 s32 amt;
269 u16 i;
270
271 osWritebackDCache(readBuffer, numBytes);
272 osCreateMesgQueue(&mesgQueue, &mesg, 1);
273
274 i = 0;
275 while (numBytes != 0) {
276 if (numBytes > sizeof(SaveGlobals)) {
277 amt = sizeof(SaveGlobals);
278 } else {
279 amt = numBytes;
280 }
281
282 osFlashWriteBuffer(&mb, 0, readBuffer, &mesgQueue);
283 osFlashWriteArray((pageNum * sizeof(SaveGlobals)) + i);
284 osRecvMesg(&mesgQueue, NULL, 1);
285 i++;
286 numBytes -= amt;
287 readBuffer += amt;
288 }
289 return TRUE;
290}

Referenced by fio_save_game(), and fio_save_globals().

◆ fio_erase_flash()

void fio_erase_flash ( s32 pageNum)

Definition at line 292 of file fio.c.

292 {
293 osFlashSectorErase(pageNum * sizeof(SaveGlobals));
294}

Referenced by fio_erase_game(), fio_save_game(), and fio_save_globals().

◆ get_spirits_rescued()

s32 get_spirits_rescued ( void )

Definition at line 29 of file fio.c.

29 {
30 s32 storyProgress = evt_get_variable(NULL, GB_StoryProgress);
31 s32 ret = 7;
32
33 if (storyProgress < evt_get_variable(NULL, STORY_CH1_STAR_SPIRIT_RESCUED)) {
34 ret = 0;
35 } else if (storyProgress < evt_get_variable(NULL, STORY_CH2_STAR_SPIRIT_RESCUED)) {
36 ret = 1;
37 } else if (storyProgress < evt_get_variable(NULL, STORY_CH3_STAR_SPIRIT_RESCUED)) {
38 ret = 2;
39 } else if (storyProgress < evt_get_variable(NULL, STORY_CH4_STAR_SPIRIT_RESCUED)) {
40 ret = 3;
41 } else if (storyProgress < evt_get_variable(NULL, STORY_CH5_OPENED_ESCAPE_ROUTE)) {
42 ret = 4;
43 } else if (storyProgress < evt_get_variable(NULL, STORY_CH6_STAR_SPIRIT_RESCUED)) {
44 ret = 5;
45 } else if (storyProgress < evt_get_variable(NULL, STORY_CH7_STAR_SPIRIT_RESCUED)) {
46 ret = 6;
47 }
48
49 return ret;
50}
@ STORY_CH6_STAR_SPIRIT_RESCUED
Definition enums.h:195
@ STORY_CH4_STAR_SPIRIT_RESCUED
Definition enums.h:143
@ STORY_CH5_OPENED_ESCAPE_ROUTE
Definition enums.h:175
@ STORY_CH3_STAR_SPIRIT_RESCUED
Definition enums.h:123
@ STORY_CH1_STAR_SPIRIT_RESCUED
Definition enums.h:61
@ STORY_CH2_STAR_SPIRIT_RESCUED
Definition enums.h:83
@ STORY_CH7_STAR_SPIRIT_RESCUED
Definition enums.h:225
s32 evt_get_variable(Evt *script, Bytecode var)
Definition evt.c:1690
@ GB_StoryProgress

Referenced by fio_save_game().

◆ fio_calc_globals_checksum()

s32 fio_calc_globals_checksum ( void )

Definition at line 52 of file fio.c.

52 {
53 s32* it = (s32*)&gSaveGlobals;
54 u32 sum = 0;
55 u32 i;
56
57 for (i = 0; i < sizeof(gSaveGlobals) / sizeof(*it); i++) {
58 sum += *it++;
59 }
60 return sum;
61}
SaveGlobals gSaveGlobals
Definition fio.c:20

Referenced by fio_save_globals(), and fio_validate_globals_checksums().

◆ fio_validate_globals_checksums()

b32 fio_validate_globals_checksums ( void )

Definition at line 63 of file fio.c.

63 {
64 SaveGlobals* header = &gSaveGlobals;
65
66 if (strcmp(header->magicString, MagicSaveString)) {
67 return FALSE;
68 }
69 if (header->crc1 != ~header->crc2) {
70 return FALSE;
71 }
72 return fio_calc_globals_checksum() == header->crc1;
73}
s32 fio_calc_globals_checksum(void)
Definition fio.c:52
char MagicSaveString[]
Definition fio.c:23
char magicString[16]
Definition versioning.h:10

Referenced by fio_load_globals().

◆ fio_load_globals()

b32 fio_load_globals ( void )

Definition at line 75 of file fio.c.

75 {
78 return TRUE;
79 }
80
83 return TRUE;
84 }
85
86 bzero(&gSaveGlobals, sizeof(gSaveGlobals));
87 return FALSE;
88}
#define GLOBALS_PAGE_2
Definition fio.c:13
b32 fio_read_flash(s32 pageNum, void *readBuffer, u32 numBytes)
Definition fio.c:236
b32 fio_validate_globals_checksums(void)
Definition fio.c:63
#define GLOBALS_PAGE_1
Definition fio.c:12

Referenced by filemenu_init(), and state_step_startup().

◆ fio_save_globals()

b32 fio_save_globals ( void )

Definition at line 90 of file fio.c.

90 {
91 s32 checksum;
92
95 gSaveGlobals.crc2 = ~gSaveGlobals.crc1;
96 checksum = fio_calc_globals_checksum();
97 gSaveGlobals.crc1 = checksum;
98 gSaveGlobals.crc2 = ~checksum;
103 return TRUE;
104}
void fio_erase_flash(s32 pageNum)
Definition fio.c:292
b32 fio_write_flash(s32 pageNum, s8 *readBuffer, u32 numBytes)
Definition fio.c:264

Referenced by filemenu_yesno_handle_input().

◆ fio_calc_file_checksum()

s32 fio_calc_file_checksum ( SaveData * saveData)

Definition at line 106 of file fio.c.

106 {
107 s32* it = (s32*)saveData;
108 u32 sum = 0;
109 u32 i;
110
111 for (i = 0; i < sizeof(*saveData) / sizeof(*it); i++) {
112 sum += *it++;
113 }
114 return sum;
115}

Referenced by fio_save_game(), and fio_validate_file_checksum().

◆ fio_validate_file_checksum()

b32 fio_validate_file_checksum ( SaveData * saveData)

Definition at line 117 of file fio.c.

117 {
118 if (!strcmp(saveData->magicString, MagicSaveString) && saveData->crc1 == ~saveData->crc2) {
119 return fio_calc_file_checksum(saveData) == saveData->crc1;
120 }
121 return FALSE;
122}
s32 fio_calc_file_checksum(SaveData *saveData)
Definition fio.c:106
char magicString[16]
Definition versioning.h:144

Referenced by fio_fetch_saved_file_info().

◆ fio_fetch_saved_file_info()

b32 fio_fetch_saved_file_info ( void )

Definition at line 124 of file fio.c.

124 {
125 SaveData* fetchBuf = &FetchSaveBuffer; // temps required to match
126 SaveData* validBuf = fetchBuf;
127 s32 i, j, minSaveCount;
128
129 for (i = 0; i < ARRAY_COUNT(LogicalSaveInfo); i++) {
130 LogicalSaveInfo[i].slot = -1;
131 LogicalSaveInfo[i].count = -1;
132 }
133
134 for (i = 0; i < ARRAY_COUNT(PhysicalSaveInfo); i++) {
135 fio_read_flash(i, fetchBuf, sizeof(SaveData));
136 if (fio_validate_file_checksum(fetchBuf)) {
137 PhysicalSaveInfo[i].slot = validBuf->saveSlot;
138 PhysicalSaveInfo[i].count = validBuf->saveCount;
139 // logical saves only track the most recent physical save for each slot
140 if (LogicalSaveInfo[validBuf->saveSlot].count < validBuf->saveCount) {
141 LogicalSaveInfo[validBuf->saveSlot].slot = i;
142 LogicalSaveInfo[validBuf->saveSlot].count = validBuf->saveCount;
143 }
144 }
145 }
146
147 minSaveCount = 0x7FFFFFFF;
148 for (j = 0; j < ARRAY_COUNT(PhysicalSaveInfo); j++) {
149 // find a logical save for each physical save
150 for (i = 0; i < ARRAY_COUNT(LogicalSaveInfo); i++) {
151 if (j == LogicalSaveInfo[i].slot) {
152 break;
153 }
154 }
155 // condition holds only for physical saves not mapped to logical ones
156 if (i == ARRAY_COUNT(LogicalSaveInfo)) {
157 if (PhysicalSaveInfo[j].count < minSaveCount) {
158 // choose the least-recent unmapped physical save slot
159 minSaveCount = PhysicalSaveInfo[j].count;
161 }
162 }
163 }
164 return TRUE;
165}
BSS SaveData FetchSaveBuffer
Definition fio.c:15
BSS SaveInfo LogicalSaveInfo[4]
Definition fio.c:16
BSS s32 NextAvailablePhysicalSave
Definition fio.c:18
s32 slot
Definition fio.c:8
s32 count
Definition fio.c:9
b32 fio_validate_file_checksum(SaveData *saveData)
Definition fio.c:117
BSS SaveInfo PhysicalSaveInfo[6]
Definition fio.c:17
#define ARRAY_COUNT(arr)
Definition macros.h:40
s32 saveSlot
Definition versioning.h:152
s32 saveCount
Definition versioning.h:153

Referenced by fio_erase_game(), fio_load_game(), and fio_save_game().

◆ fio_load_game()

b32 fio_load_game ( s32 saveSlot)

Definition at line 167 of file fio.c.

167 {
168 gGameStatusPtr->saveSlot = saveSlot;
169
171 fio_read_flash(LogicalSaveInfo[saveSlot].slot, &gCurrentSaveFile, MAX(sizeof(VanillaSaveData), sizeof(SaveData)));
172
173 if (strcmp(gCurrentSaveFile.magicString, MagicSaveString) == 0) {
176 }
179 return TRUE;
180 }
181 return FALSE;
182}
b32 fio_fetch_saved_file_info(void)
Definition fio.c:124
SaveData gCurrentSaveFile
Definition fio.c:21
SaveFileSummary gSaveSlotSummary[4]
GameStatus * gGameStatusPtr
Definition main_loop.c:32
void fio_deserialize_state()
Load game data from gCurrentSaveFile Performs version checking and supports potential migration betwe...
Definition versioning.c:36
SaveFileSummary summary
Definition versioning.h:170
SaveData struct from unmodified Paper Mario.
Definition versioning.h:107

Referenced by filemenu_init(), filemenu_main_handle_input(), filemenu_yesno_handle_input(), and state_step_startup().

◆ fio_save_game()

void fio_save_game ( s32 saveSlot)

Definition at line 184 of file fio.c.

184 {
186
187 gGameStatusPtr->saveSlot = saveSlot;
188
190
194
196
198
199 // adding 1 accounts for null terminator
201
206
207 gCurrentSaveFile.saveSlot = saveSlot;
210
212 gCurrentSaveFile.crc2 = ~gCurrentSaveFile.crc1;
214 gCurrentSaveFile.crc2 = ~gCurrentSaveFile.crc1;
215
218}
#define DX_MOD_VER_MAJOR
Definition config.h:8
#define DX_MOD_NAME
Creates the version string that appears on the file select menu.
Definition config.h:7
#define DX_MOD_VER_PATCH
Definition config.h:10
#define DX_MOD_VER_MINOR
Definition config.h:9
#define ASSERT(condition)
s32 get_spirits_rescued(void)
Definition fio.c:29
PlayerData gPlayerData
Definition 77480.c:40
void fio_serialize_state()
Store current game data to gCurrentSaveFile.
Definition versioning.c:9
s8 majorVersion
Definition versioning.h:146
s8 minorVersion
Definition versioning.h:147
char modName[28]
Definition versioning.h:145
s8 patchVersion
Definition versioning.h:148

Referenced by entity_SaveBlock_save_data(), filemenu_main_handle_input(), and filemenu_yesno_handle_input().

◆ fio_erase_game()

void fio_erase_game ( s32 saveSlot)

Definition at line 220 of file fio.c.

220 {
221 s32 i;
222
224
225 for (i = 0; i < ARRAY_COUNT(PhysicalSaveInfo); i++) {
226 if (PhysicalSaveInfo[i].slot == saveSlot) {
228 }
229 }
230}

Referenced by filemenu_yesno_handle_input().

◆ fio_init_flash()

void fio_init_flash ( void )

Definition at line 232 of file fio.c.

232 {
233 osFlashInit();
234}

Referenced by load_engine_data().

Variable Documentation

◆ FetchSaveBuffer

BSS SaveData FetchSaveBuffer

Definition at line 15 of file fio.c.

Referenced by fio_fetch_saved_file_info().

◆ LogicalSaveInfo

BSS SaveInfo LogicalSaveInfo[4]

Definition at line 16 of file fio.c.

Referenced by fio_fetch_saved_file_info(), and fio_load_game().

◆ PhysicalSaveInfo

BSS SaveInfo PhysicalSaveInfo[6]

Definition at line 17 of file fio.c.

Referenced by fio_erase_game(), and fio_fetch_saved_file_info().

◆ NextAvailablePhysicalSave

BSS s32 NextAvailablePhysicalSave

Definition at line 18 of file fio.c.

Referenced by fio_fetch_saved_file_info(), and fio_save_game().

◆ gSaveGlobals

◆ gCurrentSaveFile

◆ MagicSaveString

char MagicSaveString[] = "Mario Story 006"