2023-11-28 00:19:42 +01:00
|
|
|
#include <hex/api/imhex_api.hpp>
|
|
|
|
#include <hex/api/content_registry.hpp>
|
|
|
|
#include <hex/api_urls.hpp>
|
|
|
|
|
|
|
|
#include <hex/api/task_manager.hpp>
|
|
|
|
#include <hex/helpers/http_requests.hpp>
|
2023-11-28 10:56:56 +01:00
|
|
|
#include <hex/helpers/logger.hpp>
|
2023-11-28 00:19:42 +01:00
|
|
|
|
|
|
|
#include <wolv/hash/uuid.hpp>
|
|
|
|
|
|
|
|
namespace hex::plugin::builtin {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using namespace std::literals::string_literals;
|
|
|
|
|
|
|
|
bool checkForUpdatesSync() {
|
2024-02-03 12:16:36 +01:00
|
|
|
int checkForUpdates = ContentRegistry::Settings::read<int>("hex.builtin.setting.general", "hex.builtin.setting.general.server_contact", 2);
|
2023-11-28 00:19:42 +01:00
|
|
|
|
|
|
|
// Check if we should check for updates
|
2023-12-11 23:05:35 +01:00
|
|
|
if (checkForUpdates == 1) {
|
2023-11-28 00:19:42 +01:00
|
|
|
HttpRequest request("GET", GitHubApiURL + "/releases/latest"s);
|
|
|
|
|
|
|
|
// Query the GitHub API for the latest release version
|
|
|
|
auto response = request.execute().get();
|
|
|
|
if (response.getStatusCode() != 200)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
nlohmann::json releases;
|
|
|
|
try {
|
|
|
|
releases = nlohmann::json::parse(response.getData());
|
|
|
|
} catch (const std::exception &) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the response is valid
|
|
|
|
if (!releases.contains("tag_name") || !releases["tag_name"].is_string())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Convert the current version string to a format that can be compared to the latest release
|
|
|
|
auto versionString = ImHexApi::System::getImHexVersion();
|
|
|
|
size_t versionLength = std::min(versionString.find_first_of('-'), versionString.length());
|
|
|
|
auto currVersion = "v" + versionString.substr(0, versionLength);
|
|
|
|
|
|
|
|
// Get the latest release version string
|
|
|
|
auto latestVersion = releases["tag_name"].get<std::string_view>();
|
|
|
|
|
|
|
|
// Check if the latest release is different from the current version
|
|
|
|
if (latestVersion != currVersion)
|
|
|
|
ImHexApi::System::impl::addInitArgument("update-available", latestVersion.data());
|
|
|
|
|
|
|
|
// Check if there is a telemetry uuid
|
2024-02-03 12:16:36 +01:00
|
|
|
auto uuid = ContentRegistry::Settings::read<std::string>("hex.builtin.setting.general", "hex.builtin.setting.general.uuid", "");
|
2023-11-28 00:19:42 +01:00
|
|
|
if (uuid.empty()) {
|
|
|
|
// Generate a new uuid
|
|
|
|
uuid = wolv::hash::generateUUID();
|
|
|
|
// Save
|
2024-02-03 12:16:36 +01:00
|
|
|
ContentRegistry::Settings::write<std::string>("hex.builtin.setting.general", "hex.builtin.setting.general.uuid", uuid);
|
2023-11-28 00:19:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TaskManager::createBackgroundTask("Sending statistics...", [uuid, versionString](auto&) {
|
|
|
|
// To avoid potentially flooding our database with lots of dead users
|
|
|
|
// from people just visiting the website, don't send telemetry data from
|
|
|
|
// the web version
|
|
|
|
#if defined(OS_WEB)
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Make telemetry request
|
|
|
|
nlohmann::json telemetry = {
|
|
|
|
{ "uuid", uuid },
|
|
|
|
{ "format_version", "1" },
|
|
|
|
{ "imhex_version", versionString },
|
|
|
|
{ "imhex_commit", fmt::format("{}@{}", ImHexApi::System::getCommitHash(true), ImHexApi::System::getCommitBranch()) },
|
|
|
|
{ "install_type", ImHexApi::System::isPortableVersion() ? "Portable" : "Installed" },
|
|
|
|
{ "os", ImHexApi::System::getOSName() },
|
|
|
|
{ "os_version", ImHexApi::System::getOSVersion() },
|
|
|
|
{ "arch", ImHexApi::System::getArchitecture() },
|
|
|
|
{ "gpu_vendor", ImHexApi::System::getGPUVendor() }
|
|
|
|
};
|
|
|
|
|
|
|
|
HttpRequest telemetryRequest("POST", ImHexApiURL + "/telemetry"s);
|
|
|
|
telemetryRequest.setTimeout(500);
|
|
|
|
|
|
|
|
telemetryRequest.setBody(telemetry.dump());
|
|
|
|
telemetryRequest.addHeader("Content-Type", "application/json");
|
|
|
|
|
|
|
|
// Execute request
|
|
|
|
telemetryRequest.execute();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool checkForUpdates() {
|
|
|
|
TaskManager::createBackgroundTask("Checking for updates", [](auto&) { checkForUpdatesSync(); });
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool configureUIScale() {
|
2024-06-22 11:25:07 +02:00
|
|
|
int interfaceScaleSetting = int(ContentRegistry::Settings::read<float>("hex.builtin.setting.interface", "hex.builtin.setting.interface.scaling_factor", 1.0F) * 10.0F);
|
2023-11-28 00:19:42 +01:00
|
|
|
|
|
|
|
float interfaceScaling;
|
|
|
|
if (interfaceScaleSetting == 0)
|
|
|
|
interfaceScaling = ImHexApi::System::getNativeScale();
|
|
|
|
else
|
2023-12-10 22:37:26 +01:00
|
|
|
interfaceScaling = interfaceScaleSetting / 10.0F;
|
2023-11-28 00:19:42 +01:00
|
|
|
|
|
|
|
ImHexApi::System::impl::setGlobalScale(interfaceScaling);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-01-28 22:14:59 +01:00
|
|
|
bool loadWindowSettings() {
|
2024-02-03 12:16:36 +01:00
|
|
|
bool multiWindowEnabled = ContentRegistry::Settings::read<bool>("hex.builtin.setting.interface", "hex.builtin.setting.interface.multi_windows", false);
|
2024-01-28 22:14:59 +01:00
|
|
|
ImHexApi::System::impl::setMultiWindowMode(multiWindowEnabled);
|
|
|
|
|
2024-02-03 12:16:36 +01:00
|
|
|
bool restoreWindowPos = ContentRegistry::Settings::read<bool>("hex.builtin.setting.interface", "hex.builtin.setting.interface.restore_window_pos", false);
|
2024-01-28 22:14:59 +01:00
|
|
|
|
|
|
|
if (restoreWindowPos) {
|
|
|
|
ImHexApi::System::InitialWindowProperties properties = {};
|
|
|
|
|
2024-02-03 12:16:36 +01:00
|
|
|
properties.maximized = ContentRegistry::Settings::read<bool>("hex.builtin.setting.interface", "hex.builtin.setting.interface.window.maximized", 0);
|
|
|
|
properties.x = ContentRegistry::Settings::read<int>("hex.builtin.setting.interface", "hex.builtin.setting.interface.window.x", 0);
|
|
|
|
properties.y = ContentRegistry::Settings::read<int>("hex.builtin.setting.interface", "hex.builtin.setting.interface.window.y", 0);
|
|
|
|
properties.width = ContentRegistry::Settings::read<int>("hex.builtin.setting.interface", "hex.builtin.setting.interface.window.width", 0);
|
|
|
|
properties.height = ContentRegistry::Settings::read<int>("hex.builtin.setting.interface", "hex.builtin.setting.interface.window.height", 0);
|
2024-01-28 22:14:59 +01:00
|
|
|
|
|
|
|
ImHexApi::System::impl::setInitialWindowProperties(properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-28 00:19:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void addInitTasks() {
|
2024-01-28 22:14:59 +01:00
|
|
|
ImHexApi::System::addStartupTask("Load Window Settings", false, loadWindowSettings);
|
2023-12-10 22:37:26 +01:00
|
|
|
ImHexApi::System::addStartupTask("Configuring UI scale", true, configureUIScale);
|
2023-11-28 00:19:42 +01:00
|
|
|
ImHexApi::System::addStartupTask("Checking for updates", true, checkForUpdates);
|
|
|
|
}
|
|
|
|
}
|