Skip to content

Commit

Permalink
Fix issue when exists beatmap & auto start download
Browse files Browse the repository at this point in the history
  • Loading branch information
KyuubiRan committed Jun 26, 2023
1 parent 9e856b7 commit 1103450
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 31 deletions.
4 changes: 2 additions & 2 deletions Downloader/src/api/Bancho.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

api::Bancho::Bancho() : Provider("Bancho", "https://github.com/ppy/osu-api/wiki", features::downloader::DownloadMirror::OsuOfficial) {}

std::optional<osu::Beatmap> api::Bancho::SearchBeatmap(const features::downloader::BeatmapInfo &info) const {
std::optional<osu::Beatmap> api::Bancho::searchBeatmap(const features::downloader::BeatmapInfo &info) const {
auto &dl = features::Downloader::GetInstance();

auto un = dl.f_OsuAccount->username();
Expand Down Expand Up @@ -91,7 +91,7 @@ std::optional<osu::Beatmap> api::Bancho::SearchBeatmap(const features::downloade
return {};
}

bool api::Bancho::DownloadBeatmap(const osu::Beatmap &bm) const {
bool api::Bancho::downloadBeatmap(const osu::Beatmap &bm) const {
auto &dl = features::Downloader::GetInstance();

auto un = dl.f_OsuAccount->username();
Expand Down
4 changes: 2 additions & 2 deletions Downloader/src/api/Bancho.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Bancho : public Provider {
Bancho();

// Inherited via Provider
std::optional<osu::Beatmap> SearchBeatmap(const features::downloader::BeatmapInfo &) const override;
bool DownloadBeatmap(const osu::Beatmap &) const override;
std::optional<osu::Beatmap> searchBeatmap(const features::downloader::BeatmapInfo &) const override;
bool downloadBeatmap(const osu::Beatmap &) const override;
};
}
4 changes: 2 additions & 2 deletions Downloader/src/api/Chimu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int32_t api::Chimu::_convertToSetId(const features::downloader::BeatmapInfo &inf

api::Chimu::Chimu() : Provider("Chimu", "https://chimu.moe/docs", features::downloader::DownloadMirror::Chimu) {}

std::optional<osu::Beatmap> api::Chimu::SearchBeatmap(const features::downloader::BeatmapInfo &info) const {
std::optional<osu::Beatmap> api::Chimu::searchBeatmap(const features::downloader::BeatmapInfo &info) const {
int32_t id = _convertToSetId(info);
if (id == -1) return {};

Expand All @@ -57,7 +57,7 @@ std::optional<osu::Beatmap> api::Chimu::SearchBeatmap(const features::downloader
return mapset.to_beatmap();
}

bool api::Chimu::DownloadBeatmap(const osu::Beatmap &bm) const {
bool api::Chimu::downloadBeatmap(const osu::Beatmap &bm) const {
auto &dl = features::Downloader::GetInstance();

const auto s = std::format("https://api.chimu.moe/v1/download/{0}", bm.sid);
Expand Down
4 changes: 2 additions & 2 deletions Downloader/src/api/Chimu.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Chimu : public Provider {
Chimu();

// Inherited via Provider
std::optional<osu::Beatmap> SearchBeatmap(const features::downloader::BeatmapInfo &) const override;
bool DownloadBeatmap(const osu::Beatmap &) const override;
std::optional<osu::Beatmap> searchBeatmap(const features::downloader::BeatmapInfo &) const override;
bool downloadBeatmap(const osu::Beatmap &) const override;
};
}; // namespace api
18 changes: 10 additions & 8 deletions Downloader/src/api/Provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,33 @@

namespace api {

Provider::Provider(std::string name, std::string doc, features::downloader::DownloadMirror mirror) : _name(name), _doc(doc), _enum(mirror) {}
Provider::Provider(std::string name, std::string doc, features::downloader::DownloadMirror mirror) :
_name(std::move(name)), _doc(std::move(doc)), _enum(mirror) {
}

std::string Provider::GetDoc() const { return _doc; }
std::string Provider::GetName() const { return _name; }
std::string_view Provider::getDoc() const { return _doc; }
std::string_view Provider::getName() const { return _name; }

features::downloader::DownloadMirror Provider::GetEnum() const { return _enum; }

Provider const *Provider::GetRegisteredByName(std::string name) {
auto vec = Provider::GetRegistered();
return *std::find_if(vec.begin(), vec.end(), [&name](auto p) { return p->GetName() == name; });
return *std::ranges::find_if(vec, [&name](auto p) { return p->getName() == name; });

This comment has been minimized.

Copy link
@EnergoStalin

EnergoStalin Jun 26, 2023

Contributor

really useful 💯

}

Provider const *Provider::GetRegisteredByEnum(features::downloader::DownloadMirror mirror) {
auto vec = Provider::GetRegistered();
return *std::find_if(vec.begin(), vec.end(), [&mirror](auto p) { return p->GetEnum() == mirror; });
auto &vec = GetRegistered();
return *std::ranges::find_if(vec, [&mirror](auto p) { return p->GetEnum() == mirror; });
}

std::vector<const Provider *> Provider::_providers;

void Provider::Register(const Provider *provider) { _providers.push_back(provider); }

void Provider::UnRegisterAll() {
for (auto it : Provider::_providers) {
for (const auto it : _providers) {
delete it;
}
}

}; // namespace api
}; // namespace api
13 changes: 7 additions & 6 deletions Downloader/src/api/Provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@ class Provider {
features::downloader::DownloadMirror _enum;

public:
virtual ~Provider() = default;
Provider(std::string, std::string, features::downloader::DownloadMirror);

std::string GetDoc() const;
std::string GetName() const;
std::string_view getDoc() const;

This comment has been minimized.

Copy link
@EnergoStalin

EnergoStalin Jun 26, 2023

Contributor

silly me 🤦🏻

This comment has been minimized.

Copy link
@KyuubiRan

KyuubiRan Jun 26, 2023

Author Owner

its okay but Downloader.cpp L127 makes SearchThread broken XD

This comment has been minimized.

Copy link
@EnergoStalin

EnergoStalin Jun 26, 2023

Contributor

oh that's was break for case. didn't notice that XD

This comment has been minimized.

Copy link
@KyuubiRan

KyuubiRan Jun 26, 2023

Author Owner

yes XD

std::string_view getName() const;

features::downloader::DownloadMirror GetEnum() const;

virtual std::optional<osu::Beatmap> SearchBeatmap(const features::downloader::BeatmapInfo &) const = 0;
virtual bool DownloadBeatmap(const osu::Beatmap &) const = 0;
virtual std::optional<osu::Beatmap> searchBeatmap(const features::downloader::BeatmapInfo &) const = 0;
virtual bool downloadBeatmap(const osu::Beatmap &) const = 0;

static const std::vector<const Provider *> &GetRegistered() { return Provider::_providers; }
static const std::vector<const Provider *> &GetRegistered() { return _providers; }
static Provider const *GetRegisteredByName(std::string);
static Provider const *GetRegisteredByEnum(features::downloader::DownloadMirror);
static void Register(const Provider *provider);

static void UnRegisterAll();
};

}; // namespace api
}; // namespace api
4 changes: 2 additions & 2 deletions Downloader/src/api/Sayobot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void nlohmann::adl_serializer<api::sayobot::BidData>::from_json(const nlohmann::
api::Sayobot::Sayobot() :
Provider("Sayobot", "https://www.showdoc.com.cn/SoulDee?page_id=3969517351482508", features::downloader::DownloadMirror::Sayobot) {}

std::optional<osu::Beatmap> api::Sayobot::SearchBeatmap(const features::downloader::BeatmapInfo &info) const {
std::optional<osu::Beatmap> api::Sayobot::searchBeatmap(const features::downloader::BeatmapInfo &info) const {
const auto s = std::format("https://api.sayobot.cn/v2/beatmapinfo?K={0}&T={1}", info.id, (uint8_t)info.type);
int resCode = -1;
std::string response;
Expand Down Expand Up @@ -88,7 +88,7 @@ std::optional<osu::Beatmap> api::Sayobot::SearchBeatmap(const features::download
return {};
}

bool api::Sayobot::DownloadBeatmap(const osu::Beatmap &bm) const {
bool api::Sayobot::downloadBeatmap(const osu::Beatmap &bm) const {
auto &dl = features::Downloader::GetInstance();

const auto s = std::format("https://txy1.sayobot.cn/beatmaps/download/{0}/{1}?server=auto",
Expand Down
4 changes: 2 additions & 2 deletions Downloader/src/api/Sayobot.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class Sayobot : public Provider {
Sayobot();

// Inherited via Provider
virtual std::optional<osu::Beatmap> SearchBeatmap(const features::downloader::BeatmapInfo &) const override;
virtual bool DownloadBeatmap(const osu::Beatmap &) const override;
virtual std::optional<osu::Beatmap> searchBeatmap(const features::downloader::BeatmapInfo &) const override;
virtual bool downloadBeatmap(const osu::Beatmap &) const override;
};
};

Expand Down
10 changes: 5 additions & 5 deletions Downloader/src/features/Downloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ features::Downloader::Downloader() :
}

auto provider = api::Provider::GetRegisteredByEnum(inst.f_Mirror.getValue());
success = provider->DownloadBeatmap(bm);
success = provider->downloadBeatmap(bm);
}

DownloadQueue::GetInstance().notifyFinished(bm.sid);
Expand Down Expand Up @@ -118,18 +118,18 @@ features::Downloader::Downloader() :
auto info = inst.m_SearchQueue.pop_front();
LOGD("Poped search: %d", info.id);

auto provider = api::Provider::GetRegisteredByEnum(inst.f_Mirror.getValue());
if (auto ret = provider->SearchBeatmap(info); ret.has_value()) {
const auto provider = api::Provider::GetRegisteredByEnum(inst.f_Mirror.getValue());
if (auto ret = provider->searchBeatmap(info); ret.has_value()) {
auto &bm = *ret;
if (osu::BeatmapManager::GetInstance().hasBeatmap(bm) && info.directDownload) {
LOGI("Already has beatmapsets: %d, skipped direct download.", bm.sid);
GuiHelper::ShowInfoToast(lang.getTextCStr("ExistsBeatmapSkipAutoDownload"), bm.sid);
break;
continue;
}

info.directDownload ? inst.postDownload(bm) : ui::search::result::ShowSearchInfo(bm);
} else {
LOGW("No such map found on %s. ID=%d, Type=%s", provider->GetName().c_str(), info.id,
LOGW("No such map found on %s. ID=%d, Type=%s", provider->getName().data(), info.id,
info.type == downloader::BeatmapType::Sid ? "beatmapsets" : "beatmapid");
GuiHelper::ShowWarnToast(lang.getTextCStr("SearchFailed"), info.type == downloader::BeatmapType::Sid ? "sid" : "bid", info.id);
}
Expand Down

0 comments on commit 1103450

Please sign in to comment.