Improve cheats and mod menu

This commit is contained in:
Agent X 2024-06-29 11:14:15 -04:00
parent d81f8846e5
commit 614dbaf7ee
4 changed files with 48 additions and 28 deletions

View file

@ -13,6 +13,8 @@ local math_floor,smlua_text_utils_get_language,table_insert,approach_s32,set_mar
--- @field public func function
--- @field public allowHazardSurfaces boolean
local CHEATS_VERSION = "v1.0"
--- @type Cheat[]
local sCheats = {}
@ -56,7 +58,7 @@ local function lang_string(strings)
end
--- @param codename string
--- @param names table
--- @param names table<string, string>
--- @param hook LuaHookedEventType
--- @param func function
--- @param allowHazardSurfaces boolean
@ -134,7 +136,7 @@ end
--- @param m MarioState
local function rapid_fire_update(m)
if (m.controller.buttonDown & A_BUTTON) ~= 0 and get_network_area_timer() % 2 == 0 then
if (m.controller.buttonDown & A_BUTTON) ~= 0 and get_global_timer() % 2 == 0 then
m.controller.buttonPressed = m.controller.buttonPressed | A_BUTTON
end
end
@ -354,6 +356,23 @@ local function update_cheat(index, value)
end
end
local sReadonlyMetatable = {
__index = function(table, key)
return rawget(table, key)
end,
__newindex = function()
error("attempt to update a read-only table", 2)
end
}
_G.cheatsApi = {
version = CHEATS_VERSION,
register_cheat = register_cheat
}
setmetatable(_G.cheatsApi, sReadonlyMetatable)
hook_event(HOOK_MARIO_UPDATE, generate_mario_hook_function(HOOK_MARIO_UPDATE))
hook_event(HOOK_BEFORE_MARIO_UPDATE, generate_mario_hook_function(HOOK_BEFORE_MARIO_UPDATE))
hook_event(HOOK_BEFORE_PHYS_STEP, generate_mario_hook_function(HOOK_BEFORE_PHYS_STEP))

View file

@ -135,8 +135,7 @@ void djui_panel_mod_menu_create(struct DjuiBase* caller) {
struct DjuiButton* button = djui_button_create(layoutBase, hooked->mod->name, DJUI_BUTTON_STYLE_NORMAL, djui_panel_mod_menu_mod_create);
button->base.tag = hooked->mod->index;
addedMods[modCount] = hooked->mod;
modCount++;
addedMods[modCount++] = hooked->mod;
}
djui_paginated_calculate_height(paginated);

View file

@ -71,12 +71,34 @@ void djui_panel_pause_create(struct DjuiBase* caller) {
djui_button_create(body, DLANG(PAUSE, SERVER_SETTINGS), DJUI_BUTTON_STYLE_NORMAL, djui_panel_host_create);
}
if (gHookedModMenuElementsCount == 1 && gHookedModMenuElements[0].element == MOD_MENU_ELEMENT_BUTTON) {
struct Mod* addedMods[MAX_HOOKED_MOD_MENU_ELEMENTS] = { 0 };
int modCount = 0;
for (int i = 0; i < gHookedModMenuElementsCount; i++) {
struct LuaHookedModMenuElement* hooked = &gHookedModMenuElements[i];
bool shouldContinue = false;
for (int i = 0; i < MAX_HOOKED_MOD_MENU_ELEMENTS; i++) {
if (addedMods[i] == NULL) { break; }
if (addedMods[i] == hooked->mod) {
shouldContinue = true;
break;
}
}
if (shouldContinue) { continue; }
addedMods[modCount++] = hooked->mod;
}
if (modCount == 1) {
struct LuaHookedModMenuElement* hooked = &gHookedModMenuElements[0];
char buffer[256] = { 0 };
snprintf(buffer, 256, "%s - %s", hooked->mod->name, hooked->name);
struct DjuiButton* button = djui_button_create(body, buffer, DJUI_BUTTON_STYLE_NORMAL, djui_panel_mod_menu_mod_button);
button->base.tag = 0;
if (gHookedModMenuElementsCount == 1 && gHookedModMenuElements[0].element == MOD_MENU_ELEMENT_BUTTON) {
snprintf(buffer, 256, "%s - %s", hooked->mod->name, hooked->name);
struct DjuiButton* button = djui_button_create(body, buffer, DJUI_BUTTON_STYLE_NORMAL, djui_panel_mod_menu_mod_button);
button->base.tag = 0;
} else {
snprintf(buffer, 256, "%s", hooked->mod->name);
struct DjuiButton* button = djui_button_create(body, buffer, DJUI_BUTTON_STYLE_NORMAL, djui_panel_mod_menu_mod_create);
button->base.tag = hooked->mod->index;
}
} else if (gHookedModMenuElementsCount > 0) {
djui_button_create(body, DLANG(PAUSE, MOD_MENU), DJUI_BUTTON_STYLE_NORMAL, djui_panel_mod_menu_create);
}

View file

@ -2010,11 +2010,6 @@ int smlua_hook_mod_menu_button(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_mod_menu_button() can only be called on load.");
return 0;
}
if (gHookedModMenuElementsCount >= MAX_HOOKED_MOD_MENU_ELEMENTS) {
LOG_LUA_LINE("Hooked mod menu element exceeded maximum references!");
return 0;
@ -2052,11 +2047,6 @@ int smlua_hook_mod_menu_checkbox(lua_State* L) {
if (L == NULL) { return 0; }
if (!smlua_functions_valid_param_count(L, 3)) { return 0; }
if (gLuaLoadingMod == NULL) {
LOG_LUA_LINE("hook_mod_menu_checkbox() can only be called on load.");
return 0;
}
if (gHookedModMenuElementsCount >= MAX_HOOKED_MOD_MENU_ELEMENTS) {
LOG_LUA_LINE("Hooked mod menu element exceeded maximum references!");
return 0;
@ -2100,11 +2090,6 @@ int smlua_hook_mod_menu_slider(lua_State* L) {
if (L == NULL) { return 0; }
if (!smlua_functions_valid_param_count(L, 5)) { return 0; }
if (gLuaLoadingMod == NULL) {
LOG_LUA_LINE("hook_mod_menu_slider() can only be called on load.");
return 0;
}
if (gHookedModMenuElementsCount >= MAX_HOOKED_MOD_MENU_ELEMENTS) {
LOG_LUA_LINE("Hooked mod menu element exceeded maximum references!");
return 0;
@ -2160,11 +2145,6 @@ int smlua_hook_mod_menu_inputbox(lua_State* L) {
if (L == NULL) { return 0; }
if (!smlua_functions_valid_param_count(L, 4)) { return 0; }
if (gLuaLoadingMod == NULL) {
LOG_LUA_LINE("hook_mod_menu_inputbox() can only be called on load.");
return 0;
}
if (gHookedModMenuElementsCount >= MAX_HOOKED_MOD_MENU_ELEMENTS) {
LOG_LUA_LINE("Hooked mod menu element exceeded maximum references!");
return 0;