Skip to content

Commit

Permalink
Fix updater breaking on no internet on launch
Browse files Browse the repository at this point in the history
  • Loading branch information
Aemony committed Nov 10, 2023
1 parent 19af835 commit 4ddb481
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 13 deletions.
67 changes: 61 additions & 6 deletions src/SKIF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
#include <propvarutil.h>
#include <knownfolders.h>
#include <shlobj.h>
#include <netlistmgr.h>

const int SKIF_STEAM_APPID = 1157970;
bool RecreateSwapChains = false;
Expand All @@ -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;
Expand Down Expand Up @@ -1451,20 +1453,38 @@ 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 <INetworkListManager> 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)
SKIF_Util_RegisterHotKeyHDRToggle ( );

// 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 = { };
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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 ( );
Expand Down Expand Up @@ -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 () +
Expand Down Expand Up @@ -2835,7 +2885,12 @@ wWinMain ( _In_ HINSTANCE hInstance,

HistoryPopupWidth = std::min<float> (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");

Expand Down
11 changes: 4 additions & 7 deletions src/utility/updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ( );
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4ddb481

Please sign in to comment.