Skip to content

Commit f226df4

Browse files
BlueWithererCopilot
andcommitted
idek if this works
Co-authored-by: Copilot <copilot@github.com>
1 parent 62cfa37 commit f226df4

5 files changed

Lines changed: 132 additions & 106 deletions

File tree

src/hooks/PlayLayer/Adverts.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class $modify(AdvertsPlayLayer, PlayLayer) {
4343
if (auto ad = WeakRef(f->ad).lock()) ad.take()->removeMeAndCleanup();
4444

4545
if (auto popup = RandomAd::create()) {
46+
popup->show();
4647
f->ad = popup;
47-
f->ad->show();
4848
};
4949

5050
queueInMainThread([self = WeakRef(this)]() {

src/hooks/PlayLayer/ForceLevels.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static auto const oCongreg = Option::create(THIS_ID_CONGREG)
2626
->setOnline(true)
2727
->autoRegister();
2828

29-
static bool trySwitchToLevel(PlayLayer* pl, PlayerObject* player, GameObject* killer, jumpscares::DownloadDelegate* delegate, int chance, int rng, bool dontCreateObjects, bool useReplay) {
29+
static bool trySwitchToLevel(PlayLayer* pl, PlayerObject* player, GameObject* killer, std::shared_ptr<jumpscares::DownloadDelegate> delegate, int chance, int rng, bool dontCreateObjects, bool useReplay) {
3030
if (rng > chance) {
3131
log::debug("{} jumpscare not triggered {}", delegate->getLevelName(), chance);
3232
return false;
@@ -46,20 +46,15 @@ class $modify(GriefPlayLayer, PlayLayer) {
4646
bool dontCreateObjects = false;
4747
};
4848

49-
void setupHasCompleted() {
50-
PlayLayer::setupHasCompleted();
51-
jumpscares::saveLevel(jumpscares::get::grief());
52-
};
53-
5449
void destroyPlayer(PlayerObject* p0, GameObject* p1) {
50+
PlayLayer::destroyPlayer(p0, p1);
51+
5552
auto f = m_fields.self();
5653

5754
if (p1 == m_anticheatSpike && !p0->m_isDead) return;
5855

5956
int rng = randng::fast();
6057
trySwitchToLevel(this, p0, p1, jumpscares::get::grief(), f->chance, rng, f->dontCreateObjects, m_useReplay);
61-
62-
PlayLayer::destroyPlayer(p0, p1);
6358
};
6459
};
6560

@@ -72,19 +67,14 @@ class $modify(CongregationPlayLayer, PlayLayer) {
7267
bool dontCreateObjects = false;
7368
};
7469

75-
void setupHasCompleted() {
76-
PlayLayer::setupHasCompleted();
77-
jumpscares::saveLevel(jumpscares::get::congregation());
78-
};
79-
8070
void destroyPlayer(PlayerObject* p0, GameObject* p1) {
71+
PlayLayer::destroyPlayer(p0, p1);
72+
8173
auto f = m_fields.self();
8274

8375
if (p1 == m_anticheatSpike && !p0->m_isDead) return;
8476

8577
int rng = randng::fast();
8678
trySwitchToLevel(this, p0, p1, jumpscares::get::congregation(), f->chance, rng, f->dontCreateObjects, m_useReplay);
87-
88-
PlayLayer::destroyPlayer(p0, p1);
8979
};
9080
};

src/util/Jumpscares.hpp

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace horrible {
66
namespace util {
77
// Jumpscare level manager
88
namespace jumpscares {
9+
910
class JumpscareDelegateData {
1011
private:
1112
geode::WeakRef<PlayLayer> m_playLayer = nullptr;
@@ -32,15 +33,15 @@ namespace horrible {
3233
bool getUseReplay() const noexcept;
3334
};
3435

35-
struct SearchDelegate final : public LevelManagerDelegate, public JumpscareDelegateData {
36+
struct SearchDelegate final : public LevelManagerDelegate, public JumpscareDelegateData, std::enable_shared_from_this<SearchDelegate> {
3637
public:
3738
SearchDelegate(PlayLayer* pl, int levelID, int songID, std::string levelName, bool dontCreateObjects, bool useReplay);
3839

3940
void loadLevelsFinished(cocos2d::CCArray* levels, char const* key) override;
4041
void loadLevelsFailed(char const* key) override;
4142
};
4243

43-
struct DownloadDelegate final : public LevelDownloadDelegate, public JumpscareDelegateData {
44+
struct DownloadDelegate final : public LevelDownloadDelegate, public JumpscareDelegateData, std::enable_shared_from_this<DownloadDelegate> {
4445
public:
4546
DownloadDelegate(PlayLayer* pl, int levelID, int songID, std::string levelName, bool dontCreateObjects, bool useReplay);
4647

@@ -49,20 +50,44 @@ namespace horrible {
4950
};
5051

5152
namespace get {
52-
DownloadDelegate* grief();
53-
DownloadDelegate* congregation();
53+
std::shared_ptr<DownloadDelegate> grief();
54+
std::shared_ptr<DownloadDelegate> congregation();
5455
};
5556

56-
void switchToLevel(PlayLayer* pl, DownloadDelegate* delegate, PlayerObject* player, GameObject* killer, bool dontCreateObjects, bool useReplay);
57+
void switchToLevel(PlayLayer* pl, std::shared_ptr<DownloadDelegate> delegate, PlayerObject* player, GameObject* killer, bool dontCreateObjects, bool useReplay);
5758

58-
void saveLevel(DownloadDelegate* delegate);
59-
void downloadLevelAsync(DownloadDelegate* delegate);
59+
void downloadLevelAsync(std::shared_ptr<DownloadDelegate> delegate);
6060

6161
GJGameLevel* getSavedDownloadedLevel(int levelID);
6262
GJSearchObject* createLevelSearchObject(int levelID);
6363

64-
void clearDownloadDelegate(DownloadDelegate* delegate);
65-
void clearLevelManagerDelegate(SearchDelegate* delegate);
64+
class JumpscareDelegateManager final {
65+
private:
66+
std::shared_ptr<SearchDelegate> m_searchDelegate;
67+
std::shared_ptr<DownloadDelegate> m_downloadDelegate;
68+
69+
protected:
70+
JumpscareDelegateManager() = default;
71+
~JumpscareDelegateManager() = default;
72+
73+
JumpscareDelegateManager(const JumpscareDelegateManager&) = delete;
74+
JumpscareDelegateManager& operator=(const JumpscareDelegateManager&) = delete;
75+
76+
JumpscareDelegateManager(JumpscareDelegateManager&&) = delete;
77+
JumpscareDelegateManager& operator=(JumpscareDelegateManager&&) = delete;
78+
79+
public:
80+
static JumpscareDelegateManager* get() noexcept;
81+
82+
std::weak_ptr<SearchDelegate> getSearchDelegate() const noexcept;
83+
std::weak_ptr<DownloadDelegate> getDownloadDelegate() const noexcept;
84+
85+
void setSearchDelegate(std::shared_ptr<SearchDelegate> delegate);
86+
void setDownloadDelegate(std::shared_ptr<DownloadDelegate> delegate);
87+
88+
void clearSearchDelegate();
89+
void clearDownloadDelegate();
90+
};
6691
};
6792
};
6893
};

src/util/src/Jumpscares.cpp

Lines changed: 88 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,16 @@ jumpscares::DownloadDelegate::DownloadDelegate(PlayLayer* pl, int levelID, int s
4040
void jumpscares::DownloadDelegate::levelDownloadFinished(GJGameLevel* level) {
4141
log::trace("Download finished for level {}", getLevelID());
4242

43+
JumpscareDelegateManager::get()->clearDownloadDelegate();
44+
log::debug("Delegate cleared, checking level validity");
45+
4346
if (!level || level->m_levelID.value() != getLevelID()) {
4447
log::error("Downloaded level mismatch or null: expected {}, got {}", getLevelID(), level ? level->m_levelID.value() : -1);
45-
clearDownloadDelegate(this);
46-
delete this;
4748
return;
4849
};
4950

50-
log::debug("Delegate cleared, checking level validity");
51-
clearDownloadDelegate(this);
52-
5351
if (level->m_levelNotDownloaded) {
5452
log::error("Downloaded level {} is still not ready", getLevelID());
55-
delete this;
5653
return;
5754
};
5855

@@ -63,43 +60,41 @@ void jumpscares::DownloadDelegate::levelDownloadFinished(GJGameLevel* level) {
6360
} else {
6461
log::warn("PlayLayer weak pointer expired, cannot switch scene for level {}", getLevelName());
6562
};
66-
67-
delete this;
6863
};
6964

7065
void jumpscares::DownloadDelegate::levelDownloadFailed(int response) {
71-
clearDownloadDelegate(this);
66+
JumpscareDelegateManager::get()->clearDownloadDelegate();
7267
log::error("Failed to download {} ({})", getLevelName(), response);
73-
delete this;
7468
};
7569

7670
jumpscares::SearchDelegate::SearchDelegate(PlayLayer* pl, int levelID, int songID, std::string levelName, bool dontCreateObjects, bool useReplay) :
7771
JumpscareDelegateData(pl, levelID, songID, std::move(levelName), dontCreateObjects, useReplay) {};
7872

7973
void jumpscares::SearchDelegate::loadLevelsFinished(CCArray* levels, char const* key) {
8074
log::trace("Search finished for level {}", getLevelName());
81-
clearLevelManagerDelegate(this);
75+
JumpscareDelegateManager::get()->clearSearchDelegate();
8276

8377
if (!levels || levels->count() == 0) {
8478
log::error("No online level result returned for {}", getLevelName());
85-
delete this;
8679
return;
8780
};
8881

8982
if (auto level = typeinfo_cast<GJGameLevel*>(levels->objectAtIndex(0))) {
9083
log::debug("Level object retrieved from search, starting download for {}", getLevelName());
91-
auto delegate = new DownloadDelegate(getPlayLayer().lock().take(), getLevelID(), getSongID(), getLevelName(), getDontCreateObjects(), getUseReplay());
92-
downloadLevelAsync(delegate);
84+
if (auto pl = getPlayLayer().lock()) {
85+
auto delegate = std::make_shared<DownloadDelegate>(pl.take(), getLevelID(), getSongID(), getLevelName(), getDontCreateObjects(), getUseReplay());
86+
downloadLevelAsync(delegate);
87+
} else {
88+
log::warn("Search finished but PlayLayer expired for {}", getLevelName());
89+
};
9390
} else {
9491
log::error("Online search returned non-level object for {}", getLevelName());
95-
delete this;
9692
};
9793
};
9894

9995
void jumpscares::SearchDelegate::loadLevelsFailed(char const* key) {
100-
clearLevelManagerDelegate(this);
96+
JumpscareDelegateManager::get()->clearSearchDelegate();
10197
log::error("Failed to fetch online level info for {}", getLevelName());
102-
delete this;
10398
};
10499

105100
GJGameLevel* jumpscares::getSavedDownloadedLevel(int levelID) {
@@ -112,54 +107,10 @@ GJGameLevel* jumpscares::getSavedDownloadedLevel(int levelID) {
112107
return nullptr;
113108
};
114109

115-
void jumpscares::clearDownloadDelegate(DownloadDelegate* delegate) {
116-
if (auto glm = GameLevelManager::sharedState()) {
117-
if (glm->m_levelDownloadDelegate == delegate) {
118-
glm->m_levelDownloadDelegate = nullptr;
119-
log::trace("Cleared download delegate for level");
120-
} else {
121-
log::warn("Download delegate for level {} was not set or already cleared", delegate->getLevelID());
122-
};
123-
};
124-
};
125-
126-
void jumpscares::clearLevelManagerDelegate(SearchDelegate* delegate) {
127-
if (auto glm = GameLevelManager::sharedState()) {
128-
if (glm->m_levelManagerDelegate == delegate) {
129-
glm->m_levelManagerDelegate = nullptr;
130-
log::trace("Cleared level manager delegate for search");
131-
} else {
132-
log::warn("Level manager delegate for search {} was not set or already cleared", delegate->getLevelID());
133-
};
134-
};
135-
};
136-
137-
void jumpscares::saveLevel(DownloadDelegate* delegate) {
138-
log::debug("Download request for level {}", delegate->getLevelID());
139-
140-
if (auto glm = GameLevelManager::sharedState()) {
141-
if (glm->hasDownloadedLevel(delegate->getLevelID())) {
142-
log::info("Level {} already downloaded, skipping download", delegate->getLevelID());
143-
if (auto savedLevel = getSavedDownloadedLevel(delegate->getLevelID())) glm->updateLevel(savedLevel);
144-
145-
delete delegate;
146-
} else {
147-
log::debug("Level {} not cached, starting download", delegate->getLevelID());
148-
downloadLevelAsync(delegate);
149-
};
150-
} else {
151-
log::error("Cannot download {}: GameLevelManager not available", delegate->getLevelID());
152-
delete delegate;
153-
};
154-
};
155-
156-
void jumpscares::downloadLevelAsync(DownloadDelegate* delegate) {
110+
void jumpscares::downloadLevelAsync(std::shared_ptr<DownloadDelegate> delegate) {
157111
log::debug("Initiating download for level {} with song {}", delegate->getLevelID(), delegate->getSongID());
158112

159113
if (auto glm = GameLevelManager::sharedState()) {
160-
log::debug("Downloading {} in background", delegate->getLevelID());
161-
glm->downloadLevel(delegate->getLevelID(), false, 0);
162-
163114
if (delegate->getSongID() > 0) {
164115
if (auto mdm = MusicDownloadManager::sharedState()) {
165116
mdm->downloadSong(delegate->getSongID());
@@ -169,8 +120,11 @@ void jumpscares::downloadLevelAsync(DownloadDelegate* delegate) {
169120
};
170121
};
171122

123+
log::debug("Downloading {} in background", delegate->getLevelID());
124+
glm->downloadLevel(delegate->getLevelID(), false, 0);
125+
JumpscareDelegateManager::get()->setDownloadDelegate(std::move(delegate));
126+
172127
log::debug("Delegate set for level download");
173-
glm->m_levelDownloadDelegate = delegate;
174128
} else {
175129
log::error("GameLevelManager not available for level {}", delegate->getLevelID());
176130
};
@@ -180,7 +134,16 @@ GJSearchObject* jumpscares::createLevelSearchObject(int levelID) {
180134
return GJSearchObject::create(SearchType::Type19, numToString(levelID));
181135
};
182136

183-
void jumpscares::switchToLevel(PlayLayer* pl, DownloadDelegate* delegate, PlayerObject* player, GameObject* killer, bool dontCreateObjects, bool useReplay) {
137+
void jumpscares::switchToLevel(PlayLayer* pl, std::shared_ptr<DownloadDelegate> delegate, PlayerObject* player, GameObject* killer, bool dontCreateObjects, bool useReplay) {
138+
if (!delegate) {
139+
delegate = JumpscareDelegateManager::get()->getDownloadDelegate().lock();
140+
141+
if (!delegate) {
142+
log::error("No active download delegate available to switch to level");
143+
return;
144+
};
145+
};
146+
184147
log::debug("Attempting to switch to level {} ({})", delegate->getLevelName(), delegate->getLevelID());
185148

186149
if (auto targetLevel = getSavedDownloadedLevel(delegate->getLevelID())) {
@@ -230,18 +193,73 @@ void jumpscares::switchToLevel(PlayLayer* pl, DownloadDelegate* delegate, Player
230193

231194
log::debug("No stored levels found, initiating online search for {}", delegate->getLevelName());
232195

233-
auto del = new SearchDelegate(pl, delegate->getLevelID(), 0, delegate->getLevelName(), dontCreateObjects, useReplay);
234-
glm->m_levelManagerDelegate = del;
196+
auto del = std::make_shared<SearchDelegate>(pl, delegate->getLevelID(), 0, delegate->getLevelName(), dontCreateObjects, useReplay);
235197
glm->getOnlineLevels(search);
198+
JumpscareDelegateManager::get()->setSearchDelegate(std::move(del));
236199
} else {
237200
log::error("GameLevelManager not available for switching to level {}", delegate->getLevelName());
238201
};
239202
};
240203

241-
jumpscares::DownloadDelegate* jumpscares::get::grief() {
242-
return new jumpscares::DownloadDelegate(PlayLayer::get(), 129066933, 482872, "Grief", false, false);
204+
std::shared_ptr<jumpscares::DownloadDelegate> jumpscares::get::grief() {
205+
return std::make_shared<jumpscares::DownloadDelegate>(PlayLayer::get(), 129066933, 482872, "Grief", false, false);
206+
};
207+
208+
std::shared_ptr<jumpscares::DownloadDelegate> jumpscares::get::congregation() {
209+
return std::make_shared<jumpscares::DownloadDelegate>(PlayLayer::get(), 129066879, 895761, "Congregation", false, false);
210+
};
211+
212+
std::weak_ptr<jumpscares::SearchDelegate> jumpscares::JumpscareDelegateManager::getSearchDelegate() const noexcept {
213+
return m_searchDelegate;
214+
};
215+
216+
std::weak_ptr<jumpscares::DownloadDelegate> jumpscares::JumpscareDelegateManager::getDownloadDelegate() const noexcept {
217+
return m_downloadDelegate;
218+
};
219+
220+
void jumpscares::JumpscareDelegateManager::setSearchDelegate(std::shared_ptr<SearchDelegate> delegate) {
221+
if (auto glm = GameLevelManager::sharedState()) glm->m_levelManagerDelegate = delegate.get();
222+
m_searchDelegate = std::move(delegate);
223+
};
224+
225+
void jumpscares::JumpscareDelegateManager::setDownloadDelegate(std::shared_ptr<DownloadDelegate> delegate) {
226+
if (auto glm = GameLevelManager::sharedState()) glm->m_levelDownloadDelegate = delegate.get();
227+
m_downloadDelegate = std::move(delegate);
228+
};
229+
230+
void jumpscares::JumpscareDelegateManager::clearSearchDelegate() {
231+
if (auto glm = GameLevelManager::sharedState()) {
232+
if (m_searchDelegate && glm->m_levelManagerDelegate == m_searchDelegate.get()) {
233+
glm->m_levelManagerDelegate = nullptr;
234+
235+
log::trace("Cleared level manager delegate for search");
236+
} else if (m_searchDelegate) {
237+
log::warn("Level manager delegate for search {} was not set or already cleared", m_searchDelegate->getLevelID());
238+
} else {
239+
log::warn("No search delegate to clear");
240+
};
241+
242+
m_searchDelegate.reset();
243+
};
244+
};
245+
246+
void jumpscares::JumpscareDelegateManager::clearDownloadDelegate() {
247+
if (auto glm = GameLevelManager::sharedState()) {
248+
if (m_downloadDelegate && glm->m_levelDownloadDelegate == m_downloadDelegate.get()) {
249+
glm->m_levelDownloadDelegate = nullptr;
250+
251+
log::trace("Cleared download delegate for level");
252+
} else if (m_downloadDelegate) {
253+
log::warn("Download delegate for level {} was not set or already cleared", m_downloadDelegate->getLevelID());
254+
} else {
255+
log::warn("No download delegate to clear");
256+
};
257+
258+
m_downloadDelegate.reset();
259+
};
243260
};
244261

245-
jumpscares::DownloadDelegate* jumpscares::get::congregation() {
246-
return new jumpscares::DownloadDelegate(PlayLayer::get(), 129066879, 895761, "Congregation", false, false);
262+
jumpscares::JumpscareDelegateManager* jumpscares::JumpscareDelegateManager::get() noexcept {
263+
static auto inst = new (std::nothrow) JumpscareDelegateManager();
264+
return inst;
247265
};

0 commit comments

Comments
 (0)