Remove hook_exclamation_box()

This commit is contained in:
MysterD 2023-11-26 13:48:58 -08:00
parent 25f69605cf
commit 0204b2673d
7 changed files with 2 additions and 170 deletions

View File

@ -12307,7 +12307,7 @@ MAX_LOCAL_VERSION_LENGTH = 36
MAX_VERSION_LENGTH = 32 MAX_VERSION_LENGTH = 32
--- @type integer --- @type integer
MINOR_VERSION_NUMBER = 0 MINOR_VERSION_NUMBER = 1
--- @type integer --- @type integer
PATCH_VERSION_NUMBER = 0 PATCH_VERSION_NUMBER = 0

View File

@ -278,9 +278,3 @@ end
function level_script_parse(levelNum, func) function level_script_parse(levelNum, func)
-- ... -- ...
end end
--- @param readFunc (fun(obj:Object):nil)? Called after the exclamation box spawns an object. Use this function to read the contents of the object that spawned.
--- @param writeFunc (fun(box:Object):Object)? Called when the exclamation box is about to spawn an object. Use this function to spawn a different object, and return the object for the read function to use.
function hook_exclamation_box(readFunc, writeFunc)
end

View File

@ -146,52 +146,6 @@ hook_event(HOOK_MARIO_UPDATE, mario_update)
<br /> <br />
## [hook_exclamation_box](#hook_exclamation_box)
Activated when an exclamation box breaks, allowing mods to take control over exclamation boxes.
- `readFunction` is called after the exclamation box breaks, allowing mods to read the object that spawned.
- No return value is necessary.
- `writeFunction` is called when the exclamation box breaks, allowing mods to override the spawned object.
- Returning the spawned object is highly recommended as it prevents spawning both the vanilla and new object at the same time. It also allows `readFunction` to function properly.
### Parameters
| Field | Type |
| ----- | ---- |
| readFunction | `Lua Function` ([Object](structs.md#Object)) |
| writeFunction | `Lua Function` ([Object](structs.md#Object)): [Object](structs.md#Object) |
### Lua Example
```lua
local objects_to_spawn = {
[0] = {id_bhvGoomba, E_MODEL_GOOMBA},
[1] = {id_bhvKoopa, E_MODEL_KOOPA_WITH_SHELL},
[2] = {id_bhvTenCoinsSpawn, E_MODEL_NONE},
[3] = {id_bhvChainChomp, E_MODEL_CHAIN_CHOMP},
[4] = {id_bhvHeaveHo, E_MODEL_HEAVE_HO},
[5] = {id_bhvWingCap, E_MODEL_MARIOS_WING_CAP},
[6] = {id_bhvKoopaShell, E_MODEL_KOOPA_SHELL},
[7] = {id_bhvBoo, E_MODEL_BOO},
}
local function readFunction(obj)
if obj_has_behavior_id(obj, id_bhvFlame) ~= 0 then
print("FIREEEEEEEEE")
end
end
---@param box Object
local function writeFunction(box)
local spawn_object = objects_to_spawn[box.oBehParams2ndByte]
return spawn_sync_object(spawn_object[1], spawn_object[2], box.oPosX, box.oPosY, box.oPosZ, nil)
end
hook_exclamation_box(readFunction, writeFunction)
```
<br />
## [hook_mario_action](#hook_mario_action) ## [hook_mario_action](#hook_mario_action)
`hook_mario_action()` allows Lua mods to create new actions or override existing ones. `hook_mario_action()` allows Lua mods to create new actions or override existing ones.

View File

@ -131,19 +131,11 @@ void exclamation_box_spawn_contents(struct Struct802C0DF0 *a0, u8 a1) {
return; return;
} }
struct Object* luaSpawnedObject = NULL;
if ((luaSpawnedObject = smlua_call_exclamation_box_hook(o, true)) != NULL) {
luaSpawnedObject->parentObj = o; // Allows spawned stars to work like it was a normal exclamation box
(void *)smlua_call_exclamation_box_hook(luaSpawnedObject, false);
return;
}
while (a0->unk0 != 99) { while (a0->unk0 != 99) {
if (a1 == a0->unk0) { if (a1 == a0->unk0) {
s32 model = exclamation_replace_model(marioState, a0->model); s32 model = exclamation_replace_model(marioState, a0->model);
spawnedObject = spawn_object(o, model, a0->behavior); spawnedObject = spawn_object(o, model, a0->behavior);
(void *)smlua_call_exclamation_box_hook(spawnedObject, false);
if (spawnedObject != NULL) { if (spawnedObject != NULL) {
spawnedObject->oVelY = 20.0f; spawnedObject->oVelY = 20.0f;
spawnedObject->oForwardVel = 3.0f; spawnedObject->oForwardVel = 3.0f;

View File

@ -4296,7 +4296,7 @@ char gSmluaConstants[] = ""
"COOP_OBJ_FLAG_INITIALIZED = (1 << 3)\n" "COOP_OBJ_FLAG_INITIALIZED = (1 << 3)\n"
"VERSION_TEXT = 'beta'\n" "VERSION_TEXT = 'beta'\n"
"VERSION_NUMBER = 36\n" "VERSION_NUMBER = 36\n"
"MINOR_VERSION_NUMBER = 0\n" "MINOR_VERSION_NUMBER = 1\n"
"PATCH_VERSION_NUMBER = 0\n" "PATCH_VERSION_NUMBER = 0\n"
"VERSION_REGION = 'JP'\n" "VERSION_REGION = 'JP'\n"
"VERSION_REGION = 'EU'\n" "VERSION_REGION = 'EU'\n"

View File

@ -1752,110 +1752,6 @@ int smlua_hook_on_sync_table_change(lua_State* L) {
return 1; return 1;
} }
////////////////////////////
// hooked exclamation box //
////////////////////////////
struct LuaHookedExclamationBox {
int readFuncReference;
int writeFuncReference;
struct Mod* mod;
};
#define MAX_HOOKED_EXCLAMATION_BOXES 255 // Way more than needed, but better safe than sorry
static struct LuaHookedExclamationBox sHookedExclamationBoxes[MAX_HOOKED_EXCLAMATION_BOXES] = { 0 };
static int sHookedExclamationBoxesCount = 0;
// Bind to lua
int smlua_hook_exclamation_box(lua_State* L) {
if (L == NULL) { return 0; }
if (!smlua_functions_valid_param_count(L, 2)) { return 0; }
if (gLuaLoadingMod == NULL) {
LOG_LUA_LINE("hook_exclamation_box() can only be called on load.");
return 0;
}
if (sHookedExclamationBoxesCount > MAX_HOOKED_EXCLAMATION_BOXES) {
LOG_LUA_LINE("hook_exclamation_box() calls exceeded maximum references");
return 0;
}
int readReference = 0;
int readReferenceType = lua_type(L, 1);
if (readReferenceType == LUA_TNIL) {
// nothing
} else if (readReferenceType == LUA_TFUNCTION) {
// get reference
lua_pushvalue(L, 1);
readReference = luaL_ref(L, LUA_REGISTRYINDEX);
} else {
LOG_LUA_LINE("Hook exclamation box: tried to reference non-function for read function");
return 0;
}
int writeReference = 0;
int writeReferenceType = lua_type(L, 2);
if (writeReferenceType == LUA_TNIL) {
// nothing
} else if (writeReferenceType == LUA_TFUNCTION) {
// get reference
lua_pushvalue(L, 2);
writeReference = luaL_ref(L, LUA_REGISTRYINDEX);
} else {
LOG_LUA_LINE("Hook exclamation box: tried to reference non-function for write function");
return 0;
}
struct LuaHookedExclamationBox* hooked = &sHookedExclamationBoxes[sHookedExclamationBoxesCount];
hooked->readFuncReference = readReference;
hooked->writeFuncReference = writeReference;
hooked->mod = gLuaActiveMod;
if (!gSmLuaConvertSuccess) { return 0; }
sHookedExclamationBoxesCount++;
return 1;
}
// Called from the exclamation boxes
struct Object* smlua_call_exclamation_box_hook(struct Object* obj, bool write) {
lua_State* L = gLuaState;
if (L == NULL) { return NULL; }
for (int i = 0; i < sHookedExclamationBoxesCount; i++) {
struct LuaHookedExclamationBox* hook = &sHookedExclamationBoxes[i];
// Push 2 potential callbacks
int reference = write ? hook->writeFuncReference : hook->readFuncReference;
lua_rawgeti(L, LUA_REGISTRYINDEX, reference);
// push object
smlua_push_object(L, LOT_OBJECT, obj);
// call the callback
if (reference != 0 && 0 != smlua_call_hook(L, 1, 1, 0, hook->mod)) {
LOG_LUA("Failed to call the exclamation box callback: %s", (write ? "writeFunction" : "readFunction"));
continue;
}
// output the return value
struct Object* returnObject = NULL;
if (write && reference != 0) {
returnObject = (struct Object*)smlua_to_cobject(L, 1, LOT_OBJECT);
if (lua_type(L, 1) != LUA_TTABLE || !gSmLuaConvertSuccess) {
LOG_LUA("Return value type is invalid for writeFunction: %d", lua_type(L, 1));
continue;
}
}
lua_pop(L, 1);
return returnObject;
}
return NULL;
}
////////// //////////
// misc // // misc //
////////// //////////
@ -1931,5 +1827,4 @@ void smlua_bind_hooks(void) {
smlua_bind_function(L, "hook_on_sync_table_change", smlua_hook_on_sync_table_change); smlua_bind_function(L, "hook_on_sync_table_change", smlua_hook_on_sync_table_change);
smlua_bind_function(L, "hook_behavior", smlua_hook_behavior); smlua_bind_function(L, "hook_behavior", smlua_hook_behavior);
smlua_bind_function(L, "update_chat_command_description", smlua_update_chat_command_description); smlua_bind_function(L, "update_chat_command_description", smlua_update_chat_command_description);
smlua_bind_function(L, "hook_exclamation_box", smlua_hook_exclamation_box);
} }

View File

@ -133,9 +133,6 @@ bool smlua_call_event_hooks_mario_param_and_int_ret_int(enum LuaHookedEventType
bool smlua_call_event_hooks_mario_param_and_int_and_int_ret_int(enum LuaHookedEventType hookType, struct MarioState* m, s32 param, u32 args, s32* returnValue); bool smlua_call_event_hooks_mario_param_and_int_and_int_ret_int(enum LuaHookedEventType hookType, struct MarioState* m, s32 param, u32 args, s32* returnValue);
void smlua_call_event_hooks_graph_node_object_and_int_param(enum LuaHookedEventType hookType, struct GraphNodeObject* node, s32 param); void smlua_call_event_hooks_graph_node_object_and_int_param(enum LuaHookedEventType hookType, struct GraphNodeObject* node, s32 param);
int smlua_hook_exclamation_box(lua_State* L);
struct Object* smlua_call_exclamation_box_hook(struct Object* obj, bool write);
enum BehaviorId smlua_get_original_behavior_id(const BehaviorScript* behavior); enum BehaviorId smlua_get_original_behavior_id(const BehaviorScript* behavior);
const BehaviorScript* smlua_override_behavior(const BehaviorScript* behavior); const BehaviorScript* smlua_override_behavior(const BehaviorScript* behavior);
const BehaviorScript* smlua_get_hooked_behavior_from_id(enum BehaviorId id, bool returnOriginal); const BehaviorScript* smlua_get_hooked_behavior_from_id(enum BehaviorId id, bool returnOriginal);