diff --git a/Downloader/res/language/en_us.json b/Downloader/res/language/en_us.json index d89594c..630b9c0 100644 --- a/Downloader/res/language/en_us.json +++ b/Downloader/res/language/en_us.json @@ -108,5 +108,6 @@ "VersionCheckFailed": "Cannot check latest version! code:%d", "CurrentVersion": "Current Version: %s(%d)", "GotoDownload": "Goto Download", - "CheckUpdate": "Check Update" + "CheckUpdate": "Check Update", + "AlreadyHasDownloadTask": "Already has download task: %d %s-%s [%s]" } \ No newline at end of file diff --git a/Downloader/res/language/zh_cn.json b/Downloader/res/language/zh_cn.json index e11e2ab..bb8276e 100644 --- a/Downloader/res/language/zh_cn.json +++ b/Downloader/res/language/zh_cn.json @@ -108,5 +108,6 @@ "VersionCheckFailed": "版本更新检查失败!代码:%d", "CurrentVersion": "当前版本:%s(%d)", "GotoDownload": "前往下载", - "CheckUpdate": "检查更新" + "CheckUpdate": "检查更新", + "AlreadyHasDownloadTask": "已存在的下载任务:%d %s-%s [%s]" } \ No newline at end of file diff --git a/Downloader/src/features/DownloadQueue.cpp b/Downloader/src/features/DownloadQueue.cpp index 90641b3..353f576 100644 --- a/Downloader/src/features/DownloadQueue.cpp +++ b/Downloader/src/features/DownloadQueue.cpp @@ -1,7 +1,6 @@ #include "pch.h" #include "DownloadQueue.h" -#include #include #include "Downloader.h" @@ -13,7 +12,7 @@ namespace features { std::set removed{}; void DownloadQueue::cancel(const int sid) const { - if (!m_InQueueMap.contains(sid)) { + if (const auto iter = findTaskBySid(sid); iter == m_TaskQueueSet.end()) { LOGW("No download task: %d", sid); return; } @@ -24,45 +23,37 @@ void DownloadQueue::cancel(const int sid) const { bool DownloadQueue::addTask(const osu::Beatmap &bm) { std::unique_lock _g(m_Mutex); - if (m_InQueueMap.contains(bm.sid)) { + if (const auto iter = findTaskBySid(bm.sid); iter != m_TaskQueueSet.end()) { LOGW("Already has download task: %d %s-%s (%s)", bm.sid, bm.artist.c_str(), bm.title.c_str(), bm.author.c_str()); return false; } - m_InQueueMap.insert({ - bm.sid, DownloadTask{ - .bm = bm - } + m_TaskQueueSet.insert(DownloadTask{ + .bm = bm, + .insertTime = GetTickCount64(), }); return true; } DownloadTask *DownloadQueue::getTask(const osu::Beatmap &bm) { - std::shared_lock _g(m_Mutex); - if (m_InQueueMap.contains(bm.sid)) { - auto &ret = m_InQueueMap[bm.sid]; - ret.started = true; - return &ret; - } - - return nullptr; + return getTask(bm.sid); } DownloadTask *DownloadQueue::getTask(int sid) { - std::shared_lock _g(m_Mutex); - if (m_InQueueMap.contains(sid)) { - auto &ret = m_InQueueMap[sid]; - ret.started = true; - return &ret; + std::unique_lock _g(m_Mutex); + if (const auto iter = findTaskBySid(sid); iter != m_TaskQueueSet.end()) { + auto *ret = &const_cast(*iter); + ret->started = true; + return ret; } return nullptr; } -void DownloadQueue::notifyFinished(int sid) { +void DownloadQueue::notifyFinished(const int sid) { std::unique_lock _g(m_Mutex); - if (m_InQueueMap.contains(sid)) { - m_InQueueMap.erase(sid); + if (const auto iter = findTaskBySid(sid); iter != m_TaskQueueSet.end()) { + m_TaskQueueSet.erase(iter); } Downloader::RemoveCancelDownload(sid); } @@ -98,48 +89,28 @@ void DownloadQueue::drawTaskItem(const DownloadTask &item) const { void DownloadQueue::drawMain() { auto &lang = i18n::I18nManager::GetInstance(); - std::shared_lock _g(m_Mutex); + { + std::shared_lock _g(m_Mutex); - if (m_InQueueMap.empty()) { - ImGui::SetCursorPosX((ImGui::GetWindowSize().x - ImGui::CalcTextSize(lang.getTextCStr("Empty")).x) * 0.5f); - ImGui::SetCursorPosY((ImGui::GetWindowSize().y - ImGui::CalcTextSize(lang.getTextCStr("Empty")).y) * 0.5f); - ImGui::Text(lang.getTextCStr("Empty")); - return; - } + if (m_TaskQueueSet.empty()) { + ImGui::SetCursorPosX((ImGui::GetWindowSize().x - ImGui::CalcTextSize(lang.getTextCStr("Empty")).x) * 0.5f); + ImGui::SetCursorPosY((ImGui::GetWindowSize().y - ImGui::CalcTextSize(lang.getTextCStr("Empty")).y) * 0.5f); + ImGui::Text(lang.getTextCStr("Empty")); + return; + } - for (auto &item : m_InQueueMap | std::views::values) { - drawTaskItem(item); + for (auto &item : m_TaskQueueSet) { + drawTaskItem(item); + } } - - for (auto &sid : removed) { - m_InQueueMap.erase(sid); + + { + std::unique_lock _g(m_Mutex); + for (auto &sid : removed) { + m_TaskQueueSet.erase(findTaskBySid(sid)); + } + removed.clear(); } - - /* - static auto example = DownloadTask{ - osu::Beatmap{ - .title = "Test title", - .artist = "Test artist", - .author = "Test mapper", - .bid = {123456, 789546}, - .sid = 655352 - }, - 4069000, 8192000 - }; - static auto example2 = DownloadTask{ - osu::Beatmap{ - .title = "Test title", - .artist = "Test artist", - .author = "Test mapper", - .bid = {123456, 789546}, - .sid = 655351 - }, - 0, 0 - }; - - drawTaskItem(example); - drawTaskItem(example2); - */ } FeatureInfo &DownloadQueue::getInfo() { diff --git a/Downloader/src/features/DownloadQueue.h b/Downloader/src/features/DownloadQueue.h index 6fb2268..3a645fb 100644 --- a/Downloader/src/features/DownloadQueue.h +++ b/Downloader/src/features/DownloadQueue.h @@ -1,6 +1,6 @@ #pragma once +#include #include -#include #include "osu/Beatmap.h" #include "Feature.h" @@ -11,6 +11,7 @@ struct DownloadTask { double dlSize{}; double totalSize{}; bool started = false; + uint64_t insertTime = UINT64_MAX; [[nodiscard]] double getProgress() const { return totalSize == 0 ? 0 : dlSize / totalSize; @@ -19,12 +20,20 @@ struct DownloadTask { [[nodiscard]] bool prepared() const { return dlSize != 0 && totalSize != 0; } + + bool operator==(const int sid) const { + return bm.sid == sid; + } + + bool operator<(const DownloadTask &dl) const { + return insertTime < dl.insertTime; + } }; class DownloadQueue : public Feature { DownloadQueue(); - std::unordered_map m_InQueueMap; + std::set m_TaskQueueSet; inline static std::shared_mutex m_Mutex{}; @@ -33,19 +42,25 @@ class DownloadQueue : public Feature { void cancel(int sid) const; public: + using TaskIter = std::set::iterator; + using TaskIterConst = std::set::const_iterator; + static DownloadQueue &GetInstance() { static DownloadQueue instance; return instance; } bool hasDownloadMapSet(const int id) const { - std::shared_lock _g(m_Mutex); - return m_InQueueMap.contains(id); + return std::ranges::any_of(m_TaskQueueSet.begin(), m_TaskQueueSet.end(), + [id](const DownloadTask &task) { return task.bm.sid == id; }); } bool hasDownloadMap(const osu::Beatmap &bm) const { - std::shared_lock _g(m_Mutex); - return m_InQueueMap.contains(bm.sid); + return hasDownloadMapSet(bm.sid); + } + + TaskIter findTaskBySid(const int id) const { + return std::ranges::find_if(m_TaskQueueSet, [id](const DownloadTask &task) { return task.bm.sid == id; }); } bool addTask(const osu::Beatmap &bm); diff --git a/Downloader/src/features/Downloader.cpp b/Downloader/src/features/Downloader.cpp index 2ca2c47..40ce50f 100644 --- a/Downloader/src/features/Downloader.cpp +++ b/Downloader/src/features/Downloader.cpp @@ -5,8 +5,6 @@ #include #include "DownloadQueue.h" -#include "HandleLinkHook.h" -#include "api/Bancho.h" #include "api/Sayobot.h" #include "config/I18nManager.h" #include "misc/Color.h" diff --git a/Downloader/src/misc/VersionManager.cpp b/Downloader/src/misc/VersionManager.cpp index 2445fa8..fd014a2 100644 --- a/Downloader/src/misc/VersionManager.cpp +++ b/Downloader/src/misc/VersionManager.cpp @@ -5,6 +5,11 @@ #include "utils/gui_utils.h" void misc::VersionManager::CheckUpdate(const bool force) { +#ifdef SKIP_VERSION_CHECK + LOGI("Skipped version check!"); + GuiHelper::ShowInfoToast("Skipped version check!"); +#endif + static bool checked = false; static bool inCheck = false; diff --git a/Downloader/src/misc/VersionManager.h b/Downloader/src/misc/VersionManager.h index 09a0b58..9e856ef 100644 --- a/Downloader/src/misc/VersionManager.h +++ b/Downloader/src/misc/VersionManager.h @@ -1,5 +1,9 @@ #pragma once +#ifdef _DEBUG +#define SKIP_VERSION_CHECK +#endif + #include // dlver.h will auto generated by `update_version.py` before build @@ -22,6 +26,9 @@ class VersionManager { static std::string_view GetCurrentVersionName() { return DOWNLOADER_VERSION_STR; } static bool IsLatest() { +#ifdef SKIP_VERSION_CHECK + return true; +#endif return GetCurrentVersionCode() >= s_LatestVersionCode; } };