From c7edf85ad92269059f35ec8f8a51cac8ea4a182e Mon Sep 17 00:00:00 2001 From: David Joslin Date: Wed, 8 Nov 2023 12:34:55 -0800 Subject: [PATCH] Fix crash in tox_box_move() --- autogen/convert_functions.py | 4 +-- autogen/lua_definitions/functions.lua | 11 -------- docs/lua/functions-4.md | 38 --------------------------- docs/lua/functions.md | 2 -- src/game/behaviors/tox_box.inc.c | 19 +++++++------- src/game/object_helpers.c | 24 +++++++++++------ src/pc/lua/smlua_functions_autogen.c | 36 ------------------------- 7 files changed, 27 insertions(+), 107 deletions(-) diff --git a/autogen/convert_functions.py b/autogen/convert_functions.py index 2cecb5a9..923418f6 100644 --- a/autogen/convert_functions.py +++ b/autogen/convert_functions.py @@ -92,7 +92,7 @@ override_disallowed_functions = { "src/game/mario_actions_object.c": [ "^[us]32 act_.*" ], "src/game/mario_actions_stationary.c": [ "^[us]32 act_.*" ], "src/game/mario_actions_submerged.c": [ "^[us]32 act_.*" ], - "src/game/mario_step.h": [ " stub_mario_step", "transfer_bully_speed"], + "src/game/mario_step.h": [ " stub_mario_step", "transfer_bully_speed" ], "src/game/mario.h": [ " init_mario" ], "src/pc/djui/djui_console.h": [ " djui_console_create", "djui_console_message_create" ], "src/pc/djui/djui_chat_message.h": [ "create_from" ], @@ -100,7 +100,7 @@ override_disallowed_functions = { "src/game/sound_init.h": [ "_loop_", "thread4_", "set_sound_mode" ], "src/pc/network/network_utils.h": [ "network_get_player_text_color[^_]" ], "src/pc/network/network_player.h": [ "_init", "_connected[^_]", "_shutdown", "_disconnected", "_update", "construct_player_popup" ], - "src/game/object_helpers.c": [ "spawn_obj", "^bhv_", "abs[fi]", "^bit_shift", "_debug$", "^stub_", "_set_model" ], + "src/game/object_helpers.c": [ "spawn_obj", "^bhv_", "abs[fi]", "^bit_shift", "_debug$", "^stub_", "_set_model", "cur_obj_set_direction_table", "cur_obj_progress_direction_table" ], "src/game/obj_behaviors.c": [ "debug_" ], "src/game/obj_behaviors_2.c": [ "wiggler_jumped_on_attack_handler", "huge_goomba_weakly_attacked" ], "src/game/spawn_sound.c": [ "spawner" ], diff --git a/autogen/lua_definitions/functions.lua b/autogen/lua_definitions/functions.lua index 2f59190c..4d69460b 100644 --- a/autogen/lua_definitions/functions.lua +++ b/autogen/lua_definitions/functions.lua @@ -6990,11 +6990,6 @@ function cur_obj_outside_home_square(halfLength) -- ... end ---- @return integer -function cur_obj_progress_direction_table() - -- ... -end - --- @param radius number --- @return nil function cur_obj_push_mario_away(radius) @@ -7071,12 +7066,6 @@ function cur_obj_set_billboard_if_vanilla_cam() -- ... end ---- @param a0 Pointer_integer ---- @return integer -function cur_obj_set_direction_table(a0) - -- ... -end - --- @return nil function cur_obj_set_face_angle_to_move_angle() -- ... diff --git a/docs/lua/functions-4.md b/docs/lua/functions-4.md index 425eec0d..568147a0 100644 --- a/docs/lua/functions-4.md +++ b/docs/lua/functions-4.md @@ -3753,24 +3753,6 @@
-## [cur_obj_progress_direction_table](#cur_obj_progress_direction_table) - -### Lua Example -`local integerValue = cur_obj_progress_direction_table()` - -### Parameters -- None - -### Returns -- `integer` - -### C Prototype -`s32 cur_obj_progress_direction_table(void);` - -[:arrow_up_small:](#) - -
- ## [cur_obj_push_mario_away](#cur_obj_push_mario_away) ### Lua Example @@ -4022,26 +4004,6 @@
-## [cur_obj_set_direction_table](#cur_obj_set_direction_table) - -### Lua Example -`local integerValue = cur_obj_set_direction_table(a0)` - -### Parameters -| Field | Type | -| ----- | ---- | -| a0 | `Pointer` <`integer`> | - -### Returns -- `integer` - -### C Prototype -`s32 cur_obj_set_direction_table(s8 *a0);` - -[:arrow_up_small:](#) - -
- ## [cur_obj_set_face_angle_to_move_angle](#cur_obj_set_face_angle_to_move_angle) ### Lua Example diff --git a/docs/lua/functions.md b/docs/lua/functions.md index 72783e47..6b692166 100644 --- a/docs/lua/functions.md +++ b/docs/lua/functions.md @@ -1328,7 +1328,6 @@ - [cur_obj_nearest_object_with_behavior](functions-4.md#cur_obj_nearest_object_with_behavior) - [cur_obj_outside_home_rectangle](functions-4.md#cur_obj_outside_home_rectangle) - [cur_obj_outside_home_square](functions-4.md#cur_obj_outside_home_square) - - [cur_obj_progress_direction_table](functions-4.md#cur_obj_progress_direction_table) - [cur_obj_push_mario_away](functions-4.md#cur_obj_push_mario_away) - [cur_obj_push_mario_away_from_cylinder](functions-4.md#cur_obj_push_mario_away_from_cylinder) - [cur_obj_reflect_move_angle_off_wall](functions-4.md#cur_obj_reflect_move_angle_off_wall) @@ -1342,7 +1341,6 @@ - [cur_obj_scale_over_time](functions-4.md#cur_obj_scale_over_time) - [cur_obj_set_behavior](functions-4.md#cur_obj_set_behavior) - [cur_obj_set_billboard_if_vanilla_cam](functions-4.md#cur_obj_set_billboard_if_vanilla_cam) - - [cur_obj_set_direction_table](functions-4.md#cur_obj_set_direction_table) - [cur_obj_set_face_angle_to_move_angle](functions-4.md#cur_obj_set_face_angle_to_move_angle) - [cur_obj_set_hitbox_and_die_if_attacked](functions-4.md#cur_obj_set_hitbox_and_die_if_attacked) - [cur_obj_set_hitbox_radius_and_height](functions-4.md#cur_obj_set_hitbox_radius_and_height) diff --git a/src/game/behaviors/tox_box.inc.c b/src/game/behaviors/tox_box.inc.c index db53dea5..e0916f6a 100644 --- a/src/game/behaviors/tox_box.inc.c +++ b/src/game/behaviors/tox_box.inc.c @@ -1,12 +1,12 @@ // tox_box.c.inc -s8 D_8032F8F0[] = { 4, 1, 4, 1, 6, 1, 6, 1, 5, 1, 5, 1, 6, 1, 6, 1, 5, 1, 2, 4, 1, 4, 1, 4, 1, 2, - 5, 1, 5, 1, 7, 1, 7, 1, 4, 1, 4, 1, 7, 1, 7, 1, 5, 1, 5, 1, 5, 1, 2, 4, 1, -1 }; -s8 D_8032F924[] = { 4, 1, 4, 1, 7, 1, 7, 1, 7, 1, 2, 6, 1, 6, 1, 6, 1, 5, - 1, 5, 1, 6, 1, 5, 1, 5, 1, 2, 4, 1, 4, 1, 7, 1, -1 }; -s8 D_8032F948[] = { 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 2, 5, 1, 5, 1, 5, 1, 5, - 1, 5, 1, 7, 1, 2, 6, 1, 6, 1, 5, 1, 2, 4, 1, 7, 1, -1 }; -s8 *D_8032F96C[] = { D_8032F8F0, D_8032F924, D_8032F948 }; +s8 sToxBoxDirectionTable0[] = { 4, 1, 4, 1, 6, 1, 6, 1, 5, 1, 5, 1, 6, 1, 6, 1, 5, 1, 2, 4, 1, 4, 1, 4, 1, 2, + 5, 1, 5, 1, 7, 1, 7, 1, 4, 1, 4, 1, 7, 1, 7, 1, 5, 1, 5, 1, 5, 1, 2, 4, 1, -1 }; +s8 sToxBoxDirectionTable1[] = { 4, 1, 4, 1, 7, 1, 7, 1, 7, 1, 2, 6, 1, 6, 1, 6, 1, 5, + 1, 5, 1, 6, 1, 5, 1, 5, 1, 2, 4, 1, 4, 1, 7, 1, -1 }; +s8 sToxBoxDirectionTable2[] = { 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 2, 5, 1, 5, 1, 5, 1, 5, + 1, 5, 1, 7, 1, 2, 6, 1, 6, 1, 5, 1, 2, 4, 1, 7, 1, -1 }; +s8 *sToxBoxDirectionTables[] = { sToxBoxDirectionTable0, sToxBoxDirectionTable1, sToxBoxDirectionTable2 }; void tox_box_shake_screen(void) { if (o && o->oDistanceToMario < 3000.0f) @@ -71,9 +71,8 @@ void tox_box_act_3(void) { void tox_box_act_0(void) { if (!o) { return; } - if (!BHV_ARR_CHECK(D_8032F96C, o->oBehParams2ndByte, s8*)) { return; } - s8 *sp1C = D_8032F96C[o->oBehParams2ndByte]; - o->oAction = cur_obj_set_direction_table(sp1C); + if (!BHV_ARR_CHECK(sToxBoxDirectionTables, o->oBehParams2ndByte, s8*)) { return; } + o->oAction = cur_obj_set_direction_table(sToxBoxDirectionTables[o->oBehParams2ndByte]); } void (*sToxBoxActions[])(void) = { tox_box_act_0, tox_box_act_1, tox_box_act_2, tox_box_act_3, diff --git a/src/game/object_helpers.c b/src/game/object_helpers.c index eea989af..227ba4fe 100644 --- a/src/game/object_helpers.c +++ b/src/game/object_helpers.c @@ -2716,20 +2716,28 @@ s32 cur_obj_set_direction_table(s8 *a0) { s32 cur_obj_progress_direction_table(void) { if (!o) { return 0; } - s8 spF; - s8 *sp8 = o->oToxBoxMovementPattern; - s32 sp4 = o->oToxBoxMovementStep + 1; - if (!sp8) { return 0; } + s8 ret; + s8 *table = o->oToxBoxMovementPattern; + s32 index = o->oToxBoxMovementStep + 1; + if (!table) { return 0; } - if (sp8[sp4] != -1) { - spF = sp8[sp4]; + s32 tableLength = 0; + while (table[tableLength] != -1 && tableLength < 50) { + tableLength++; + } + + if (tableLength >= 50 || index >= tableLength) { + ret = table[0]; + o->oToxBoxMovementStep = 0; + } else if (table[index] != -1) { + ret = table[index]; o->oToxBoxMovementStep++; } else { - spF = sp8[0]; + ret = table[0]; o->oToxBoxMovementStep = 0; } - return spF; + return ret; } void stub_obj_helpers_3(UNUSED s32 sp0, UNUSED s32 sp4) { diff --git a/src/pc/lua/smlua_functions_autogen.c b/src/pc/lua/smlua_functions_autogen.c index 21b92519..3012caf9 100644 --- a/src/pc/lua/smlua_functions_autogen.c +++ b/src/pc/lua/smlua_functions_autogen.c @@ -23355,22 +23355,6 @@ int smlua_func_cur_obj_outside_home_square(lua_State* L) { return 1; } -int smlua_func_cur_obj_progress_direction_table(UNUSED lua_State* L) { - if (L == NULL) { return 0; } - - int top = lua_gettop(L); - if (top != 0) { - LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "cur_obj_progress_direction_table", 0, top); - return 0; - } - - - extern s32 cur_obj_progress_direction_table(void); - lua_pushinteger(L, cur_obj_progress_direction_table()); - - return 1; -} - int smlua_func_cur_obj_push_mario_away(lua_State* L) { if (L == NULL) { return 0; } @@ -23601,24 +23585,6 @@ int smlua_func_cur_obj_set_billboard_if_vanilla_cam(UNUSED lua_State* L) { return 1; } -int smlua_func_cur_obj_set_direction_table(lua_State* L) { - if (L == NULL) { return 0; } - - int top = lua_gettop(L); - if (top != 1) { - LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "cur_obj_set_direction_table", 1, top); - return 0; - } - - s8 * a0 = (s8 *)smlua_to_cpointer(L, 1, LVT_S8_P); - if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "cur_obj_set_direction_table"); return 0; } - - extern s32 cur_obj_set_direction_table(s8 *a0); - lua_pushinteger(L, cur_obj_set_direction_table(a0)); - - return 1; -} - int smlua_func_cur_obj_set_face_angle_to_move_angle(UNUSED lua_State* L) { if (L == NULL) { return 0; } @@ -32520,7 +32486,6 @@ void smlua_bind_functions_autogen(void) { smlua_bind_function(L, "cur_obj_nearest_object_with_behavior", smlua_func_cur_obj_nearest_object_with_behavior); smlua_bind_function(L, "cur_obj_outside_home_rectangle", smlua_func_cur_obj_outside_home_rectangle); smlua_bind_function(L, "cur_obj_outside_home_square", smlua_func_cur_obj_outside_home_square); - smlua_bind_function(L, "cur_obj_progress_direction_table", smlua_func_cur_obj_progress_direction_table); smlua_bind_function(L, "cur_obj_push_mario_away", smlua_func_cur_obj_push_mario_away); smlua_bind_function(L, "cur_obj_push_mario_away_from_cylinder", smlua_func_cur_obj_push_mario_away_from_cylinder); smlua_bind_function(L, "cur_obj_reflect_move_angle_off_wall", smlua_func_cur_obj_reflect_move_angle_off_wall); @@ -32534,7 +32499,6 @@ void smlua_bind_functions_autogen(void) { smlua_bind_function(L, "cur_obj_scale_over_time", smlua_func_cur_obj_scale_over_time); smlua_bind_function(L, "cur_obj_set_behavior", smlua_func_cur_obj_set_behavior); smlua_bind_function(L, "cur_obj_set_billboard_if_vanilla_cam", smlua_func_cur_obj_set_billboard_if_vanilla_cam); - smlua_bind_function(L, "cur_obj_set_direction_table", smlua_func_cur_obj_set_direction_table); smlua_bind_function(L, "cur_obj_set_face_angle_to_move_angle", smlua_func_cur_obj_set_face_angle_to_move_angle); smlua_bind_function(L, "cur_obj_set_hitbox_and_die_if_attacked", smlua_func_cur_obj_set_hitbox_and_die_if_attacked); smlua_bind_function(L, "cur_obj_set_hitbox_radius_and_height", smlua_func_cur_obj_set_hitbox_radius_and_height);