From cfbd0b10250e20bc5807f64209a730c77936490a Mon Sep 17 00:00:00 2001 From: Prince Frizzy Date: Wed, 12 Feb 2025 21:58:29 -0500 Subject: [PATCH] Add Dynamic Custom Model Allocation (#642) --- src/pc/lua/smlua.c | 1 + src/pc/lua/utils/smlua_model_utils.c | 27 ++++++++++++++++++++------- src/pc/lua/utils/smlua_model_utils.h | 1 + 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/pc/lua/smlua.c b/src/pc/lua/smlua.c index 69e5adaca..4e2a50041 100644 --- a/src/pc/lua/smlua.c +++ b/src/pc/lua/smlua.c @@ -309,6 +309,7 @@ void smlua_init(void) { smlua_exec_str(gSmluaConstants); smlua_cobject_init_globals(); + smlua_model_util_initialize(); // load scripts mods_size_enforce(&gActiveMods); diff --git a/src/pc/lua/utils/smlua_model_utils.c b/src/pc/lua/utils/smlua_model_utils.c index 830376525..e45cc7938 100644 --- a/src/pc/lua/utils/smlua_model_utils.c +++ b/src/pc/lua/utils/smlua_model_utils.c @@ -462,12 +462,21 @@ struct ModelUtilsInfo sModels[E_MODEL_MAX] = { MODEL_UTIL_GEO_PERM(E_MODEL_WARIOS_WINGED_METAL_CAP, warios_winged_metal_cap_geo, MODEL_WARIOS_WINGED_METAL_CAP), }; -#define MAX_CUSTOM_MODELS 1024 +#define CUSTOM_MODEL_CHUNK_SIZE 256 static u16 sCustomModelsCount = 0; -struct ModelUtilsInfo sCustomModels[MAX_CUSTOM_MODELS] = { 0 }; +static u16 sMaxCustomModelsCount = CUSTOM_MODEL_CHUNK_SIZE; +struct ModelUtilsInfo *sCustomModels = NULL; + +void smlua_model_util_initialize(void) { + // Allocate the custom models array. We start off with a maximum of 256 custom models. + sCustomModels = (struct ModelUtilsInfo *)calloc(sMaxCustomModelsCount, sizeof(struct ModelUtilsInfo)); +} void smlua_model_util_clear(void) { sCustomModelsCount = 0; + sMaxCustomModelsCount = CUSTOM_MODEL_CHUNK_SIZE; + if (sCustomModels) { free(sCustomModels); } + sCustomModels = NULL; } void smlua_model_util_store_in_slot(u32 slot, const char* name) { @@ -494,14 +503,14 @@ u16 smlua_model_util_load(enum ModelExtendedId extId) { } enum ModelExtendedId smlua_model_util_get_id(const char* name) { - // find geolayout + // Find geolayout const void* asset = dynos_geolayout_get(name); if (asset == NULL) { LOG_LUA_LINE("Could not find model: '%s'", name); return E_MODEL_ERROR_MODEL; } - // find existing built-in model + // Find existing built-in model for (u32 i = 0; i < E_MODEL_MAX; i++) { struct ModelUtilsInfo* m = &sModels[i]; if (m->asset == asset) { @@ -509,7 +518,7 @@ enum ModelExtendedId smlua_model_util_get_id(const char* name) { } } - // find existing custom model + // Find existing custom model for (u32 i = 0; i < sCustomModelsCount; i++) { struct ModelUtilsInfo* m = &sCustomModels[i]; if (m->asset == asset) { @@ -517,12 +526,16 @@ enum ModelExtendedId smlua_model_util_get_id(const char* name) { } } - if (sCustomModelsCount >= MAX_CUSTOM_MODELS) { + // If we've extended past our current custom model limit. Reallocate so we have more space. + if (sCustomModelsCount >= sMaxCustomModelsCount && sMaxCustomModelsCount + CUSTOM_MODEL_CHUNK_SIZE < 65535) { + sMaxCustomModelsCount += CUSTOM_MODEL_CHUNK_SIZE; + sCustomModels = (struct ModelUtilsInfo *)realloc(sCustomModels, sMaxCustomModelsCount * sizeof(struct ModelUtilsInfo)); + } else { LOG_LUA("Failed to get model: '%s' (too many custom models!)", name); return E_MODEL_ERROR_MODEL; } - // allocate custom model + // Allocate custom model u16 customIndex = sCustomModelsCount++; struct ModelUtilsInfo* info = &sCustomModels[customIndex]; info->asset = asset; diff --git a/src/pc/lua/utils/smlua_model_utils.h b/src/pc/lua/utils/smlua_model_utils.h index 2e78b1219..13e06f68a 100644 --- a/src/pc/lua/utils/smlua_model_utils.h +++ b/src/pc/lua/utils/smlua_model_utils.h @@ -395,6 +395,7 @@ enum ModelExtendedId { E_MODEL_MAX }; +void smlua_model_util_initialize(void); void smlua_model_util_clear(void); void smlua_model_util_store_in_slot(u32 slot, const char* name); u16 smlua_model_util_load(enum ModelExtendedId extId);