geode/loader/src/hooks/LoadingLayer.cpp

84 lines
2.8 KiB
C++
Raw Normal View History

2022-10-30 14:56:36 -04:00
#include <Geode/modify/LoadingLayer.hpp>
#include <Geode/utils/cocos.hpp>
2022-12-10 11:30:14 -05:00
#include <array>
#include <fmt/format.h>
2022-12-14 06:11:19 -05:00
#include <loader/InternalLoader.hpp>
USE_GEODE_NAMESPACE();
struct CustomLoadingLayer : Modify<CustomLoadingLayer, LoadingLayer> {
bool m_updatingResources;
CustomLoadingLayer() : m_updatingResources(false) {}
bool init(bool fromReload) {
Loader::get()->waitForModsToBeLoaded();
if (!LoadingLayer::init(fromReload)) return false;
auto winSize = CCDirector::sharedDirector()->getWinSize();
auto count = Loader::get()->getAllMods().size();
2022-12-10 11:30:14 -05:00
auto label =
CCLabelBMFont::create(fmt::format("Geode: Loaded {} mods", count).c_str(), "goldFont.fnt");
label->setPosition(winSize.width / 2, 30.f);
label->setScale(.45f);
label->setID("geode-loaded-info");
this->addChild(label);
2022-12-10 11:30:14 -05:00
// for some reason storing the listener as a field caused the
// destructor for the field not to be run
this->addChild(EventListenerNode<ResourceDownloadFilter>::create(
2022-12-07 05:35:50 -05:00
this, &CustomLoadingLayer::updateResourcesProgress
));
// verify loader resources
2022-12-14 06:11:19 -05:00
if (!InternalLoader::get()->verifyLoaderResources()) {
m_fields->m_updatingResources = true;
this->setUpdateText("Downloading Resources");
}
return true;
}
void setUpdateText(std::string const& text) {
m_textArea->setString(text.c_str());
}
void updateResourcesProgress(ResourceDownloadEvent* event) {
std::visit(makeVisitor {
[&](UpdateProgress const& progress) {
this->setUpdateText(fmt::format(
"Downloading Resources: {}%", progress.first
));
},
[&](UpdateFinished) {
this->setUpdateText("Resources Downloaded");
m_fields->m_updatingResources = false;
this->loadAssets();
},
2022-12-09 05:53:49 -05:00
[&](UpdateFailed const& error) {
2022-12-14 06:11:19 -05:00
InternalLoader::get()->platformMessageBox(
"Error updating resources",
"Unable to update Geode resources: " +
error + ".\n"
"The game will be loaded as normal, but please be aware "
"that it may very likely crash."
);
this->setUpdateText("Resource Download Failed");
m_fields->m_updatingResources = false;
this->loadAssets();
}
}, event->status);
}
void loadAssets() {
if (m_fields->m_updatingResources) {
return;
}
LoadingLayer::loadAssets();
}
};