From 6a97b361d2cee5c5c17386152e6fbf8c345abe95 Mon Sep 17 00:00:00 2001 From: fed <58637860+fedddddd@users.noreply.github.com> Date: Mon, 11 Dec 2023 03:18:03 +0100 Subject: [PATCH] Load motd asynchronously --- src/client/component/dvars.cpp | 2 +- src/client/component/motd.cpp | 31 ++++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/client/component/dvars.cpp b/src/client/component/dvars.cpp index 64d4101b..d658b0a2 100644 --- a/src/client/component/dvars.cpp +++ b/src/client/component/dvars.cpp @@ -463,4 +463,4 @@ namespace dvars }; } -REGISTER_COMPONENT(dvars::component) \ No newline at end of file +REGISTER_COMPONENT(dvars::component) diff --git a/src/client/component/motd.cpp b/src/client/component/motd.cpp index f63dc757..42bd0bd7 100644 --- a/src/client/component/motd.cpp +++ b/src/client/component/motd.cpp @@ -36,6 +36,8 @@ namespace motd cached_file_header header{}; std::string data; }; + + std::atomic_bool killed; std::filesystem::path get_cache_folder() { @@ -147,6 +149,11 @@ namespace motd std::optional download_image(const std::string& url) { + if (killed) + { + return {}; + } + const auto cached = read_cached_file(url); if (cached.has_value()) { @@ -368,12 +375,25 @@ namespace motd }); } + std::thread init_thread; + class component final : public component_interface { public: + void post_start() override + { + init_thread = std::thread([] + { + init(); + }); + } + void post_unpack() override { - init(); + if (init_thread.joinable()) + { + init_thread.join(); + } command::add("reloadmotd", []() { @@ -385,6 +405,15 @@ namespace motd init(false); }); } + + void pre_destroy() override + { + killed = true; + if (init_thread.joinable()) + { + init_thread.join(); + } + } }; }