Skip to content

Commit

Permalink
Fixed some bugs & dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouca committed Jun 7, 2024
1 parent 66b6ca3 commit 6c7e0a0
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/MoreLeaderboards/MoreLeaderboards.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include "../includes.h"
#include <Geode/utils/web2.hpp>
#include <Geode/utils/web.hpp>
#include "MoreLeaderboardsListView.h"

#include <iostream>
Expand Down
1 change: 0 additions & 1 deletion src/MoreLeaderboards/MoreLeaderboardsCell.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "MoreLeaderboardsCell.h"
#include <Geode/utils/web.hpp>

void MoreLeaderboardsCell::loadFromDict(cocos2d::CCDictionary* content)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Notifications/EventsPush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "../Settings/CustomSettings.hpp"
#include "Geode/utils/general.hpp"
#include <queue>
#include <Geode/utils/web2.hpp>
#include <Geode/utils/web.hpp>

static std::unordered_map<std::string, web::WebTask> RUNNING_REQUESTS {};

Expand Down
91 changes: 55 additions & 36 deletions src/Utils/DemonList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

// demon list
std::unordered_map<int, int> demonListCache; // Will clear after game exit, or if user deletes level
static std::unordered_map<std::string, web::WebTask> RUNNING_REQUESTS {};

// love url encoded characters :D
// also for some reason this is required on mac because Geode's web requests doesnt automatically append this for some reason.
Expand Down Expand Up @@ -97,43 +98,61 @@ class $modify(LevelInfoLayer) {
}
loading_circle->setParentLayer(this);
loading_circle->show();
web::AsyncWebRequest()
.fetch(fmt::format("https://pointercrate.com/api/v2/demons/listed/?name={}", url_encode(level->m_levelName).c_str()))
.json()
.then([this, level, levelID, loading_circle, positionLabel, demonSpr, winSize](matjson::Value const& json) {
if (loading_circle != nullptr) {
loading_circle->fadeAndRemove();
}
auto scene = CCDirector::sharedDirector()->getRunningScene();
if (json.dump() == "[]") { //idk how to check size, doing .count crashes
log::info("Level not found in pointercrate.");
this->release();
} else {
auto info = json.get<matjson::Value>(0);
auto position = info.get<int>("position");
positionLabel->setString(fmt::format("#{}", position).c_str());
positionLabel->setScale(getScaleBasedPos(position));
positionLabel->setVisible(true);
demonSpr->setVisible(true);
set(levelID, position);
log::info("Level found in Pointercrate! {} at #{}", level->m_levelName.c_str(), position);
this->release();
}
})
.expect([this, loading_circle](std::string const& error) {
if (loading_circle != nullptr) {
loading_circle->fadeAndRemove();
}
log::error("Error while sending a request to Pointercrate: {}", error);
FLAlertLayer::create(nullptr,
"Error",
"Failed to make a request to <cy>Pointercrate</c>. Please either <cg>try again later</c>, look at the error logs to see what might have happened, or report this to the developers.",
"OK",
nullptr,
350.0F
)->show();

const geode::utils::MiniFunction<void(Result<matjson::Value>)> then = [this, level, levelID, loading_circle, positionLabel, demonSpr, winSize](Result<matjson::Value> const& result_json) {
matjson::Value json = result_json.value();

if (loading_circle != nullptr) {
loading_circle->fadeAndRemove();
}
auto scene = CCDirector::sharedDirector()->getRunningScene();
if (json.dump() == "[]") { //idk how to check size, doing .count crashes
log::info("Level not found in pointercrate.");
this->release();
});
} else {
auto info = json.get<matjson::Value>(0);
auto position = info.get<int>("position");
positionLabel->setString(fmt::format("#{}", position).c_str());
positionLabel->setScale(getScaleBasedPos(position));
positionLabel->setVisible(true);
demonSpr->setVisible(true);
set(levelID, position);
log::info("Level found in Pointercrate! {} at #{}", level->m_levelName.c_str(), position);
this->release();
}
};
const geode::utils::MiniFunction<void(std::string const&)> expect = [this, loading_circle](std::string const& error) {
if (loading_circle != nullptr) {
loading_circle->fadeAndRemove();
}
log::error("Error while sending a request to Pointercrate: {}", error);
FLAlertLayer::create(nullptr,
"Error",
"Failed to make a request to <cy>Pointercrate</c>. Please either <cg>try again later</c>, look at the error logs to see what might have happened, or report this to the developers.",
"OK",
nullptr,
350.0F
)->show();
this->release();
};

geode::utils::web::WebRequest request = web::WebRequest();

RUNNING_REQUESTS.emplace(
"@loaderDemonListLevelInfo",
request.get(fmt::format("https://pointercrate.com/api/v2/demons/listed/?name={}", url_encode(level->m_levelName).c_str())).map(
[expect = std::move(expect), then = std::move(then)](web::WebResponse* response) {
if (response->ok()) {
then(response->json());
} else {
expect("Request failed");
}

RUNNING_REQUESTS.erase("@loaderDemonListLevelInfo");
return *response;
}
)
);
}

return true;
Expand Down
35 changes: 33 additions & 2 deletions src/Utils/DownloadManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <Geode/utils/web.hpp>
#include <Geode/utils/file.hpp>

static std::unordered_map<std::string, web::WebTask> RUNNING_REQUESTS {};

// ------
// 204 = MAX
// 0 = MIN
Expand Down Expand Up @@ -77,7 +79,36 @@ void DownloadManager::setup() {

// def not copied from geode hahahaha

web::AsyncWebRequest()
const geode::utils::MiniFunction<void(geode::ByteVector const&)> then = [this](geode::ByteVector const& data) {
// Save the file
geode::utils::file::writeBinary(this->m_sDestination, data);

ProcessLambdas::callMenuHandler(this, this->m_pSelector);
this->onClose(CCNode::create());
};
const geode::utils::MiniFunction<void(int const&)> expect = [this](int const& error) {
FLAlertLayer::create(nullptr, "Error!", "An error occured while trying to send a request to the servers. Check <cy>logs</c> for more information.", "OK", nullptr, 200.0F)->show();
log::error("Error downloading: {}", error);
};

geode::utils::web::WebRequest request = web::WebRequest();
RUNNING_REQUESTS.emplace(
"@loaderDownloadManagerGDUtils",
request.get(this->m_sUrl).map(
[expect = std::move(expect), then = std::move(then)](web::WebResponse* response) {
if (response->ok()) {
then(response->data());
} else {
expect(response->code());
}

RUNNING_REQUESTS.erase("@loaderDownloadManagerGDUtils");
return *response;
}
)
);

/*web::AsyncWebRequest()
.join("bad-apple")
.fetch(this->m_sUrl)
.into(this->m_sDestination)
Expand All @@ -91,7 +122,7 @@ void DownloadManager::setup() {
})
.progress([](auto&, double now, double total) {
progress_func(NULL, total, now, 0.0, 0.0);
});
});*/
}

DownloadManager* DownloadManager::create(const char* url, const char* destination, cocos2d::SEL_MenuHandler selector) {
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <Geode/modify/CCSprite.hpp>
#include <Geode/modify/CCScale9Sprite.hpp>
#include <Geode/loader/Log.hpp>
#include <Geode/utils/web2.hpp>
#include <Geode/utils/web.hpp>
#include "includes.h"
#include "Settings/CustomSettings.hpp"
#include "Notifications/EventsPush.h"
Expand Down

0 comments on commit 6c7e0a0

Please sign in to comment.