Skip to content

Commit

Permalink
Optimised download queue sorting & bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
KyuubiRan committed Jun 29, 2023
1 parent 0b0f399 commit 33462c9
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 71 deletions.
3 changes: 2 additions & 1 deletion Downloader/res/language/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
}
3 changes: 2 additions & 1 deletion Downloader/res/language/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,6 @@
"VersionCheckFailed": "版本更新检查失败!代码:%d",
"CurrentVersion": "当前版本:%s(%d)",
"GotoDownload": "前往下载",
"CheckUpdate": "检查更新"
"CheckUpdate": "检查更新",
"AlreadyHasDownloadTask": "已存在的下载任务:%d %s-%s [%s]"
}
93 changes: 32 additions & 61 deletions Downloader/src/features/DownloadQueue.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "pch.h"
#include "DownloadQueue.h"

#include <ranges>
#include <set>

#include "Downloader.h"
Expand All @@ -13,7 +12,7 @@ namespace features {
std::set<int> 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;
}
Expand All @@ -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<DownloadTask &>(*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);
}
Expand Down Expand Up @@ -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() {
Expand Down
27 changes: 21 additions & 6 deletions Downloader/src/features/DownloadQueue.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include <set>
#include <shared_mutex>
#include <unordered_map>

#include "osu/Beatmap.h"
#include "Feature.h"
Expand All @@ -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;
Expand All @@ -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<int, DownloadTask> m_InQueueMap;
std::set<DownloadTask> m_TaskQueueSet;

inline static std::shared_mutex m_Mutex{};

Expand All @@ -33,19 +42,25 @@ class DownloadQueue : public Feature {
void cancel(int sid) const;

public:
using TaskIter = std::set<DownloadTask>::iterator;
using TaskIterConst = std::set<DownloadTask>::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);
Expand Down
2 changes: 0 additions & 2 deletions Downloader/src/features/Downloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include <shellapi.h>

#include "DownloadQueue.h"
#include "HandleLinkHook.h"
#include "api/Bancho.h"
#include "api/Sayobot.h"
#include "config/I18nManager.h"
#include "misc/Color.h"
Expand Down
5 changes: 5 additions & 0 deletions Downloader/src/misc/VersionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 7 additions & 0 deletions Downloader/src/misc/VersionManager.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#pragma once

#ifdef _DEBUG
#define SKIP_VERSION_CHECK
#endif

#include <string>

// dlver.h will auto generated by `update_version.py` before build
Expand All @@ -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;
}
};
Expand Down

0 comments on commit 33462c9

Please sign in to comment.