From f6d4d5ab229cfa7cf47e0a828f0a8480517133d2 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 28 Nov 2023 00:47:03 +0100 Subject: [PATCH] refactor: More init sequence refactors --- main/gui/include/init/splash_window.hpp | 4 +- main/gui/source/init/splash_window.cpp | 72 ++++++++++--------- main/gui/source/init/tasks.cpp | 2 +- .../builtin/source/content/welcome_screen.cpp | 28 ++++---- 4 files changed, 59 insertions(+), 47 deletions(-) diff --git a/main/gui/include/init/splash_window.hpp b/main/gui/include/init/splash_window.hpp index 6ef8456df..1bef8b489 100644 --- a/main/gui/include/init/splash_window.hpp +++ b/main/gui/include/init/splash_window.hpp @@ -65,8 +65,8 @@ namespace hex::init { std::future processTasksAsync(); std::atomic m_totalTaskCount, m_completedTaskCount; - std::atomic m_taskStatus; - std::vector m_tasks; + std::atomic m_taskStatus = true; + std::list m_tasks; std::mutex m_tasksMutex; std::string m_gpuVendor; diff --git a/main/gui/source/init/splash_window.cpp b/main/gui/source/init/splash_window.cpp index eaf4deca2..797a5d080 100644 --- a/main/gui/source/init/splash_window.cpp +++ b/main/gui/source/init/splash_window.cpp @@ -86,33 +86,6 @@ namespace hex::init { glfwSetWindowPos(window, monitorX + (mode->width - windowWidth) / 2, monitorY + (mode->height - windowHeight) / 2); } - std::future WindowSplash::processTasksAsync() { - return std::async(std::launch::async, [this] { - // Loop over all registered init tasks - for (const auto &task : this->m_tasks) { - - // Construct a new task callback - this->createTask(task); - } - - // Check every 100ms if all tasks have run - while (true) { - { - std::scoped_lock lock(this->m_tasksMutex); - if (this->m_completedTaskCount >= this->m_totalTaskCount) - break; - } - - std::this_thread::sleep_for(100ms); - } - - // Small extra delay so the last progress step is visible - std::this_thread::sleep_for(100ms); - - return this->m_taskStatus.load(); - }); - } - void WindowSplash::createTask(const Task& task) { auto runTask = [&, task] { try { @@ -135,11 +108,12 @@ namespace hex::init { bool taskStatus = task.callback(); auto endTime = std::chrono::high_resolution_clock::now(); - log::info("Task '{}' finished {} in {} ms", - task.name, - taskStatus ? "successfully" : "unsuccessfully", - std::chrono::duration_cast(endTime - startTime).count() - ); + auto milliseconds = std::chrono::duration_cast(endTime - startTime).count(); + + if (taskStatus) + log::info("Task '{}' finished successfully in {} ms", task.name, milliseconds); + else + log::warn("Task '{}' finished unsuccessfully in {} ms", task.name, milliseconds); // Track the overall status of the tasks this->m_taskStatus = this->m_taskStatus && taskStatus; @@ -169,6 +143,40 @@ namespace hex::init { } } + std::future WindowSplash::processTasksAsync() { + return std::async(std::launch::async, [this] { + auto startTime = std::chrono::high_resolution_clock::now(); + + // Loop over all registered init tasks + for (const auto &task : this->m_tasks) { + + // Construct a new task callback + this->createTask(task); + } + + // Check every 100ms if all tasks have run + while (true) { + { + std::scoped_lock lock(this->m_tasksMutex); + if (this->m_completedTaskCount >= this->m_totalTaskCount) + break; + } + + std::this_thread::sleep_for(100ms); + } + + auto endTime = std::chrono::high_resolution_clock::now(); + + auto milliseconds = std::chrono::duration_cast(endTime - startTime).count(); + log::info("ImHex fully started in {}ms", milliseconds); + + // Small extra delay so the last progress step is visible + std::this_thread::sleep_for(100ms); + + return this->m_taskStatus.load(); + }); + } + FrameResult WindowSplash::fullFrame() { glfwSetWindowSize(this->m_window, 640_scaled, 400_scaled); diff --git a/main/gui/source/init/tasks.cpp b/main/gui/source/init/tasks.cpp index 341809448..497abeed2 100644 --- a/main/gui/source/init/tasks.cpp +++ b/main/gui/source/init/tasks.cpp @@ -317,7 +317,7 @@ namespace hex::init { { "Setting up environment", setupEnvironment, false }, { "Creating directories", createDirectories, false }, { "Loading settings", loadSettings, false }, - { "Loading plugins", loadPlugins, true }, + { "Loading plugins", loadPlugins, false }, }; } diff --git a/plugins/builtin/source/content/welcome_screen.cpp b/plugins/builtin/source/content/welcome_screen.cpp index 60fbeb2bc..639a899c4 100644 --- a/plugins/builtin/source/content/welcome_screen.cpp +++ b/plugins/builtin/source/content/welcome_screen.cpp @@ -596,21 +596,25 @@ namespace hex::plugin::builtin { const auto infoBannerPath = defaultPath / "info_banner.png"; if (wolv::io::fs::exists(infoBannerPath)) { s_infoBannerTexture = ImGuiExt::Texture(wolv::util::toUTF8String(infoBannerPath).c_str()); - break; - } else { - TaskManager::createBackgroundTask("Load banner", [](auto&) { - HttpRequest request("GET", ImHexApiURL + std::string("/info_banner")); - auto response = request.downloadFile().get(); - if (response.isSuccess()) { - const auto &data = response.getData(); - TaskManager::doLater([data] { - s_infoBannerTexture = ImGuiExt::Texture(data.data(), data.size()); - }); - } - }); + if (s_infoBannerTexture.isValid()) + break; } } + + if (!s_infoBannerTexture.isValid()) { + TaskManager::createBackgroundTask("Load banner", [](auto&) { + HttpRequest request("GET", ImHexApiURL + std::string("/info_banner")); + auto response = request.downloadFile().get(); + + if (response.isSuccess()) { + const auto &data = response.getData(); + TaskManager::doLater([data] { + s_infoBannerTexture = ImGuiExt::Texture(data.data(), data.size()); + }); + } + }); + } }); }