-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathMenuLayer.cpp
More file actions
465 lines (405 loc) · 19.6 KB
/
MenuLayer.cpp
File metadata and controls
465 lines (405 loc) · 19.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#include "../ui/mods/ModsLayer.hpp"
#include <Geode/loader/GameEvent.hpp>
#include <Geode/modify/MenuLayer.hpp>
#include <Geode/modify/Modify.hpp>
#include <Geode/modify/IDManager.hpp>
#include <Geode/utils/NodeIDs.hpp>
#include <Geode/ui/BasedButtonSprite.hpp>
#include <Geode/ui/Notification.hpp>
#include <Geode/ui/Popup.hpp>
#include <Geode/ui/MDPopup.hpp>
#include <Geode/utils/cocos.hpp>
#include <Geode/utils/web.hpp>
#include <loader/ModImpl.hpp>
#include <loader/LoaderImpl.hpp>
#include <loader/updater.hpp>
#include <Geode/binding/ButtonSprite.hpp>
#include <Geode/modify/LevelSelectLayer.hpp>
#include <Geode/utils/ColorProvider.hpp>
using namespace geode::prelude;
#pragma warning(disable : 4217)
static size_t FOUND_MOD_UPDATES = 0;
static size_t FOUND_MOD_DEPRECATIONS = 0;
static size_t FOUND_MOD_ERRORS = 0;
class CustomMenuLayer;
struct CustomMenuLayer : Modify<CustomMenuLayer, MenuLayer> {
static void onModify(auto& self) {
if (!self.setHookPriority("MenuLayer::init", geode::node_ids::GEODE_ID_PRIORITY)) {
log::warn("Failed to set MenuLayer::init hook priority, node IDs may not work properly");
}
GEODE_FORWARD_COMPAT_DISABLE_HOOKS_INNER("MenuLayer stuff disabled")
}
struct Fields {
bool m_menuDisabled = false;
CCNode* m_geodeButton = nullptr;
async::TaskHolder<Result<InstalledModsUpdateCheck, server::ServerError>> m_updateCheckTask;
};
bool init() {
if (!MenuLayer::init()) return false;
// make sure to add the string IDs for nodes (Geode has no manual
// hook order support yet so gotta do this to ensure)
NodeIDs::provideFor(this);
auto winSize = CCDirector::sharedDirector()->getWinSize();
m_fields->m_menuDisabled = Loader::get()->getLaunchFlag("disable-custom-menu");
// add geode button
if (!m_fields->m_menuDisabled) {
auto listener = NodeProvidingEvent("geode-button-sprite"_spr).listen([this](cocos2d::CCNode*& nodeOut, std::string_view theme) -> void {
if (nodeOut) return; // someone overrode it already
CCSprite* geodeButton;
if (theme == "sapphire") {
geodeButton = CircleButtonSprite::createWithSpriteFrameName(
"sapphire-logo-outline-gold.png"_spr,
.95f,
CircleBaseColor::Green,
CircleBaseSize::MediumAlt
);
}
else {
geodeButton = CircleButtonSprite::createWithSpriteFrameName(
"geode-logo-outline-gold.png"_spr,
.95f,
CircleBaseColor::Green,
CircleBaseSize::MediumAlt
);
}
if (!geodeButton || geodeButton->isUsingFallback()) {
nodeOut = nullptr;
}
else {
nodeOut = geodeButton;
}
}, Priority::Last);
cocos2d::CCNode* geodeBtn = nullptr;
NodeProvidingEvent("geode-button-sprite"_spr).send(geodeBtn);
auto geodeBtnSelector = &CustomMenuLayer::onGeode;
if (geodeBtn) {
m_fields->m_geodeButton = static_cast<CCSprite*>(geodeBtn);
}
else {
log::warn("No node provided for geode-button-sprite, using fallback instead");
m_fields->m_geodeButton = ButtonSprite::create("!!");
geodeBtnSelector = &CustomMenuLayer::onMissingTextures;
}
auto bottomMenu = static_cast<CCMenu*>(this->getChildByID("bottom-menu"));
auto btn = CCMenuItemSpriteExtra::create(
m_fields->m_geodeButton, this,
static_cast<SEL_MenuHandler>(geodeBtnSelector)
);
btn->setID("geode-button"_spr);
bottomMenu->addChild(btn);
bottomMenu->setContentSize({ winSize.width / 2, bottomMenu->getScaledContentSize().height });
bottomMenu->updateLayout();
this->fixSocialMenu();
//this code doesnt run have fun figuring out why idc enough
if (auto node = this->getChildByID("settings-gamepad-icon")) {
node->setPositionX(
bottomMenu->getChildByID("settings-button")->getPositionX() + winSize.width / 2
);
}
}
// show "download mods here" tip if the geode menu hasnt ever been opened and there arent mods already installed
if (!Mod::get()->getSavedValue<bool>("has-used-geode-before") && Loader::get()->getAllMods().size() == 1) {
if (auto bottomMenu = this->getChildByID("bottom-menu")) {
auto geodeBtn = bottomMenu->getChildByID("geode-button"_spr);
if (auto downloadModsHereSpr = CCSprite::createWithSpriteFrameName("download-mods-here.png"_spr)) {
downloadModsHereSpr->setID("download-mods-here"_spr);
downloadModsHereSpr->setPosition(this->convertToNodeSpace(geodeBtn->convertToWorldSpace(CCPointZero) + CCPoint(75.f, 55.f)));
downloadModsHereSpr->setScale(0.8f);
this->addChild(downloadModsHereSpr);
}
}
}
// show if some mods failed to load
static bool checkedLoadProblems = false;
if (!checkedLoadProblems) {
checkedLoadProblems = true;
if ((FOUND_MOD_ERRORS = Loader::get()->getLoadProblems().size())) {
Notification::create("There were errors - see Geode page!", NotificationIcon::Error)->show();
}
}
// show in safe mode
auto isSafeMode = LoaderImpl::get()->isSafeMode();
if (isSafeMode) {
Loader::get()->queueInMainThread([] {
auto popup = createQuickPopup(
"Safe Mode",
"Geode is running in <cy>Safe Mode</c>.\n"
"Mods are <cr>not loaded</c> in this mode.\n"
"\n"
"You can use this to <co>disable</c> or <co>update</c> mods "
"causing issues but all mod features\n"
"<cy>are not available</c>.",
"OK",
nullptr,
[](auto, bool btn2) {},
false
);
popup->m_noElasticity = true;
popup->show();
});
}
// show if the user tried to be naughty and load arbitrary DLLs
static bool shownTriedToLoadDlls = false;
if (!isSafeMode && !shownTriedToLoadDlls) {
shownTriedToLoadDlls = true;
if (LoaderImpl::get()->userTriedToLoadDLLs()) {
Loader::get()->queueInMainThread([] {
auto popup = FLAlertLayer::create(
"Hold up!",
"It appears that you have tried to <cr>load DLLs</c> with Geode. "
"Please note that <cy>Geode is incompatible with ALL DLLs</c>, "
"as they can cause Geode mods to <cr>error</c>, or even "
"<cr>crash</c>.\n\n"
"Remove the DLLs / other mod loaders you have, or <cr>proceed at "
"your own risk.</c>",
"OK"
);
popup->m_noElasticity = true;
popup->show();
});
}
}
// show auto update message
static bool shownUpdateInfo = false;
if (updater::isNewUpdateDownloaded() && !shownUpdateInfo) {
shownUpdateInfo = true;
Loader::get()->queueInMainThread([] {
auto popup = FLAlertLayer::create(
"Update downloaded",
"A new <cy>update</c> for Geode has been installed! "
"Please <cy>restart the game</c> to apply.",
"OK"
);
popup->m_noElasticity = true;
popup->show();
});
}
// show crash info
static bool shownLastCrash = false;
if (
crashlog::didLastLaunchCrash() &&
!shownLastCrash &&
!Mod::get()->getSettingValue<bool>("disable-last-crashed-popup")
) {
shownLastCrash = true;
// open the dialog a frame later (after the scene is set) for proper key priority
Loader::get()->queueInMainThread([] {
auto popup = createQuickPopup(
"Crashed",
"It appears that the last session crashed. Would you like to "
"open the <cy>crashlog folder</c>?",
"No",
"Yes",
[](auto, bool btn2) {
if (btn2) {
file::openFolder(dirs::getCrashlogsDir());
}
},
false,
false
);
popup->m_noElasticity = true;
popup->show();
});
}
// Check for mod updates
static bool checkedModUpdates = false;
if (!checkedModUpdates) {
// only run it once
checkedModUpdates = true;
m_fields->m_updateCheckTask.spawn(ModsLayer::checkInstalledModsForUpdates(), [this](auto result) {
if (result.isOk()) {
auto updatesFound = result.unwrap();
if (updatesFound.modsWithUpdates.size() || updatesFound.modsWithDeprecations.size()) {
if (updatesFound.modsWithUpdates.size()) {
log::info(
"Found updates for mods: {}!",
ranges::map<std::vector<std::string>>(
updatesFound.modsWithUpdates,
+[](Mod* mod) { return mod->getID(); }
)
);
FOUND_MOD_UPDATES = updatesFound.modsWithUpdates.size();
}
if (updatesFound.modsWithDeprecations.size()) {
log::info(
"Found deprecations for mods: {}!",
ranges::map<std::vector<std::string>>(
updatesFound.modsWithDeprecations,
+[](Mod* mod) { return mod->getID(); }
)
);
FOUND_MOD_DEPRECATIONS = updatesFound.modsWithDeprecations.size();
}
this->updateGeodeButtonMarkers();
}
else {
log::info("All mods up to date!");
}
}
else {
auto error = result.unwrapErr();
log::error("Unable to check for mod updates ({}): {}", error.code, error.details);
}
});
}
// Update markers on Geode button (errors, updates, etc.)
this->updateGeodeButtonMarkers();
// Delay the event by a frame so that MenuLayer is already in the scene
// and popups show up fine
static bool gameEventPosted = false;
if (!gameEventPosted) {
gameEventPosted = true;
Loader::get()->queueInMainThread([] {
GameEvent(GameEventType::Loaded).send();
});
}
return true;
}
void addMarkerToGeodeButton(ZStringView spr, ZStringView id, size_t count) {
m_fields->m_geodeButton->removeChildByID(id);
if (auto icon = CCSprite::createWithSpriteFrameName(spr.c_str())) {
icon->setPosition(m_fields->m_geodeButton->getContentSize() - ccp(10, 10));
icon->setID(id);
icon->setZOrder(99);
icon->setScale(.65f);
if (count > 0) {
auto countLabel = CCLabelBMFont::create(std::to_string(count).c_str(), "bigFont.fnt");
countLabel->setScale(.5f);
icon->addChildAtPosition(countLabel, Anchor::Center);
}
m_fields->m_geodeButton->addChild(icon);
}
}
void updateGeodeButtonMarkers() {
auto geodeButton = m_fields->m_geodeButton;
if (!geodeButton) {
return;
}
// clear all old markers first
geodeButton->removeChildByID("multiple-notifications");
geodeButton->removeChildByID("updates-deprecated");
geodeButton->removeChildByID("updates-available");
geodeButton->removeChildByID("errors-found");
if (((FOUND_MOD_UPDATES > 0) + (FOUND_MOD_DEPRECATIONS > 0) + (FOUND_MOD_ERRORS > 0)) > 1) {
this->addMarkerToGeodeButton(
"updates-multiple.png"_spr,
"multiple-notifications",
FOUND_MOD_UPDATES + FOUND_MOD_DEPRECATIONS + FOUND_MOD_ERRORS
);
}
else if (FOUND_MOD_DEPRECATIONS) {
this->addMarkerToGeodeButton("updates-deprecated.png"_spr, "updates-deprecated", FOUND_MOD_DEPRECATIONS);
}
else if (FOUND_MOD_UPDATES) {
this->addMarkerToGeodeButton("updates-available.png"_spr, "updates-available", FOUND_MOD_UPDATES);
}
else if (FOUND_MOD_ERRORS) {
// Don't show a silly number on top of the error exclamation
this->addMarkerToGeodeButton("exMark_001.png", "errors-found", 0);
}
}
void fixSocialMenu() {
// I did NOT have fun doing this
auto socialMenu = static_cast<CCMenu*>(this->getChildByID("social-media-menu"));
socialMenu->ignoreAnchorPointForPosition(false);
socialMenu->setAnchorPoint({0.0f, 0.0f});
socialMenu->setPosition({13.f, 13.f});
auto robtopButton = static_cast<CCMenuItemSpriteExtra*>(socialMenu->getChildByID("robtop-logo-button"));
robtopButton->setPosition(robtopButton->getScaledContentSize() / 2);
float horizontalGap = 3.5f;
float verticalGap = 5.0f;
auto facebookButton = static_cast<CCMenuItemSpriteExtra*>(socialMenu->getChildByID("facebook-button"));
facebookButton->setPosition({
facebookButton->getScaledContentSize().width / 2,
robtopButton->getScaledContentSize().height + verticalGap + facebookButton->getScaledContentSize().height / 2
});
auto twitterButton = static_cast<CCMenuItemSpriteExtra*>(socialMenu->getChildByID("twitter-button"));
twitterButton->setPosition({
facebookButton->getScaledContentSize().width + horizontalGap + twitterButton->getScaledContentSize().width / 2,
robtopButton->getScaledContentSize().height + verticalGap + twitterButton->getScaledContentSize().height / 2
});
auto youtubeButton = static_cast<CCMenuItemSpriteExtra*>(socialMenu->getChildByID("youtube-button"));
youtubeButton->setPosition({
twitterButton->getPositionX() + twitterButton->getScaledContentSize().width / 2 + horizontalGap + youtubeButton->getScaledContentSize().width / 2,
robtopButton->getScaledContentSize().height + verticalGap + youtubeButton->getScaledContentSize().height / 2
});
auto twitchButton = static_cast<CCMenuItemSpriteExtra*>(socialMenu->getChildByID("twitch-button"));
twitchButton->setPosition({
youtubeButton->getPositionX() + youtubeButton->getScaledContentSize().width / 2 + horizontalGap + twitchButton->getScaledContentSize().width / 2,
robtopButton->getScaledContentSize().height + verticalGap + twitchButton->getScaledContentSize().height / 2
});
auto discordButton = static_cast<CCMenuItemSpriteExtra*>(socialMenu->getChildByID("discord-button"));
discordButton->setPosition({
twitchButton->getPositionX(),
discordButton->getScaledContentSize().height / 2
});
socialMenu->setContentSize({
discordButton->getPositionX() + discordButton->getScaledContentSize().width / 2,
facebookButton->getPositionY() + facebookButton->getScaledContentSize().height / 2
});
auto bottomMenu = static_cast<CCMenu*>(this->getChildByID("bottom-menu"));
float spacing = 5.0f;
float buttonMenuLeftMargin = bottomMenu->getPositionX() - bottomMenu->getScaledContentSize().width * bottomMenu->getAnchorPoint().x;
float overlap = (socialMenu->getPositionX() + socialMenu->getScaledContentSize().width) - buttonMenuLeftMargin + spacing;
if (overlap > 0) {
float neededContentSize = buttonMenuLeftMargin - spacing - socialMenu->getPositionX();
float neededSize = neededContentSize * socialMenu->getScale() / socialMenu->getScaledContentSize().width;
socialMenu->setScale(neededSize);
}
}
void onMissingTextures(CCObject*) {
#ifdef GEODE_IS_DESKTOP
(void) utils::file::createDirectoryAll(dirs::getGeodeDir() / "update" / "resources" / "geode.loader");
createQuickPopup(
"Missing Textures",
"You appear to be missing textures, and the automatic texture fixer "
"hasn't fixed the issue.\n"
"Download <cy>resources.zip</c> from the latest release on GitHub, "
"and <cy>unzip its contents</c> into <cb>geode/update/resources/geode.loader</c>.\n"
"Afterwards, <cg>restart the game</c>.\n"
"You may also continue without installing resources, but be aware that "
"the Geode menu won't render properly.",
"Continue Anyway", "Open Github",
[this](auto, bool btn2) {
if (btn2) {
web::openLinkInBrowser("https://github.com/geode-sdk/geode/releases/latest");
file::openFolder(dirs::getGeodeDir() / "update" / "resources");
FLAlertLayer::create(
"Info",
"Opened GitHub in your browser and the destination in "
"your file browser.\n"
"Download <cy>resources.zip</c>, "
"and <cy>unzip its contents</c> into the destination "
"folder.\n"
"<cb>Don't add any new folders to the destination!</c>",
"OK"
)->show();
} else {
this->onGeode(nullptr);
}
},
true,
false
);
#else
// dunno if we can auto-create target directory on mobile, nor if the
// user has access to moving stuff there
createQuickPopup(
"Missing Textures",
"You appear to be missing textures, and the automatic texture fixer "
"hasn't fixed the issue.\n"
"**<cy>Report this bug to the Geode developers</c>**. It is very likely "
"that your game <cr>will crash</c> until the issue is resolved.",
"OK",
nullptr,
[this](auto, bool btn2) {
this->onGeode(nullptr);
}
);
#endif
}
void onGeode(CCObject*) {
ModsLayer::scene();
}
};