Fix Mod Menu

This commit is contained in:
Agent X 2024-06-05 15:53:53 -04:00
parent 695b1cc84e
commit deb2c00dba
4 changed files with 34 additions and 23 deletions

View File

@ -2,8 +2,7 @@
#include "djui_panel.h"
#include "djui_panel_menu.h"
#include "pc/lua/smlua_hooks.h"
static char* sDjuiPanelModMenuModName = NULL;
#include "pc/mods/mods.h"
static char* to_uppercase(char* str) {
char* buffer = strdup(str);
@ -82,13 +81,23 @@ static void djui_panel_mod_menu_mod_create_element(struct DjuiBase* parent, int
}
void djui_panel_mod_menu_mod_create(struct DjuiBase* caller) {
struct DjuiThreePanel* panel = djui_panel_menu_create(to_uppercase(sDjuiPanelModMenuModName));
struct Mod* mod = NULL;
for (int i = 0; i < gActiveMods.entryCount; i++) {
if (gActiveMods.entries[i]->index == caller->tag) {
mod = gActiveMods.entries[i];
}
}
if (mod == NULL) { return; }
struct DjuiThreePanel* panel = djui_panel_menu_create(to_uppercase(mod->name));
struct DjuiBase* body = djui_three_panel_get_body(panel);
{
struct DjuiPaginated* paginated = djui_paginated_create(body, 8);
struct DjuiBase* layoutBase = &paginated->layout->base;
for (int i = 0; i < gHookedModMenuElementsCount; i++) {
djui_panel_mod_menu_mod_create_element(layoutBase, i);
if (gHookedModMenuElements[i].mod == mod) {
djui_panel_mod_menu_mod_create_element(layoutBase, i);
}
}
djui_paginated_calculate_height(paginated);
@ -104,13 +113,24 @@ void djui_panel_mod_menu_create(struct DjuiBase* caller) {
{
struct DjuiPaginated* paginated = djui_paginated_create(body, 8);
struct DjuiBase* layoutBase = &paginated->layout->base;
struct Mod* lastMod = NULL;
struct Mod* addedMods[MAX_HOOKED_MOD_MENU_ELEMENTS] = { 0 };
int modCount = 0;
for (int i = 0; i < gHookedModMenuElementsCount; i++) {
struct LuaHookedModMenuElement* hooked = &gHookedModMenuElements[i];
if (lastMod == hooked->mod) { continue; }
lastMod = hooked->mod;
sDjuiPanelModMenuModName = lastMod->name;
djui_button_create(layoutBase, hooked->mod->name, DJUI_BUTTON_STYLE_NORMAL, djui_panel_mod_menu_mod_create);
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; }
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++;
}
djui_paginated_calculate_height(paginated);

View File

@ -74,11 +74,7 @@ void djui_panel_pause_create(struct DjuiBase* caller) {
if (gHookedModMenuElementsCount == 1 && gHookedModMenuElements[0].element == MOD_MENU_ELEMENT_BUTTON) {
struct LuaHookedModMenuElement* hooked = &gHookedModMenuElements[0];
char buffer[256] = { 0 };
if (strlen(hooked->name) > 0) {
snprintf(buffer, 256, "%s - %s", hooked->mod->name, hooked->name);
} else {
snprintf(buffer, 256, "%s", hooked->mod->name);
}
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 if (gHookedModMenuElementsCount > 0) {

View File

@ -1989,8 +1989,6 @@ int smlua_hook_on_sync_table_change(lua_State* L) {
// hooked mod menu button //
////////////////////////////
#define MAX_HOOKED_MOD_MENU_ELEMENTS 256
struct LuaHookedModMenuElement gHookedModMenuElements[MAX_HOOKED_MOD_MENU_ELEMENTS] = { 0 };
int gHookedModMenuElementsCount = 0;
@ -2009,7 +2007,7 @@ int smlua_hook_mod_menu_button(lua_State* L) {
}
const char* name = smlua_to_string(L, 1);
if (name == NULL || !gSmLuaConvertSuccess) {
if (name == NULL || strlen(name) == 0 || !gSmLuaConvertSuccess) {
LOG_LUA_LINE("Hook mod menu element: tried to hook invalid element");
return 0;
}
@ -2210,12 +2208,7 @@ int smlua_update_mod_menu_element_name(lua_State* L) {
}
const char* name = smlua_to_string(L, 2);
if (name == NULL || !gSmLuaConvertSuccess) {
LOG_LUA_LINE("Update mod menu element: tried to update invalid name");
return 0;
}
if (gHookedModMenuElements[index].element != MOD_MENU_ELEMENT_BUTTON && strlen(name) == 0) {
if (name == NULL || strlen(name) == 0 || !gSmLuaConvertSuccess) {
LOG_LUA_LINE("Update mod menu element: tried to update invalid name");
return 0;
}

View File

@ -120,6 +120,8 @@ static const char* LuaActionHookTypeArgName[] = {
"max (dummy)",
};
#define MAX_HOOKED_MOD_MENU_ELEMENTS 256
enum LuaModMenuElementType {
MOD_MENU_ELEMENT_BUTTON,
MOD_MENU_ELEMENT_CHECKBOX,