From a6f8169422696994bc01fc887838a6c518570ce0 Mon Sep 17 00:00:00 2001 From: Skyth <19259897+blueskythlikesclouds@users.noreply.github.com> Date: Sat, 22 Mar 2025 14:49:34 +0300 Subject: [PATCH 1/6] Check if the 4 GB memory allocation failed. --- UnleashedRecomp/kernel/memory.cpp | 6 ++++++ UnleashedRecomp/locale/locale.cpp | 11 +++++++++++ UnleashedRecomp/main.cpp | 10 ++++++++-- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/UnleashedRecomp/kernel/memory.cpp b/UnleashedRecomp/kernel/memory.cpp index c2aa18a2..a52b7e7a 100644 --- a/UnleashedRecomp/kernel/memory.cpp +++ b/UnleashedRecomp/kernel/memory.cpp @@ -9,6 +9,9 @@ Memory::Memory() if (base == nullptr) base = (uint8_t*)VirtualAlloc(nullptr, PPC_MEMORY_SIZE, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + if (base == nullptr) + return; + DWORD oldProtect; VirtualProtect(base, 4096, PAGE_NOACCESS, &oldProtect); #else @@ -17,6 +20,9 @@ Memory::Memory() if (base == (uint8_t*)MAP_FAILED) base = (uint8_t*)mmap(NULL, PPC_MEMORY_SIZE, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); + if (base == nullptr) + return; + mprotect(base, 4096, PROT_NONE); #endif diff --git a/UnleashedRecomp/locale/locale.cpp b/UnleashedRecomp/locale/locale.cpp index 138dc417..8188e1da 100644 --- a/UnleashedRecomp/locale/locale.cpp +++ b/UnleashedRecomp/locale/locale.cpp @@ -703,6 +703,17 @@ std::unordered_map> { ELanguage::Italian, "Impossibile trovare il modulo \"%s\".\n\nAssicurati che:\n\n- Hai estratto questa copia di Unleashed Recompiled correttamente e non solo il file *.exe.\n- Non stai eseguendo Unleashed Recompiled da un file *.zip." } } }, + { + "System_MemoryAllocationFailed", + { + { ELanguage::English, "Memory allocation failed blablabla enable your pagefile, ensure it's minimum 4 or 8 GB blablablabla" }, + { ELanguage::Japanese, "" }, + { ELanguage::German, "" }, + { ELanguage::French, "" }, + { ELanguage::Spanish, "" }, + { ELanguage::Italian, "" } + } + }, { "Common_On", { diff --git a/UnleashedRecomp/main.cpp b/UnleashedRecomp/main.cpp index a55c0181..16f59397 100644 --- a/UnleashedRecomp/main.cpp +++ b/UnleashedRecomp/main.cpp @@ -51,14 +51,20 @@ void HostStartup() CoInitializeEx(nullptr, COINIT_MULTITHREADED); #endif - g_userHeap.Init(); - hid::Init(); } // Name inspired from nt's entry point void KiSystemStartup() { + if (g_memory.base == nullptr) + { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, GameWindow::GetTitle(), Localise("System_MemoryAllocationFailed").c_str(), GameWindow::s_pWindow); + std::_Exit(1); + } + + g_userHeap.Init(); + const auto gameContent = XamMakeContent(XCONTENTTYPE_RESERVED, "Game"); const auto updateContent = XamMakeContent(XCONTENTTYPE_RESERVED, "Update"); XamRegisterContent(gameContent, GAME_INSTALL_DIRECTORY "/game"); From 343ac4c85b4597e4ad58befe1c2a08c7ed77cf2c Mon Sep 17 00:00:00 2001 From: "Skyth (Asilkan)" <19259897+blueskythlikesclouds@users.noreply.github.com> Date: Sun, 23 Mar 2025 23:27:54 +0300 Subject: [PATCH 2/6] Update UnleashedRecomp/locale/locale.cpp Co-authored-by: Hyper <34012267+hyperbx@users.noreply.github.com> --- UnleashedRecomp/locale/locale.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UnleashedRecomp/locale/locale.cpp b/UnleashedRecomp/locale/locale.cpp index 8188e1da..a9772b40 100644 --- a/UnleashedRecomp/locale/locale.cpp +++ b/UnleashedRecomp/locale/locale.cpp @@ -706,7 +706,7 @@ std::unordered_map> { "System_MemoryAllocationFailed", { - { ELanguage::English, "Memory allocation failed blablabla enable your pagefile, ensure it's minimum 4 or 8 GB blablablabla" }, + { ELanguage::English, "Failed to allocate game memory.\n\nPlease make sure that:\n\n- You meet the minimum system requirements (8 GB).\n- Your pagefile is configured with at least 4-8 GB of virtual memory." }, { ELanguage::Japanese, "" }, { ELanguage::German, "" }, { ELanguage::French, "" }, From 4906940f214b900d4d6b168c59cd2ad1ae544348 Mon Sep 17 00:00:00 2001 From: "Skyth (Asilkan)" <19259897+blueskythlikesclouds@users.noreply.github.com> Date: Tue, 25 Mar 2025 20:19:38 +0300 Subject: [PATCH 3/6] Update locale.cpp --- UnleashedRecomp/locale/locale.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UnleashedRecomp/locale/locale.cpp b/UnleashedRecomp/locale/locale.cpp index a9772b40..3e8e1cd1 100644 --- a/UnleashedRecomp/locale/locale.cpp +++ b/UnleashedRecomp/locale/locale.cpp @@ -706,7 +706,7 @@ std::unordered_map> { "System_MemoryAllocationFailed", { - { ELanguage::English, "Failed to allocate game memory.\n\nPlease make sure that:\n\n- You meet the minimum system requirements (8 GB).\n- Your pagefile is configured with at least 4-8 GB of virtual memory." }, + { ELanguage::English, "Failed to allocate game memory.\n\nPlease make sure that:\n\n- You meet the minimum system requirements (8 GB).\n- Your page file is configured with at least 4-8 GB of virtual memory." }, { ELanguage::Japanese, "" }, { ELanguage::German, "" }, { ELanguage::French, "" }, From efe04e1ee3abca601df73d4983e3ba78108e6b48 Mon Sep 17 00:00:00 2001 From: Skyth <19259897+blueskythlikesclouds@users.noreply.github.com> Date: Fri, 28 Mar 2025 17:35:44 +0300 Subject: [PATCH 4/6] Add localizations. --- UnleashedRecomp/locale/locale.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/UnleashedRecomp/locale/locale.cpp b/UnleashedRecomp/locale/locale.cpp index 3e8e1cd1..d6cddeca 100644 --- a/UnleashedRecomp/locale/locale.cpp +++ b/UnleashedRecomp/locale/locale.cpp @@ -707,11 +707,11 @@ std::unordered_map> "System_MemoryAllocationFailed", { { ELanguage::English, "Failed to allocate game memory.\n\nPlease make sure that:\n\n- You meet the minimum system requirements (8 GB).\n- Your page file is configured with at least 4-8 GB of virtual memory." }, - { ELanguage::Japanese, "" }, - { ELanguage::German, "" }, - { ELanguage::French, "" }, - { ELanguage::Spanish, "" }, - { ELanguage::Italian, "" } + { ELanguage::Japanese, "ゲームメモリの割り当てに失敗しました\n\n次の点を確認してください:\n\n※最小システム要件(8 GB)を満たしていること。\n※ページファイルに少なくとも4~8 GBの仮想メモリが設定されていること" }, + { ELanguage::German, "Fehler beim Zuweisen des Spielspeichers.\n\nBitte stelle sicher, dass:\n\n- Die Mindestanforderungen für das System erfüllt sind (8 GB).\n- Die Auslagerungsdatei mit mindestens 4-8 GB virtuellem Speicher konfiguriert ist." }, + { ELanguage::French, "Échec d'allocation de la mémoire du jeu.\n\nVeuillez vous assurer que :\n\n- Vous disposez de la configuration minimale requise (8 Go).\n- Votre fichier d’échange est configuré avec au moins 4 à 8 Go de mémoire virtuelle." }, + { ELanguage::Spanish, "Fallo al asignar memoria del juego.\n\nPor favor, asegúrate de que:\n\n- Cumples los requisitos mínimos del sistema (8 GB).\n- Tu archivo de páginación está configurado con al menos 4 u 8 GB de memoria virtual." }, + { ELanguage::Italian, "Impossibile allocare la memoria per il gioco.\n\nAssicurati che:\n\n- Soddisfi i requisiti minimi di sistema (8 GB).\n- Il tuo file di paging sia configurato con almeno 4 o 8 GB di memoria virtuale." } } }, { From 908ebaec1605bb69138460f261e20fda6ec44fae Mon Sep 17 00:00:00 2001 From: Skyth <19259897+blueskythlikesclouds@users.noreply.github.com> Date: Fri, 28 Mar 2025 17:53:39 +0300 Subject: [PATCH 5/6] Fix installer not booting with the changes. --- UnleashedRecomp/gpu/video.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/UnleashedRecomp/gpu/video.cpp b/UnleashedRecomp/gpu/video.cpp index e24ce4de..29ebb842 100644 --- a/UnleashedRecomp/gpu/video.cpp +++ b/UnleashedRecomp/gpu/video.cpp @@ -320,6 +320,7 @@ static constexpr RenderFormat BACKBUFFER_FORMAT = RenderFormat::B8G8R8A8_UNORM; static std::unique_ptr g_acquireSemaphores[NUM_FRAMES]; static std::unique_ptr g_renderSemaphores[NUM_FRAMES]; static uint32_t g_backBufferIndex; +static std::unique_ptr g_backBufferHolder; static GuestSurface* g_backBuffer; static std::unique_ptr g_intermediaryBackBufferTexture; @@ -1948,7 +1949,10 @@ bool Video::CreateHostDevice(const char *sdlVideoDriver) desc.renderTargetCount = 1; g_gammaCorrectionPipeline = g_device->createGraphicsPipeline(desc); - g_backBuffer = g_userHeap.AllocPhysical(ResourceType::RenderTarget); + // NOTE: We initially allocate this on host memory to make the installer work, even if the 4 GB memory allocation fails. + g_backBufferHolder = std::make_unique(ResourceType::RenderTarget); + + g_backBuffer = g_backBufferHolder.get(); g_backBuffer->width = 1280; g_backBuffer->height = 720; g_backBuffer->format = BACKBUFFER_FORMAT; @@ -2006,6 +2010,11 @@ static uint32_t CreateDevice(uint32_t a1, uint32_t a2, uint32_t a3, uint32_t a4, LoadTexture((uint8_t *)achievement.pImageBuffer, achievement.ImageBufferSize).release(); } + // Move backbuffer to guest memory. + assert(!g_memory.IsInMemoryRange(g_backBuffer) && g_backBufferHolder != nullptr); + g_backBuffer = g_userHeap.AllocPhysical(std::move(*g_backBufferHolder)); + g_backBufferHolder = nullptr; + auto device = g_userHeap.AllocPhysical(); memset(device, 0, sizeof(*device)); From 839bff7638f8bad5636c7766688ee61255e15f11 Mon Sep 17 00:00:00 2001 From: Skyth <19259897+blueskythlikesclouds@users.noreply.github.com> Date: Fri, 28 Mar 2025 18:15:19 +0300 Subject: [PATCH 6/6] Fix stale reference crash. --- UnleashedRecomp/gpu/video.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/UnleashedRecomp/gpu/video.cpp b/UnleashedRecomp/gpu/video.cpp index 29ebb842..182fd031 100644 --- a/UnleashedRecomp/gpu/video.cpp +++ b/UnleashedRecomp/gpu/video.cpp @@ -2013,6 +2013,12 @@ static uint32_t CreateDevice(uint32_t a1, uint32_t a2, uint32_t a3, uint32_t a4, // Move backbuffer to guest memory. assert(!g_memory.IsInMemoryRange(g_backBuffer) && g_backBufferHolder != nullptr); g_backBuffer = g_userHeap.AllocPhysical(std::move(*g_backBufferHolder)); + + // Check for stale reference. BeginCommandList() gets called before CreateDevice() which is where the assignment happens. + if (g_renderTarget == g_backBufferHolder.get()) g_renderTarget = g_backBuffer; + if (g_depthStencil == g_backBufferHolder.get()) g_depthStencil = g_backBuffer; + + // Free the host backbuffer. g_backBufferHolder = nullptr; auto device = g_userHeap.AllocPhysical();