fix nest issues on errors

This commit is contained in:
altalk23 2024-11-16 13:01:22 +03:00
parent d6f0c597f1
commit 22210956bb
5 changed files with 126 additions and 117 deletions

View file

@ -114,6 +114,37 @@ namespace geode {
popNest(getMod()); popNest(getMod());
} }
struct NestScope {
private:
bool m_active = true;
public:
NestScope() {
pushNest();
}
NestScope(NestScope const&) {
pushNest();
}
NestScope(NestScope&& other) {
other.m_active = false;
}
NestScope& operator=(NestScope const&) {
pushNest();
return *this;
}
NestScope& operator=(NestScope&& other) {
other.m_active = false;
return *this;
}
~NestScope() {
if (m_active) popNest();
}
};
class Nest final { class Nest final {
private: private:
class Impl; class Impl;

View file

@ -8,7 +8,7 @@ using namespace geode::prelude;
namespace { namespace {
void saveModData() { void saveModData() {
log::info("Saving mod data..."); log::info("Saving mod data...");
log::pushNest(); log::NestScope nest;
auto begin = std::chrono::high_resolution_clock::now(); auto begin = std::chrono::high_resolution_clock::now();
@ -17,8 +17,6 @@ namespace {
auto end = std::chrono::high_resolution_clock::now(); auto end = std::chrono::high_resolution_clock::now();
auto time = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count(); auto time = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
log::info("Took {}s", static_cast<float>(time) / 1000.f); log::info("Took {}s", static_cast<float>(time) / 1000.f);
log::popNest();
} }
} }

View file

@ -138,9 +138,9 @@ int geodeEntry(void* platformData) {
// set up internal mod, settings and data // set up internal mod, settings and data
log::info("Setting up internal mod"); log::info("Setting up internal mod");
log::pushNest(); {
log::NestScope nest;
auto internalSetupRes = LoaderImpl::get()->setupInternalMod(); auto internalSetupRes = LoaderImpl::get()->setupInternalMod();
log::popNest();
if (!internalSetupRes) { if (!internalSetupRes) {
console::messageBox( console::messageBox(
"Unable to Load Geode!", "Unable to Load Geode!",
@ -150,6 +150,7 @@ int geodeEntry(void* platformData) {
LoaderImpl::get()->forceReset(); LoaderImpl::get()->forceReset();
return 1; return 1;
} }
}
tryShowForwardCompat(); tryShowForwardCompat();
@ -161,9 +162,9 @@ int geodeEntry(void* platformData) {
// set up loader, load mods, etc. // set up loader, load mods, etc.
log::info("Setting up loader"); log::info("Setting up loader");
log::pushNest(); {
log::NestScope nest;
auto setupRes = LoaderImpl::get()->setup(); auto setupRes = LoaderImpl::get()->setup();
log::popNest();
if (!setupRes) { if (!setupRes) {
console::messageBox( console::messageBox(
"Unable to Load Geode!", "Unable to Load Geode!",
@ -174,13 +175,15 @@ int geodeEntry(void* platformData) {
LoaderImpl::get()->forceReset(); LoaderImpl::get()->forceReset();
return 1; return 1;
} }
}
crashlog::setupPlatformHandlerPost(); crashlog::setupPlatformHandlerPost();
log::debug("Setting up IPC"); log::debug("Setting up IPC");
log::pushNest(); {
log::NestScope nest;
ipc::setup(); ipc::setup();
log::popNest(); }
// download and install new loader update in the background // download and install new loader update in the background

View file

@ -81,35 +81,32 @@ Result<> Loader::Impl::setup() {
if (this->supportsLaunchArguments()) { if (this->supportsLaunchArguments()) {
log::debug("Loading launch arguments"); log::debug("Loading launch arguments");
log::pushNest(); log::NestScope nest;
this->initLaunchArguments(); this->initLaunchArguments();
log::popNest();
} }
// on some platforms, using the crash handler overrides more convenient native handlers // on some platforms, using the crash handler overrides more convenient native handlers
if (!this->getLaunchFlag("disable-crash-handler")) { if (!this->getLaunchFlag("disable-crash-handler")) {
log::debug("Setting up crash handler"); log::debug("Setting up crash handler");
log::pushNest(); log::NestScope nest;
if (!crashlog::setupPlatformHandler()) { if (!crashlog::setupPlatformHandler()) {
log::debug("Failed to set up crash handler"); log::debug("Failed to set up crash handler");
} }
log::popNest();
} else { } else {
log::debug("Crash handler setup skipped"); log::debug("Crash handler setup skipped");
} }
log::debug("Loading hooks"); log::debug("Loading hooks");
log::pushNest(); if (log::NestScope nest; !this->loadHooks()) {
if (!this->loadHooks()) {
return Err("There were errors loading some hooks, see console for details"); return Err("There were errors loading some hooks, see console for details");
} }
log::popNest();
log::debug("Setting up directories"); log::debug("Setting up directories");
log::pushNest(); {
log::NestScope nest;
this->createDirectories(); this->createDirectories();
this->addSearchPaths(); this->addSearchPaths();
log::popNest(); }
// Trigger on_mod(Loaded) for the internal mod // Trigger on_mod(Loaded) for the internal mod
// this function is already on the gd thread, so this should be fine // this function is already on the gd thread, so this should be fine
@ -129,7 +126,7 @@ void Loader::Impl::addSearchPaths() {
void Loader::Impl::updateResources(bool forceReload) { void Loader::Impl::updateResources(bool forceReload) {
log::debug("Adding resources"); log::debug("Adding resources");
log::pushNest(); log::NestScope nest;
for (auto const& [_, mod] : m_mods) { for (auto const& [_, mod] : m_mods) {
if (!forceReload && ModImpl::getImpl(mod)->m_resourcesLoaded) if (!forceReload && ModImpl::getImpl(mod)->m_resourcesLoaded)
continue; continue;
@ -140,7 +137,6 @@ void Loader::Impl::updateResources(bool forceReload) {
// we have to call it in both places since setup is only called once ever, but updateResources is called // we have to call it in both places since setup is only called once ever, but updateResources is called
// on every texture reload // on every texture reload
CCFileUtils::get()->updatePaths(); CCFileUtils::get()->updatePaths();
log::popNest();
} }
std::vector<Mod*> Loader::Impl::getAllMods() { std::vector<Mod*> Loader::Impl::getAllMods() {
@ -178,24 +174,22 @@ bool Loader::Impl::isModVersionSupported(VersionInfo const& target) {
void Loader::Impl::saveData() { void Loader::Impl::saveData() {
for (auto& [id, mod] : m_mods) { for (auto& [id, mod] : m_mods) {
log::debug("{}", mod->getID()); log::debug("{}", mod->getID());
log::pushNest(); log::NestScope nest;
auto r = mod->saveData(); auto r = mod->saveData();
if (!r) { if (!r) {
log::warn("Unable to save data for mod \"{}\": {}", mod->getID(), r.unwrapErr()); log::warn("Unable to save data for mod \"{}\": {}", mod->getID(), r.unwrapErr());
} }
log::popNest();
} }
} }
void Loader::Impl::loadData() { void Loader::Impl::loadData() {
for (auto& [_, mod] : m_mods) { for (auto& [_, mod] : m_mods) {
log::debug("{}", mod->getID()); log::debug("{}", mod->getID());
log::pushNest(); log::NestScope nest;
auto r = mod->loadData(); auto r = mod->loadData();
if (!r) { if (!r) {
log::warn("Unable to load data for mod \"{}\": {}", mod->getID(), r.unwrapErr()); log::warn("Unable to load data for mod \"{}\": {}", mod->getID(), r.unwrapErr());
} }
log::popNest();
} }
} }
@ -238,7 +232,7 @@ void Loader::Impl::updateModResources(Mod* mod) {
return; return;
log::debug("{}", mod->getID()); log::debug("{}", mod->getID());
log::pushNest(); log::NestScope nest;
for (auto const& sheet : mod->getMetadata().getSpritesheets()) { for (auto const& sheet : mod->getMetadata().getSpritesheets()) {
log::debug("Adding sheet {}", sheet); log::debug("Adding sheet {}", sheet);
@ -258,8 +252,6 @@ void Loader::Impl::updateModResources(Mod* mod) {
CCSpriteFrameCache::get()->addSpriteFramesWithFile(plist.c_str()); CCSpriteFrameCache::get()->addSpriteFramesWithFile(plist.c_str());
} }
} }
log::popNest();
} }
void Loader::Impl::addProblem(LoadProblem const& problem) { void Loader::Impl::addProblem(LoadProblem const& problem) {
@ -275,14 +267,14 @@ void Loader::Impl::addProblem(LoadProblem const& problem) {
void Loader::Impl::queueMods(std::vector<ModMetadata>& modQueue) { void Loader::Impl::queueMods(std::vector<ModMetadata>& modQueue) {
for (auto const& dir : m_modSearchDirectories) { for (auto const& dir : m_modSearchDirectories) {
log::debug("Searching {}", dir); log::debug("Searching {}", dir);
log::pushNest(); log::NestScope nest;
for (auto const& entry : std::filesystem::directory_iterator(dir)) { for (auto const& entry : std::filesystem::directory_iterator(dir)) {
if (!std::filesystem::is_regular_file(entry) || if (!std::filesystem::is_regular_file(entry) ||
entry.path().extension() != GEODE_MOD_EXTENSION) entry.path().extension() != GEODE_MOD_EXTENSION)
continue; continue;
log::debug("Found {}", entry.path().filename()); log::debug("Found {}", entry.path().filename());
log::pushNest(); log::NestScope nest;
auto res = ModMetadata::createFromGeodeFile(entry.path()); auto res = ModMetadata::createFromGeodeFile(entry.path());
if (!res) { if (!res) {
@ -292,7 +284,6 @@ void Loader::Impl::queueMods(std::vector<ModMetadata>& modQueue) {
res.unwrapErr() res.unwrapErr()
}); });
log::error("Failed to queue: {}", res.unwrapErr()); log::error("Failed to queue: {}", res.unwrapErr());
log::popNest();
continue; continue;
} }
auto modMetadata = res.unwrap(); auto modMetadata = res.unwrap();
@ -310,14 +301,11 @@ void Loader::Impl::queueMods(std::vector<ModMetadata>& modQueue) {
"A mod with the same ID is already present." "A mod with the same ID is already present."
}); });
log::error("Failed to queue: a mod with the same ID is already queued"); log::error("Failed to queue: a mod with the same ID is already queued");
log::popNest();
continue; continue;
} }
modQueue.push_back(modMetadata); modQueue.push_back(modMetadata);
log::popNest();
} }
log::popNest();
} }
} }
@ -335,7 +323,7 @@ void Loader::Impl::populateModList(std::vector<ModMetadata>& modQueue) {
for (auto const& metadata : modQueue) { for (auto const& metadata : modQueue) {
log::debug("{} {}", metadata.getID(), metadata.getVersion()); log::debug("{} {}", metadata.getID(), metadata.getVersion());
log::pushNest(); log::NestScope nest;
auto mod = new Mod(metadata); auto mod = new Mod(metadata);
@ -347,20 +335,17 @@ void Loader::Impl::populateModList(std::vector<ModMetadata>& modQueue) {
res.unwrapErr() res.unwrapErr()
}); });
log::error("Failed to set up: {}", res.unwrapErr()); log::error("Failed to set up: {}", res.unwrapErr());
log::popNest();
continue; continue;
} }
m_mods.insert({metadata.getID(), mod}); m_mods.insert({metadata.getID(), mod});
log::popNest();
} }
} }
void Loader::Impl::buildModGraph() { void Loader::Impl::buildModGraph() {
for (auto const& [id, mod] : m_mods) { for (auto const& [id, mod] : m_mods) {
log::debug("{}", mod->getID()); log::debug("{}", mod->getID());
log::pushNest(); log::NestScope nest;
for (auto& dependency : mod->m_impl->m_metadata.m_impl->m_dependencies) { for (auto& dependency : mod->m_impl->m_metadata.m_impl->m_dependencies) {
log::debug("{}", dependency.id); log::debug("{}", dependency.id);
if (!m_mods.contains(dependency.id)) { if (!m_mods.contains(dependency.id)) {
@ -387,7 +372,6 @@ void Loader::Impl::buildModGraph() {
incompatibility.mod = incompatibility.mod =
m_mods.contains(incompatibility.id) ? m_mods[incompatibility.id] : nullptr; m_mods.contains(incompatibility.id) ? m_mods[incompatibility.id] : nullptr;
} }
log::popNest();
} }
} }
@ -404,7 +388,6 @@ void Loader::Impl::loadModGraph(Mod* node, bool early) {
res.unwrapErr() res.unwrapErr()
}); });
log::error("{}", res.unwrapErr()); log::error("{}", res.unwrapErr());
log::popNest();
return; return;
} }
@ -418,7 +401,6 @@ void Loader::Impl::loadModGraph(Mod* node, bool early) {
geodeVerRes.unwrapErr() geodeVerRes.unwrapErr()
}); });
log::error("{}", geodeVerRes.unwrapErr()); log::error("{}", geodeVerRes.unwrapErr());
log::popNest();
return; return;
} }
@ -431,12 +413,10 @@ void Loader::Impl::loadModGraph(Mod* node, bool early) {
return; return;
} }
log::debug("{} {}", node->getID(), node->getVersion()); log::NestScope nest;
log::pushNest();
if (node->isEnabled()) { if (node->isEnabled()) {
log::warn("Mod {} already loaded, this should never happen", node->getID()); log::warn("Mod {} already loaded, this should never happen", node->getID());
log::popNest();
return; return;
} }
@ -447,6 +427,7 @@ void Loader::Impl::loadModGraph(Mod* node, bool early) {
auto unzipFunction = [this, node]() { auto unzipFunction = [this, node]() {
log::debug("Unzip"); log::debug("Unzip");
log::NestScope nest;
auto res = node->m_impl->unzipGeodeFile(node->getMetadata()); auto res = node->m_impl->unzipGeodeFile(node->getMetadata());
return res; return res;
}; };
@ -454,6 +435,7 @@ void Loader::Impl::loadModGraph(Mod* node, bool early) {
auto loadFunction = [this, node, early]() { auto loadFunction = [this, node, early]() {
if (node->shouldLoad()) { if (node->shouldLoad()) {
log::debug("Load"); log::debug("Load");
log::NestScope nest;
auto res = node->m_impl->loadBinary(); auto res = node->m_impl->loadBinary();
if (!res) { if (!res) {
this->addProblem({ this->addProblem({
@ -479,7 +461,6 @@ void Loader::Impl::loadModGraph(Mod* node, bool early) {
}); });
log::error("{}", reason.value()); log::error("{}", reason.value());
m_refreshingModCount -= 1; m_refreshingModCount -= 1;
log::popNest();
return; return;
} }
} }
@ -494,7 +475,6 @@ void Loader::Impl::loadModGraph(Mod* node, bool early) {
}); });
log::error("Failed to unzip: {}", res.unwrapErr()); log::error("Failed to unzip: {}", res.unwrapErr());
m_refreshingModCount -= 1; m_refreshingModCount -= 1;
log::popNest();
return; return;
} }
loadFunction(); loadFunction();
@ -524,7 +504,6 @@ void Loader::Impl::loadModGraph(Mod* node, bool early) {
}); });
}).detach(); }).detach();
} }
log::popNest();
} }
void Loader::Impl::findProblems() { void Loader::Impl::findProblems() {
@ -538,7 +517,7 @@ void Loader::Impl::findProblems() {
continue; continue;
} }
log::debug("{}", id); log::debug("{}", id);
log::pushNest(); log::NestScope nest;
for (auto const& dep : mod->getMetadata().getDependencies()) { for (auto const& dep : mod->getMetadata().getDependencies()) {
if (dep.mod && dep.mod->isEnabled() && dep.version.compare(dep.mod->getVersion())) if (dep.mod && dep.mod->isEnabled() && dep.version.compare(dep.mod->getVersion()))
@ -665,18 +644,15 @@ void Loader::Impl::findProblems() {
}); });
log::error("{} failed to load for an unknown reason", id); log::error("{} failed to load for an unknown reason", id);
} }
log::popNest();
} }
} }
void Loader::Impl::refreshModGraph() { void Loader::Impl::refreshModGraph() {
log::info("Refreshing mod graph"); log::info("Refreshing mod graph");
log::pushNest(); log::NestScope nest;
if (m_isSetup) { if (m_isSetup) {
log::error("Cannot refresh mod graph after startup"); log::error("Cannot refresh mod graph after startup");
log::popNest();
return; return;
} }
@ -686,48 +662,51 @@ void Loader::Impl::refreshModGraph() {
m_loadingState = LoadingState::Queue; m_loadingState = LoadingState::Queue;
log::debug("Queueing mods"); log::debug("Queueing mods");
log::pushNest();
std::vector<ModMetadata> modQueue; std::vector<ModMetadata> modQueue;
{
log::NestScope nest;
this->queueMods(modQueue); this->queueMods(modQueue);
log::popNest(); }
m_loadingState = LoadingState::List; m_loadingState = LoadingState::List;
log::debug("Populating mod list"); log::debug("Populating mod list");
log::pushNest(); {
log::NestScope nest;
this->populateModList(modQueue); this->populateModList(modQueue);
modQueue.clear(); modQueue.clear();
log::popNest(); }
m_loadingState = LoadingState::Graph; m_loadingState = LoadingState::Graph;
log::debug("Building mod graph"); log::debug("Building mod graph");
log::pushNest(); {
log::NestScope nest;
this->buildModGraph(); this->buildModGraph();
log::popNest(); }
log::debug("Ordering mod stack"); log::debug("Ordering mod stack");
log::pushNest(); {
log::NestScope nest;
this->orderModStack(); this->orderModStack();
log::popNest(); }
m_loadingState = LoadingState::EarlyMods; m_loadingState = LoadingState::EarlyMods;
log::debug("Loading early mods"); log::debug("Loading early mods");
log::pushNest(); {
log::NestScope nest;
while (!m_modsToLoad.empty() && m_modsToLoad.front()->needsEarlyLoad()) { while (!m_modsToLoad.empty() && m_modsToLoad.front()->needsEarlyLoad()) {
auto mod = m_modsToLoad.front(); auto mod = m_modsToLoad.front();
m_modsToLoad.pop_front(); m_modsToLoad.pop_front();
this->loadModGraph(mod, true); this->loadModGraph(mod, true);
} }
log::popNest(); }
auto end = std::chrono::high_resolution_clock::now(); auto end = std::chrono::high_resolution_clock::now();
auto time = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count(); auto time = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
log::info("Took {}s. Continuing next frame...", static_cast<float>(time) / 1000.f); log::info("Took {}s. Continuing next frame...", static_cast<float>(time) / 1000.f);
log::popNest();
m_loadingState = LoadingState::Mods; m_loadingState = LoadingState::Mods;
queueInMainThread([&]() { queueInMainThread([this]() {
this->continueRefreshModGraph(); this->continueRefreshModGraph();
}); });
} }
@ -781,7 +760,7 @@ void Loader::Impl::orderModStack() {
void Loader::Impl::continueRefreshModGraph() { void Loader::Impl::continueRefreshModGraph() {
if (m_refreshingModCount != 0) { if (m_refreshingModCount != 0) {
queueInMainThread([&]() { queueInMainThread([this]() {
this->continueRefreshModGraph(); this->continueRefreshModGraph();
}); });
return; return;
@ -794,28 +773,27 @@ void Loader::Impl::continueRefreshModGraph() {
} }
log::info("Continuing mod graph refresh..."); log::info("Continuing mod graph refresh...");
log::pushNest(); log::NestScope nest;
m_timerBegin = std::chrono::high_resolution_clock::now(); m_timerBegin = std::chrono::high_resolution_clock::now();
switch (m_loadingState) { switch (m_loadingState) {
case LoadingState::Mods: case LoadingState::Mods:
if (!m_modsToLoad.empty()) { if (!m_modsToLoad.empty()) {
log::debug("Loading mods");
log::pushNest();
auto mod = m_modsToLoad.front(); auto mod = m_modsToLoad.front();
m_modsToLoad.pop_front(); m_modsToLoad.pop_front();
log::debug("Loading mod {} {}", mod->getID(), mod->getVersion());
this->loadModGraph(mod, false); this->loadModGraph(mod, false);
log::popNest();
break; break;
} }
m_loadingState = LoadingState::Problems; m_loadingState = LoadingState::Problems;
[[fallthrough]]; [[fallthrough]];
case LoadingState::Problems: case LoadingState::Problems:
log::debug("Finding problems"); log::debug("Finding problems");
log::pushNest(); {
log::NestScope nest;
this->findProblems(); this->findProblems();
log::popNest(); }
m_loadingState = LoadingState::Done; m_loadingState = LoadingState::Done;
{ {
auto end = std::chrono::high_resolution_clock::now(); auto end = std::chrono::high_resolution_clock::now();
@ -831,12 +809,10 @@ void Loader::Impl::continueRefreshModGraph() {
} }
if (m_loadingState != LoadingState::Done) { if (m_loadingState != LoadingState::Done) {
queueInMainThread([&]() { queueInMainThread([this]() {
this->continueRefreshModGraph(); this->continueRefreshModGraph();
}); });
} }
log::popNest();
} }
std::vector<LoadProblem> Loader::Impl::getProblems() const { std::vector<LoadProblem> Loader::Impl::getProblems() const {

View file

@ -39,23 +39,24 @@ struct $modify(MenuLayer) {
// Launch arguments // Launch arguments
log::info("Testing launch args..."); log::info("Testing launch args...");
log::pushNest(); log::NestScope nest;
log::info("For global context:"); log::info("For global context:");
log::pushNest(); {
log::NestScope nest;
for (const auto& arg : Loader::get()->getLaunchArgumentNames()) { for (const auto& arg : Loader::get()->getLaunchArgumentNames()) {
log::info("{}", arg); log::info("{}", arg);
} }
log::popNest(); }
log::info("For this mod:"); log::info("For this mod:");
log::pushNest(); {
log::NestScope nest;
for (const auto& arg : Mod::get()->getLaunchArgumentNames()) { for (const auto& arg : Mod::get()->getLaunchArgumentNames()) {
log::info("{}", arg); log::info("{}", arg);
} }
log::popNest(); }
log::info("Mod has launch arg 'mod-arg': {}", Mod::get()->hasLaunchArgument("mod-arg")); log::info("Mod has launch arg 'mod-arg': {}", Mod::get()->hasLaunchArgument("mod-arg"));
log::info("Loader flag 'bool-arg': {}", Loader::get()->getLaunchFlag("bool-arg")); log::info("Loader flag 'bool-arg': {}", Loader::get()->getLaunchFlag("bool-arg"));
log::info("Loader int 'int-arg': {}", Loader::get()->parseLaunchArgument<int>("int-arg").unwrapOr(0)); log::info("Loader int 'int-arg': {}", Loader::get()->parseLaunchArgument<int>("int-arg").unwrapOr(0));
log::popNest();
return true; return true;
} }