Skip to content

Commit

Permalink
Damn...
Browse files Browse the repository at this point in the history
  • Loading branch information
hiimjustin000 committed Nov 26, 2024
1 parent 6a7448f commit 18dff33
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64")
set(CMAKE_CXX_VISIBILITY_PRESET hidden)

project(DemonsInBetween VERSION 1.4.4)
project(DemonsInBetween VERSION 1.4.5)

add_library(${PROJECT_NAME} SHARED
src/classes/DIBInfoPopup.cpp
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Demons In Between Changelog
## v1.4.5 (2024-11-24)
- Fixed some crashes related to exiting menus too quickly
- Fixed difficulties disappearing on failed requests

## v1.4.4 (2024-11-24)
- Fixed constant reloading of the demon difficulties

Expand Down
2 changes: 1 addition & 1 deletion mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"win": "2.2074",
"mac": "2.2074"
},
"version": "v1.4.4",
"version": "v1.4.5",
"id": "hiimjustin000.demons_in_between",
"name": "Demons In Between",
"developer": "hiimjustin000",
Expand Down
21 changes: 15 additions & 6 deletions src/DemonsInBetween.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,40 @@ GJFeatureState DemonsInBetween::stateForLevel(GJGameLevel* level) {
return state;
}

void DemonsInBetween::loadDemonForLevel(EventListener<web::WebTask>&& listenerRef, int levelID, bool main, std::function<void(LadderDemon&)> const& callback) {
void DemonsInBetween::loadDemonForLevel(
EventListener<web::WebTask>&& listenerRef, int levelID, bool main,
std::function<void(LadderDemon&)> const& callback, std::function<void()> const& preCallback
) {
if (LEVELS_LOADED.contains(levelID)) return;

auto&& listener = std::move(listenerRef);
listener.bind([callback, levelID, main](web::WebTask::Event* e) {
listener.bind([callback, levelID, main, preCallback](web::WebTask::Event* e) {
if (auto res = e->getValue()) {
LEVELS_LOADED.insert(levelID);

if (!res->ok()) return;

auto json = res->json().unwrapOr(matjson::Value());
if (json["Rating"].isNull()) return;
if (!json.contains("Rating") || json["Rating"].isNull()) return;

auto rating = round(json["Rating"].asDouble().unwrapOr(0.0) * 100) / 100;
auto difficulty = DemonsInBetween::DIFFICULTY_INDICES[(int)round(rating)];
auto enjoyment = !json["Enjoyment"].isNull() ? round(json["Enjoyment"].asDouble().unwrapOr(-999.0) * 100) / 100 : -999.0;
auto enjoyment = !json.contains("Enjoyment") || json["Enjoyment"].isNull() ? round(json["Enjoyment"].asDouble().unwrapOr(-999.0) * 100) / 100 : -999.0;

auto& gddl = main ? GDDL_MAIN : GDDL;
gddl.push_back({ levelID, rating, enjoyment, difficulty });
preCallback();
queueInMainThread([callback, levelID, main] { callback(demonForLevel(levelID, main)); });
}
});

listener.setFilter(web::WebRequest().get(fmt::format("https://gdladder.com/api/level/{}", levelID)));
}

void DemonsInBetween::searchObjectForPage(EventListener<web::WebTask>&& listenerRef, int page, bool refresh, std::function<void(GJSearchObject*)> const& callback) {
void DemonsInBetween::searchObjectForPage(
EventListener<web::WebTask>&& listenerRef, int page, bool refresh,
std::function<void(GJSearchObject*)> const& callback, std::function<void()> const& preCallback
) {
auto glm = GameLevelManager::get();
auto searchCache = static_cast<CCDictionary*>(glm->getUserObject("search-cache"_spr));
if (!searchCache) {
Expand All @@ -84,14 +91,15 @@ void DemonsInBetween::searchObjectForPage(EventListener<web::WebTask>&& listener
if (!refresh && searchCache->objectForKey(searchKey)) {
SEARCH_SIZE = SEARCH_SIZES[DIFFICULTY];
MAX_PAGE = (SEARCH_SIZE - 1) / 10;
preCallback();
callback(static_cast<GJSearchObject*>(searchCache->objectForKey(searchKey)));
return;
}

auto tierBound = TIER_BOUNDS[DIFFICULTY];

auto&& listener = std::move(listenerRef);
listener.bind([callback, page](web::WebTask::Event* e) {
listener.bind([callback, page, preCallback](web::WebTask::Event* e) {
if (auto res = e->getValue()) {
if (!res->ok()) return;

Expand All @@ -107,6 +115,7 @@ void DemonsInBetween::searchObjectForPage(EventListener<web::WebTask>&& listener
}
}

preCallback();
queueInMainThread([callback, levels, page] {
auto searchObject = GJSearchObject::create(SearchType::MapPackOnClick, string::join(levels, ","));
if (auto searchCache = static_cast<CCDictionary*>(GameLevelManager::get()->getUserObject("search-cache"_spr)))
Expand Down
10 changes: 8 additions & 2 deletions src/DemonsInBetween.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class DemonsInBetween {
static cocos2d::CCSprite* spriteForDifficulty(GJDifficultySprite*, int, GJDifficultyName, GJFeatureState);
static int difficultyForDemonDifficulty(int);
static GJFeatureState stateForLevel(GJGameLevel*);
static void loadDemonForLevel(geode::EventListener<geode::utils::web::WebTask>&&, int, bool, std::function<void(LadderDemon&)> const&);
static void searchObjectForPage(geode::EventListener<geode::utils::web::WebTask>&&, int, bool, std::function<void(GJSearchObject*)> const&);
static void loadDemonForLevel(
geode::EventListener<geode::utils::web::WebTask>&&, int, bool,
std::function<void(LadderDemon&)> const&, std::function<void()> const&
);
static void searchObjectForPage(
geode::EventListener<geode::utils::web::WebTask>&&, int, bool,
std::function<void(GJSearchObject*)> const&, std::function<void()> const&
);
};
6 changes: 5 additions & 1 deletion src/classes/DIBInfoPopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ bool DIBInfoPopup::setup() {
CACHED_DEMONS.push_back(demon);
}

queueInMainThread([this] { setupDemonInfo(); });
retain();
queueInMainThread([this] {
setupDemonInfo();
release();
});
}
});

Expand Down
6 changes: 5 additions & 1 deletion src/classes/DIBSearchPopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,15 @@ bool DIBSearchPopup::setup() {

for (int i = 1; i < 21; i++) {
table->addButton(CCMenuItemExt::createSpriteExtraWithFrameName(fmt::format("DIB_{:02d}_btn2_001.png"_spr, i).c_str(), 1.0f, [this, i](auto) {
if (m_isBusy) return;
m_isBusy = true;
DemonsInBetween::DIFFICULTY = i;
DemonsInBetween::SEARCHING = true;
DemonsInBetween::searchObjectForPage(std::move(m_listener), 0, false, [this](GJSearchObject* obj) {
CCDirector::get()->pushScene(CCTransitionFade::create(0.5f, LevelBrowserLayer::scene(obj)));
});
m_isBusy = false;
release();
}, [this] { retain(); });
}));
}

Expand Down
1 change: 1 addition & 0 deletions src/classes/DIBSearchPopup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class TableNode : public cocos2d::CCNode {
class DIBSearchPopup : public geode::Popup<> {
protected:
geode::EventListener<geode::utils::web::WebTask> m_listener;
bool m_isBusy;

bool setup() override;
public:
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/LevelBrowserLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class $modify(DIBLevelBrowserLayer, LevelBrowserLayer) {
DemonsInBetween::searchObjectForPage(std::move(f->m_listener), f->m_currentPage, refresh, [this](GJSearchObject* obj) {
m_fields->m_loadingPage = false;
loadPage(obj);
});
release();
}, [this] { retain(); });
}

void updatePageButtons() {
Expand Down
7 changes: 7 additions & 0 deletions src/hooks/LevelCell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class $modify(DIBLevelCell, LevelCell) {

DemonsInBetween::loadDemonForLevel(std::move(m_fields->m_listener), levelID, false, [this, difficultyContainer, difficultySprite](LadderDemon& demon) {
createUI(demon, difficultyContainer, difficultySprite);
release();
difficultyContainer->release();
difficultySprite->release();
}, [this, difficultyContainer, difficultySprite] {
retain();
difficultyContainer->retain();
difficultySprite->retain();
});
}

Expand Down
3 changes: 2 additions & 1 deletion src/hooks/LevelInfoLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class $modify(DIBLevelInfoLayer, LevelInfoLayer) {

DemonsInBetween::loadDemonForLevel(std::move(m_fields->m_listener), levelID, false, [this, createDemon](LadderDemon& demon) {
createUI(demon, createDemon);
});
release();
}, [this] { retain(); });

return true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/LevelSelectLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ class $modify(DIBLevelSelectLayer, LevelSelectLayer) {

auto ladderDemon1 = DemonsInBetween::demonForLevel(14, true);
if (ladderDemon1.id == 0 || ladderDemon1.difficulty == 0)
DemonsInBetween::loadDemonForLevel(std::move(f->m_listener1), 1, true, [this](LadderDemon& demon) { demon.id = 14; });
DemonsInBetween::loadDemonForLevel(std::move(f->m_listener1), 1, true, [this](LadderDemon& demon) { demon.id = 14; }, [this] {});

auto ladderDemon2 = DemonsInBetween::demonForLevel(18, true);
if (ladderDemon2.id == 0 || ladderDemon2.difficulty == 0)
DemonsInBetween::loadDemonForLevel(std::move(f->m_listener2), 2, true, [this](LadderDemon& demon) { demon.id = 18; });
DemonsInBetween::loadDemonForLevel(std::move(f->m_listener2), 2, true, [this](LadderDemon& demon) { demon.id = 18; }, [this] {});

auto ladderDemon3 = DemonsInBetween::demonForLevel(20, true);
if (ladderDemon3.id == 0 || ladderDemon3.difficulty == 0)
DemonsInBetween::loadDemonForLevel(std::move(f->m_listener3), 3, true, [this](LadderDemon& demon) { demon.id = 20; });
DemonsInBetween::loadDemonForLevel(std::move(f->m_listener3), 3, true, [this](LadderDemon& demon) { demon.id = 20; }, [this] {});

return true;
}
Expand Down

0 comments on commit 18dff33

Please sign in to comment.