Skip to content

Commit

Permalink
Fixed requests making random crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouca committed Jun 20, 2024
1 parent 698c562 commit 3ccbbf0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
26 changes: 16 additions & 10 deletions src/MoreLeaderboards/MoreLeaderboards.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ static int end_count = 0;
static int total_count = 0;
std::string MoreLeaderboards::data_response_moreLB = "";
static std::unordered_map<std::string, web::WebTask> RUNNING_REQUESTS {};
static std::mutex lock_var;

EventListener<web::WebTask> m_listener;

std::vector<std::string> MoreLeaderboards::getWords(std::string s, std::string delim) {
std::vector<std::string> res;
Expand Down Expand Up @@ -418,11 +421,13 @@ void MoreLeaderboards::startLoadingMods() {
fadeLoadingCircle();
};

const std::lock_guard<std::mutex> lock(lock_var);
geode::utils::web::WebRequest request = web::WebRequest();
RUNNING_REQUESTS.emplace(
"@loaderModListCheck",
request.get("https://clarifygdps.com/gdutils/modslist.php").map(
[expect = std::move(expect), then = std::move(then)](web::WebResponse* response) {
const std::lock_guard<std::mutex> lock(lock_var);
if (response->ok()) {
then(response->string().value());
} else {
Expand Down Expand Up @@ -540,6 +545,7 @@ void MoreLeaderboards::startLoadingMore() {

const geode::utils::MiniFunction<void(std::string const&)> expect = [this](std::string const& error) {
loading = false;

auto scene = CCDirector::sharedDirector()->getRunningScene();
auto layer = scene->getChildren()->objectAtIndex(0);
if (layer == nullptr) return this->release();
Expand All @@ -562,6 +568,8 @@ void MoreLeaderboards::startLoadingMore() {

const geode::utils::MiniFunction<void(std::string const&)> then = [this](std::string const& data) {
loading = false;

const std::lock_guard<std::mutex> lock(lock_var);
auto scene = CCDirector::sharedDirector()->getRunningScene();
auto layer = scene->getChildren()->objectAtIndex(0);
if (layer == nullptr) return this->release();
Expand Down Expand Up @@ -606,6 +614,7 @@ void MoreLeaderboards::startLoadingMore() {
this->release();
};

const std::lock_guard<std::mutex> lock(lock_var);
RUNNING_REQUESTS.emplace(
"@loaderMoreLeaderboardCheck",
request.param("type", type).param("page", page).param("country", country_id).param("username", username).get("https://clarifygdps.com/gdutils/moreleaderboards.php").map(
Expand All @@ -624,20 +633,17 @@ void MoreLeaderboards::startLoadingMore() {
};

if (data_region == "") {
RUNNING_REQUESTS.emplace(
"@loaderMoreLeaderboardRegionGet",
request.get("https://clarifygdps.com/gdutils/moreleaderboards_region.php").map(
[expect = std::move(expect), then = std::move(then)](web::WebResponse* response) {
if (response->ok()) {
then(response->string().value());
m_listener.bind([expect = std::move(expect), then = std::move(then)] (web::WebTask::Event* e) {
if (web::WebResponse* res = e->getValue()) {
if (res->ok()) {
then(res->string().value());
} else {
expect("An error occured while sending a request on our server. Please try again later.");
}

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

m_listener.setFilter(request.get("https://clarifygdps.com/gdutils/moreleaderboards_region.php"));
} else {
then(data_region);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Notifications/EventsPush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <Geode/utils/web.hpp>

static std::unordered_map<std::string, web::WebTask> RUNNING_REQUESTS {};
static std::mutex lock_var;
const std::lock_guard<std::mutex> lock(lock_var);

std::vector<std::string> EventsPush::getWords(std::string s, std::string delim) {
std::vector<std::string> res;
Expand Down Expand Up @@ -307,11 +309,13 @@ void EventsPush::onClickBtn(CCObject* ret) {
#endif
std::string const& fields = "secret=Wmfd2893gb7&type=0&str=" + std::to_string(level_id);

const std::lock_guard<std::mutex> lock(lock_var);
geode::utils::web::WebRequest request = web::WebRequest();
RUNNING_REQUESTS.emplace(
"@loaderEventRateNotification",
request.bodyString(fields).post(url).map(
[](web::WebResponse* response) {
const std::lock_guard<std::mutex> lock(lock_var);
if (response->ok()) {
if (response->data().empty()) {
FLAlertLayer::create(nullptr,
Expand Down
4 changes: 3 additions & 1 deletion src/Utils/DemonList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,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 {};
static std::mutex lock_var;

// 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 @@ -137,11 +138,12 @@ class $modify(LevelInfoLayer) {
};

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

const std::lock_guard<std::mutex> lock(lock_var);
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) {
const std::lock_guard<std::mutex> lock(lock_var);
if (response->ok()) {
then(response->json());
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/Utils/DownloadManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <Geode/utils/file.hpp>

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

// ------
// 204 = MAX
Expand Down Expand Up @@ -92,10 +93,12 @@ void DownloadManager::setup() {
};

geode::utils::web::WebRequest request = web::WebRequest();
const std::lock_guard<std::mutex> lock(lock_var);
RUNNING_REQUESTS.emplace(
"@loaderDownloadManagerGDUtils",
request.get(this->m_sUrl).map(
[expect = std::move(expect), then = std::move(then)](web::WebResponse* response) {
const std::lock_guard<std::mutex> lock(lock_var);
if (response->ok()) {
then(response->data());
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <codecvt>

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

int reconnectionDelay = 1000;
int reconnectionDelayMax = 5000;
Expand Down Expand Up @@ -238,10 +239,12 @@ class $modify(CCScheduler) { // GD Protocol part
geode::utils::web::WebRequest request = web::WebRequest();
request.userAgent("");
request.header("Content-Type", "application/x-www-form-urlencoded");
const std::lock_guard<std::mutex> lock(lock_var);
RUNNING_REQUESTS.emplace(
"@loaderLevelProtocolURL",
request.bodyString(fields).post(url).map(
[](web::WebResponse* response) {
const std::lock_guard<std::mutex> lock(lock_var);
if (response->ok()) {
if (response->data().empty()) {
FLAlertLayer::create(nullptr,
Expand Down

0 comments on commit 3ccbbf0

Please sign in to comment.