Skip to content

Commit 1dec96d

Browse files
committed
fix playlayer leak in whack-a-face
1 parent 8566b1f commit 1dec96d

7 files changed

Lines changed: 118 additions & 90 deletions

File tree

.github/workflows/release.yml

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,4 @@ jobs:
4444
with:
4545
webhook-url: ${{ steps.check-discord.outputs.webhook-url }}
4646
content: "> -# @everyone"
47-
changelog: ${{ steps.publish-release.outputs.changelog }}
48-
49-
# publish:
50-
# name: Publish to Geode
51-
# runs-on: ubuntu-latest
52-
# needs: ["release"]
53-
54-
# steps:
55-
# - uses: actions/checkout@v6
56-
57-
# - name: Publish to Geode
58-
# uses: hiimjasmine00/release-geode-mod/publish@main
59-
# with:
60-
# token: ${{ secrets.GITHUB_TOKEN }}
47+
changelog: ${{ steps.publish-release.outputs.changelog }}

src/hooks/obstructive/Math.cpp

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,54 +22,71 @@ class $modify(MathPlayLayer, PlayLayer) {
2222
struct Fields final {
2323
uint8_t chance = options::getChance(THIS_ID);
2424

25-
Ref<MathQuiz> m_currentMath = nullptr;
25+
Ref<MathQuiz> currentMath = nullptr;
2626
};
2727

2828
void setupHasCompleted() {
2929
PlayLayer::setupHasCompleted();
3030
nextQuiz();
3131
};
3232

33+
void resetLevelFromStart() {
34+
PlayLayer::resetLevelFromStart();
35+
cue::resetNode(m_fields->currentMath);
36+
}
37+
3338
void levelComplete() {
3439
PlayLayer::levelComplete();
35-
cue::resetNode(m_fields->m_currentMath);
40+
cue::resetNode(m_fields->currentMath);
41+
};
42+
43+
void togglePracticeMode(bool practiceMode) {
44+
PlayLayer::togglePracticeMode(practiceMode);
45+
46+
cue::resetNode(m_fields->currentMath);
47+
nextQuiz();
48+
};
49+
50+
void onQuit() {
51+
PlayLayer::onQuit();
52+
cue::resetNode(m_fields->currentMath);
3653
};
3754

3855
void nextQuiz() {
3956
log::trace("scheduling math quiz");
57+
58+
unschedule(schedule_selector(MathPlayLayer::doQuiz));
4059
if (!m_hasCompletedLevel) scheduleOnce(schedule_selector(MathPlayLayer::doQuiz), rng::get(30.f, 5.f) * chanceToDelayPct(m_fields->chance));
4160
};
4261

4362
void doQuiz(float) {
44-
if (m_isPracticeMode && !m_hasCompletedLevel && !m_playerDied) {
45-
log::debug("Showing math quiz");
63+
auto f = m_fields.self();
4664

47-
if (options::isEnabled(THIS_ID)) {
48-
if (auto quiz = MathQuiz::create()) {
49-
// handle correct/wrong answer
50-
quiz->setCallback([self = WeakRef(this), math = WeakRef(quiz)](bool correct) {
51-
if (auto s = self.lock()) {
52-
log::debug("math {}", correct ? "succeeded" : "failed");
65+
if (options::isEnabled(THIS_ID) && !f->currentMath && m_isPracticeMode && !m_hasCompletedLevel && !m_playerDied) {
66+
log::debug("Showing math quiz");
5367

54-
if (!correct) s->resetLevelFromStart();
55-
s->nextQuiz();
68+
if (auto quiz = MathQuiz::create()) {
69+
// handle correct/wrong answer
70+
quiz->setCallback([self = WeakRef(this), math = WeakRef(quiz)](bool correct) {
71+
if (auto s = self.lock()) {
72+
if (!correct) s->resetLevelFromStart();
73+
s->nextQuiz();
5674

57-
cursor::hide();
75+
cursor::hide();
5876

59-
if (auto quiz = math.lock()) quiz->removeFromParent();
60-
};
61-
});
77+
if (auto quiz = math.lock()) quiz->removeFromParent();
78+
};
79+
});
6280

63-
m_uiLayer->addChild(quiz, 99);
64-
m_fields->m_currentMath = quiz;
81+
m_uiLayer->addChild(quiz, 99);
82+
f->currentMath = quiz;
6583

66-
cursor::show();
67-
};
68-
} else {
69-
queueInMainThread([self = WeakRef(this)]() {
70-
if (auto s = self.lock()) s->nextQuiz();
71-
});
84+
cursor::show();
7285
};
7386
};
87+
88+
queueInMainThread([self = WeakRef(this)]() {
89+
if (auto s = self.lock()) s->nextQuiz();
90+
});
7491
};
7592
};

src/hooks/obstructive/Spam.cpp

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,28 @@ class $modify(SpamPlayLayer, PlayLayer) {
3030
nextSpam();
3131
};
3232

33+
void resetLevelFromStart() {
34+
PlayLayer::resetLevelFromStart();
35+
cue::resetNode(m_fields->currentSpam);
36+
}
37+
3338
void levelComplete() {
3439
PlayLayer::levelComplete();
3540
cue::resetNode(m_fields->currentSpam);
3641
};
3742

43+
void togglePracticeMode(bool practiceMode) {
44+
PlayLayer::togglePracticeMode(practiceMode);
45+
46+
cue::resetNode(m_fields->currentSpam);
47+
nextSpam();
48+
};
49+
50+
void onQuit() {
51+
PlayLayer::onQuit();
52+
cue::resetNode(m_fields->currentSpam);
53+
};
54+
3855
void destroyPlayer(PlayerObject* player, GameObject* object) {
3956
PlayLayer::destroyPlayer(player, object);
4057

@@ -46,41 +63,41 @@ class $modify(SpamPlayLayer, PlayLayer) {
4663

4764
void nextSpam() {
4865
log::trace("scheduling spam challenge");
66+
67+
unschedule(schedule_selector(SpamPlayLayer::doSpam));
4968
if (!m_hasCompletedLevel) scheduleOnce(schedule_selector(SpamPlayLayer::doSpam), rng::get(30.f, 5.f) * chanceToDelayPct(m_fields->chance));
5069
};
5170

5271
void doSpam(float) {
53-
if (!m_isPracticeMode && !m_hasCompletedLevel && !m_playerDied) {
54-
log::debug("Showing spam challenge");
72+
auto f = m_fields.self();
5573

56-
if (options::isEnabled(THIS_ID)) {
57-
if (auto spam = SpamChallenge::create()) {
58-
auto f = m_fields.self();
74+
if (options::isEnabled(THIS_ID) && !f->currentSpam && !m_isPracticeMode && !m_hasCompletedLevel && !m_playerDied) {
75+
log::debug("Showing spam challenge");
5976

60-
// handle correct/wrong answer
61-
spam->setCallback([self = WeakRef(this), challenge = WeakRef(spam)](bool success) {
62-
if (auto s = self.lock()) {
63-
log::debug("spam {}", success ? "succeeded" : "failed");
77+
if (auto spam = SpamChallenge::create()) {
78+
auto f = m_fields.self();
6479

65-
if (!success) s->resetLevelFromStart();
66-
s->nextSpam();
80+
// handle correct/wrong answer
81+
spam->setCallback([self = WeakRef(this), challenge = WeakRef(spam)](bool success) {
82+
if (auto s = self.lock()) {
83+
if (!success) s->resetLevelFromStart();
84+
s->nextSpam();
6785

68-
cursor::hide();
86+
cursor::hide();
6987

70-
if (auto spam = challenge.lock()) spam->removeFromParent();
71-
};
72-
});
88+
if (auto spam = challenge.lock()) spam->removeFromParent();
89+
};
90+
});
7391

74-
m_uiLayer->addChild(spam, 99);
75-
f->currentSpam = spam;
92+
m_uiLayer->addChild(spam, 99);
93+
f->currentSpam = spam;
7694

77-
cursor::show();
78-
};
79-
} else {
80-
queueInMainThread([self = WeakRef(this)]() {
81-
if (auto s = self.lock()) s->nextSpam();
82-
});
95+
cursor::show();
8396
};
8497
};
98+
99+
queueInMainThread([self = WeakRef(this)]() {
100+
if (auto s = self.lock()) s->nextSpam();
101+
});
85102
};
86103
};

src/hooks/obstructive/WhackAFace.cpp

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ class $modify(WhackAFacePlayLayer, PlayLayer) {
2323
uint8_t chance = options::getChance(THIS_ID);
2424

2525
std::vector<WeakRef<WhackButton>> active;
26+
27+
void clearWhackBtns() {
28+
log::trace("Clearing {} whackable button references", active.size());
29+
30+
for (auto& whackBtn : active) {
31+
if (auto btn = whackBtn.lock()) btn->removeFromParent();
32+
};
33+
34+
active.clear();
35+
active.shrink_to_fit();
36+
};
2637
};
2738

2839
void setupHasCompleted() {
@@ -34,7 +45,26 @@ class $modify(WhackAFacePlayLayer, PlayLayer) {
3445

3546
void levelComplete() {
3647
PlayLayer::levelComplete();
37-
clearWhackBtns();
48+
m_fields->clearWhackBtns();
49+
};
50+
51+
void resetLevel() {
52+
PlayLayer::resetLevel();
53+
54+
m_fields->clearWhackBtns();
55+
cursor::show();
56+
};
57+
58+
void resetLevelFromStart() {
59+
PlayLayer::resetLevelFromStart();
60+
61+
m_fields->clearWhackBtns();
62+
cursor::show();
63+
};
64+
65+
void onQuit() {
66+
PlayLayer::onQuit();
67+
m_fields->clearWhackBtns();
3868
};
3969

4070
void destroyPlayer(PlayerObject* player, GameObject* object) {
@@ -48,25 +78,11 @@ class $modify(WhackAFacePlayLayer, PlayLayer) {
4878
};
4979
});
5080

51-
clearWhackBtns();
81+
m_fields->clearWhackBtns();
5282
cursor::show();
5383
};
5484
};
5585

56-
void resetLevel() {
57-
PlayLayer::resetLevel();
58-
59-
clearWhackBtns();
60-
cursor::show();
61-
};
62-
63-
void resetLevelFromStart() {
64-
PlayLayer::resetLevelFromStart();
65-
66-
clearWhackBtns();
67-
cursor::show();
68-
};
69-
7086
void nextWhack() {
7187
log::trace("scheduling new whack btn");
7288
if (!m_hasCompletedLevel) scheduleOnce(schedule_selector(WhackAFacePlayLayer::doWhack), rng::get(12.5f, 1.25f) * chanceToDelayPct(m_fields->chance));
@@ -87,7 +103,7 @@ class $modify(WhackAFacePlayLayer, PlayLayer) {
87103
};
88104

89105
cursor::show();
90-
cue::resetNode(whack);
106+
whack->removeFromParent();
91107
};
92108
});
93109
whack->setPosition(CCPoint{winSize.width * rng::get(0.75f, 0.25f), winSize.height * rng::get(0.75f, 0.25f)} / 2.f);
@@ -102,15 +118,4 @@ class $modify(WhackAFacePlayLayer, PlayLayer) {
102118
};
103119
};
104120
};
105-
106-
void clearWhackBtns() {
107-
auto f = m_fields.self();
108-
109-
for (auto& whackBtn : f->active) {
110-
if (auto btn = whackBtn.lock()) btn->removeFromParent();
111-
};
112-
113-
f->active.clear();
114-
f->active.shrink_to_fit();
115-
};
116121
};

src/util/ui/src/MathQuiz.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ bool MathQuiz::hasAnswer(int answer) const noexcept {
280280

281281
void MathQuiz::callAfterFeedback(CCNode*) {
282282
if (m_impl->callback) m_impl->callback(m_impl->correct);
283+
unscheduleAllSelectors();
283284
};
284285

285286
void MathQuiz::update(float dt) {

src/util/ui/src/SpamChallenge.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ bool SpamChallenge::ccTouchBegan(CCTouch* touch, CCEvent* event) {
106106

107107
void SpamChallenge::callAfterFeedback(float) {
108108
if (m_impl->callback) m_impl->callback(m_impl->success);
109+
unscheduleAllSelectors();
109110
};
110111

111112
void SpamChallenge::setSuccess(bool v) {

src/util/ui/src/WhackButton.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ void WhackButton::reload() {
6464
sfx::play("chestClick.ogg");
6565

6666
if (s->m_impl->inputCount >= s->m_impl->inputTarget) {
67-
s->unscheduleUpdate();
6867
s->setSuccess(true);
68+
s->unscheduleUpdate();
6969
} else {
7070
s->reload();
7171
};

0 commit comments

Comments
 (0)