Fixed crash when spawning a custom behavior that doesn't override fomr Lua

This commit is contained in:
MysterD 2022-03-08 00:32:12 -08:00
parent d777b7cfba
commit 7a5a91007c
3 changed files with 6 additions and 5 deletions

View file

@ -526,7 +526,7 @@ enum BehaviorId get_id_from_behavior(const BehaviorScript* behavior) {
} }
const BehaviorScript* get_behavior_from_id(enum BehaviorId id) { const BehaviorScript* get_behavior_from_id(enum BehaviorId id) {
const BehaviorScript* behavior = get_lua_behavior_from_id(id); const BehaviorScript* behavior = get_lua_behavior_from_id(id, true);
if (behavior != NULL) { return behavior; } if (behavior != NULL) { return behavior; }
if (id < 0 || id >= id_bhv_max_count) { if (id < 0 || id >= id_bhv_max_count) {

View file

@ -324,17 +324,18 @@ const BehaviorScript* smlua_override_behavior(const BehaviorScript* behavior) {
if (L == NULL) { return behavior; } if (L == NULL) { return behavior; }
enum BehaviorId id = get_id_from_behavior(behavior); enum BehaviorId id = get_id_from_behavior(behavior);
const BehaviorScript* luaBehavior = get_lua_behavior_from_id(id); const BehaviorScript* luaBehavior = get_lua_behavior_from_id(id, false);
if (luaBehavior != NULL) { return luaBehavior; } if (luaBehavior != NULL) { return luaBehavior; }
return behavior + *sBehaviorOffset; return behavior + *sBehaviorOffset;
} }
const BehaviorScript* get_lua_behavior_from_id(enum BehaviorId id) { const BehaviorScript* get_lua_behavior_from_id(enum BehaviorId id, bool returnOriginal) {
lua_State* L = gLuaState; lua_State* L = gLuaState;
if (L == NULL) { return false; } if (L == NULL) { return false; }
for (int i = 0; i < sHookedBehaviorsCount; i++) { for (int i = 0; i < sHookedBehaviorsCount; i++) {
struct LuaHookedBehavior* hooked = &sHookedBehaviors[i]; struct LuaHookedBehavior* hooked = &sHookedBehaviors[i];
if (hooked->behaviorId != id && hooked->overrideId != id) { continue; } if (hooked->behaviorId != id && hooked->overrideId != id) { continue; }
if (returnOriginal && !hooked->replace) { return hooked->originalBehavior; }
return hooked->behavior; return hooked->behavior;
} }
return NULL; return NULL;
@ -450,7 +451,7 @@ bool smlua_call_behavior_hook(const BehaviorScript** behavior, struct Object* ob
} }
// retrieve and remember first run // retrieve and remember first run
bool firstRun = (object->curBhvCommand == hooked->originalBehavior); bool firstRun = (object->curBhvCommand == hooked->originalBehavior) || (object->curBhvCommand == hooked->behavior);
if (firstRun && hooked->replace) { *behavior = &hooked->behavior[1]; } if (firstRun && hooked->replace) { *behavior = &hooked->behavior[1]; }
// get function and null check it // get function and null check it

View file

@ -38,7 +38,7 @@ void smlua_call_event_hooks_mario_params(enum LuaHookedEventType hookType, struc
void smlua_call_event_hooks_interact_params(enum LuaHookedEventType hookType, struct MarioState* m, struct Object* obj, u32 interactType, bool interactValue); void smlua_call_event_hooks_interact_params(enum LuaHookedEventType hookType, struct MarioState* m, struct Object* obj, u32 interactType, bool interactValue);
const BehaviorScript* smlua_override_behavior(const BehaviorScript* behavior); const BehaviorScript* smlua_override_behavior(const BehaviorScript* behavior);
const BehaviorScript* get_lua_behavior_from_id(enum BehaviorId id); const BehaviorScript* get_lua_behavior_from_id(enum BehaviorId id, bool returnOriginal);
bool smlua_call_behavior_hook(const BehaviorScript** behavior, struct Object* object, bool before); bool smlua_call_behavior_hook(const BehaviorScript** behavior, struct Object* object, bool before);
bool smlua_call_action_hook(struct MarioState* m, s32* returnValue); bool smlua_call_action_hook(struct MarioState* m, s32* returnValue);