From 1dd70283dadcb910614f16bdee7946e8259814c5 Mon Sep 17 00:00:00 2001 From: MysterD Date: Sat, 20 May 2023 20:07:17 -0700 Subject: [PATCH] Fixed bounds checks in save_file_is_cannon_unlocked() --- autogen/lua_definitions/functions.lua | 7 +++++++ docs/lua/functions-4.md | 21 +++++++++++++++++++++ docs/lua/functions.md | 1 + src/game/save_file.c | 4 ++-- src/pc/lua/smlua_functions_autogen.c | 20 ++++++++++++++++++++ 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/autogen/lua_definitions/functions.lua b/autogen/lua_definitions/functions.lua index eea5dedc..d841e9c8 100644 --- a/autogen/lua_definitions/functions.lua +++ b/autogen/lua_definitions/functions.lua @@ -7897,6 +7897,13 @@ function save_file_get_total_star_count(fileIndex, minCourse, maxCourse) -- ... end +--- @param fileIndex integer +--- @param courseIndex integer +--- @return integer +function save_file_is_cannon_unlocked(fileIndex, courseIndex) + -- ... +end + --- @param load_all integer --- @return nil function save_file_reload(load_all) diff --git a/docs/lua/functions-4.md b/docs/lua/functions-4.md index 9174a464..d4b35c93 100644 --- a/docs/lua/functions-4.md +++ b/docs/lua/functions-4.md @@ -6705,6 +6705,27 @@
+## [save_file_is_cannon_unlocked](#save_file_is_cannon_unlocked) + +### Lua Example +`local integerValue = save_file_is_cannon_unlocked(fileIndex, courseIndex)` + +### Parameters +| Field | Type | +| ----- | ---- | +| fileIndex | `integer` | +| courseIndex | `integer` | + +### Returns +- `integer` + +### C Prototype +`s32 save_file_is_cannon_unlocked(s32 fileIndex, s32 courseIndex);` + +[:arrow_up_small:](#) + +
+ ## [save_file_reload](#save_file_reload) ### Lua Example diff --git a/docs/lua/functions.md b/docs/lua/functions.md index cc94c290..c66c5e9a 100644 --- a/docs/lua/functions.md +++ b/docs/lua/functions.md @@ -1461,6 +1461,7 @@ - [save_file_get_sound_mode](functions-4.md#save_file_get_sound_mode) - [save_file_get_star_flags](functions-4.md#save_file_get_star_flags) - [save_file_get_total_star_count](functions-4.md#save_file_get_total_star_count) + - [save_file_is_cannon_unlocked](functions-4.md#save_file_is_cannon_unlocked) - [save_file_reload](functions-4.md#save_file_reload) - [save_file_set_flags](functions-4.md#save_file_set_flags) - [save_file_set_star_flags](functions-4.md#save_file_set_star_flags) diff --git a/src/game/save_file.c b/src/game/save_file.c index 25e6fc0d..f60aefa6 100644 --- a/src/game/save_file.c +++ b/src/game/save_file.c @@ -697,9 +697,9 @@ s32 save_file_get_course_coin_score(s32 fileIndex, s32 courseIndex) { * Return TRUE if the cannon is unlocked in the current course. */ s32 save_file_is_cannon_unlocked(s32 fileIndex, s32 courseIndex) { - if (INVALID_FILE_INDEX(gCurrSaveFileNum - 1)) { return 0; } + if (INVALID_FILE_INDEX(fileIndex)) { return 0; } if (INVALID_SRC_SLOT(gSaveFileUsingBackupSlot)) { return 0; } - if (INVALID_COURSE_STAR_INDEX(gCurrCourseNum)) { return 0; } + if (INVALID_COURSE_STAR_INDEX(courseIndex)) { return 0; } return (gSaveBuffer.files[fileIndex][gSaveFileUsingBackupSlot].courseStars[courseIndex] & 0x80) != 0; } diff --git a/src/pc/lua/smlua_functions_autogen.c b/src/pc/lua/smlua_functions_autogen.c index 80e7d5e6..26755c04 100644 --- a/src/pc/lua/smlua_functions_autogen.c +++ b/src/pc/lua/smlua_functions_autogen.c @@ -26466,6 +26466,25 @@ int smlua_func_save_file_get_total_star_count(lua_State* L) { return 1; } +int smlua_func_save_file_is_cannon_unlocked(lua_State* L) { + if (L == NULL) { return 0; } + + int top = lua_gettop(L); + if (top != 2) { + LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "save_file_is_cannon_unlocked", 2, top); + return 0; + } + + s32 fileIndex = smlua_to_integer(L, 1); + if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "save_file_is_cannon_unlocked"); return 0; } + s32 courseIndex = smlua_to_integer(L, 2); + if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 2, "save_file_is_cannon_unlocked"); return 0; } + + lua_pushinteger(L, save_file_is_cannon_unlocked(fileIndex, courseIndex)); + + return 1; +} + int smlua_func_save_file_reload(lua_State* L) { if (L == NULL) { return 0; } @@ -31442,6 +31461,7 @@ void smlua_bind_functions_autogen(void) { smlua_bind_function(L, "save_file_get_sound_mode", smlua_func_save_file_get_sound_mode); smlua_bind_function(L, "save_file_get_star_flags", smlua_func_save_file_get_star_flags); smlua_bind_function(L, "save_file_get_total_star_count", smlua_func_save_file_get_total_star_count); + smlua_bind_function(L, "save_file_is_cannon_unlocked", smlua_func_save_file_is_cannon_unlocked); smlua_bind_function(L, "save_file_reload", smlua_func_save_file_reload); smlua_bind_function(L, "save_file_set_flags", smlua_func_save_file_set_flags); smlua_bind_function(L, "save_file_set_star_flags", smlua_func_save_file_set_star_flags);