From 74526663a5c68019d1463835c65f2576372e82c6 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Wed, 16 Oct 2019 22:31:10 -0700 Subject: [PATCH] Cleanup qt launcher paths, and add --version flag --- launchers/qt/src/CommandlineOptions.cpp | 2 - launchers/qt/src/Helper.cpp | 8 +- .../qt/src/LauncherInstaller_windows.cpp | 54 ++++++------- launchers/qt/src/LauncherState.cpp | 77 ++++++------------- launchers/qt/src/LauncherState.h | 6 -- launchers/qt/src/PathUtils.cpp | 55 +++++++++++++ launchers/qt/src/PathUtils.h | 16 +++- launchers/qt/src/main.cpp | 14 +++- 8 files changed, 137 insertions(+), 95 deletions(-) diff --git a/launchers/qt/src/CommandlineOptions.cpp b/launchers/qt/src/CommandlineOptions.cpp index fa60e0d236..e8b1ad0a12 100644 --- a/launchers/qt/src/CommandlineOptions.cpp +++ b/launchers/qt/src/CommandlineOptions.cpp @@ -20,14 +20,12 @@ void CommandlineOptions::parse(const int argc, char** argv) { for (int index = 1; index < argc; index++) { std::string option = argv[index]; if (isCommandlineOption(option)) { - qDebug() << "adding commandline option: " << QString::fromStdString(option); _commandlineOptions.push_back(option); } } } void CommandlineOptions::append(const std::string& command) { - qDebug() << "appending option: " << QString::fromStdString(command); _commandlineOptions.push_back(command); } diff --git a/launchers/qt/src/Helper.cpp b/launchers/qt/src/Helper.cpp index 42f76fe0fa..43f633bb57 100644 --- a/launchers/qt/src/Helper.cpp +++ b/launchers/qt/src/Helper.cpp @@ -1,5 +1,7 @@ #include "Helper.h" +#include "PathUtils.h" + #include #include #include @@ -47,9 +49,9 @@ void messageHandler(QtMsgType type, const QMessageLogContext& context, const QSt break; } - QDir launcherDirectory = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); - launcherDirectory.mkpath(launcherDirectory.absolutePath()); - QString filename = launcherDirectory.absoluteFilePath("Log.txt"); + QDir logsDir = PathUtils::getLogsDirectory(); + logsDir.mkpath(logsDir.absolutePath()); + QString filename = logsDir.absoluteFilePath("Log.txt"); QFile outFile(filename); outFile.open(QIODevice::WriteOnly | QIODevice::Append); diff --git a/launchers/qt/src/LauncherInstaller_windows.cpp b/launchers/qt/src/LauncherInstaller_windows.cpp index 3423815d7c..9be8c6cc1b 100644 --- a/launchers/qt/src/LauncherInstaller_windows.cpp +++ b/launchers/qt/src/LauncherInstaller_windows.cpp @@ -2,6 +2,7 @@ #include "CommandlineOptions.h" #include "Helper.h" +#include "PathUtils.h" #include #include @@ -15,10 +16,11 @@ #include LauncherInstaller::LauncherInstaller(const QString& applicationFilePath) { - _launcherInstallDir = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation); - _launcherApplicationsDir = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation) + "/Launcher"; + _launcherInstallDir = PathUtils::getLauncherDirectory(); + _launcherApplicationsDir = PathUtils::getApplicationsDirectory(); qDebug() << "Launcher install dir: " << _launcherInstallDir.absolutePath(); qDebug() << "Launcher Application dir: " << _launcherApplicationsDir.absolutePath(); + _launcherInstallDir.mkpath(_launcherInstallDir.absolutePath()); _launcherApplicationsDir.mkpath(_launcherApplicationsDir.absolutePath()); QFileInfo fileInfo(applicationFilePath); @@ -36,7 +38,7 @@ void LauncherInstaller::install() { if (runningOutsideOfInstallDir()) { qDebug() << "Installing HQ Launcher...."; uninstallOldLauncher(); - QString oldLauncherPath = _launcherInstallDir.absolutePath() + "/HQ Launcher.exe"; + QString oldLauncherPath = PathUtils::getLauncherFilePath(); if (QFile::exists(oldLauncherPath)) { bool didRemove = QFile::remove(oldLauncherPath); @@ -59,15 +61,15 @@ void LauncherInstaller::install() { } void LauncherInstaller::createShortcuts() { - QString launcherPath = _launcherInstallDir.absolutePath() + "/HQ Launcher.exe"; + QString launcherPath = PathUtils::getLauncherFilePath(); - QString uninstallLinkPath = _launcherInstallDir.absolutePath() + "/Uninstall HQ.lnk"; - QString desktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); - QString applicationPath = _launcherApplicationsDir.absolutePath(); + QString uninstallLinkPath = _launcherInstallDir.absoluteFilePath("Uninstall HQ.lnk"); - QString appStartLinkPath = applicationPath + "/HQ Launcher.lnk"; - QString uninstallAppStartLinkPath = applicationPath + "/Uninstall HQ.lnk"; - QString desktopAppLinkPath = desktopPath + "/HQ Launcher.lnk"; + QDir desktopDir = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); + + QString appStartLinkPath = _launcherApplicationsDir.absoluteFilePath("HQ Launcher.lnk"); + QString uninstallAppStartLinkPath = _launcherApplicationsDir.absoluteFilePath("Uninstall HQ.lnk"); + QString desktopAppLinkPath = desktopDir.absoluteFilePath("HQ Launcher.lnk"); createSymbolicLink((LPCSTR)launcherPath.toStdString().c_str(), (LPCSTR)uninstallLinkPath.toStdString().c_str(), @@ -122,7 +124,7 @@ void LauncherInstaller::uninstall() { } return; } - QString launcherPath = _launcherInstallDir.absolutePath() + "/HQ Launcher.exe"; + QString launcherPath = _launcherInstallDir.absoluteFilePath("HQ Launcher.exe"); if (QFile::exists(launcherPath)) { bool removed = QFile::remove(launcherPath); qDebug() << "Successfully removed " << launcherPath << ": " << removed; @@ -131,58 +133,58 @@ void LauncherInstaller::uninstall() { } void LauncherInstaller::deleteShortcuts() { - QString desktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); + QDir desktopDir = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); QString applicationPath = _launcherApplicationsDir.absolutePath(); - QString uninstallLinkPath = _launcherInstallDir.absolutePath() + "/Uninstall HQ.lnk"; + QString uninstallLinkPath = _launcherInstallDir.absoluteFilePath("Uninstall HQ.lnk"); if (QFile::exists(uninstallLinkPath)) { QFile::remove(uninstallLinkPath); } - QString appStartLinkPath = applicationPath + "/HQ Launcher.lnk"; + QString appStartLinkPath = _launcherApplicationsDir.absoluteFilePath("HQ Launcher.lnk"); if (QFile::exists(appStartLinkPath)) { QFile::remove(appStartLinkPath); } - QString uninstallAppStartLinkPath = applicationPath + "/Uninstall HQ.lnk"; + QString uninstallAppStartLinkPath = _launcherApplicationsDir.absoluteFilePath("Uninstall HQ.lnk"); if (QFile::exists(uninstallAppStartLinkPath)) { QFile::remove(uninstallAppStartLinkPath); } - QString desktopAppLinkPath = desktopPath + "/HQ Launcher.lnk"; + QString desktopAppLinkPath = desktopDir.absoluteFilePath("HQ Launcher.lnk"); if (QFile::exists(desktopAppLinkPath)) { QFile::remove(desktopAppLinkPath); } } void LauncherInstaller::uninstallOldLauncher() { - QDir localAppPath = QStandardPaths::standardLocations(QStandardPaths::AppLocalDataLocation).value(0) + "/../../HQ"; - QDir startAppPath = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation).value(0) + "/HQ"; - QString desktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); + QDir localAppDir = QStandardPaths::standardLocations(QStandardPaths::AppLocalDataLocation).value(0) + "/../../HQ"; + QDir startAppDir = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation).value(0) + "/HQ"; + QDir desktopDir = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); - qDebug() << localAppPath.absolutePath(); - qDebug() << startAppPath.absolutePath(); - QString desktopAppLinkPath = desktopPath + "/HQ Launcher.lnk"; + qDebug() << localAppDir.absolutePath(); + qDebug() << startAppDir.absolutePath(); + QString desktopAppLinkPath = desktopDir.absoluteFilePath("HQ Launcher.lnk"); if (QFile::exists(desktopAppLinkPath)) { QFile::remove(desktopAppLinkPath); } - QString uninstallLinkPath = localAppPath.absolutePath() + "/Uninstall HQ.lnk"; + QString uninstallLinkPath = localAppDir.absoluteFilePath("Uninstall HQ.lnk"); if (QFile::exists(uninstallLinkPath)) { QFile::remove(uninstallLinkPath); } - QString applicationPath = localAppPath.absolutePath() + "/HQ Launcher.exe"; + QString applicationPath = localAppDir.absoluteFilePath("HQ Launcher.exe"); if (QFile::exists(applicationPath)) { QFile::remove(applicationPath); } - QString appStartLinkPath = startAppPath.absolutePath() + "/HQ Launcher.lnk"; + QString appStartLinkPath = startAppDir.absoluteFilePath("HQ Launcher.lnk"); if (QFile::exists(appStartLinkPath)) { QFile::remove(appStartLinkPath); } - QString uninstallAppStartLinkPath = startAppPath.absolutePath() + "/Uninstall HQ.lnk"; + QString uninstallAppStartLinkPath = startAppDir.absoluteFilePath("Uninstall HQ.lnk"); if (QFile::exists(uninstallAppStartLinkPath)) { QFile::remove(uninstallAppStartLinkPath); } diff --git a/launchers/qt/src/LauncherState.cpp b/launchers/qt/src/LauncherState.cpp index 763718a08e..f8299c0ef4 100644 --- a/launchers/qt/src/LauncherState.cpp +++ b/launchers/qt/src/LauncherState.cpp @@ -23,7 +23,6 @@ #include -#include #include #include @@ -75,38 +74,8 @@ void LauncherState::gotoPreviousDebugScreen() { } } -QString LauncherState::getContentCachePath() const { - return _launcherDirectory.filePath("cache"); -} - -QString LauncherState::getClientDirectory() const { - return _launcherDirectory.filePath("interface_install"); -} - -QString LauncherState::getClientExecutablePath() const { - QDir clientDirectory = getClientDirectory(); -#if defined(Q_OS_WIN) - return clientDirectory.absoluteFilePath("interface.exe"); -#elif defined(Q_OS_MACOS) - return clientDirectory.absoluteFilePath("interface.app/Contents/MacOS/interface"); -#endif -} - -QString LauncherState::getConfigFilePath() const { - QDir clientDirectory = getClientDirectory(); - return clientDirectory.absoluteFilePath("config.json"); -} - -QString LauncherState::getLauncherFilePath() const { -#if defined(Q_OS_WIN) - return _launcherDirectory.absoluteFilePath("launcher.exe"); -#elif defined(Q_OS_MACOS) - return getBundlePath() + "/Contents/MacOS/HQ Launcher"; -#endif -} - bool LauncherState::shouldDownloadContentCache() const { - return !_contentCacheURL.isEmpty() && !QFile::exists(getContentCachePath()); + return !_contentCacheURL.isEmpty() && !QFile::exists(PathUtils::getContentCachePath()); } void LauncherState::setLastSignupErrorMessage(const QString& msg) { @@ -146,10 +115,10 @@ void LauncherState::ASSERT_STATE(const std::vector& states) { } LauncherState::LauncherState() { - _launcherDirectory = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); - // TODO Fix launcher directory + _launcherDirectory = PathUtils::getLauncherDirectory(); qDebug() << "Launcher directory: " << _launcherDirectory.absolutePath(); _launcherDirectory.mkpath(_launcherDirectory.absolutePath()); + _launcherDirectory.mkpath(PathUtils::getDownloadDirectory().absolutePath()); requestBuilds(); } @@ -255,7 +224,7 @@ void LauncherState::getCurrentClientVersion() { connect(&client, QOverload::of(&QProcess::finished), &loop, &QEventLoop::exit, Qt::QueuedConnection); connect(&client, &QProcess::errorOccurred, &loop, &QEventLoop::exit, Qt::QueuedConnection); - client.start(getClientExecutablePath(), { "--version" }); + client.start(PathUtils::getClientExecutablePath(), { "--version" }); loop.exec(); // TODO Handle errors @@ -273,14 +242,14 @@ void LauncherState::getCurrentClientVersion() { qDebug() << "Current client version is: " << _currentClientVersion; { - auto path = getConfigFilePath(); + auto path = PathUtils::getConfigFilePath(); QFile configFile{ path }; if (configFile.open(QIODevice::ReadOnly)) { QJsonDocument doc = QJsonDocument::fromJson(configFile.readAll()); auto root = doc.object(); - _config.launcherPath = getLauncherFilePath(); + _config.launcherPath = PathUtils::getLauncherFilePath(); _config.loggedIn = false; if (root.contains(configLoggedInKey)) { _config.loggedIn = root[configLoggedInKey].toBool(); @@ -509,7 +478,8 @@ void LauncherState::downloadClient() { auto request = new QNetworkRequest(QUrl(build.installerZipURL)); auto reply = _networkAccessManager.get(*request); - _clientZipFile.setFileName(_launcherDirectory.absoluteFilePath("client.zip")); + QDir downloadDir{ PathUtils::getDownloadDirectory() }; + _clientZipFile.setFileName(downloadDir.absoluteFilePath("client.zip")); qDebug() << "Opening " << _clientZipFile.fileName(); if (!_clientZipFile.open(QIODevice::WriteOnly)) { @@ -566,15 +536,18 @@ void LauncherState::installClient() { ASSERT_STATE(ApplicationState::DownloadingClient); setApplicationState(ApplicationState::InstallingClient); - _launcherDirectory.rmpath("interface_install"); - _launcherDirectory.mkpath("interface_install"); - auto installDir = _launcherDirectory.absoluteFilePath("interface_install"); + + auto clientDir = PathUtils::getClientDirectory(); + + auto clientPath = clientDir.absolutePath(); + _launcherDirectory.rmpath(clientPath); + _launcherDirectory.mkpath(clientPath); _interfaceInstallProgress = 0; - qDebug() << "Unzipping " << _clientZipFile.fileName() << " to " << installDir; + qDebug() << "Unzipping " << _clientZipFile.fileName() << " to " << clientDir.absolutePath(); - auto unzipper = new Unzipper(_clientZipFile.fileName(), QDir(installDir)); + auto unzipper = new Unzipper(_clientZipFile.fileName(), clientDir); unzipper->setAutoDelete(true); connect(unzipper, &Unzipper::progress, this, [this](float progress) { _interfaceInstallProgress = progress; @@ -666,7 +639,8 @@ void LauncherState::downloadContentCache() { request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true); auto reply = _networkAccessManager.get(request); - _contentZipFile.setFileName(_launcherDirectory.absoluteFilePath("content_cache.zip")); + QDir downloadDir{ PathUtils::getDownloadDirectory() }; + _contentZipFile.setFileName(downloadDir.absoluteFilePath("content_cache.zip")); qDebug() << "Opening " << _contentZipFile.fileName(); if (!_contentZipFile.open(QIODevice::WriteOnly)) { @@ -713,7 +687,7 @@ void LauncherState::installContentCache() { ASSERT_STATE(ApplicationState::DownloadingContentCache); setApplicationState(ApplicationState::InstallingContentCache); - auto installDir = getContentCachePath(); + auto installDir = PathUtils::getContentCachePath(); qDebug() << "Unzipping " << _contentZipFile.fileName() << " to " << installDir; @@ -750,15 +724,10 @@ void LauncherState::launchClient() { setApplicationState(ApplicationState::LaunchingHighFidelity); - QDir installDirectory = _launcherDirectory.filePath("interface_install"); - QString clientPath; -#if defined(Q_OS_WIN) - clientPath = installDirectory.absoluteFilePath("interface.exe"); -#elif defined(Q_OS_MACOS) - clientPath = installDirectory.absoluteFilePath("interface.app/Contents/MacOS/interface"); -#endif + QDir installDirectory = PathUtils::getClientDirectory(); + QString clientPath = PathUtils::getClientExecutablePath(); - auto path = getConfigFilePath(); + auto path = PathUtils::getConfigFilePath(); QFile configFile{ path }; if (configFile.open(QIODevice::ReadWrite | QIODevice::Truncate)) { QJsonDocument doc = QJsonDocument::fromJson(configFile.readAll()); @@ -766,7 +735,7 @@ void LauncherState::launchClient() { { configHomeLocationKey, _config.homeLocation }, { configLastLoginKey, _config.lastLogin }, { configLoggedInKey, _config.loggedIn }, - { configLauncherPathKey, getLauncherFilePath() }, + { configLauncherPathKey, PathUtils::getLauncherFilePath() }, }); qint64 result = configFile.write(doc.toJson()); configFile.close(); diff --git a/launchers/qt/src/LauncherState.h b/launchers/qt/src/LauncherState.h index 11a8fbaaaf..39c4141b81 100644 --- a/launchers/qt/src/LauncherState.h +++ b/launchers/qt/src/LauncherState.h @@ -155,12 +155,6 @@ private: bool shouldDownloadContentCache() const; void getCurrentClientVersion(); - QString getContentCachePath() const; - QString getClientDirectory() const; - QString getClientExecutablePath() const; - QString getConfigFilePath() const; - QString getLauncherFilePath() const; - float calculateDownloadProgress() const; bool shouldDownloadLauncher(); diff --git a/launchers/qt/src/PathUtils.cpp b/launchers/qt/src/PathUtils.cpp index eba74721cb..0debda9d24 100644 --- a/launchers/qt/src/PathUtils.cpp +++ b/launchers/qt/src/PathUtils.cpp @@ -1,6 +1,8 @@ #include "PathUtils.h" +#include #include +#include QUrl PathUtils::resourcePath(const QString& source) { QString filePath = RESOURCE_PREFIX_URL + source; @@ -18,3 +20,56 @@ QString PathUtils::fontPath(const QString& fontName) { return ":/fonts/" + fontName; #endif } + +QDir PathUtils::getLauncherDirectory() { + return QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation); +} + +QDir PathUtils::getApplicationsDirectory() { + return QDir(QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation)).absoluteFilePath("Launcher"); +} + +// The client directory is where interface is installed to. +QDir PathUtils::getClientDirectory() { + return getLauncherDirectory().filePath("client"); +} + +QDir PathUtils::getLogsDirectory() { + return getLauncherDirectory().filePath("logs"); +} + +// The download directory is used to store files downloaded during installation. +QDir PathUtils::getDownloadDirectory() { + return getLauncherDirectory().filePath("downloads"); +} + +// The content cache path is the directory interface uses for caching data. +// It is pre-populated on startup with domain content. +QString PathUtils::getContentCachePath() { + return getLauncherDirectory().filePath("contentcache"); +} + +// The path to the interface binary. +QString PathUtils::getClientExecutablePath() { + QDir clientDirectory = getClientDirectory(); +#if defined(Q_OS_WIN) + return clientDirectory.absoluteFilePath("interface.exe"); +#elif defined(Q_OS_MACOS) + return clientDirectory.absoluteFilePath("interface.app/Contents/MacOS/interface"); +#endif +} + +// The path to the config.json file that the launcher uses to store information like +// the last user that logged in. +QString PathUtils::getConfigFilePath() { + return getClientDirectory().absoluteFilePath("config.json"); +} + +// The path to the launcher binary. +QString PathUtils::getLauncherFilePath() { +#if defined(Q_OS_WIN) + return getLauncherDirectory().absoluteFilePath("HQ Launcher.exe"); +#elif defined(Q_OS_MACOS) + return getBundlePath() + "/Contents/MacOS/HQ Launcher"; +#endif +} diff --git a/launchers/qt/src/PathUtils.h b/launchers/qt/src/PathUtils.h index c33041736b..d9f5279408 100644 --- a/launchers/qt/src/PathUtils.h +++ b/launchers/qt/src/PathUtils.h @@ -1,14 +1,28 @@ #pragma once +#include #include #include #include #include + class PathUtils : public QObject { Q_OBJECT public: PathUtils() = default; ~PathUtils() = default; Q_INVOKABLE static QUrl resourcePath(const QString& source); - QString static fontPath(const QString& fontName); + + static QString fontPath(const QString& fontName); + + static QDir getLauncherDirectory(); + static QDir getApplicationsDirectory(); + static QDir getDownloadDirectory(); + static QDir getClientDirectory(); + static QDir getLogsDirectory(); + + static QString getContentCachePath(); + static QString getClientExecutablePath(); + static QString getConfigFilePath(); + static QString getLauncherFilePath(); }; diff --git a/launchers/qt/src/main.cpp b/launchers/qt/src/main.cpp index 4e0e1b7437..33351dfbdf 100644 --- a/launchers/qt/src/main.cpp +++ b/launchers/qt/src/main.cpp @@ -30,14 +30,15 @@ bool hasSuffix(const std::string& path, const std::string& suffix) { } int main(int argc, char *argv[]) { - QString name { "High Fidelity" }; QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); - QCoreApplication::setOrganizationName(name); + QCoreApplication::setOrganizationName("High Fidelity"); QCoreApplication::setApplicationName("Launcher"); + Q_INIT_RESOURCE(resources); cleanLogFile(); qInstallMessageHandler(messageHandler); bool didUpdate = false; + #ifdef Q_OS_MAC if (isLauncherAlreadyRunning()) { return 0; @@ -50,8 +51,15 @@ int main(int argc, char *argv[]) { } } #endif + CommandlineOptions* options = CommandlineOptions::getInstance(); options->parse(argc, argv); + + if (options->contains("--version")) { + std::cout << LAUNCHER_BUILD_VERSION << std::endl; + return 0; + } + #ifdef Q_OS_WIN LauncherInstaller launcherInstaller(argv[0]); if (options->contains("--uninstall") || options->contains("--resumeUninstall")) { @@ -65,7 +73,6 @@ int main(int argc, char *argv[]) { if (isProcessRunning("interface.exe", interfacePID)) { shutdownProcess(interfacePID, 0); } - #endif QProcessEnvironment processEnvironment = QProcessEnvironment::systemEnvironment(); @@ -74,6 +81,7 @@ int main(int argc, char *argv[]) { options->append("--noUpdate"); } } + Launcher launcher(argc, argv); return launcher.exec(); }