From 6a7b9c0202b13b36f888c75dc23a1f456bb09172 Mon Sep 17 00:00:00 2001 From: luiscuenca Date: Fri, 5 Jul 2019 13:59:35 -0700 Subject: [PATCH] Delete install directory right before installing and keep a copy of config.json --- launchers/win32/LauncherDlg.cpp | 31 +++++----------------- launchers/win32/LauncherManager.cpp | 41 ++++++++++++++++++++++------- launchers/win32/LauncherUtils.cpp | 21 +++++---------- launchers/win32/LauncherUtils.h | 20 ++++---------- 4 files changed, 48 insertions(+), 65 deletions(-) diff --git a/launchers/win32/LauncherDlg.cpp b/launchers/win32/LauncherDlg.cpp index c308efe3cc..1493dec445 100644 --- a/launchers/win32/LauncherDlg.cpp +++ b/launchers/win32/LauncherDlg.cpp @@ -174,19 +174,11 @@ void CLauncherDlg::startProcess() { theApp._manager.addToLog(_T("Starting Process Setup")); setDrawDialog(DrawStep::DrawProcessSetup); } - theApp._manager.addToLog(_T("Deleting directories before install")); - - CString installDir; - theApp._manager.getAndCreatePaths(LauncherManager::PathType::Interface_Directory, installDir); + theApp._manager.addToLog(_T("Deleting download directory")); CString downloadDir; theApp._manager.getAndCreatePaths(LauncherManager::PathType::Download_Directory, downloadDir); - - LauncherUtils::deleteDirectoriesOnThread(installDir, downloadDir, [&](int error) { - LauncherUtils::DeleteDirError deleteError = (LauncherUtils::DeleteDirError)error; - switch(error) { - case LauncherUtils::DeleteDirError::NoErrorDeleting: - theApp._manager.addToLog(_T("Install directory deleted.")); - theApp._manager.addToLog(_T("Downloads directory deleted.")); + LauncherUtils::deleteDirectoryOnThread(downloadDir, [&](bool error) { + if (!error) { if (!theApp._manager.isLoggedIn()) { theApp._manager.addToLog(_T("Downloading Content")); theApp._manager.downloadContent(); @@ -194,23 +186,12 @@ void CLauncherDlg::startProcess() { theApp._manager.addToLog(_T("Downloading App")); theApp._manager.downloadApplication(); } - break; - case LauncherUtils::DeleteDirError::ErrorDeletingBothDirs: - theApp._manager.addToLog(_T("Error deleting directories.")); - break; - case LauncherUtils::DeleteDirError::ErrorDeletingApplicationDir: - theApp._manager.addToLog(_T("Error deleting application directory.")); - break; - case LauncherUtils::DeleteDirError::ErrorDeletingDownloadsDir: - theApp._manager.addToLog(_T("Error deleting downloads directory.")); - break; - default: - break; - } - if (error != LauncherUtils::DeleteDirError::NoErrorDeleting) { + } else { + theApp._manager.addToLog(_T("Error deleting download directory.")); theApp._manager.setFailed(true); } }); + } BOOL CLauncherDlg::getHQInfo(const CString& orgname) { diff --git a/launchers/win32/LauncherManager.cpp b/launchers/win32/LauncherManager.cpp index ab3158d220..146e871028 100644 --- a/launchers/win32/LauncherManager.cpp +++ b/launchers/win32/LauncherManager.cpp @@ -38,6 +38,9 @@ void LauncherManager::init() { addToLog(_T("New build found. Updating")); _shouldUpdate = TRUE; } + } else if (_loggedIn) { + addToLog(_T("Interface not found but logged in. Reinstalling")); + _shouldUpdate = TRUE; } } else { _hasFailed = true; @@ -189,9 +192,9 @@ BOOL LauncherManager::isApplicationInstalled(CString& version, CString& domain, CString applicationPath = applicationDir + "interface\\interface.exe"; BOOL isApplicationInstalled = PathFileExistsW(applicationPath); BOOL configFileExist = PathFileExistsW(applicationDir + _T("interface\\config.json")); - if (isApplicationInstalled && configFileExist) { + if (configFileExist) { LauncherUtils::ResponseError status = readConfigJSON(version, domain, content, loggedIn); - return status == LauncherUtils::ResponseError::NoError; + return isApplicationInstalled && status == LauncherUtils::ResponseError::NoError; } return FALSE; } @@ -441,10 +444,6 @@ void LauncherManager::onZipExtracted(ZipType type, int size) { downloadApplication(); } else if (type == ZipType::ZipApplication) { createShortcuts(); - CString versionPath; - getAndCreatePaths(LauncherManager::PathType::Launcher_Directory, versionPath); - addToLog(_T("Creating config.json")); - createConfigJSON(); addToLog(_T("Launching application.")); _shouldLaunch = TRUE; if (!_shouldUpdate) { @@ -458,6 +457,8 @@ void LauncherManager::onZipExtracted(ZipType type, int size) { BOOL LauncherManager::extractApplication() { CString installPath; getAndCreatePaths(LauncherManager::PathType::Interface_Directory, installPath); + addToLog(_T("Creating config.json")); + createConfigJSON(); BOOL success = LauncherUtils::unzipFileOnThread(ZipType::ZipApplication, LauncherUtils::cStringToStd(_applicationZipPath), LauncherUtils::cStringToStd(installPath), [&](int type, int size) { if (size > 0) { @@ -477,11 +478,31 @@ BOOL LauncherManager::extractApplication() { void LauncherManager::onFileDownloaded(DownloadType type) { if (type == DownloadType::DownloadContent) { - addToLog(_T("Installing content.")); - installContent(); + addToLog(_T("Deleting content directory before install")); + CString contentDir; + getAndCreatePaths(PathType::Content_Directory, contentDir); + LauncherUtils::deleteDirectoryOnThread(contentDir, [&](bool error) { + if (!error) { + addToLog(_T("Installing content.")); + installContent(); + } else { + addToLog(_T("Error deleting content directory.")); + setFailed(true); + } + }); } else if (type == DownloadType::DownloadApplication) { - addToLog(_T("Installing application.")); - extractApplication(); + addToLog(_T("Deleting application directory before install")); + CString applicationDir; + getAndCreatePaths(PathType::Interface_Directory, applicationDir); + LauncherUtils::deleteDirectoryOnThread(applicationDir, [&](bool error) { + if (!error) { + addToLog(_T("Installing application.")); + extractApplication(); + } else { + addToLog(_T("Error deleting install directory.")); + setFailed(true); + } + }); } } diff --git a/launchers/win32/LauncherUtils.cpp b/launchers/win32/LauncherUtils.cpp index e72720eb55..094ab09307 100644 --- a/launchers/win32/LauncherUtils.cpp +++ b/launchers/win32/LauncherUtils.cpp @@ -480,16 +480,10 @@ DWORD WINAPI LauncherUtils::downloadThread(LPVOID lpParameter) { return 0; } -DWORD WINAPI LauncherUtils::deleteDirectoriesThread(LPVOID lpParameter) { +DWORD WINAPI LauncherUtils::deleteDirectoryThread(LPVOID lpParameter) { DeleteThreadData& data = *((DeleteThreadData*)lpParameter); - DeleteDirError error = DeleteDirError::NoErrorDeleting; - if (!LauncherUtils::deleteFileOrDirectory(data._applicationDir)) { - error = DeleteDirError::ErrorDeletingApplicationDir; - } - if (!LauncherUtils::deleteFileOrDirectory(data._downloadsDir)) { - error = error == NoError ? DeleteDirError::ErrorDeletingDownloadsDir : DeleteDirError::ErrorDeletingBothDirs; - } - data.callback(error); + BOOL success = LauncherUtils::deleteFileOrDirectory(data._dirPath); + data.callback(!success); return 0; } @@ -523,15 +517,12 @@ BOOL LauncherUtils::downloadFileOnThread(int type, const CString& url, const CSt return FALSE; } -BOOL LauncherUtils::deleteDirectoriesOnThread(const CString& applicationDir, - const CString& downloadsDir, - std::function callback) { +BOOL LauncherUtils::deleteDirectoryOnThread(const CString& dirPath, std::function callback) { DWORD myThreadID; DeleteThreadData* deleteThreadData = new DeleteThreadData(); - deleteThreadData->_applicationDir = applicationDir; - deleteThreadData->_downloadsDir = downloadsDir; + deleteThreadData->_dirPath = dirPath; deleteThreadData->setCallback(callback); - HANDLE myHandle = CreateThread(0, 0, deleteDirectoriesThread, deleteThreadData, 0, &myThreadID); + HANDLE myHandle = CreateThread(0, 0, deleteDirectoryThread, deleteThreadData, 0, &myThreadID); if (myHandle) { CloseHandle(myHandle); return TRUE; diff --git a/launchers/win32/LauncherUtils.h b/launchers/win32/LauncherUtils.h index bf1516155a..a373428ad6 100644 --- a/launchers/win32/LauncherUtils.h +++ b/launchers/win32/LauncherUtils.h @@ -30,13 +30,6 @@ public: NoError }; - enum DeleteDirError { - NoErrorDeleting = 0, - ErrorDeletingApplicationDir, - ErrorDeletingDownloadsDir, - ErrorDeletingBothDirs - }; - struct DownloadThreadData { int _type; CString _url; @@ -60,10 +53,9 @@ public: }; struct DeleteThreadData { - CString _applicationDir; - CString _downloadsDir; - std::function callback; - void setCallback(std::function fn) { callback = std::bind(fn, std::placeholders::_1); } + CString _dirPath; + std::function callback; + void setCallback(std::function fn) { callback = std::bind(fn, std::placeholders::_1); } }; struct ProcessData { @@ -91,9 +83,7 @@ public: static BOOL deleteRegistryKey(const CString& registryPath); static BOOL unzipFileOnThread(int type, const std::string& zipFile, const std::string& path, std::function callback); static BOOL downloadFileOnThread(int type, const CString& url, const CString& file, std::function callback); - static BOOL deleteDirectoriesOnThread(const CString& applicationDir, - const CString& downloadsDir, - std::function callback); + static BOOL deleteDirectoryOnThread(const CString& dirPath, std::function callback); static CString urlEncodeString(const CString& url); static HWND executeOnForeground(const CString& path, const CString& params); @@ -101,5 +91,5 @@ private: // Threads static DWORD WINAPI unzipThread(LPVOID lpParameter); static DWORD WINAPI downloadThread(LPVOID lpParameter); - static DWORD WINAPI deleteDirectoriesThread(LPVOID lpParameter); + static DWORD WINAPI deleteDirectoryThread(LPVOID lpParameter); }; \ No newline at end of file