diff --git a/src/core/co_stub.cc b/src/core/co_stub.cc index 3cce57e4..21a72e46 100644 --- a/src/core/co_stub.cc +++ b/src/core/co_stub.cc @@ -397,6 +397,40 @@ const std::string ConstructOnLoadModule(uint16_t ftpPort, uint16_t ipcPort) return GetBootstrapModule(scriptImportTable, ipcPort); } +/** + * Restores the original `SharedJSContext` by renaming the backup file to the original file. + * It reverses the patches done in the preloader module + * + * @note this function is only applicable to Windows + */ +const void UnPatchSharedJSContext() +{ + #ifdef _WIN32 + Logger.Log("Restoring SharedJSContext..."); + + const auto SteamUIModulePath = SystemIO::GetSteamPath() / "steamui" / "index.html"; + const auto SteamUIModulePathBackup = SystemIO::GetSteamPath() / "steamui" / "orig.html"; + + try + { + if (std::filesystem::exists(SteamUIModulePathBackup) && std::filesystem::is_regular_file(SteamUIModulePathBackup)) + { + std::filesystem::remove(SteamUIModulePath); + } + + std::filesystem::rename(SteamUIModulePathBackup, SteamUIModulePath); + } + catch (const std::exception& e) + { + Logger.Warn("Failed to restore SharedJSContext: {}", e.what()); + } + + Logger.Log("Restored SharedJSContext..."); + #endif + Sockets::PostShared({ { "id", 9773 }, { "method", "Page.reload" } }); +} + + /** * Notifies the frontend of the backend load and handles script injection and state updates. * @@ -418,6 +452,7 @@ const std::string ConstructOnLoadModule(uint16_t ftpPort, uint16_t ipcPort) */ void OnBackendLoad(uint16_t ftpPort, uint16_t ipcPort) { + UnPatchSharedJSContext(); // Restore the original SharedJSContext Logger.Log("Notifying frontend of backend load..."); static uint16_t m_ftpPort = ftpPort; diff --git a/src/core/loader.cc b/src/core/loader.cc index a4ae7861..ebae053d 100644 --- a/src/core/loader.cc +++ b/src/core/loader.cc @@ -138,8 +138,6 @@ class CEFBrowser { sharedJsContextSessionId = json["params"]["sessionId"]; Sockets::PostShared({ {"id", 9494 }, {"method", "Log.enable"}, {"sessionId", sharedJsContextSessionId} }); - - this->UnPatchSharedJSContext(); } else if (json.value("id", -1) == 9773) { @@ -157,33 +155,6 @@ class CEFBrowser Sockets::PostGlobal({ { "id", 0 }, { "method", "Target.getTargets" } }); } - const void UnPatchSharedJSContext() - { - #ifdef _WIN32 - Logger.Log("Restoring SharedJSContext..."); - - const auto SteamUIModulePath = SystemIO::GetSteamPath() / "steamui" / "index.html"; - const auto SteamUIModulePathBackup = SystemIO::GetSteamPath() / "steamui" / "orig.html"; - - try - { - if (std::filesystem::exists(SteamUIModulePathBackup) && std::filesystem::is_regular_file(SteamUIModulePathBackup)) - { - std::filesystem::remove(SteamUIModulePath); - } - - std::filesystem::rename(SteamUIModulePathBackup, SteamUIModulePath); - } - catch (const std::exception& e) - { - Logger.Warn("Failed to restore SharedJSContext: {}", e.what()); - } - - Logger.Log("Restored SharedJSContext..."); - #endif - Sockets::PostShared({ { "id", 9773 }, { "method", "Page.reload" } }); - } - const void onSharedJsConnect() { std::thread([this]() {