diff --git a/launchers/win32/LauncherApp.cpp b/launchers/win32/LauncherApp.cpp index ff63841c5a..38bf7eebcc 100644 --- a/launchers/win32/LauncherApp.cpp +++ b/launchers/win32/LauncherApp.cpp @@ -72,9 +72,7 @@ BOOL CLauncherApp::InitInstance() { } else { _manager.init(!noUpdate, continueAction); } - if (!_manager.hasFailed() && !_manager.installLauncher()) { - return FALSE; - } + _manager.tryToInstallLauncher(); installFont(IDR_FONT_REGULAR); installFont(IDR_FONT_BOLD); CWinApp::InitInstance(); diff --git a/launchers/win32/LauncherDlg.cpp b/launchers/win32/LauncherDlg.cpp index 928bf7010f..1fb6207c95 100644 --- a/launchers/win32/LauncherDlg.cpp +++ b/launchers/win32/LauncherDlg.cpp @@ -656,7 +656,6 @@ BOOL CLauncherDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) void CLauncherDlg::OnTimer(UINT_PTR nIDEvent) { - if (theApp._manager.hasFailed() && _drawStep != DrawStep::DrawError) { theApp._manager.saveErrorLog(); prepareProcess(DrawStep::DrawError); @@ -757,6 +756,9 @@ void CLauncherDlg::OnTimer(UINT_PTR nIDEvent) { _applicationWND = theApp._manager.launchApplication(); } } + if (theApp._manager.needsToSelfInstall()) { + theApp._manager.tryToInstallLauncher(TRUE); + } } void CLauncherDlg::setVerticalElement(CWnd* element, int verticalOffset, int heightOffset, bool fromMainWindowBottom) { diff --git a/launchers/win32/LauncherManager.cpp b/launchers/win32/LauncherManager.cpp index 49ae058ef5..5efcb38cf6 100644 --- a/launchers/win32/LauncherManager.cpp +++ b/launchers/win32/LauncherManager.cpp @@ -108,7 +108,7 @@ void LauncherManager::saveErrorLog() { } } -BOOL LauncherManager::installLauncher() { +void LauncherManager::tryToInstallLauncher(BOOL retry) { CString appPath; BOOL result = getAndCreatePaths(PathType::Running_Path, appPath); if (!result) { @@ -126,26 +126,49 @@ BOOL LauncherManager::installLauncher() { if (!_shouldUninstall) { // The installer is not running on the desired location and has to be installed // Kill of running before self-copy - addToLog(_T("Installing Launcher.")); + addToLog(_T("Trying to install launcher.")); int launcherPID = -1; if (LauncherUtils::isProcessRunning(LAUNCHER_EXE_FILENAME, launcherPID)) { if (!LauncherUtils::shutdownProcess(launcherPID, 0)) { addToLog(_T("Error shutting down the Launcher")); } } - CopyFile(appPath, instalationPath, FALSE); + const int LAUNCHER_INSTALL_RETRYS = 10; + const int WAIT_BETWEEN_RETRYS_MS = 10; + int installTrys = retry ? LAUNCHER_INSTALL_RETRYS : 0; + for (int i = 0; i <= installTrys; i++) { + _retryLauncherInstall = !CopyFile(appPath, instalationPath, FALSE); + if (!_retryLauncherInstall) { + addToLog(_T("Launcher installed successfully.")); + break; + } else if (i < installTrys) { + CString msg; + msg.Format(_T("Installing launcher try: %d"), i); + addToLog(msg); + Sleep(WAIT_BETWEEN_RETRYS_MS); + } else if (installTrys > 0) { + addToLog(_T("Error installing launcher.")); + _retryLauncherInstall = false; + _hasFailed = true; + } else { + addToLog(_T("Old launcher is still running. Install could not be completed.")); + } + } } } else if (_shouldUninstall) { addToLog(_T("Launching Uninstall mode.")); CString tempPath; if (getAndCreatePaths(PathType::Temp_Directory, tempPath)) { tempPath += _T("\\HQ_uninstaller_tmp.exe"); - CopyFile(instalationPath, tempPath, false); - LauncherUtils::launchApplication(tempPath, _T(" --uninstall")); - exit(0); + if (!CopyFile(instalationPath, tempPath, false)) { + addToLog(_T("Error copying uninstaller to tmp directory.")); + _hasFailed = true; + } else { + LauncherUtils::launchApplication(tempPath, _T(" --uninstall")); + exit(0); + } } } - return TRUE; } BOOL LauncherManager::restartLauncher() { diff --git a/launchers/win32/LauncherManager.h b/launchers/win32/LauncherManager.h index 5169edfa75..18ac041b3b 100644 --- a/launchers/win32/LauncherManager.h +++ b/launchers/win32/LauncherManager.h @@ -92,7 +92,7 @@ public: BOOL deleteShortcuts(); HWND launchApplication(); BOOL uninstallApplication(); - BOOL installLauncher(); + void tryToInstallLauncher(BOOL retry = FALSE); BOOL restartLauncher(); // getters @@ -108,6 +108,7 @@ public: BOOL needsInstall() const { return _shouldInstall; } BOOL needsToWait() const { return _shouldWait; } BOOL needsRestartNewLauncher() const { return _shouldRestartNewLauncher; } + BOOL needsToSelfInstall() const { return _retryLauncherInstall; } BOOL willContinueUpdating() const { return _keepUpdating; } ContinueActionOnStart getContinueAction() { return _continueAction; } void setDisplayName(const CString& displayName) { _displayName = displayName; } @@ -164,6 +165,7 @@ private: BOOL _shouldRestartNewLauncher { FALSE }; BOOL _keepLoggingIn { FALSE }; BOOL _keepUpdating { FALSE }; + BOOL _retryLauncherInstall { FALSE }; ContinueActionOnStart _continueAction; float _progressOffset { 0.0f }; float _progress { 0.0f };