Skip to content

Commit

Permalink
switch to web::webrequest
Browse files Browse the repository at this point in the history
  • Loading branch information
cdc-sys committed Jun 8, 2024
1 parent 8c28e19 commit db3be6d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 41 deletions.
41 changes: 19 additions & 22 deletions src/ThumbnailPopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,29 @@ bool ThumbnailPopup::setup(int id) {
this->onDownloadFinished(CCSprite::createWithTexture(txtr));
}
else {
this->retain();
std::string URL = fmt::format("https://raw.githubusercontent.com/cdc-sys/level-thumbnails/main/thumbs/{}.png", levelID);
web::AsyncWebRequest()
.fetch(URL)
.bytes()
.then([=, this](geode::ByteVector const& data) {
if (this->fetchFailed == true) {
this->fetched = true;
}
else {
std::thread imageThread = std::thread([data,this](){
auto image = Ref(new CCImage());
image->initWithImageData(const_cast<uint8_t*>(data.data()),data.size());
geode::Loader::get()->queueInMainThread([image,this](){
this->imageCreationFinished(image);
auto req = web::WebRequest();
this->downloadListener.bind([this](web::WebTask::Event* e){
if (auto res = e->getValue()){
if (!res->ok()) {
this->onDownloadFail();
fetchFailed = true;
} else {
auto data = res->data();
std::thread imageThread = std::thread([data,this](){
auto image = Ref(new CCImage());
image->initWithImageData(const_cast<uint8_t*>(data.data()),data.size());
geode::Loader::get()->queueInMainThread([image,this](){
this->imageCreationFinished(image);
});
});
});
this->loadingCircle->fadeAndRemove();
imageThread.detach();
}
}
})
.expect([=, this](std::string const& error) {
this->openDiscordServerPopup();
this->loadingCircle->fadeAndRemove();
this->onDownloadFail();
this->fetchFailed = true;
});
});
auto downloadTask = req.get(URL);
this->downloadListener.setFilter(downloadTask);
}

return true;
Expand Down
3 changes: 3 additions & 0 deletions src/ThumbnailPopup.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#pragma once

#include <Geode/utils/web.hpp>

using namespace geode::prelude;

class ThumbnailPopup : public Popup<int> {
protected:
bool setup(int id) override;
bool fetched = false;
bool fetchFailed = false;
EventListener<web::WebTask> downloadListener;
int levelID;
LoadingCircle* loadingCircle = LoadingCircle::create();
CCTexture2D* texture = nullptr;
Expand Down
55 changes: 36 additions & 19 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Geode/Geode.hpp>
#include <variant>

using namespace geode::prelude;

Expand All @@ -13,7 +14,7 @@ class $modify(MyLevelCell, LevelCell) {
CCSprite* separatorSprite;
CCLabelBMFont* notAvailable;
CCLayerColor* background;
web::AsyncWebRequest downloadRequest;
EventListener<web::WebTask> downloadListener;

bool fetched = false;
bool fetchFailed = false;
Expand Down Expand Up @@ -64,29 +65,45 @@ class $modify(MyLevelCell, LevelCell) {
this->onDownloadFinished(CCSprite::createWithTexture(txtr));
return;
}
this->retain();
std::string URL = fmt::format("https://raw.githubusercontent.com/cdc-sys/level-thumbnails/main/thumbs/{}.png",(int)this->m_level->m_levelID);
web::AsyncWebRequest()
.fetch(URL)
.bytes()
.then([this](geode::ByteVector const& data) {
if (this->m_fields->fetchFailed == true) {
this->m_fields->fetched = true;
} else {
std::thread imageThread = std::thread([data,this](){
auto image = Ref(new CCImage());
image->initWithImageData(const_cast<uint8_t*>(data.data()),data.size());
geode::Loader::get()->queueInMainThread([image,this](){
this->imageCreationFinished(image);

auto downloadProgressText = CCLabelBMFont::create("0%","bigFont.fnt");
downloadProgressText->setPosition({50,50});
this->addChild(downloadProgressText);

auto req = web::WebRequest();
m_fields->downloadListener.bind([this,downloadProgressText](web::WebTask::Event* e){
if (auto res = e->getValue()){
if (!res->ok()) {
this->onDownloadFailed();
this->m_fields->fetchFailed = true;
} else {
downloadProgressText->removeFromParent();
auto data = res->data();
std::thread imageThread = std::thread([data,this](){
auto image = Ref(new CCImage());
image->initWithImageData(const_cast<uint8_t*>(data.data()),data.size());
geode::Loader::get()->queueInMainThread([image,this](){
this->imageCreationFinished(image);
});
});
});
imageThread.detach();
}
} else if (e->isCancelled()){
geode::log::warn("Exited before letting it finish fuck you");
} else if (e->getProgress()){
if (!e->getProgress()->downloadProgress().has_value()){
return;
}
geode::log::info("{}",e->getProgress()->downloadProgress());
downloadProgressText->setString(fmt::format("{}%",round(e->getProgress()->downloadProgress().value())).c_str());
}
})
.expect([this](std::string const& error) {
this->onDownloadFailed();
this->m_fields->fetchFailed = true;
});
auto downloadTask = req.get(URL);
m_fields->downloadListener.setFilter(downloadTask);
}
void destructor(){
geode::log::info("destructor");
}
void imageCreationFinished(CCImage* image){
std::string theKey = fmt::format("thumb-{}",(int)this->m_level->m_levelID);
Expand Down

0 comments on commit db3be6d

Please sign in to comment.