Fixed lua event hooks sometimes attaching to the wrong mod entry

This commit is contained in:
MysterD 2022-03-10 20:18:54 -08:00
parent e747f50f8b
commit 9aa7a7588d
1 changed files with 23 additions and 20 deletions

View File

@ -10,8 +10,8 @@ static u64* sBehaviorOffset = &gPcDebug.bhvOffset;
struct LuaHookedEvent { struct LuaHookedEvent {
int reference[MAX_HOOKED_REFERENCES]; int reference[MAX_HOOKED_REFERENCES];
struct ModListEntry* entry[MAX_HOOKED_REFERENCES];
int count; int count;
struct ModListEntry* entry;
}; };
static struct LuaHookedEvent sHookedEvents[HOOK_MAX] = { 0 }; static struct LuaHookedEvent sHookedEvents[HOOK_MAX] = { 0 };
@ -52,8 +52,8 @@ int smlua_hook_event(lua_State* L) {
} }
hook->reference[hook->count] = ref; hook->reference[hook->count] = ref;
hook->entry[hook->count] = gLuaActiveEntry;
hook->count++; hook->count++;
hook->entry = gLuaActiveEntry;
return 1; return 1;
} }
@ -67,7 +67,7 @@ void smlua_call_event_hooks(enum LuaHookedEventType hookType) {
lua_rawgeti(L, LUA_REGISTRYINDEX, hook->reference[i]); lua_rawgeti(L, LUA_REGISTRYINDEX, hook->reference[i]);
// call the callback // call the callback
if (0 != smlua_call_hook(L, 0, 0, 0, hook->entry)) { if (0 != smlua_call_hook(L, 0, 0, 0, hook->entry[i])) {
LOG_LUA("Failed to call the event_hook callback: %u, %s", hookType, lua_tostring(L, -1)); LOG_LUA("Failed to call the event_hook callback: %u, %s", hookType, lua_tostring(L, -1));
smlua_logline(); smlua_logline();
continue; continue;
@ -90,7 +90,7 @@ void smlua_call_event_hooks_mario_param(enum LuaHookedEventType hookType, struct
lua_remove(L, -2); lua_remove(L, -2);
// call the callback // call the callback
if (0 != smlua_call_hook(L, 1, 0, 0, hook->entry)) { if (0 != smlua_call_hook(L, 1, 0, 0, hook->entry[i])) {
LOG_LUA("Failed to call the callback: %u, %s", hookType, lua_tostring(L, -1)); LOG_LUA("Failed to call the callback: %u, %s", hookType, lua_tostring(L, -1));
smlua_logline(); smlua_logline();
continue; continue;
@ -119,7 +119,7 @@ void smlua_call_event_hooks_mario_params(enum LuaHookedEventType hookType, struc
lua_remove(L, -2); lua_remove(L, -2);
// call the callback // call the callback
if (0 != smlua_call_hook(L, 2, 0, 0, hook->entry)) { if (0 != smlua_call_hook(L, 2, 0, 0, hook->entry[i])) {
LOG_LUA("Failed to call the callback: %u, %s", hookType, lua_tostring(L, -1)); LOG_LUA("Failed to call the callback: %u, %s", hookType, lua_tostring(L, -1));
smlua_logline(); smlua_logline();
continue; continue;
@ -151,7 +151,7 @@ void smlua_call_event_hooks_interact_params(enum LuaHookedEventType hookType, st
lua_pushboolean(L, interactValue); lua_pushboolean(L, interactValue);
// call the callback // call the callback
if (0 != smlua_call_hook(L, 4, 0, 0, hook->entry)) { if (0 != smlua_call_hook(L, 4, 0, 0, hook->entry[i])) {
LOG_LUA("Failed to call the callback: %u, %s", hookType, lua_tostring(L, -1)); LOG_LUA("Failed to call the callback: %u, %s", hookType, lua_tostring(L, -1));
smlua_logline(); smlua_logline();
continue; continue;
@ -174,7 +174,7 @@ void smlua_call_event_hooks_network_player_param(enum LuaHookedEventType hookTyp
lua_remove(L, -2); lua_remove(L, -2);
// call the callback // call the callback
if (0 != smlua_call_hook(L, 1, 0, 0, hook->entry)) { if (0 != smlua_call_hook(L, 1, 0, 0, hook->entry[i])) {
LOG_LUA("Failed to call the callback: %u, %s", hookType, lua_tostring(L, -1)); LOG_LUA("Failed to call the callback: %u, %s", hookType, lua_tostring(L, -1));
smlua_logline(); smlua_logline();
continue; continue;
@ -668,29 +668,32 @@ int smlua_hook_on_sync_table_change(lua_State* L) {
static void smlua_clear_hooks(void) { static void smlua_clear_hooks(void) {
for (int i = 0; i < HOOK_MAX; i++) { for (int i = 0; i < HOOK_MAX; i++) {
for (int j = 0; j < sHookedEvents[i].count; j++) { struct LuaHookedEvent* hooked = &sHookedEvents[i];
sHookedEvents[i].reference[j] = 0; for (int j = 0; j < hooked->count; j++) {
hooked->reference[j] = 0;
hooked->entry[j] = NULL;
} }
sHookedEvents[i].count = 0; hooked->count = 0;
sHookedEvents[i].entry = NULL;
} }
for (int i = 0; i < sHookedMarioActionsCount; i++) { for (int i = 0; i < sHookedMarioActionsCount; i++) {
sHookedMarioActions[i].action = 0; struct LuaHookedMarioAction* hooked = &sHookedMarioActions[i];
sHookedMarioActions[i].reference = 0; hooked->action = 0;
sHookedMarioActions[i].entry = NULL; hooked->entry = NULL;
hooked->reference = 0;
} }
sHookedMarioActionsCount = 0; sHookedMarioActionsCount = 0;
for (int i = 0; i < sHookedChatCommandsCount; i++) { for (int i = 0; i < sHookedChatCommandsCount; i++) {
if (sHookedChatCommands[i].command != NULL) { free(sHookedChatCommands[i].command); } struct LuaHookedChatCommand* hooked = &sHookedChatCommands[i];
sHookedChatCommands[i].command = NULL; if (hooked->command != NULL) { free(hooked->command); }
hooked->command = NULL;
if (sHookedChatCommands[i].description != NULL) { free(sHookedChatCommands[i].description); } if (hooked->description != NULL) { free(sHookedChatCommands[i].description); }
sHookedChatCommands[i].description = NULL; hooked->description = NULL;
sHookedChatCommands[i].reference = 0; hooked->reference = 0;
sHookedChatCommands[i].entry = NULL; hooked->entry = NULL;
} }
sHookedChatCommandsCount = 0; sHookedChatCommandsCount = 0;