From 703caebe6bd4c960f53fab0d8c37ce46211dd88e Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Fri, 7 Oct 2022 22:22:32 +0200 Subject: [PATCH] Unload SDK pak files before Engine pak files This is required, as we use assets within the engine pak files. Unloading the engine pak files first will result in a seg fault during shutdown. --- r5dev/engine/cmodel_bsp.cpp | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/r5dev/engine/cmodel_bsp.cpp b/r5dev/engine/cmodel_bsp.cpp index 4dd87d8f..0cb9122a 100644 --- a/r5dev/engine/cmodel_bsp.cpp +++ b/r5dev/engine/cmodel_bsp.cpp @@ -213,8 +213,34 @@ void MOD_ProcessPakQueue() } if (!v14 || v13 == 9) { - g_pakLoadApi->UnloadPak(*(_DWORD*)v10); - MOD_UnloadPakFile(); + // SDK pak files must be unloaded before the engine pak files, + // as we reference assets within engine pak files. + const RPakLoadedInfo_t* pLoadedPakInfo = g_pRTech->GetPakLoadedInfo(*(RPakHandle_t*)v10); + if (pLoadedPakInfo) + { + const char* pszLoadedPakName = pLoadedPakInfo->m_pszFileName; + + if (strcmp(pszLoadedPakName, "common_mp.rpak") == 0 || + strcmp(pszLoadedPakName, "common_sp.rpak") == 0 || + strcmp(pszLoadedPakName, "common_pve.rpak") == 0) + { + const RPakLoadedInfo_t* pLoadedSdkPak = g_pRTech->GetPakLoadedInfo("common_sdk.rpak"); + + if (pLoadedSdkPak) // Only unload if sdk pak file is loaded. + g_pakLoadApi->UnloadPak(pLoadedSdkPak->m_nHandle); + + } + else if (strcmp(pszLoadedPakName, "ui_mp.rpak") == 0) + { + const RPakLoadedInfo_t* pLoadedSdkPak = g_pRTech->GetPakLoadedInfo("ui_sdk.rpak"); + + if (pLoadedSdkPak) // Only unload if sdk pak file is loaded. + g_pakLoadApi->UnloadPak(pLoadedSdkPak->m_nHandle); + } + } + + g_pakLoadApi->UnloadPak(*(RPakHandle_t*)v10); + MOD_UnloadPakFile(); // Unload mod pak files. } if (v13 && (unsigned int)(v13 - 13) > 1) return; @@ -388,7 +414,7 @@ void MOD_PreloadPakFile(const string& svLevelName) //----------------------------------------------------------------------------- void MOD_UnloadPakFile(void) { - for (auto& it : g_vLoadedPakHandle) + for (const RPakHandle_t& it : g_vLoadedPakHandle) { if (it >= 0) {