Skip to content

Commit d17e163

Browse files
committed
build: add IMHEX_ENABLE_UPDATER
Signed-off-by: xtex <[email protected]>
1 parent a16e168 commit d17e163

File tree

7 files changed

+294
-269
lines changed

7 files changed

+294
-269
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ option(IMHEX_ENABLE_PRECOMPILED_HEADERS "Enable precompiled headers"
1919
option(IMHEX_ENABLE_CXX_MODULES "Enable C++20 Module compilation. Testing only!" OFF)
2020
option(IMHEX_ENABLE_CPPCHECK "Enable cppcheck static analysis" OFF)
2121
option(IMHEX_BUNDLE_PLUGIN_SDK "Enable bundling of Plugin SDK into install package" ON )
22+
option(IMHEX_ENABLE_UPDATER "Enable automatic updater" ON )
2223
## Testing
2324
option(IMHEX_ENABLE_UNIT_TESTS "Enable building unit tests" ON )
2425
option(IMHEX_ENABLE_IMGUI_TEST_ENGINE "Enable the ImGui Test Engine" OFF)

cmake/build_helpers.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ macro(addDefines)
135135
if (IMHEX_STATIC_LINK_PLUGINS)
136136
add_compile_definitions(IMHEX_STATIC_LINK_PLUGINS)
137137
endif ()
138+
139+
if (IMHEX_ENABLE_UPDATER)
140+
add_compile_definitions(IMHEX_ENABLE_UPDATER)
141+
endif()
138142
endmacro()
139143

140144
function(addDefineToSource SOURCE DEFINE)

lib/libimhex/source/api/imhex_api.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ namespace hex {
961961
}
962962

963963
std::optional<std::string> checkForUpdate() {
964-
#if defined(OS_WEB)
964+
#if defined(OS_WEB) || !defined(IMHEX_ENABLE_UPDATER)
965965
return std::nullopt;
966966
#else
967967
if (ImHexApi::System::isNightlyBuild()) {
@@ -1034,7 +1034,9 @@ namespace hex {
10341034
}
10351035

10361036
bool updateImHex(UpdateType updateType) {
1037-
#if defined(OS_WEB)
1037+
#if !defined(IMHEX_ENABLE_UPDATER)
1038+
return false;
1039+
#elif defined(OS_WEB)
10381040
switch (updateType) {
10391041
case UpdateType::Stable:
10401042
EM_ASM({ window.location.href = window.location.origin; });

main/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ if (WIN32)
99
add_subdirectory(forwarder)
1010
endif ()
1111

12-
if (NOT EMSCRIPTEN)
12+
if (IMHEX_ENABLE_UPDATER AND NOT EMSCRIPTEN)
1313
add_subdirectory(updater)
1414
endif ()

main/updater/source/main.cpp

Lines changed: 197 additions & 185 deletions
Large diffs are not rendered by default.

plugins/builtin/source/content/init_tasks.cpp

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -25,75 +25,77 @@ namespace hex::plugin::builtin {
2525

2626
using namespace std::literals::string_literals;
2727

28-
bool checkForUpdatesSync() {
29-
int checkForUpdates = ContentRegistry::Settings::read<int>("hex.builtin.setting.general", "hex.builtin.setting.general.server_contact", 2);
30-
if (checkForUpdates != 1)
31-
return true;
28+
#if defined(IMHEX_ENABLE_UPDATER)
29+
bool checkForUpdatesSync() {
30+
int checkForUpdates = ContentRegistry::Settings::read<int>("hex.builtin.setting.general", "hex.builtin.setting.general.server_contact", 2);
31+
if (checkForUpdates != 1)
32+
return true;
33+
34+
// Check if we should check for updates
35+
TaskManager::createBackgroundTask("Update Check", [] {
36+
const auto updateString = ImHexApi::System::checkForUpdate();
3237

33-
// Check if we should check for updates
34-
TaskManager::createBackgroundTask("Update Check", [] {
35-
const auto updateString = ImHexApi::System::checkForUpdate();
38+
if (!updateString.has_value())
39+
return;
3640

37-
if (!updateString.has_value())
38-
return;
41+
TaskManager::doLater([updateString] {
42+
ContentRegistry::UserInterface::addTitleBarButton(ICON_TA_DOWNLOAD, ImGuiCustomCol_ToolbarGreen, "hex.builtin.welcome.update.title", [] {
43+
ImHexApi::System::updateImHex(ImHexApi::System::isNightlyBuild() ? ImHexApi::System::UpdateType::Nightly : ImHexApi::System::UpdateType::Stable);
44+
});
3945

40-
TaskManager::doLater([updateString] {
41-
ContentRegistry::UserInterface::addTitleBarButton(ICON_TA_DOWNLOAD, ImGuiCustomCol_ToolbarGreen, "hex.builtin.welcome.update.title", [] {
42-
ImHexApi::System::updateImHex(ImHexApi::System::isNightlyBuild() ? ImHexApi::System::UpdateType::Nightly : ImHexApi::System::UpdateType::Stable);
46+
ui::ToastInfo::open(fmt::format("hex.builtin.welcome.update.desc"_lang, *updateString));
4347
});
48+
});
4449

45-
ui::ToastInfo::open(fmt::format("hex.builtin.welcome.update.desc"_lang, *updateString));
50+
// Check if there is a telemetry uuid
51+
auto uuid = ContentRegistry::Settings::read<std::string>("hex.builtin.setting.general", "hex.builtin.setting.general.uuid", "");
52+
if (uuid.empty()) {
53+
// Generate a new uuid
54+
uuid = wolv::hash::generateUUID();
55+
// Save
56+
ContentRegistry::Settings::write<std::string>("hex.builtin.setting.general", "hex.builtin.setting.general.uuid", uuid);
57+
}
58+
59+
TaskManager::createBackgroundTask("hex.builtin.task.sending_statistics", [uuid](auto&) {
60+
// To avoid potentially flooding our database with lots of dead users
61+
// from people just visiting the website, don't send telemetry data from
62+
// the web version
63+
#if defined(OS_WEB)
64+
return;
65+
#endif
66+
67+
// Make telemetry request
68+
nlohmann::json telemetry = {
69+
{ "uuid", uuid },
70+
{ "format_version", "1" },
71+
{ "imhex_version", ImHexApi::System::getImHexVersion().get(false) },
72+
{ "imhex_commit", fmt::format("{}@{}", ImHexApi::System::getCommitHash(true), ImHexApi::System::getCommitBranch()) },
73+
{ "install_type", ImHexApi::System::isPortableVersion() ? "Portable" : "Installed" },
74+
{ "os", ImHexApi::System::getOSName() },
75+
{ "os_version", ImHexApi::System::getOSVersion() },
76+
{ "arch", ImHexApi::System::getArchitecture() },
77+
{ "gpu_vendor", ImHexApi::System::getGPUVendor() },
78+
{ "corporate_env", ImHexApi::System::isCorporateEnvironment() }
79+
};
80+
81+
HttpRequest telemetryRequest("POST", ImHexApiURL + "/telemetry"s);
82+
telemetryRequest.setTimeout(500);
83+
84+
telemetryRequest.setBody(telemetry.dump());
85+
telemetryRequest.addHeader("Content-Type", "application/json");
86+
87+
// Execute request
88+
telemetryRequest.execute();
4689
});
47-
});
4890

49-
// Check if there is a telemetry uuid
50-
auto uuid = ContentRegistry::Settings::read<std::string>("hex.builtin.setting.general", "hex.builtin.setting.general.uuid", "");
51-
if (uuid.empty()) {
52-
// Generate a new uuid
53-
uuid = wolv::hash::generateUUID();
54-
// Save
55-
ContentRegistry::Settings::write<std::string>("hex.builtin.setting.general", "hex.builtin.setting.general.uuid", uuid);
91+
return true;
5692
}
5793

58-
TaskManager::createBackgroundTask("hex.builtin.task.sending_statistics", [uuid](auto&) {
59-
// To avoid potentially flooding our database with lots of dead users
60-
// from people just visiting the website, don't send telemetry data from
61-
// the web version
62-
#if defined(OS_WEB)
63-
return;
64-
#endif
65-
66-
// Make telemetry request
67-
nlohmann::json telemetry = {
68-
{ "uuid", uuid },
69-
{ "format_version", "1" },
70-
{ "imhex_version", ImHexApi::System::getImHexVersion().get(false) },
71-
{ "imhex_commit", fmt::format("{}@{}", ImHexApi::System::getCommitHash(true), ImHexApi::System::getCommitBranch()) },
72-
{ "install_type", ImHexApi::System::isPortableVersion() ? "Portable" : "Installed" },
73-
{ "os", ImHexApi::System::getOSName() },
74-
{ "os_version", ImHexApi::System::getOSVersion() },
75-
{ "arch", ImHexApi::System::getArchitecture() },
76-
{ "gpu_vendor", ImHexApi::System::getGPUVendor() },
77-
{ "corporate_env", ImHexApi::System::isCorporateEnvironment() }
78-
};
79-
80-
HttpRequest telemetryRequest("POST", ImHexApiURL + "/telemetry"s);
81-
telemetryRequest.setTimeout(500);
82-
83-
telemetryRequest.setBody(telemetry.dump());
84-
telemetryRequest.addHeader("Content-Type", "application/json");
85-
86-
// Execute request
87-
telemetryRequest.execute();
88-
});
89-
90-
return true;
91-
}
92-
93-
bool checkForUpdates() {
94-
TaskManager::createBackgroundTask("hex.builtin.task.check_updates", [](auto&) { checkForUpdatesSync(); });
95-
return true;
96-
}
94+
bool checkForUpdates() {
95+
TaskManager::createBackgroundTask("hex.builtin.task.check_updates", [](auto&) { checkForUpdatesSync(); });
96+
return true;
97+
}
98+
#endif
9799

98100
bool configureUIScale() {
99101
EventDPIChanged::subscribe([](float, float newScaling) {
@@ -140,6 +142,8 @@ namespace hex::plugin::builtin {
140142
void addInitTasks() {
141143
ImHexApi::System::addStartupTask("Load Window Settings", false, loadWindowSettings);
142144
ImHexApi::System::addStartupTask("Configuring UI scale", false, configureUIScale);
143-
ImHexApi::System::addStartupTask("Checking for updates", true, checkForUpdates);
145+
#if defined(IMHEX_ENABLE_UPDATER)
146+
ImHexApi::System::addStartupTask("Checking for updates", true, checkForUpdates);
147+
#endif
144148
}
145149
}

plugins/builtin/source/content/main_menu_items.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -670,29 +670,31 @@ namespace hex::plugin::builtin {
670670

671671
ContentRegistry::UserInterface::addMenuItemSeparator({ "hex.builtin.menu.extras" }, 2600);
672672

673-
ContentRegistry::UserInterface::addMenuItem({ "hex.builtin.menu.extras", "hex.builtin.menu.extras.check_for_update" }, ICON_VS_SYNC, 2700, Shortcut::None, [] {
674-
TaskManager::createBackgroundTask("Checking for updates", [] {
675-
auto versionString = ImHexApi::System::checkForUpdate();
676-
if (!versionString.has_value()) {
677-
ui::ToastInfo::open("hex.builtin.popup.no_update_available"_lang);
678-
return;
679-
}
673+
#if defined(IMHEX_ENABLE_UPDATER)
674+
ContentRegistry::UserInterface::addMenuItem({ "hex.builtin.menu.extras", "hex.builtin.menu.extras.check_for_update" }, ICON_VS_SYNC, 2700, Shortcut::None, [] {
675+
TaskManager::createBackgroundTask("Checking for updates", [] {
676+
auto versionString = ImHexApi::System::checkForUpdate();
677+
if (!versionString.has_value()) {
678+
ui::ToastInfo::open("hex.builtin.popup.no_update_available"_lang);
679+
return;
680+
}
680681

681-
ui::PopupQuestion::open(fmt::format(fmt::runtime("hex.builtin.popup.update_available"_lang.get()), versionString.value()), [] {
682-
ImHexApi::System::updateImHex(ImHexApi::System::isNightlyBuild() ? ImHexApi::System::UpdateType::Nightly : ImHexApi::System::UpdateType::Stable);
683-
}, [] { });
682+
ui::PopupQuestion::open(fmt::format(fmt::runtime("hex.builtin.popup.update_available"_lang.get()), versionString.value()), [] {
683+
ImHexApi::System::updateImHex(ImHexApi::System::isNightlyBuild() ? ImHexApi::System::UpdateType::Nightly : ImHexApi::System::UpdateType::Stable);
684+
}, [] { });
685+
});
684686
});
685-
});
686687

687-
if (ImHexApi::System::isNightlyBuild()) {
688-
ContentRegistry::UserInterface::addMenuItem({ "hex.builtin.menu.extras", "hex.builtin.menu.extras.switch_to_stable" }, ICON_VS_ROCKET, 2750, Shortcut::None, [] {
689-
ImHexApi::System::updateImHex(ImHexApi::System::UpdateType::Stable);
690-
});
691-
} else {
692-
ContentRegistry::UserInterface::addMenuItem({ "hex.builtin.menu.extras", "hex.builtin.menu.extras.switch_to_nightly" }, ICON_VS_ROCKET, 2750, Shortcut::None, [] {
693-
ImHexApi::System::updateImHex(ImHexApi::System::UpdateType::Nightly);
694-
});
695-
}
688+
if (ImHexApi::System::isNightlyBuild()) {
689+
ContentRegistry::UserInterface::addMenuItem({ "hex.builtin.menu.extras", "hex.builtin.menu.extras.switch_to_stable" }, ICON_VS_ROCKET, 2750, Shortcut::None, [] {
690+
ImHexApi::System::updateImHex(ImHexApi::System::UpdateType::Stable);
691+
});
692+
} else {
693+
ContentRegistry::UserInterface::addMenuItem({ "hex.builtin.menu.extras", "hex.builtin.menu.extras.switch_to_nightly" }, ICON_VS_ROCKET, 2750, Shortcut::None, [] {
694+
ImHexApi::System::updateImHex(ImHexApi::System::UpdateType::Nightly);
695+
});
696+
}
697+
#endif
696698
}
697699

698700
static void createHelpMenu() {

0 commit comments

Comments
 (0)