From d75b0e9133db5e5e6139c66861f433f0202208d1 Mon Sep 17 00:00:00 2001 From: MysterD Date: Mon, 9 May 2022 07:27:47 -0700 Subject: [PATCH] Started working toward allowing disconnection --- data/dynos.c.h | 3 +++ data/dynos.cpp.h | 9 ++++++++ data/dynos_c.cpp | 5 +++++ data/dynos_level.cpp | 11 ++++++++++ data/dynos_main.cpp | 12 ++++++++++ data/dynos_mgr_actor.cpp | 28 ++++++++++++++++++++++-- data/dynos_mgr_col.cpp | 10 +++++++++ data/dynos_mgr_lvl.cpp | 16 ++++++++++++++ data/dynos_mgr_movtexqc.cpp | 9 ++++++++ data/dynos_mgr_tex.cpp | 9 ++++++++ src/pc/djui/djui_panel_join_message.c | 2 +- src/pc/lua/smlua.c | 1 + src/pc/network/network.c | 21 +++++++++++++++++- src/pc/network/network.h | 2 +- src/pc/network/network_player.c | 4 ++-- src/pc/network/packets/packet_join.c | 4 ++-- src/pc/network/packets/packet_kick.c | 2 +- src/pc/network/packets/packet_mod_list.c | 4 ++-- src/pc/pc_main.c | 2 +- 19 files changed, 141 insertions(+), 13 deletions(-) diff --git a/data/dynos.c.h b/data/dynos.c.h index 682fc668..98b47353 100644 --- a/data/dynos.c.h +++ b/data/dynos.c.h @@ -53,5 +53,8 @@ const char* dynos_level_get_token(u32 index); Trajectory* dynos_level_get_trajectory(const char* name); void dynos_level_load_background(void *ptr); +// -- other -- // +void dynos_mod_shutdown(void); + #endif #endif diff --git a/data/dynos.cpp.h b/data/dynos.cpp.h index d0cec21a..497ee3c7 100644 --- a/data/dynos.cpp.h +++ b/data/dynos.cpp.h @@ -12,6 +12,8 @@ extern "C" { #define POINTER_CODE (u32) 0x52544E50 #define LUA_VAR_CODE (u32) 0x5641554C +#define MOD_PACK_INDEX 99 + // // Enums // @@ -644,6 +646,7 @@ void *DynOS_SwapCmd(void *aCmd); void *DynOS_UpdateCmd(void *aCmd); void DynOS_UpdateGfx(); bool DynOS_IsTransitionActive(); +void DynOS_Mod_Shutdown(); void DynOS_ReturnToMainMenu(); // @@ -695,6 +698,7 @@ s32 DynOS_Level_GetCount(); const s32 *DynOS_Level_GetList(); s32 DynOS_Level_GetCourse(s32 aLevel); void DynOS_Level_Override(void* originalScript, void* newScript); +void DynOS_Level_Unoverride(); const void *DynOS_Level_GetScript(s32 aLevel); const u8 *DynOS_Level_GetName(s32 aLevel, bool aDecaps, bool aAddCourseNumber); const u8 *DynOS_Level_GetActName(s32 aLevel, s32 aAct, bool aDecaps, bool aAddStarNumber); @@ -764,6 +768,7 @@ void DynOS_Actor_Valid(const void* aGeoref, ActorGfx& aActorGfx); void DynOS_Actor_Invalid(const void* aGeoref, s32 aPackIndex); void DynOS_Actor_Override(void** aSharedChild); void DynOS_Actor_Override_All(void); +void DynOS_Actor_ModShutdown(); // // Anim Manager @@ -784,6 +789,7 @@ void DynOS_Tex_Activate(DataNode* aNode, bool aCustomTexture); void DynOS_Tex_Deactivate(DataNode* aNode); void DynOS_Tex_AddCustom(const SysPath &aFilename, const char *aTexName); bool DynOS_Tex_Get(const char* aTexName, struct TextureInfo* aOutTexInfo); +void DynOS_Tex_ModShutdown(); // // Lvl Manager @@ -797,6 +803,7 @@ DataNode* DynOS_Lvl_GetMovtexQuadCollection(s32 index); Trajectory* DynOS_Lvl_GetTrajectory(const char* aName); void DynOS_Lvl_LoadBackground(void *aPtr); void* DynOS_Lvl_Override(void *aCmd); +void DynOS_Lvl_ModShutdown(); // // Col Manager @@ -804,6 +811,7 @@ void* DynOS_Lvl_Override(void *aCmd); void DynOS_Col_Activate(const SysPath &aFilePath, const char *aCollisionName); Collision* DynOS_Col_Get(const char* collisionName); +void DynOS_Col_ModShutdown(); // // Movtexqc Manager @@ -812,6 +820,7 @@ Collision* DynOS_Col_Get(const char* collisionName); void DynOS_MovtexQC_Register(const char* name, s16 level, s16 area, s16 type); DataNode* DynOS_MovtexQC_GetFromId(u32 id); DataNode* DynOS_MovtexQC_GetFromIndex(s32 index); +void DynOS_MovtexQC_ModShutdown(); // // Bin diff --git a/data/dynos_c.cpp b/data/dynos_c.cpp index 2ee95abd..42196c13 100644 --- a/data/dynos_c.cpp +++ b/data/dynos_c.cpp @@ -156,4 +156,9 @@ void dynos_level_load_background(void *ptr) { DynOS_Lvl_LoadBackground(ptr); } +// -- other -- // +void dynos_mod_shutdown(void) { + DynOS_Mod_Shutdown(); +} + } \ No newline at end of file diff --git a/data/dynos_level.cpp b/data/dynos_level.cpp index 85137fde..a2aea408 100644 --- a/data/dynos_level.cpp +++ b/data/dynos_level.cpp @@ -46,6 +46,7 @@ struct DynosWarp { }; static void *sDynosLevelScripts[LEVEL_COUNT] = { NULL }; +static void *sDynosLevelScriptsOriginal[LEVEL_COUNT] = { NULL }; static Array sDynosLevelWarps[LEVEL_COUNT] = { Array() }; static Array sDynosLevelList = Array(); // Ordered by Course Id, COURSE_NONE excluded @@ -90,6 +91,7 @@ static s32 DynOS_Level_PreprocessMasterScript(u8 aType, void *aCmd) { void *_Script = (void *) DynOS_Level_CmdGet(aCmd, 0x0C); if (sDynosLevelNum >= 0 && sDynosLevelNum < LEVEL_COUNT && !sDynosLevelScripts[sDynosLevelNum]) { sDynosLevelScripts[sDynosLevelNum] = _Script; + sDynosLevelScriptsOriginal[sDynosLevelNum] = _Script; } sDynosLevelNum = -1; return 2; @@ -237,6 +239,15 @@ void DynOS_Level_Override(void* originalScript, void* newScript) { } } +void DynOS_Level_Unoverride() { + for (s32 i = 0; i < LEVEL_COUNT; i++) { + sDynosCurrentLevelNum = i; + sDynosLevelWarps[i].Clear(); + sDynosLevelScripts[i] = sDynosLevelScriptsOriginal[i]; + DynOS_Level_ParseScript(sDynosLevelScripts[i], DynOS_Level_PreprocessScript); + } +} + const void *DynOS_Level_GetScript(s32 aLevel) { DynOS_Level_Init(); return sDynosLevelScripts[aLevel]; diff --git a/data/dynos_main.cpp b/data/dynos_main.cpp index 80b175d0..866bae37 100644 --- a/data/dynos_main.cpp +++ b/data/dynos_main.cpp @@ -66,3 +66,15 @@ void DynOS_UpdateGfx() { bool DynOS_IsTransitionActive() { return gWarpTransition.isActive; } + +// +// Misc +// + +void DynOS_Mod_Shutdown() { + DynOS_Actor_ModShutdown(); + DynOS_Col_ModShutdown(); + DynOS_Lvl_ModShutdown(); + DynOS_MovtexQC_ModShutdown(); + DynOS_Tex_ModShutdown(); +} diff --git a/data/dynos_mgr_actor.cpp b/data/dynos_mgr_actor.cpp index cf9832c8..cc1ec52a 100644 --- a/data/dynos_mgr_actor.cpp +++ b/data/dynos_mgr_actor.cpp @@ -51,10 +51,10 @@ void DynOS_Actor_AddCustom(const SysPath &aFilename, const char *aActorName) { } // Alloc and init the actors gfx list - ActorGfx actorGfx; + ActorGfx actorGfx = { 0 }; actorGfx.mGfxData = _GfxData; actorGfx.mGraphNode = (GraphNode *) DynOS_Geo_GetGraphNode(geoLayout, false); - actorGfx.mPackIndex = 99; + actorGfx.mPackIndex = MOD_PACK_INDEX; // Add to list DynOS_Actor_Valid(georef, actorGfx); @@ -150,3 +150,27 @@ void DynOS_Actor_Override_All(void) { } } } + +void DynOS_Actor_ModShutdown() { + auto& _DynosCustomActors = DynosCustomActors(); + while (_DynosCustomActors.Count() > 0) { + auto& pair = _DynosCustomActors[0]; + DynOS_Actor_Invalid(pair.second, MOD_PACK_INDEX); + free((void*)pair.first); + _DynosCustomActors.Remove(0); + } + + auto& _ValidActors = DynosValidActors(); + for (auto it = _ValidActors.cbegin(); it != _ValidActors.cend();) { + auto& actorGfx = it->second; + if (actorGfx.mPackIndex == MOD_PACK_INDEX) { + free(actorGfx.mGraphNode); + DynOS_Gfx_Free(actorGfx.mGfxData); + _ValidActors.erase(it++); + } else { + ++it; + } + } + + DynOS_Actor_Override_All(); +} diff --git a/data/dynos_mgr_col.cpp b/data/dynos_mgr_col.cpp index 591e5600..c7346a99 100644 --- a/data/dynos_mgr_col.cpp +++ b/data/dynos_mgr_col.cpp @@ -54,3 +54,13 @@ Collision* DynOS_Col_Get(const char* collisionName) { // check builtin collisions return (Collision*)DynOS_Builtin_LvlCol_GetFromName(collisionName); } + +void DynOS_Col_ModShutdown() { + auto& _DynosCollisions = DynosCollisions(); + while (_DynosCollisions.Count() > 0) { + auto& pair = _DynosCollisions[0]; + free((void*)pair.first); + Delete(pair.second); + _DynosCollisions.Remove(0); + } +} diff --git a/data/dynos_mgr_lvl.cpp b/data/dynos_mgr_lvl.cpp index 3742e733..54a1fb5e 100644 --- a/data/dynos_mgr_lvl.cpp +++ b/data/dynos_mgr_lvl.cpp @@ -20,6 +20,22 @@ Array> &DynOS_Lvl_GetArray() { return sDynosCustomLevelScripts; } +void DynOS_Lvl_ModShutdown() { + DynOS_Level_Unoverride(); + + auto& _CustomLevelScripts = DynOS_Lvl_GetArray(); + while (_CustomLevelScripts.Count() > 0) { + auto& pair = _CustomLevelScripts[0]; + DynOS_Tex_Invalid(pair.second); + Delete(pair.second); + free((void*)pair.first); + _CustomLevelScripts.Remove(0); + } + + auto& _OverrideLevelScripts = DynosOverrideLevelScripts(); + _OverrideLevelScripts.Clear(); +} + void DynOS_Lvl_Activate(s32 modIndex, const SysPath &aFilename, const char *aLevelName) { auto& _CustomLevelScripts = DynOS_Lvl_GetArray(); auto& _OverrideLevelScripts = DynosOverrideLevelScripts(); diff --git a/data/dynos_mgr_movtexqc.cpp b/data/dynos_mgr_movtexqc.cpp index 0d2fdb40..e47b72f3 100644 --- a/data/dynos_mgr_movtexqc.cpp +++ b/data/dynos_mgr_movtexqc.cpp @@ -67,3 +67,12 @@ DataNode* DynOS_MovtexQC_GetFromIndex(s32 index) { return mMovtexQCs[index]; } + +void DynOS_MovtexQC_ModShutdown() { + auto& _DynosRegisteredMovtexQCs = DynosRegisteredMovtexQCs(); + while (_DynosRegisteredMovtexQCs.Count() > 0) { + auto& registered = _DynosRegisteredMovtexQCs[0]; + Delete(registered.dataNode); + _DynosRegisteredMovtexQCs.Remove(0); + } +} diff --git a/data/dynos_mgr_tex.cpp b/data/dynos_mgr_tex.cpp index 59ce38a4..dc0489bd 100644 --- a/data/dynos_mgr_tex.cpp +++ b/data/dynos_mgr_tex.cpp @@ -422,6 +422,7 @@ void DynOS_Tex_AddCustom(const SysPath &aFilename, const char *aTexName) { // Load SysPath _PackFolder = ""; DataNode* _Node = DynOS_Tex_LoadFromBinary(_PackFolder, aFilename, _TexName, false); + free(_TexName); if (_Node) { DynOS_Tex_Activate(_Node, true); } @@ -468,3 +469,11 @@ bool DynOS_Tex_Get(const char* aTexName, struct TextureInfo* aOutTexInfo) { aOutTexInfo->texture = (u8*)info->pointer; return true; } + +void DynOS_Tex_ModShutdown() { + auto& _DynosCustomTexs = DynosCustomTexs(); + while (_DynosCustomTexs.Count() > 0) { + auto& pair = _DynosCustomTexs[0]; + DynOS_Tex_Deactivate(pair.second); + } +} \ No newline at end of file diff --git a/src/pc/djui/djui_panel_join_message.c b/src/pc/djui/djui_panel_join_message.c index c119b7a4..b42a00f5 100644 --- a/src/pc/djui/djui_panel_join_message.c +++ b/src/pc/djui/djui_panel_join_message.c @@ -16,7 +16,7 @@ void djui_panel_join_message_error(char* message) { } void djui_panel_join_message_cancel(struct DjuiBase* caller) { - network_shutdown(true); + network_shutdown(true, false); djui_panel_menu_back(caller); } diff --git a/src/pc/lua/smlua.c b/src/pc/lua/smlua.c index 700882d0..d748f319 100644 --- a/src/pc/lua/smlua.c +++ b/src/pc/lua/smlua.c @@ -180,6 +180,7 @@ void smlua_update(void) { void smlua_shutdown(void) { smlua_text_utils_reset_all(); smlua_audio_utils_reset_all(); + audio_custom_shutdown(); smlua_cobject_allowlist_shutdown(); smlua_cpointer_allowlist_shutdown(); lua_State* L = gLuaState; diff --git a/src/pc/network/network.c b/src/pc/network/network.c index 0e145730..3fb0889b 100644 --- a/src/pc/network/network.c +++ b/src/pc/network/network.c @@ -418,7 +418,7 @@ void network_register_mod(char* modName) { string_linked_list_append(&gRegisteredMods, modName); } -void network_shutdown(bool sendLeaving) { +void network_shutdown(bool sendLeaving, bool exiting) { if (gDjuiChatBox != NULL) { djui_base_destroy(&gDjuiChatBox->base); gDjuiChatBox = NULL; @@ -440,4 +440,23 @@ void network_shutdown(bool sendLeaving) { } gNetworkType = NT_NONE; + + if (exiting) { return; } + + // reset other stuff + extern u8* gOverrideEeprom; + gOverrideEeprom = NULL; + dynos_mod_shutdown(); + mods_clear(&gActiveMods); + mods_clear(&gRemoteMods); + smlua_shutdown(); + extern s16 gChangeLevel; + gChangeLevel = LEVEL_CASTLE_GROUNDS; + + // TODO: enable + /*extern bool gDjuiInMainMenu; + if (!gDjuiInMainMenu) { + gDjuiInMainMenu = true; + djui_panel_main_create(NULL); + }*/ } diff --git a/src/pc/network/network.h b/src/pc/network/network.h index d57ef4d8..77a05709 100644 --- a/src/pc/network/network.h +++ b/src/pc/network/network.h @@ -130,6 +130,6 @@ void network_receive(u8 localIndex, void* addr, u8* data, u16 dataLength); void* network_duplicate_address(u8 localIndex); void network_update(void); void network_register_mod(char* modName); -void network_shutdown(bool sendLeaving); +void network_shutdown(bool sendLeaving, bool exiting); #endif diff --git a/src/pc/network/network_player.c b/src/pc/network/network_player.c index 19e99901..cf264b5b 100644 --- a/src/pc/network/network_player.c +++ b/src/pc/network/network_player.c @@ -157,7 +157,7 @@ void network_player_update(void) { #ifndef DEVELOPMENT if (elapsed > NETWORK_PLAYER_TIMEOUT * 1.5f) { LOG_INFO("dropping due to no server connectivity"); - network_shutdown(false); + network_shutdown(false, false); } #endif @@ -273,7 +273,7 @@ u8 network_player_disconnected(u8 globalIndex) { LOG_ERROR("player disconnected, but it's local.. this shouldn't happen!"); return UNKNOWN_GLOBAL_INDEX; } else { - network_shutdown(true); + network_shutdown(true, false); } } diff --git a/src/pc/network/packets/packet_join.c b/src/pc/network/packets/packet_join.c index 313c9819..b464ba4a 100644 --- a/src/pc/network/packets/packet_join.c +++ b/src/pc/network/packets/packet_join.c @@ -153,7 +153,7 @@ void network_receive_join(struct Packet* p) { packet_read(p, &remoteVersion, sizeof(u8) * MAX_VERSION_LENGTH); LOG_INFO("server has version: %s", version); if (memcmp(version, remoteVersion, MAX_VERSION_LENGTH) != 0) { - network_shutdown(true); + network_shutdown(true, false); LOG_ERROR("version mismatch"); char mismatchMessage[256] = { 0 }; snprintf(mismatchMessage, 256, "\\#ffa0a0\\Error:\\#c8c8c8\\ Version mismatch.\n\nYour version: \\#a0a0ff\\%s\\#c8c8c8\\\nTheir version: \\#a0a0ff\\%s\\#c8c8c8\\\n\nSomeone is out of date!\n", version, remoteVersion); @@ -186,7 +186,7 @@ void network_receive_join(struct Packet* p) { } if (string_linked_list_mismatch(&gRegisteredMods, &head)) { - network_shutdown(true); + network_shutdown(true, false); struct StringBuilder* builder = string_builder_create(512); string_builder_append(builder, "\\#ffa0a0\\Error:\\#c8c8c8\\ mods don't match.\n\n"); diff --git a/src/pc/network/packets/packet_kick.c b/src/pc/network/packets/packet_kick.c index b231e6db..4a77b020 100644 --- a/src/pc/network/packets/packet_kick.c +++ b/src/pc/network/packets/packet_kick.c @@ -32,5 +32,5 @@ void network_receive_kick(struct Packet* p) { case EKT_BANNED: djui_panel_join_message_error("\\#ffa0a0\\Error:\\#c8c8c8\\ The server banned you."); break; default: djui_panel_join_message_error("\\#ffa0a0\\Error:\\#c8c8c8\\ Host has closed the connection."); break; } - network_shutdown(false); + network_shutdown(false, false); } diff --git a/src/pc/network/packets/packet_mod_list.c b/src/pc/network/packets/packet_mod_list.c index eb9be536..efd9084f 100644 --- a/src/pc/network/packets/packet_mod_list.c +++ b/src/pc/network/packets/packet_mod_list.c @@ -121,7 +121,7 @@ void network_receive_mod_list(struct Packet* p) { packet_read(p, &remoteVersion, sizeof(u8) * MAX_VERSION_LENGTH); LOG_INFO("server has version: %s", version); if (memcmp(version, remoteVersion, MAX_VERSION_LENGTH) != 0) { - network_shutdown(true); + network_shutdown(true, false); LOG_ERROR("version mismatch"); char mismatchMessage[256] = { 0 }; snprintf(mismatchMessage, 256, "\\#ffa0a0\\Error:\\#c8c8c8\\ Version mismatch.\n\nYour version: \\#a0a0ff\\%s\\#c8c8c8\\\nTheir version: \\#a0a0ff\\%s\\#c8c8c8\\\n\nSomeone is out of date!\n", version, remoteVersion); @@ -205,7 +205,7 @@ void network_receive_mod_list_entry(struct Packet* p) { // sanity check mod size if (mod->size >= MAX_MOD_SIZE) { djui_popup_create("Server had too large of a mod.\nQuitting.", 4); - network_shutdown(false); + network_shutdown(false, false); return; } diff --git a/src/pc/pc_main.c b/src/pc/pc_main.c index 2afe9e7d..9b58b305 100644 --- a/src/pc/pc_main.c +++ b/src/pc/pc_main.c @@ -258,7 +258,7 @@ void game_deinit(void) { audio_custom_shutdown(); audio_shutdown(); gfx_shutdown(); - network_shutdown(true); + network_shutdown(true, true); smlua_shutdown(); mods_shutdown(); inited = false;