From 4e63798f00e8060fdeadd5d3a6e003d55ec6c6d1 Mon Sep 17 00:00:00 2001 From: Agent X <44549182+Agent-11@users.noreply.github.com> Date: Thu, 7 Dec 2023 18:05:37 -0500 Subject: [PATCH] Rewrite Lua autoexec Now put autoexec.lua in one of the mods folders, it now has all of the capabilities of a full mod without the crashes and weird bugs, which is pretty awesome. --- src/pc/djui/djui_panel_host_mods.c | 3 +++ src/pc/djui/djui_panel_modlist.c | 9 ++++++--- src/pc/lua/smlua.c | 7 ------- src/pc/mods/mod.c | 5 +++++ src/pc/mods/mod.h | 1 + src/pc/mods/mods.c | 25 +++++++++++++++++++++--- src/pc/mods/mods.h | 1 + src/pc/network/coopnet/coopnet.c | 6 ++++-- src/pc/network/packets/packet_mod_list.c | 3 ++- src/pc/pc_main.c | 14 +++++++++++++ 10 files changed, 58 insertions(+), 16 deletions(-) diff --git a/src/pc/djui/djui_panel_host_mods.c b/src/pc/djui/djui_panel_host_mods.c index 286350956..7ec047670 100644 --- a/src/pc/djui/djui_panel_host_mods.c +++ b/src/pc/djui/djui_panel_host_mods.c @@ -138,6 +138,9 @@ void djui_panel_host_mods_create(struct DjuiBase* caller) { struct DjuiBase* layoutBase = &paginated->layout->base; for (int i = 0; i < gLocalMods.entryCount; i++) { struct Mod* mod = gLocalMods.entries[i]; + if (mod_get_is_autoexec(mod)) { + continue; + } if (isRomHacks != (mod->incompatible && strstr(mod->incompatible, "romhack"))) { continue; } diff --git a/src/pc/djui/djui_panel_modlist.c b/src/pc/djui/djui_panel_modlist.c index ae83edb48..b7e21230e 100644 --- a/src/pc/djui/djui_panel_modlist.c +++ b/src/pc/djui/djui_panel_modlist.c @@ -18,10 +18,12 @@ void djui_panel_modlist_create(UNUSED struct DjuiBase* caller) { gDjuiModList = NULL; } - // only create if we have mods - if (gActiveMods.entryCount == 0) { return; } + u8 autoexecMod = mods_has_autoexec_mod(); - f32 bodyHeight = (gActiveMods.entryCount * 32) + (gActiveMods.entryCount - 1) * 4; + // only create if we have mods + if (gActiveMods.entryCount - autoexecMod == 0) { return; } + + f32 bodyHeight = (gActiveMods.entryCount * 32) + (gActiveMods.entryCount - 1 - autoexecMod) * 4; struct DjuiThreePanel* panel = djui_panel_menu_create(DLANG(MODLIST, MODS)); djui_three_panel_set_body_size(panel, bodyHeight); gDjuiModList = panel; @@ -40,6 +42,7 @@ void djui_panel_modlist_create(UNUSED struct DjuiBase* caller) { for (int i = 0; i < gActiveMods.entryCount; i++) { struct Mod* mod = gActiveMods.entries[i]; + if (mod_get_is_autoexec(mod)) { continue; } struct DjuiFlowLayout* row = djui_flow_layout_create(body); djui_base_set_size_type(&row->base, DJUI_SVT_RELATIVE, DJUI_SVT_ABSOLUTE); diff --git a/src/pc/lua/smlua.c b/src/pc/lua/smlua.c index 7128d9956..8216c6477 100644 --- a/src/pc/lua/smlua.c +++ b/src/pc/lua/smlua.c @@ -288,13 +288,6 @@ void smlua_init(void) { gLuaActiveMod = NULL; gLuaLoadingMod = NULL; } - -#ifdef DEVELOPMENT - // autoexec - if (path_exists("autoexec.lua")) { - smlua_exec_file("autoexec.lua"); - } -#endif } void smlua_update(void) { diff --git a/src/pc/mods/mod.c b/src/pc/mods/mod.c index 652ada812..5e5d5cecb 100644 --- a/src/pc/mods/mod.c +++ b/src/pc/mods/mod.c @@ -20,6 +20,11 @@ size_t mod_get_lua_size(struct Mod* mod) { return size; } +bool mod_get_is_autoexec(struct Mod* mod) { + if (!strcmp(mod->name, "autoexec") || !strcmp(mod->name, "autoexec.lua")) { return true; } + return false; +} + static void mod_activate_bin(struct ModFile* file) { // copy geo name char geoName[64] = { 0 }; diff --git a/src/pc/mods/mod.h b/src/pc/mods/mod.h index bec06b480..fc51a0eb1 100644 --- a/src/pc/mods/mod.h +++ b/src/pc/mods/mod.h @@ -40,6 +40,7 @@ struct Mod { }; size_t mod_get_lua_size(struct Mod* mod); +bool mod_get_is_autoexec(struct Mod* mod); void mod_activate(struct Mod* mod); void mod_clear(struct Mod* mod); bool mod_load(struct Mods* mods, char* basePath, char* modName); diff --git a/src/pc/mods/mods.c b/src/pc/mods/mods.c index a9eb20554..12463afac 100644 --- a/src/pc/mods/mods.c +++ b/src/pc/mods/mods.c @@ -49,6 +49,13 @@ u16 mods_get_enabled_count(void) { return enabled; } +u8 mods_has_autoexec_mod(void) { + for (u16 i = 0; i < gLocalMods.entryCount; i++) { + if (mod_get_is_autoexec(gLocalMods.entries[i])) { return TRUE; } + } + return FALSE; +} + static void mods_local_store_enabled(void) { assert(sLocalEnabledPaths == NULL); struct LocalEnabledPath* prev = NULL; @@ -106,6 +113,14 @@ bool mods_generate_remote_base_path(void) { return true; } +static struct Mod* get_autoexec_mod(void) { + for (u16 i = 0; i < gLocalMods.entryCount; i++) { + if (mod_get_is_autoexec(gLocalMods.entries[i])) { + return gLocalMods.entries[i]; + } + } +} + void mods_activate(struct Mods* mods) { mods_clear(&gActiveMods); @@ -116,8 +131,11 @@ void mods_activate(struct Mods* mods) { if (mod->enabled) { enabledCount++; } } + // is joining a game and has an autoexec mod + bool autoexec = mods == &gRemoteMods && mods_has_autoexec_mod(); + // allocate - gActiveMods.entries = calloc(enabledCount, sizeof(struct Mod*)); + gActiveMods.entries = calloc(enabledCount + autoexec, sizeof(struct Mod*)); if (gActiveMods.entries == NULL) { LOG_ERROR("Failed to allocate active mods table!"); return; @@ -126,8 +144,9 @@ void mods_activate(struct Mods* mods) { // copy enabled entries gActiveMods.entryCount = 0; gActiveMods.size = 0; - for (int i = 0; i < mods->entryCount; i++) { - struct Mod* mod = mods->entries[i]; + for (int i = 0; i < mods->entryCount + autoexec; i++) { + // checks if the mod is out of the remote mods bounds and if so, use the autoexec mod + struct Mod* mod = i == mods->entryCount ? get_autoexec_mod() : mods->entries[i]; if (mod->enabled) { mod->index = gActiveMods.entryCount; gActiveMods.entries[gActiveMods.entryCount++] = mod; diff --git a/src/pc/mods/mods.h b/src/pc/mods/mods.h index a9c557f29..3e2dbdad5 100644 --- a/src/pc/mods/mods.h +++ b/src/pc/mods/mods.h @@ -24,6 +24,7 @@ extern char gRemoteModsBasePath[]; void mods_get_main_mod_name(char* destination, u32 maxSize); u16 mods_get_enabled_count(void); +u8 mods_has_autoexec_mod(void); bool mods_generate_remote_base_path(void); void mods_activate(struct Mods* mods); void mods_clear(struct Mods* mods); diff --git a/src/pc/network/coopnet/coopnet.c b/src/pc/network/coopnet/coopnet.c index c2600727f..a6dd6b92b 100644 --- a/src/pc/network/coopnet/coopnet.c +++ b/src/pc/network/coopnet/coopnet.c @@ -181,9 +181,11 @@ static void coopnet_populate_description(void) { // get mod strings if (gActiveMods.entryCount <= 0) { return; } - char* strings[gActiveMods.entryCount]; + char* strings[gActiveMods.entryCount - mods_has_autoexec_mod()]; for (int i = 0; i < gActiveMods.entryCount; i++) { - strings[i] = gActiveMods.entries[i]->name; + struct Mod* mod = gActiveMods.entries[i]; + if (mod_get_is_autoexec(mod)) { continue; } + strings[i] = mod->name; } // add seperator diff --git a/src/pc/network/packets/packet_mod_list.c b/src/pc/network/packets/packet_mod_list.c index 83cf80b52..f77cd8096 100644 --- a/src/pc/network/packets/packet_mod_list.c +++ b/src/pc/network/packets/packet_mod_list.c @@ -51,12 +51,13 @@ void network_send_mod_list(void) { snprintf(version, MAX_VERSION_LENGTH, "%s", get_version()); LOG_INFO("sending version: %s", version); packet_write(&p, &version, sizeof(u8) * MAX_VERSION_LENGTH); - packet_write(&p, &gActiveMods.entryCount, sizeof(u16)); + packet_write(&p, &gActiveMods.entryCount - mods_has_autoexec_mod(), sizeof(u16)); network_send_to(0, &p); LOG_INFO("sent mod list (%u):", gActiveMods.entryCount); for (u16 i = 0; i < gActiveMods.entryCount; i++) { struct Mod* mod = gActiveMods.entries[i]; + if (mod_get_is_autoexec(mod)) { continue; } u16 nameLength = strlen(mod->name); if (nameLength > MOD_NAME_MAX_LENGTH) { nameLength = MOD_NAME_MAX_LENGTH; } diff --git a/src/pc/pc_main.c b/src/pc/pc_main.c index e0f8519e3..7a71ac9d0 100644 --- a/src/pc/pc_main.c +++ b/src/pc/pc_main.c @@ -264,6 +264,19 @@ void game_exit(void) { exit(0); } +void enable_autoexec_mod(void) { + for (int i = 0; i < gLocalMods.entryCount; i ++) { + struct Mod* mod = gLocalMods.entries[i]; + if (mod_get_is_autoexec(mod)) { +#ifdef DEVELOPMENT + mod->enabled = true; +#else + mod->enabled = false; +#endif + } + } +} + void* main_game_init(UNUSED void* arg) { const char *gamedir = gCLIOpts.GameDir[0] ? gCLIOpts.GameDir : FS_BASEDIR; const char *userpath = gCLIOpts.SavePath[0] ? gCLIOpts.SavePath : sys_user_path(); @@ -281,6 +294,7 @@ void* main_game_init(UNUSED void* arg) { mods_init(); enable_queued_mods(); + enable_autoexec_mod(); if (gIsThreaded) { REFRESH_MUTEX( gCurrLoadingSegment.percentage = 0;