Prevent crash and end-of-level corruption when someone collects a non-course star

This commit is contained in:
MysterD 2022-01-28 00:01:34 -08:00
parent bffcd7c30b
commit 35ac2165ad
4 changed files with 11 additions and 9 deletions

View file

@ -917,7 +917,7 @@ u32 interact_star_or_key(struct MarioState *m, UNUSED u32 interactType, struct O
// sync the star collection
network_send_collect_star(o, m->numCoins, starIndex);
}
save_file_collect_star_or_key(m->numCoins, starIndex);
save_file_collect_star_or_key(m->numCoins, starIndex, 0);
s32 numStars = save_file_get_total_star_count(gCurrSaveFileNum - 1, COURSE_MIN - 1, COURSE_MAX - 1);
for (int i = 0; i < MAX_PLAYERS; i++) {

View file

@ -485,7 +485,7 @@ void save_file_reload(void) {
* Update the current save file after collecting a star or a key.
* If coin score is greater than the current high score, update it.
*/
void save_file_collect_star_or_key(s16 coinScore, s16 starIndex) {
void save_file_collect_star_or_key(s16 coinScore, s16 starIndex, u8 fromNetwork) {
s32 fileIndex = gCurrSaveFileNum - 1;
s32 courseIndex = gCurrCourseNum - 1;
@ -493,12 +493,14 @@ void save_file_collect_star_or_key(s16 coinScore, s16 starIndex) {
s32 starFlag = 1 << starIndex;
UNUSED s32 flags = save_file_get_flags();
gLastCompletedCourseNum = courseIndex + 1;
gLastCompletedStarNum = starIndex + 1;
sUnusedGotGlobalCoinHiScore = 0;
gGotFileCoinHiScore = 0;
if (!fromNetwork) {
gLastCompletedCourseNum = courseIndex + 1;
gLastCompletedStarNum = starIndex + 1;
sUnusedGotGlobalCoinHiScore = 0;
gGotFileCoinHiScore = 0;
}
if (courseIndex >= 0 && courseIndex < COURSE_STAGES_COUNT) {
if (courseIndex >= 0 && courseIndex < COURSE_STAGES_COUNT && !fromNetwork) {
//! Compares the coin score as a 16 bit value, but only writes the 8 bit
// truncation. This can allow a high score to decrease.

View file

@ -126,7 +126,7 @@ void save_file_erase(s32 fileIndex);
BAD_RETURN(s32) save_file_copy(s32 srcFileIndex, s32 destFileIndex);
void save_file_load_all(u8 reload);
void save_file_reload(void);
void save_file_collect_star_or_key(s16 coinScore, s16 starIndex);
void save_file_collect_star_or_key(s16 coinScore, s16 starIndex, u8 fromNetwork);
s32 save_file_exists(s32 fileIndex);
u32 save_file_get_max_coin_score(s32 courseIndex);
s32 save_file_get_course_star_count(s32 fileIndex, s32 courseIndex);

View file

@ -84,7 +84,7 @@ void network_receive_collect_star(struct Packet* p) {
const void* behavior = get_behavior_from_id(behaviorId);
save_file_collect_star_or_key(coinScore, starIndex);
save_file_collect_star_or_key(coinScore, starIndex, 1);
s32 numStars = save_file_get_total_star_count(gCurrSaveFileNum - 1, COURSE_MIN - 1, COURSE_MAX - 1);
for (int i = 0; i < MAX_PLAYERS; i++) {