1
0
mirror of synced 2024-11-24 07:40:17 +01:00

refactor: More init sequence refactors

This commit is contained in:
WerWolv 2023-11-28 00:47:03 +01:00
parent 284f8534ab
commit f6d4d5ab22
4 changed files with 59 additions and 47 deletions

View File

@ -65,8 +65,8 @@ namespace hex::init {
std::future<bool> processTasksAsync();
std::atomic<u32> m_totalTaskCount, m_completedTaskCount;
std::atomic<bool> m_taskStatus;
std::vector<Task> m_tasks;
std::atomic<bool> m_taskStatus = true;
std::list<Task> m_tasks;
std::mutex m_tasksMutex;
std::string m_gpuVendor;

View File

@ -86,33 +86,6 @@ namespace hex::init {
glfwSetWindowPos(window, monitorX + (mode->width - windowWidth) / 2, monitorY + (mode->height - windowHeight) / 2);
}
std::future<bool> 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<std::chrono::milliseconds>(endTime - startTime).count()
);
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(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<bool> 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<std::chrono::milliseconds>(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);

View File

@ -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 },
};
}

View File

@ -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());
});
}
});
}
});
}