Clear custom actors on disconnect properly

This commit is contained in:
MysterD 2022-05-30 00:37:14 -07:00
parent e719875b48
commit 668544f62d
4 changed files with 26 additions and 1 deletions

View File

@ -5,6 +5,7 @@
#include "pc/crash_handler.h" #include "pc/crash_handler.h"
#include "pc/lua/utils/smlua_text_utils.h" #include "pc/lua/utils/smlua_text_utils.h"
#include "pc/lua/utils/smlua_audio_utils.h" #include "pc/lua/utils/smlua_audio_utils.h"
#include "pc/lua/utils/smlua_model_utils.h"
#include "pc/djui/djui.h" #include "pc/djui/djui.h"
lua_State* gLuaState = NULL; lua_State* gLuaState = NULL;
@ -186,6 +187,7 @@ void smlua_shutdown(void) {
smlua_cobject_allowlist_shutdown(); smlua_cobject_allowlist_shutdown();
smlua_cpointer_allowlist_shutdown(); smlua_cpointer_allowlist_shutdown();
smlua_clear_hooks(); smlua_clear_hooks();
smlua_model_util_reset();
lua_State* L = gLuaState; lua_State* L = gLuaState;
if (L != NULL) { if (L != NULL) {
lua_close(L); lua_close(L);

View File

@ -62,6 +62,7 @@ struct ModelUtilsInfo {
bool permanent; bool permanent;
bool isDisplayList; bool isDisplayList;
const void* asset; const void* asset;
u8 shouldFreeAsset;
}; };
#define UNLOADED_ID 0xFF #define UNLOADED_ID 0xFF
@ -509,6 +510,20 @@ void smlua_model_util_remember(u8 loadedId, UNUSED u8 layer, const void* asset,
//LOG_INFO("Remember model: %u -> %u", found->extId, loadedId); //LOG_INFO("Remember model: %u -> %u", found->extId, loadedId);
} }
void smlua_model_util_reset(void) {
smlua_model_util_clear();
for (u32 i = 0; i < sCustomModelsCount; i++) {
struct ModelUtilsInfo* m = &sCustomModels[i];
m->loadedId = UNLOADED_ID;
if (m->asset && m->shouldFreeAsset) {
free((void*)m->asset);
m->asset = NULL;
}
m->shouldFreeAsset = false;
}
sCustomModelsCount = 0;
}
void smlua_model_util_clear(void) { void smlua_model_util_clear(void) {
// TODO: we need to restore replaced permanent models // TODO: we need to restore replaced permanent models
for (int i = 0; i < MAX_CACHED_ASSETS; i++) { for (int i = 0; i < MAX_CACHED_ASSETS; i++) {
@ -516,6 +531,11 @@ void smlua_model_util_clear(void) {
if (m == NULL || m->permanent) { continue; } if (m == NULL || m->permanent) { continue; }
//LOG_INFO("Forget: %u -> %u", m->extId, m->loadedId); //LOG_INFO("Forget: %u -> %u", m->extId, m->loadedId);
m->loadedId = UNLOADED_ID; m->loadedId = UNLOADED_ID;
if (m->asset && m->shouldFreeAsset) {
free((void*)m->asset);
m->asset = NULL;
}
m->shouldFreeAsset = false;
sCachedAssets[i] = NULL; sCachedAssets[i] = NULL;
sCachedAssetTaken[i] = false; sCachedAssetTaken[i] = false;
} }
@ -566,6 +586,7 @@ u8 smlua_model_util_load_with_pool_and_cache_id(enum ModelExtendedId extId, stru
pool = alloc_only_pool_init(main_pool_available() - sizeof(struct AllocOnlyPool), MEMORY_POOL_LEFT); pool = alloc_only_pool_init(main_pool_available() - sizeof(struct AllocOnlyPool), MEMORY_POOL_LEFT);
resizePool = true; resizePool = true;
} }
info->shouldFreeAsset = false;
if (pool != NULL) { if (pool != NULL) {
if (info->isDisplayList) { if (info->isDisplayList) {
@ -586,6 +607,7 @@ u8 smlua_model_util_load_with_pool_and_cache_id(enum ModelExtendedId extId, stru
if (info->isDisplayList) { if (info->isDisplayList) {
const GeoLayout displayListToGeoLayout[] = { GEO_NODE_START(), GEO_DISPLAY_LIST(info->layer, info->asset), GEO_END() }; const GeoLayout displayListToGeoLayout[] = { GEO_NODE_START(), GEO_DISPLAY_LIST(info->layer, info->asset), GEO_END() };
info->asset = memcpy(calloc(1, sizeof(displayListToGeoLayout)), displayListToGeoLayout, sizeof(displayListToGeoLayout)); info->asset = memcpy(calloc(1, sizeof(displayListToGeoLayout)), displayListToGeoLayout, sizeof(displayListToGeoLayout));
info->shouldFreeAsset = true;
info->isDisplayList = false; info->isDisplayList = false;
} }
gLoadedGraphNodes[pickLoadedId] = dynos_geolayout_to_graphnode(info->asset, true); gLoadedGraphNodes[pickLoadedId] = dynos_geolayout_to_graphnode(info->asset, true);
@ -634,6 +656,7 @@ u32 smlua_model_util_get_id(const char* name) {
u16 customIndex = sCustomModelsCount++; u16 customIndex = sCustomModelsCount++;
struct ModelUtilsInfo* info = &sCustomModels[customIndex]; struct ModelUtilsInfo* info = &sCustomModels[customIndex];
info->asset = layout; info->asset = layout;
info->shouldFreeAsset = false;
info->loadedId = UNLOADED_ID; info->loadedId = UNLOADED_ID;
info->extId = E_MODEL_MAX + customIndex; info->extId = E_MODEL_MAX + customIndex;
info->isDisplayList = false; info->isDisplayList = false;

View File

@ -391,6 +391,7 @@ enum ModelExtendedId {
}; };
void smlua_model_util_remember(u8 loadedId, u8 layer, const void* asset, u8 isDisplayList); void smlua_model_util_remember(u8 loadedId, u8 layer, const void* asset, u8 isDisplayList);
void smlua_model_util_reset(void);
void smlua_model_util_clear(void); void smlua_model_util_clear(void);
u8 smlua_model_util_load_with_pool_and_cache_id(enum ModelExtendedId extId, struct AllocOnlyPool* pool, u8 loadedId); u8 smlua_model_util_load_with_pool_and_cache_id(enum ModelExtendedId extId, struct AllocOnlyPool* pool, u8 loadedId);
u8 smlua_model_util_load_with_pool(enum ModelExtendedId extId, struct AllocOnlyPool* pool); u8 smlua_model_util_load_with_pool(enum ModelExtendedId extId, struct AllocOnlyPool* pool);

View File

@ -497,7 +497,6 @@ void network_shutdown(bool sendLeaving, bool exiting) {
mods_clear(&gActiveMods); mods_clear(&gActiveMods);
mods_clear(&gRemoteMods); mods_clear(&gRemoteMods);
smlua_shutdown(); smlua_shutdown();
smlua_model_util_clear();
extern s16 gChangeLevel; extern s16 gChangeLevel;
gChangeLevel = LEVEL_CASTLE_GROUNDS; gChangeLevel = LEVEL_CASTLE_GROUNDS;
network_player_init(); network_player_init();