From 62a402d05aa2b4722201506794fd20e95b05845c Mon Sep 17 00:00:00 2001 From: Rachel Powers <508861+Ryex@users.noreply.github.com> Date: Sat, 6 May 2023 19:10:58 -0700 Subject: [PATCH] refactor: move functions to utils + code-review fixes Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com> --- launcher/Application.cpp | 8 +- launcher/MMCTime.cpp | 64 ++++++++++ launcher/MMCTime.h | 9 ++ launcher/StringUtils.cpp | 67 ++++++++++ launcher/StringUtils.h | 12 ++ .../atlauncher/ATLPackInstallTask.cpp | 2 +- launcher/net/Download.cpp | 119 +++--------------- launcher/net/NetAction.h | 36 ++---- launcher/qtlogging.ini | 2 + launcher/tasks/ConcurrentTask.h | 21 ++-- 10 files changed, 194 insertions(+), 146 deletions(-) diff --git a/launcher/Application.cpp b/launcher/Application.cpp index 28c2a9ce..1659eb44 100644 --- a/launcher/Application.cpp +++ b/launcher/Application.cpp @@ -423,16 +423,16 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv) foundLoggingRules = QFile::exists(logRulesPath); // search the dataPath() - + // seach app data standard path if(!foundLoggingRules && !isPortable() && dirParam.isEmpty()) { - logRulesPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, logRulesFile); + logRulesPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, FS::PathCombine("..", logRulesFile)); if(!logRulesPath.isEmpty()) { qDebug() << "Found" << logRulesPath << "..."; foundLoggingRules = true; } } - - if(!QFile::exists(logRulesPath)) { + // seach root path + if(!foundLoggingRules) { logRulesPath = FS::PathCombine(m_rootPath, logRulesFile); qDebug() << "Testing" << logRulesPath << "..."; foundLoggingRules = QFile::exists(logRulesPath); diff --git a/launcher/MMCTime.cpp b/launcher/MMCTime.cpp index 70bc4135..1702ec06 100644 --- a/launcher/MMCTime.cpp +++ b/launcher/MMCTime.cpp @@ -18,6 +18,8 @@ #include #include +#include +#include QString Time::prettifyDuration(int64_t duration) { int seconds = (int) (duration % 60); @@ -36,3 +38,65 @@ QString Time::prettifyDuration(int64_t duration) { } return QObject::tr("%1d %2h %3min").arg(days).arg(hours).arg(minutes); } + +QString Time::humanReadableDuration(double duration, int precision) { + + using days = std::chrono::duration>; + + QString outStr; + QTextStream os(&outStr); + + bool neg = false; + if (duration < 0) { + neg = true; // flag + duration *= -1; // invert + } + + auto std_duration = std::chrono::duration(duration); + auto d = std::chrono::duration_cast(std_duration); + std_duration -= d; + auto h = std::chrono::duration_cast(std_duration); + std_duration -= h; + auto m = std::chrono::duration_cast(std_duration); + std_duration -= m; + auto s = std::chrono::duration_cast(std_duration); + std_duration -= s; + auto ms = std::chrono::duration_cast(std_duration); + + auto dc = d.count(); + auto hc = h.count(); + auto mc = m.count(); + auto sc = s.count(); + auto msc = ms.count(); + + if (neg) { + os << '-'; + } + if (dc) { + os << dc << QObject::tr("days"); + } + if (hc) { + if (dc) + os << " "; + os << qSetFieldWidth(2) << hc << QObject::tr("h"); // hours + } + if (mc) { + if (dc || hc) + os << " "; + os << qSetFieldWidth(2) << mc << QObject::tr("m"); // minutes + } + if (dc || hc || mc || sc) { + if (dc || hc || mc) + os << " "; + os << qSetFieldWidth(2) << sc << QObject::tr("s"); // seconds + } + if ((msc && (precision > 0)) || !(dc || hc || mc || sc)) { + if (dc || hc || mc || sc) + os << " "; + os << qSetFieldWidth(0) << qSetRealNumberPrecision(precision) << msc << QObject::tr("ms"); // miliseconds + } + + os.flush(); + + return outStr; +} \ No newline at end of file diff --git a/launcher/MMCTime.h b/launcher/MMCTime.h index 10ff2ffe..576b837f 100644 --- a/launcher/MMCTime.h +++ b/launcher/MMCTime.h @@ -22,4 +22,13 @@ namespace Time { QString prettifyDuration(int64_t duration); +/** + * @brief Returns a string with short form time duration ie. `2days 1h3m4s56.0ms` + * miliseconds are only included if `percison` is greater than 0 + * + * @param duration a number of seconds as floating point + * @param precision number of decmial points to display on fractons of a second, defualts to 0. + * @return QString + */ +QString humanReadableDuration(double duration, int precision = 0); } diff --git a/launcher/StringUtils.cpp b/launcher/StringUtils.cpp index 0f3c3669..f677ebf6 100644 --- a/launcher/StringUtils.cpp +++ b/launcher/StringUtils.cpp @@ -1,5 +1,8 @@ #include "StringUtils.h" +#include +#include + /// If you're wondering where these came from exactly, then know you're not the only one =D /// TAKEN FROM Qt, because it doesn't expose it intelligently @@ -74,3 +77,67 @@ int StringUtils::naturalCompare(const QString& s1, const QString& s2, Qt::CaseSe // The two strings are the same (02 == 2) so fall back to the normal sort return QString::compare(s1, s2, cs); } + +/// Truncate a url while keeping its readability py placing the `...` in the middle of the path +QString StringUtils::truncateUrlHumanFriendly(QUrl &url, int max_len, bool hard_limit) +{ + auto display_options = QUrl::RemoveUserInfo | QUrl::RemoveFragment | QUrl::NormalizePathSegments; + auto str_url = url.toDisplayString(display_options); + + if (str_url.length() <= max_len) + return str_url; + + auto url_path_parts = url.path().split('/'); + QString last_path_segment = url_path_parts.takeLast(); + + if (url_path_parts.size() >= 1 && url_path_parts.first().isEmpty()) + url_path_parts.removeFirst(); // drop empty first segment (from leading / ) + + if (url_path_parts.size() >= 1) + url_path_parts.removeLast(); // drop the next to last path segment + + auto url_template = QStringLiteral("%1://%2/%3%4"); + + auto url_compact = url_path_parts.isEmpty() + ? url_template.arg(url.scheme(), url.host(), QStringList({ "...", last_path_segment }).join('/'), url.query()) + : url_template.arg(url.scheme(), url.host(), + QStringList({ url_path_parts.join('/'), "...", last_path_segment }).join('/'), url.query()); + + // remove url parts one by one if it's still too long + while (url_compact.length() > max_len && url_path_parts.size() >= 1) { + url_path_parts.removeLast(); // drop the next to last path segment + url_compact = url_path_parts.isEmpty() + ? url_template.arg(url.scheme(), url.host(), QStringList({ "...", last_path_segment }).join('/'), url.query()) + : url_template.arg(url.scheme(), url.host(), + QStringList({ url_path_parts.join('/'), "...", last_path_segment }).join('/'), url.query()); + } + + if ((url_compact.length() >= max_len) && hard_limit) { + // still too long, truncate normaly + url_compact = QString(str_url); + auto to_remove = url_compact.length() - max_len + 3; + url_compact.remove(url_compact.length() - to_remove - 1, to_remove); + url_compact.append("..."); + } + + return url_compact; + +} + +static const QStringList s_units_si {"KB", "MB", "GB", "TB"}; +static const QStringList s_units_kibi {"KiB", "MiB", "Gib", "TiB"}; + +QString StringUtils::humanReadableFileSize(double bytes, bool use_si, int decimal_points) { + const QStringList units = use_si ? s_units_si : s_units_kibi; + const int scale = use_si ? 1000 : 1024; + + int u = -1; + double r = pow(10, decimal_points); + + do { + bytes /= scale; + u++; + } while (round(abs(bytes) * r) / r >= scale && u < units.length() - 1); + + return QString::number(bytes, 'f', 2) + " " + units[u]; +} \ No newline at end of file diff --git a/launcher/StringUtils.h b/launcher/StringUtils.h index 1799605b..271e5099 100644 --- a/launcher/StringUtils.h +++ b/launcher/StringUtils.h @@ -1,6 +1,7 @@ #pragma once #include +#include namespace StringUtils { @@ -29,4 +30,15 @@ inline QString fromStdString(string s) #endif int naturalCompare(const QString& s1, const QString& s2, Qt::CaseSensitivity cs); + +/** + * @brief Truncate a url while keeping its readability py placing the `...` in the middle of the path + * @param url Url to truncate + * @param max_len max lenght of url in charaters + * @param hard_limit if truncating the path can't get the url short enough, truncate it normaly. + */ +QString truncateUrlHumanFriendly(QUrl &url, int max_len, bool hard_limit = false); + +QString humanReadableFileSize(double bytes, bool use_si = false, int decimal_points = 1); + } // namespace StringUtils diff --git a/launcher/modplatform/atlauncher/ATLPackInstallTask.cpp b/launcher/modplatform/atlauncher/ATLPackInstallTask.cpp index d130914f..96cea7b7 100644 --- a/launcher/modplatform/atlauncher/ATLPackInstallTask.cpp +++ b/launcher/modplatform/atlauncher/ATLPackInstallTask.cpp @@ -846,7 +846,7 @@ void PackInstallTask::downloadMods() emitFailed(reason); }); connect(jobPtr.get(), &NetJob::progress, [&](qint64 current, qint64 total) - { + { setDetails(tr("%1 out of %2 complete").arg(current).arg(total)); abortable = true; setProgress(current, total); diff --git a/launcher/net/Download.cpp b/launcher/net/Download.cpp index 082f963d..7617b5a0 100644 --- a/launcher/net/Download.cpp +++ b/launcher/net/Download.cpp @@ -37,7 +37,6 @@ */ #include "Download.h" -#include #include #include @@ -45,107 +44,19 @@ #include "ByteArraySink.h" #include "ChecksumValidator.h" -#include "FileSystem.h" #include "MetaCacheSink.h" -#include "BuildConfig.h" #include "Application.h" +#include "BuildConfig.h" #include "net/Logging.h" #include "net/NetAction.h" +#include "MMCTime.h" +#include "StringUtils.h" + namespace Net { -QString truncateUrlHumanFriendly(QUrl &url, int max_len, bool hard_limit = false) -{ - auto display_options = QUrl::RemoveUserInfo | QUrl::RemoveFragment | QUrl::NormalizePathSegments; - auto str_url = url.toDisplayString(display_options); - if (str_url.length() <= max_len) - return str_url; - - /* this is a PCRE regular expression that splits a URL (given by the display rules above) into 5 capture groups - * the scheme (ie https://) is group 1 - * the host (with trailing /) is group 2 - * the first part of the path (with trailing /) is group 3 - * the last part of the path (with leading /) is group 5 - * the remainder of the URL is in the .* and in group 4 - * - * See: https://regex101.com/r/inHkek/1 - * for an interactive breakdown - */ - QRegularExpression re(R"(^([\w]+:\/\/)([\w._-]+\/)([\w._-]+\/)(.*)(\/[^]+[^]+)$)"); - - auto url_compact = QString(str_url); - url_compact.replace(re, "\\1\\2\\3...\\5"); - if (url_compact.length() >= max_len) { - url_compact = QString(str_url); - url_compact.replace(re, "\\1\\2...\\5"); - } - - - if ((url_compact.length() >= max_len) && hard_limit) { - auto to_remove = url_compact.length() - max_len + 3; - url_compact.remove(url_compact.length() - to_remove - 1, to_remove); - url_compact.append("..."); - } - - return url_compact; - -} - -QString humanReadableDuration(double duration, int precision = 0) { - - using days = std::chrono::duration>; - - QString outStr; - QTextStream os(&outStr); - - auto std_duration = std::chrono::duration(duration); - auto d = std::chrono::duration_cast(std_duration); - std_duration -= d; - auto h = std::chrono::duration_cast(std_duration); - std_duration -= h; - auto m = std::chrono::duration_cast(std_duration); - std_duration -= m; - auto s = std::chrono::duration_cast(std_duration); - std_duration -= s; - auto ms = std::chrono::duration_cast(std_duration); - - auto dc = d.count(); - auto hc = h.count(); - auto mc = m.count(); - auto sc = s.count(); - auto msc = ms.count(); - - if (dc) { - os << dc << "days"; - } - if (hc) { - if (dc) - os << " "; - os << qSetFieldWidth(2) << hc << "h"; - } - if (mc) { - if (dc || hc) - os << " "; - os << qSetFieldWidth(2) << mc << "m"; - } - if (dc || hc || mc || sc) { - if (dc || hc || mc) - os << " "; - os << qSetFieldWidth(2) << sc << "s"; - } - if ((msc && (precision > 0)) || !(dc || hc || mc || sc)) { - if (dc || hc || mc || sc) - os << " "; - os << qSetFieldWidth(0) << qSetRealNumberPrecision(precision) << msc << "ms"; - } - - os.flush(); - - return outStr; -} - auto Download::makeCached(QUrl url, MetaEntryPtr entry, Options options) -> Download::Ptr { auto dl = makeShared(); @@ -185,7 +96,7 @@ void Download::addValidator(Validator* v) void Download::executeTask() { - setStatus(tr("Downloading %1").arg(truncateUrlHumanFriendly(m_url, 100))); + setStatus(tr("Downloading %1").arg(StringUtils::truncateUrlHumanFriendly(m_url, 80))); if (getState() == Task::State::AbortedByUser) { qCWarning(taskDownloadLogC) << getUid().toString() << "Attempt to start an aborted Download:" << m_url.toString(); @@ -222,12 +133,12 @@ void Download::executeTask() if (!token.isNull()) request.setRawHeader("Authorization", token.toUtf8()); } - + m_last_progress_time = m_clock.now(); m_last_progress_bytes = 0; QNetworkReply* rep = m_network->get(request); - + m_reply.reset(rep); connect(rep, &QNetworkReply::downloadProgress, this, &Download::downloadProgress); connect(rep, &QNetworkReply::finished, this, &Download::downloadFinished); @@ -252,16 +163,19 @@ void Download::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) auto remaing_time_s = (bytesTotal - bytesReceived) / dl_speed_bps; //: Current amount of bytes downloaded, out of the total amount of bytes in the download - QString dl_progress = tr("%1 / %2").arg(humanReadableFileSize(bytesReceived)).arg(humanReadableFileSize(bytesTotal)); - + QString dl_progress = + tr("%1 / %2").arg(StringUtils::humanReadableFileSize(bytesReceived)).arg(StringUtils::humanReadableFileSize(bytesTotal)); + QString dl_speed_str; if (elapsed_ms.count() > 0) { + auto str_eta = bytesTotal > 0 ? Time::humanReadableDuration(remaing_time_s) : tr("unknown"); //: Download speed, in bytes per second (remaining download time in parenthesis) - dl_speed_str = tr("%1 /s (%2)").arg(humanReadableFileSize(dl_speed_bps)).arg(humanReadableDuration(remaing_time_s)); + dl_speed_str = + tr("%1 /s (%2)").arg(StringUtils::humanReadableFileSize(dl_speed_bps)).arg(str_eta); } else { - //: Download speed at 0 bytes per second + //: Download speed at 0 bytes per second dl_speed_str = tr("0 B/s"); - } + } setDetails(dl_progress + "\n" + dl_speed_str); @@ -290,7 +204,8 @@ void Download::sslErrors(const QList& errors) { int i = 1; for (auto error : errors) { - qCCritical(taskDownloadLogC) << getUid().toString() << "Download" << m_url.toString() << "SSL Error #" << i << " : " << error.errorString(); + qCCritical(taskDownloadLogC) << getUid().toString() << "Download" << m_url.toString() << "SSL Error #" << i << " : " + << error.errorString(); auto cert = error.certificate(); qCCritical(taskDownloadLogC) << getUid().toString() << "Certificate in question:\n" << cert.toText(); i++; diff --git a/launcher/net/NetAction.h b/launcher/net/NetAction.h index a13d115f..32095041 100644 --- a/launcher/net/NetAction.h +++ b/launcher/net/NetAction.h @@ -36,38 +36,18 @@ #pragma once -#include - #include #include #include "QObjectPtr.h" #include "tasks/Task.h" -static const QStringList s_units_si {"kB", "MB", "GB", "TB"}; -static const QStringList s_units_kibi {"KiB", "MiB", "Gib", "TiB"}; - -inline QString humanReadableFileSize(double bytes, bool use_si = false, int decimal_points = 1) { - const QStringList units = use_si ? s_units_si : s_units_kibi; - const int scale = use_si ? 1000 : 1024; - - int u = -1; - double r = pow(10, decimal_points); - - do { - bytes /= scale; - u++; - } while (round(abs(bytes) * r) / r >= scale && u < units.length() - 1); - - return QString::number(bytes, 'f', 2) + " " + units[u]; -} - class NetAction : public Task { Q_OBJECT -protected: - explicit NetAction() : Task() {}; + protected: + explicit NetAction() : Task(){}; -public: + public: using Ptr = shared_qobject_ptr; virtual ~NetAction() = default; @@ -76,23 +56,23 @@ public: void setNetwork(shared_qobject_ptr network) { m_network = network; } -protected slots: + protected slots: virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) = 0; virtual void downloadError(QNetworkReply::NetworkError error) = 0; virtual void downloadFinished() = 0; virtual void downloadReadyRead() = 0; -public slots: + public slots: void startAction(shared_qobject_ptr network) { m_network = network; executeTask(); } -protected: - void executeTask() override {}; + protected: + void executeTask() override{}; -public: + public: shared_qobject_ptr m_network; /// the network reply diff --git a/launcher/qtlogging.ini b/launcher/qtlogging.ini index 4543378f..27f4e676 100644 --- a/launcher/qtlogging.ini +++ b/launcher/qtlogging.ini @@ -3,6 +3,8 @@ # prevent log spam and strange bugs # qt.qpa.drawing in particular causes theme artifacts on MacOS qt.*.debug=false +# dont log credentials buy defualt +launcher.auth.credentials.debug=false # remove the debug lines, other log levels still get through launcher.task.net.download.debug=false # enable or disable whole catageries diff --git a/launcher/tasks/ConcurrentTask.h b/launcher/tasks/ConcurrentTask.h index aec707bc..6325fc9e 100644 --- a/launcher/tasks/ConcurrentTask.h +++ b/launcher/tasks/ConcurrentTask.h @@ -35,17 +35,17 @@ */ #pragma once -#include #include #include #include +#include #include #include "tasks/Task.h" class ConcurrentTask : public Task { Q_OBJECT -public: + public: using Ptr = shared_qobject_ptr; explicit ConcurrentTask(QObject* parent = nullptr, QString task_name = "", int max_concurrent = 6); @@ -58,7 +58,7 @@ public: void addTask(Task::Ptr task); -public slots: + public slots: bool abort() override; /** Resets the internal state of the task. @@ -66,20 +66,19 @@ public slots: */ void clear(); -protected -slots: + protected slots: void executeTask() override; virtual void startNext(); void subTaskSucceeded(Task::Ptr); - void subTaskFailed(Task::Ptr, const QString &msg); - void subTaskStatus(Task::Ptr task, const QString &msg); - void subTaskDetails(Task::Ptr task, const QString &msg); + void subTaskFailed(Task::Ptr, const QString& msg); + void subTaskStatus(Task::Ptr task, const QString& msg); + void subTaskDetails(Task::Ptr task, const QString& msg); void subTaskProgress(Task::Ptr task, qint64 current, qint64 total); void subTaskStepProgress(Task::Ptr task, TaskStepProgress const& task_step_progress); -protected: + protected: // NOTE: This is not thread-safe. [[nodiscard]] unsigned int totalSize() const { return m_queue.size() + m_doing.size() + m_done.size(); } @@ -88,13 +87,13 @@ protected: virtual void updateState(); -protected: + protected: QString m_name; QString m_step_status; QQueue m_queue; - QHash m_doing; + QHash m_doing; QHash m_done; QHash m_failed; QHash m_succeeded;