From 4ddb481197fca0afb2759f60dd415d4fd6ad37af Mon Sep 17 00:00:00 2001 From: aemony Date: Fri, 10 Nov 2023 23:32:06 +0100 Subject: [PATCH] Fix updater breaking on no internet on launch --- src/SKIF.cpp | 67 +++++++++++++++++++++++++++++++++++++---- src/utility/updater.cpp | 11 +++---- 2 files changed, 65 insertions(+), 13 deletions(-) diff --git a/src/SKIF.cpp b/src/SKIF.cpp index 55e701e4..ec10aaca 100644 --- a/src/SKIF.cpp +++ b/src/SKIF.cpp @@ -70,6 +70,7 @@ #include #include #include +#include const int SKIF_STEAM_APPID = 1157970; bool RecreateSwapChains = false; @@ -87,6 +88,7 @@ bool startedMinimized = false; bool msgDontRedraw = false; bool coverFadeActive = false; bool SKIF_Shutdown = false; +bool SKIF_NoInternet = false; int SKIF_ExitCode = 0; int SKIF_nCmdShow = -1; int startupFadeIn = 0; @@ -1451,11 +1453,13 @@ wWinMain ( _In_ HINSTANCE hInstance, // Force a one-time check before we enter the main loop _inject._TestServletRunlevel (true); - // Initialize the updater - static SKIF_Updater& _updater = - SKIF_Updater::GetInstance ( ); + // Variable related to checking for an internet connection + CComPtr pNLM; + HRESULT hrNLM = E_HANDLE; + DWORD dwNLM = NULL; + DWORD dwNLMinterval = 5000; - // Register hotkeys + // Do final checks and actions if we are expected to live longer than a few seconds if (! _Signal.Launcher && ! _Signal.Quit) { // Register HDR toggle hotkey (if applicable) @@ -1463,8 +1467,24 @@ wWinMain ( _In_ HINSTANCE hInstance, // Register service (auto-stop) hotkey SKIF_Util_RegisterHotKeySVCTemp ( ); + + // Check if we have an internet connection + hrNLM = CoCreateInstance (CLSID_NetworkListManager, NULL, CLSCTX_ALL, __uuidof (INetworkListManager), (LPVOID*) &pNLM); + if (SUCCEEDED (hrNLM)) + { + VARIANT_BOOL connStatus = 0; + dwNLM = SKIF_Util_timeGetTime ( ); + + PLOG_DEBUG << "Checking for an online connection..."; + if (SUCCEEDED (pNLM->get_IsConnectedToInternet (&connStatus))) + SKIF_NoInternet = (VARIANT_FALSE == connStatus); + } } + // Initialize the updater + static SKIF_Updater& _updater = + SKIF_Updater::GetInstance ( ); + // Main loop while (! SKIF_Shutdown ) // && IsWindow (hWnd) ) { msg = { }; @@ -1679,6 +1699,24 @@ wWinMain ( _In_ HINSTANCE hInstance, : false; // and false if OS prerequisites are disabled } + // In case we have no internet connection, check for one every 5 seconds + if (SKIF_NoInternet) + { + if (SUCCEEDED (hrNLM) && dwNLM + dwNLMinterval < SKIF_Util_timeGetTime ( )) + { + VARIANT_BOOL connStatus = 0; + dwNLM = SKIF_Util_timeGetTime ( ); + + PLOG_DEBUG << "Checking for an online connection..."; + if (SUCCEEDED (pNLM->get_IsConnectedToInternet (&connStatus))) + SKIF_NoInternet = (VARIANT_FALSE == connStatus); + + // Kickstart the update thread + if (! SKIF_NoInternet) + _updater.CheckForUpdates ( ); + } + } + // F6 to toggle DPI scaling if ( changedHiDPIScaling || ( io.KeysDown[VK_F6] && io.KeysDownDuration[VK_F6] == 0.0f)) @@ -2002,6 +2040,7 @@ wWinMain ( _In_ HINSTANCE hInstance, // If we switch to small mode, close all popups ImGui::ClosePopupsOverWindow (ImGui::GetCurrentWindowRead ( ), false); } + else { // If we switch back to large mode, re-open a few specific ones if (AddGamePopup == PopupState_Opened) @@ -2022,6 +2061,17 @@ wWinMain ( _In_ HINSTANCE hInstance, // Register service (auto-stop) hotkey SKIF_Util_RegisterHotKeySVCTemp ( ); + + // Check for the presence of an internet connection + if (SUCCEEDED (hrNLM)) + { + VARIANT_BOOL connStatus = 0; + dwNLM = SKIF_Util_timeGetTime ( ); + + PLOG_DEBUG << "Checking for an online connection..."; + if (SUCCEEDED (pNLM->get_IsConnectedToInternet (&connStatus))) + SKIF_NoInternet = (VARIANT_FALSE == connStatus); + } // Kickstart the update thread _updater.CheckForUpdates ( ); @@ -2372,7 +2422,7 @@ wWinMain ( _In_ HINSTANCE hInstance, // End Add Game // Begin Pulsating Refresh Icon - if (_updater.IsRunning ( )) + if (! SKIF_NoInternet && _updater.IsRunning ( )) { ImGui::SetCursorPosX ( ImGui::GetCursorPosX () + @@ -2835,7 +2885,12 @@ wWinMain ( _In_ HINSTANCE hInstance, HistoryPopupWidth = std::min (HistoryPopupWidth, SKIF_vecCurrentMode.x * 0.9f); - HistoryPopupTitle = "Changelog (" + _updater.GetChannel()->first + ")###History"; + HistoryPopupTitle = "Changelog"; + + if (! _updater.GetChannel()->first.empty()) + HistoryPopupTitle += "(" + _updater.GetChannel()->first + ")"; + + HistoryPopupTitle += "###History"; ImGui::OpenPopup ("###History"); diff --git a/src/utility/updater.cpp b/src/utility/updater.cpp index 7ea08a97..8d25aa06 100644 --- a/src/utility/updater.cpp +++ b/src/utility/updater.cpp @@ -22,7 +22,7 @@ SKIF_Updater::SKIF_Updater (void) InitializeConditionVariable (&UpdaterPaused); extern SKIF_Signals _Signal; - if (!_Signal.Launcher && !_Signal.Quit) + if (! _Signal.Launcher && ! _Signal.Quit) { // Clearing out old installers... ClearOldUpdates ( ); @@ -48,9 +48,10 @@ SKIF_Updater::SKIF_Updater (void) static SKIF_Updater& parent = SKIF_Updater::GetInstance ( ); extern SKIF_Signals _Signal; extern bool SKIF_Shutdown; + extern bool SKIF_NoInternet; - // Sleep if SKIF is being used as a lancher - while (_Signal.Launcher || _Signal.Quit) + // Sleep if SKIF is being used as a lancher, exiting, or we have no internet + while (_Signal.Launcher || _Signal.Quit || SKIF_NoInternet) { SleepConditionVariableCS ( &UpdaterPaused, &UpdaterJob, @@ -103,14 +104,10 @@ SKIF_Updater::SKIF_Updater (void) while (! parent.awake.load ( )) { - PLOG_DEBUG << "SKIF_UpdaterJob thread going to sleep!"; - SleepConditionVariableCS ( &UpdaterPaused, &UpdaterJob, INFINITE ); - - PLOG_DEBUG << "SKIF_UpdaterJob thread woke up!"; } } while (! SKIF_Shutdown); // Keep thread alive until exit