From 35ac2165ad77094449f03bacf9da78fdef3e5c66 Mon Sep 17 00:00:00 2001 From: MysterD Date: Fri, 28 Jan 2022 00:01:34 -0800 Subject: [PATCH] Prevent crash and end-of-level corruption when someone collects a non-course star --- src/game/interaction.c | 2 +- src/game/save_file.c | 14 ++++++++------ src/game/save_file.h | 2 +- src/pc/network/packets/packet_collect_star.c | 2 +- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/game/interaction.c b/src/game/interaction.c index dd2a7ae04..06f1d2120 100644 --- a/src/game/interaction.c +++ b/src/game/interaction.c @@ -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++) { diff --git a/src/game/save_file.c b/src/game/save_file.c index 7c2c15541..408ab1d79 100644 --- a/src/game/save_file.c +++ b/src/game/save_file.c @@ -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. diff --git a/src/game/save_file.h b/src/game/save_file.h index 5b9da27e5..bc86ddfc0 100644 --- a/src/game/save_file.h +++ b/src/game/save_file.h @@ -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); diff --git a/src/pc/network/packets/packet_collect_star.c b/src/pc/network/packets/packet_collect_star.c index 8609068be..493ee657c 100644 --- a/src/pc/network/packets/packet_collect_star.c +++ b/src/pc/network/packets/packet_collect_star.c @@ -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++) {