-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathGeodeUI.cpp
More file actions
395 lines (335 loc) · 13.3 KB
/
GeodeUI.cpp
File metadata and controls
395 lines (335 loc) · 13.3 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
#include "mods/ModsLayer.hpp"
#include <Geode/cocos/base_nodes/CCNode.h>
#include <Geode/loader/Dirs.hpp>
#include <Geode/ui/GeodeUI.hpp>
#include <Geode/ui/MDPopup.hpp>
#include <Geode/ui/LoadingSpinner.hpp>
#include <Geode/ui/LazySprite.hpp>
#include <Geode/utils/ColorProvider.hpp>
#include <Geode/utils/web.hpp>
#include <server/Server.hpp>
#include "mods/GeodeStyle.hpp"
#include "mods/settings/ModSettingsPopup.hpp"
#include "mods/settings/KeybindsPopup.hpp"
#include "mods/popups/ModPopup.hpp"
class LoadServerModLayer : public Popup {
protected:
enum class MetadataLoaded {
NotLoaded = 0,
Loaded = 1,
Error = 2,
};
std::string m_id;
ListenerHandle m_listenerHandle;
ListenerHandle m_versionListenerHandle;
arc::Notify m_metadataLoaded;
std::atomic<MetadataLoaded> m_metadataLoadedState{MetadataLoaded::NotLoaded};
async::TaskHolder<Result<server::ServerModMetadata, server::ServerError>> m_listener;
async::TaskHolder<Result<server::ServerModVersion, server::ServerError>> m_versionListener;
std::optional<server::ServerModMetadata> m_loadedMod{};
bool init(std::string id) {
if (!Popup::init(180.f, 100.f, "square01_001.png"))
return false;
m_closeBtn->setVisible(false);
this->setTitle("Loading mod...");
auto spinner = LoadingSpinner::create(40);
m_mainLayer->addChildAtPosition(spinner, Anchor::Center, ccp(0, -10));
m_id = std::move(id);
m_listener.spawn(
server::getMod(m_id),
[this](auto result) {
this->onModRequest(std::move(result));
}
);
return true;
}
void onModRequest(Result<server::ServerModMetadata, server::ServerError> result) {
if (result.isOk()) {
// Copy info first as onClose may free the listener which will free the event
auto info = std::move(result).unwrap();
m_loadedMod = std::move(info);
m_metadataLoadedState.store(MetadataLoaded::Loaded);
m_versionListener.spawn(
server::getModVersion(m_id),
[this](auto result) {
this->onVersionRequest(std::move(result));
}
);
}
else {
this->onClose(nullptr);
FLAlertLayer::create(
"Error Loading Mod",
fmt::format("Unable to find mod with the ID <cr>{}</c>!", m_id),
"OK"
)->show();
m_metadataLoadedState.store(MetadataLoaded::Error);
}
m_metadataLoaded.notifyOne(true);
}
void onVersionRequest(Result<server::ServerModVersion, server::ServerError> result) {
// this is promised non optional by this point
auto info = std::move(*m_loadedMod);
if (result.isOk()) {
// i don't actually think there's a better way to do this
// sorry guys
info.versions = {std::move(result).unwrap()};
}
// if there's an error, just load whatever the last fetched version was
// (this can happen for mods not on current gd version)
this->onClose(nullptr);
// Run this on next frame because otherwise the popup is unable to call server::getMod for some reason
Loader::get()->queueInMainThread([info = std::move(info)]() mutable {
ModPopup::create(ModSource(std::move(info)))->show();
});
}
public:
arc::Future<bool> listen() const {
co_await m_metadataLoaded.notified();
m_metadataLoaded.notifyOne(true);
co_return m_metadataLoadedState.load() == MetadataLoaded::Loaded;
}
static LoadServerModLayer* create(std::string id) {
auto ret = new LoadServerModLayer();
if (ret->init(std::move(id))) {
ret->autorelease();
return ret;
}
delete ret;
return nullptr;
}
};
void geode::openModsList() {
ModsLayer::scene();
}
void geode::openIssueReportPopup(Mod* mod) {
if (mod->getMetadata().getIssues()) {
MDPopup::create(
"Issue Report",
fmt::format(
"Please report the issue to the mod that caused the crash.\n"
"If your issue relates to a <cr>game crash</c>, <cb>please include</c> the "
"latest crash log(s) from `{}`",
dirs::getCrashlogsDir()
),
"OK", "Open Folder",
[mod](bool btn2) {
if (btn2) {
file::openFolder(dirs::getCrashlogsDir());
return;
}
auto issues = mod->getMetadata().getIssues();
if (issues && issues->getURL()) {
auto& url = *issues->getURL();
web::openLinkInBrowser(url);
}
}
)->show();
}
else {
MDPopup::create(
"Issue Report",
fmt::format(
"Please report your issue on the "
"[#support](https://discord.com/channels/911701438269386882/979352389985390603) "
"channel in the [Geode Discord Server](https://discord.gg/9e43WMKzhp)\n\n"
"If your issue relates to a <cr>game crash</c>, <cb>please include</c> the "
"latest crash log(s) from `{}`",
dirs::getCrashlogsDir()
),
"OK"
)->show();
}
}
void geode::openSupportPopup(Mod* mod) {
openSupportPopup(mod->getMetadata());
}
void geode::openSupportPopup(ModMetadata const& metadata) {
MDPopup::create(
fmt::format("Support {}", metadata.getName()),
metadata.getSupportInfo().value_or(
"Developing mods takes a lot of time and effort! "
"Consider <cy>supporting the developers</c> of your favorite mods "
"to show them thanks for all their hard work <3"
),
"OK"
)->show();
}
void geode::openInfoPopup(Mod* mod) {
ModPopup::create(mod)->show();
}
std::optional<arc::TaskHandle<bool>> geode::openInfoPopup(std::string modID) {
if (auto mod = Loader::get()->getInstalledMod(modID)) {
openInfoPopup(mod);
return std::nullopt;
}
auto popup = LoadServerModLayer::create(std::move(modID));
return async::runtime().spawn([popup = Ref(popup)] -> arc::Future<bool> {
auto ret = co_await popup->listen();
if (ret) {
geode::queueInMainThread([popup] {
popup->show();
});
}
co_return ret;
});
}
void geode::openChangelogPopup(Mod* mod) {
auto popup = ModPopup::create(mod);
popup->loadTab(ModPopup::Tab::Changelog);
popup->show();
}
void geode::openSettingsPopup(Mod* mod) {
openSettingsPopup(mod, true);
}
Popup* geode::openSettingsPopup(Mod* mod, bool disableGeodeTheme) {
if (mod->hasSettings()) {
auto popup = ModSettingsPopup::create(mod, disableGeodeTheme);
popup->show();
return popup;
}
return nullptr;
}
Popup* geode::openKeybindsPopup(std::optional<KeybindCategory> category, Mod* mod) {
auto tab = KeybindsPopupTab::All;
if (category) switch (*category) {
case KeybindCategory::Universal: tab = KeybindsPopupTab::Universal; break;
case KeybindCategory::Gameplay: tab = KeybindsPopupTab::Gameplay; break;
case KeybindCategory::Editor: tab = KeybindsPopupTab::Editor; break;
}
auto popup = KeybindsPopup::create(tab, mod);
popup->show();
return popup;
}
using ModLogoSrc = std::variant<Mod*, std::string, std::filesystem::path>;
class ModLogoSprite : public CCNodeRGBA {
protected:
LazySprite* m_sprite;
std::string m_modID;
async::TaskHolder<Result<ByteVector, server::ServerError>> m_listener;
bool init(ModLogoSrc&& src) {
if (!CCNode::init())
return false;
this->setAnchorPoint({ .5f, .5f });
this->setContentSize({ 50, 50 });
m_sprite = LazySprite::create(this->getContentSize());
this->addChildAtPosition(m_sprite, Anchor::Center);
std::visit(makeVisitor {
[this](Mod* mod) {
m_modID = mod->getID();
m_sprite->setLoadCallback([this](Result<> res) {
this->onLoaded(std::move(res));
});
// Load from Resources
if (!mod->isInternal()) {
m_sprite->loadFromFile(dirs::getModRuntimeDir() / mod->getID() / "logo.png");
} else {
// We only support lazySprite for this because i have no idea how to better integrate this
auto listener = NodeProvidingEvent("geode-mod-logo-sprite"_spr).listen([this](cocos2d::CCNode*& nodeOut, std::string_view theme) -> void {
auto sprite = typeinfo_cast<LazySprite*>(nodeOut);
if (!sprite) return; // someone overrode it, which will probably break maybe
if (theme == "rainbow") {
m_sprite->initWithSpriteFrameName("geode-logo-alternate.png"_spr);
}
else if (theme == "sapphire") {
m_sprite->initWithSpriteFrameName("sapphire-logo.png"_spr);
}
else {
m_sprite->initWithSpriteFrameName("geode-logo.png"_spr);
}
}, Priority::Last);
CCNode* ptr = m_sprite;
NodeProvidingEvent("geode-mod-logo-sprite"_spr).send(ptr);
// fallback
if (ptr != m_sprite) {
m_sprite->initWithSpriteFrameName("geode-logo.png"_spr);
}
}
},
[this](std::string const& id) {
m_modID = id;
// Asynchronously fetch from server
m_listener.spawn(
server::getModLogo(id),
[this](auto result) {
this->onFetch(std::move(result));
}
);
},
[this](std::filesystem::path const& path) {
m_sprite->setLoadCallback([this](Result<> res) {
this->onLoaded(std::move(res));
});
if (auto unzip = file::Unzip::create(path)) {
if (auto logo = unzip.unwrap().extract("logo.png")) {
m_sprite->loadFromData(std::move(logo).unwrap());
}
}
// if the logo.png was not found then switch to fallback label
if (!m_sprite->isLoading()) {
this->onLoadFailed(true);
}
},
}, src);
// This is a default ID, nothing should ever rely on the ID of any ModLogoSprite being this
this->setID(Mod::get()->expandSpriteName(fmt::format("sprite-{}", m_modID)));
ModLogoUIEvent().send(this, m_modID, std::nullopt);
return true;
}
void doPostEvent() {
ModLogoUIEvent().send(this, m_modID, std::nullopt);
}
void onLoaded(Result<> res) {
if (!res) {
log::debug("Failed to load image: {}", res.err().value_or(std::string{}));
this->onLoadFailed(true);
return;
}
limitNodeSize(m_sprite, m_obContentSize, 99.f, 0.f);
this->doPostEvent();
}
void onLoadFailed(bool postEvent) {
// Fallback to default logo if the image failed to load
auto sprite = CCLabelBMFont::create("N/A", "bigFont.fnt");
sprite->setPosition(this->getScaledContentSize() / 2.f + CCSize{1.f, 2.f});
sprite->setOpacity(90);
limitNodeSize(sprite, m_obContentSize, 99.f, 0.f);
this->addChildAtPosition(sprite, Anchor::Center);
this->doPostEvent();
}
void onFetch(Result<ByteVector, server::ServerError> result) {
// Set default sprite on error
if (result.isErr()) {
this->onLoadFailed(true);
}
// Otherwise load downloaded sprite to memory
else {
m_sprite->setLoadCallback([this](Result<> res) {
this->onLoaded(std::move(res));
});
m_sprite->loadFromData(std::move(result).unwrap());
}
}
public:
static ModLogoSprite* create(ModLogoSrc&& src) {
auto ret = new ModLogoSprite();
if (ret->init(std::move(src))) {
ret->autorelease();
return ret;
}
delete ret;
return nullptr;
}
};
CCNode* geode::createDefaultLogo() {
return ModLogoSprite::create(ModLogoSrc(nullptr));
}
CCNode* geode::createModLogo(Mod* mod) {
return ModLogoSprite::create(ModLogoSrc(mod));
}
CCNode* geode::createModLogo(std::filesystem::path const& geodePackage) {
return ModLogoSprite::create(ModLogoSrc(geodePackage));
}
CCNode* geode::createServerModLogo(std::string id) {
return ModLogoSprite::create(ModLogoSrc(std::move(id)));
}