geode/loader/src/hooks/LoadingLayer.cpp

105 lines
3.7 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>
#include <loader/LoaderImpl.hpp>
using namespace geode::prelude;
struct CustomLoadingLayer : Modify<CustomLoadingLayer, LoadingLayer> {
2023-08-09 11:55:14 -04:00
CCLabelBMFont* m_loadedModsLabel;
bool m_updatingResources;
2023-08-09 11:55:14 -04:00
CustomLoadingLayer() : m_loadedModsLabel(nullptr), m_updatingResources(false) {}
2023-08-09 11:55:14 -04:00
void updateLoadedModsLabel() const {
auto allMods = Loader::get()->getAllMods();
auto count = std::count_if(allMods.begin(), allMods.end(), [&](auto& item) {
return item->isLoaded();
});
auto str = fmt::format("Geode: Loaded {}/{} mods", count, allMods.size());
m_loadedModsLabel->setCString(str.c_str());
}
2023-08-09 11:55:14 -04:00
bool init(bool fromReload) {
CCFileUtils::get()->updatePaths();
if (!LoadingLayer::init(fromReload)) return false;
2023-08-09 11:55:14 -04:00
if (fromReload) return true;
auto winSize = CCDirector::sharedDirector()->getWinSize();
m_loadedModsLabel = CCLabelBMFont::create("Geode: Loaded 0/0 mods", "goldFont.fnt");
m_loadedModsLabel->setPosition(winSize.width / 2, 30.f);
m_loadedModsLabel->setScale(.45f);
m_loadedModsLabel->setID("geode-loaded-info");
this->addChild(m_loadedModsLabel);
this->updateLoadedModsLabel();
// fields have unpredictable destructors
this->addChild(EventListenerNode<ResourceDownloadFilter>::create(
this, &CustomLoadingLayer::updateResourcesProgress
));
// verify loader resources
if (!LoaderImpl::get()->verifyLoaderResources()) {
m_fields->m_updatingResources = true;
this->setUpdateText("Downloading Resources");
}
else {
LoaderImpl::get()->updateSpecialFiles();
}
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) {
LoaderImpl::get()->platformMessageBox(
"Error updating resources",
error + ".\n"
"You will have to install resources manually by downloading resources.zip "
"from the latest release on GitHub: "
"https://github.com/geode-sdk/geode/releases/latest.\n"
"The game will be loaded as normal, but please be aware "
"that it is very likely to crash. "
);
this->setUpdateText("Resource Download Failed");
m_fields->m_updatingResources = false;
this->loadAssets();
}
}, event->status);
}
void loadAssets() {
2023-08-09 11:55:14 -04:00
if (Loader::get()->getLoadingState() != Loader::LoadingState::Done) {
this->updateLoadedModsLabel();
Loader::get()->queueInGDThread([this]() {
this->loadAssets();
});
return;
}
if (m_fields->m_updatingResources) {
return;
}
LoadingLayer::loadAssets();
}
};