2021-04-20 21:46:48 +02:00
|
|
|
#include "init/tasks.hpp"
|
|
|
|
|
2021-08-31 15:22:00 +02:00
|
|
|
#include <imgui.h>
|
2022-12-05 15:29:19 +01:00
|
|
|
#include <romfs/romfs.hpp>
|
2021-08-31 15:22:00 +02:00
|
|
|
|
2023-05-11 23:21:52 +02:00
|
|
|
#include <hex/helpers/http_requests.hpp>
|
|
|
|
#include <hex/helpers/fs.hpp>
|
|
|
|
#include <hex/helpers/logger.hpp>
|
|
|
|
|
2023-04-19 21:56:34 +02:00
|
|
|
#include <hex/api_urls.hpp>
|
2021-04-20 21:46:48 +02:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2022-08-20 13:43:26 +02:00
|
|
|
#include <hex/api/project_file_manager.hpp>
|
2023-01-05 09:29:16 +01:00
|
|
|
#include <hex/api/theme_manager.hpp>
|
2023-05-11 23:21:52 +02:00
|
|
|
#include <hex/api/plugin_manager.hpp>
|
|
|
|
#include <hex/api/layout_manager.hpp>
|
2023-08-06 21:33:15 +02:00
|
|
|
#include <hex/api/achievement_manager.hpp>
|
2023-05-11 23:21:52 +02:00
|
|
|
|
2023-04-08 12:08:45 +02:00
|
|
|
#include <hex/ui/view.hpp>
|
|
|
|
#include <hex/ui/popup.hpp>
|
2021-04-20 21:46:48 +02:00
|
|
|
|
2022-07-26 14:59:08 +02:00
|
|
|
#include <fonts/fontawesome_font.h>
|
|
|
|
#include <fonts/codicons_font.h>
|
|
|
|
#include <fonts/unifont_font.h>
|
2021-08-31 15:22:00 +02:00
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
#include <filesystem>
|
|
|
|
|
2021-08-29 14:18:45 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
#include <wolv/io/fs.hpp>
|
|
|
|
#include <wolv/io/file.hpp>
|
2023-06-20 11:55:56 +02:00
|
|
|
#include <wolv/hash/uuid.hpp>
|
2023-03-12 18:27:29 +01:00
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
namespace hex::init {
|
|
|
|
|
2021-08-29 14:18:45 +02:00
|
|
|
using namespace std::literals::string_literals;
|
|
|
|
|
2023-10-26 23:45:43 +02:00
|
|
|
static bool checkForUpdatesSync() {
|
2023-06-20 11:55:56 +02:00
|
|
|
int checkForUpdates = ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.server_contact", 2);
|
2023-02-17 12:03:53 +01:00
|
|
|
|
|
|
|
// Check if we should check for updates
|
2023-06-20 11:55:56 +02:00
|
|
|
if (checkForUpdates == 1){
|
2023-03-23 11:23:07 +01:00
|
|
|
HttpRequest request("GET", GitHubApiURL + "/releases/latest"s);
|
2021-04-20 21:46:48 +02:00
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Query the GitHub API for the latest release version
|
2023-03-23 11:23:07 +01:00
|
|
|
auto response = request.execute().get();
|
|
|
|
if (response.getStatusCode() != 200)
|
2022-10-29 23:43:40 +02:00
|
|
|
return false;
|
2021-04-20 21:46:48 +02:00
|
|
|
|
2023-03-23 11:23:07 +01:00
|
|
|
nlohmann::json releases;
|
|
|
|
try {
|
|
|
|
releases = nlohmann::json::parse(response.getData());
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Check if the response is valid
|
2023-03-23 11:23:07 +01:00
|
|
|
if (!releases.contains("tag_name") || !releases["tag_name"].is_string())
|
2022-10-29 23:43:40 +02:00
|
|
|
return false;
|
2021-04-20 21:46:48 +02:00
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Convert the current version string to a format that can be compared to the latest release
|
2023-06-26 14:01:45 +02:00
|
|
|
auto versionString = ImHexApi::System::getImHexVersion();
|
2022-10-29 23:43:40 +02:00
|
|
|
size_t versionLength = std::min(versionString.find_first_of('-'), versionString.length());
|
|
|
|
auto currVersion = "v" + versionString.substr(0, versionLength);
|
2023-02-17 12:03:53 +01:00
|
|
|
|
|
|
|
// Get the latest release version string
|
2023-03-23 11:23:07 +01:00
|
|
|
auto latestVersion = releases["tag_name"].get<std::string_view>();
|
2021-04-20 21:46:48 +02:00
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Check if the latest release is different from the current version
|
2022-10-29 23:43:40 +02:00
|
|
|
if (latestVersion != currVersion)
|
|
|
|
ImHexApi::System::impl::addInitArgument("update-available", latestVersion.data());
|
2021-04-20 21:46:48 +02:00
|
|
|
|
2023-06-20 11:55:56 +02:00
|
|
|
// Check if there is a telemetry uuid
|
2023-10-21 23:07:33 +02:00
|
|
|
std::string uuid = ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.uuid", "").get<std::string>();
|
2023-06-20 11:55:56 +02:00
|
|
|
if(uuid.empty()) {
|
|
|
|
// Generate a new uuid
|
|
|
|
uuid = wolv::hash::generateUUID();
|
|
|
|
// Save
|
|
|
|
ContentRegistry::Settings::write("hex.builtin.setting.general", "hex.builtin.setting.general.uuid", uuid);
|
|
|
|
}
|
|
|
|
|
2023-06-26 14:01:45 +02:00
|
|
|
TaskManager::createBackgroundTask("Sending statistics...", [uuid, versionString](auto&) {
|
2023-10-04 12:00:32 +02:00
|
|
|
// 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
|
|
|
|
|
2023-06-26 14:01:45 +02:00
|
|
|
// Make telemetry request
|
|
|
|
nlohmann::json telemetry = {
|
|
|
|
{ "uuid", uuid },
|
|
|
|
{ "format_version", "1" },
|
|
|
|
{ "imhex_version", versionString },
|
2023-06-27 01:46:11 +02:00
|
|
|
{ "imhex_commit", fmt::format("{}@{}", ImHexApi::System::getCommitHash(true), ImHexApi::System::getCommitBranch()) },
|
2023-06-26 14:01:45 +02:00
|
|
|
{ "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();
|
|
|
|
});
|
2022-10-29 23:43:40 +02:00
|
|
|
}
|
2021-04-20 21:46:48 +02:00
|
|
|
return true;
|
|
|
|
}
|
2023-10-26 23:45:43 +02:00
|
|
|
|
|
|
|
static bool checkForUpdates() {
|
|
|
|
TaskManager::createBackgroundTask("Checking for updates", [](auto&) { checkForUpdatesSync(); });
|
|
|
|
return true;
|
|
|
|
}
|
2021-04-20 21:46:48 +02:00
|
|
|
|
2022-12-05 15:29:19 +01:00
|
|
|
bool setupEnvironment() {
|
|
|
|
hex::log::debug("Using romfs: '{}'", romfs::name());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
bool createDirectories() {
|
2021-05-21 23:46:36 +02:00
|
|
|
bool result = true;
|
|
|
|
|
2022-08-14 10:07:45 +02:00
|
|
|
using enum fs::ImHexPath;
|
2021-04-20 21:46:48 +02:00
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Try to create all default directories
|
2022-10-02 14:18:40 +02:00
|
|
|
for (u32 path = 0; path < u32(fs::ImHexPath::END); path++) {
|
|
|
|
for (auto &folder : fs::getDefaultPaths(static_cast<fs::ImHexPath>(path), true)) {
|
2021-05-21 23:46:36 +02:00
|
|
|
try {
|
2023-03-12 18:27:29 +01:00
|
|
|
wolv::io::fs::createDirectories(folder);
|
2021-05-21 23:46:36 +02:00
|
|
|
} catch (...) {
|
2023-03-12 18:43:05 +01:00
|
|
|
log::error("Failed to create folder {}!", wolv::util::toUTF8String(folder));
|
2021-05-21 23:46:36 +02:00
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-20 21:46:48 +02:00
|
|
|
|
2021-05-21 23:46:36 +02:00
|
|
|
if (!result)
|
2022-09-19 16:54:19 +02:00
|
|
|
ImHexApi::System::impl::addInitArgument("folder-creation-error");
|
2021-05-21 23:46:36 +02:00
|
|
|
|
|
|
|
return result;
|
2021-04-20 21:46:48 +02:00
|
|
|
}
|
|
|
|
|
2023-03-26 11:02:23 +02:00
|
|
|
bool migrateConfig(){
|
|
|
|
|
2023-04-08 12:08:45 +02:00
|
|
|
// Check if there is a new config in folder
|
2023-03-26 11:02:23 +02:00
|
|
|
auto configPaths = hex::fs::getDefaultPaths(hex::fs::ImHexPath::Config, false);
|
2023-04-08 12:08:45 +02:00
|
|
|
|
2023-03-26 11:02:23 +02:00
|
|
|
// There should always be exactly one config path on Linux
|
|
|
|
std::fs::path newConfigPath = configPaths[0];
|
|
|
|
wolv::io::File newConfigFile(newConfigPath / "settings.json", wolv::io::File::Mode::Read);
|
|
|
|
if (!newConfigFile.isValid()) {
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// Find an old config
|
2023-03-26 11:02:23 +02:00
|
|
|
std::fs::path oldConfigPath;
|
2023-04-08 12:08:45 +02:00
|
|
|
for (const auto &dir : hex::fs::appendPath(hex::fs::getDataPaths(), "config")) {
|
2023-03-26 11:02:23 +02:00
|
|
|
wolv::io::File oldConfigFile(dir / "settings.json", wolv::io::File::Mode::Read);
|
|
|
|
if (oldConfigFile.isValid()) {
|
|
|
|
oldConfigPath = dir;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!oldConfigPath.empty()) {
|
2023-04-08 12:08:45 +02:00
|
|
|
log::info("Found config file in {}! Migrating to {}", oldConfigPath.string(), newConfigPath.string());
|
2023-03-26 11:02:23 +02:00
|
|
|
|
|
|
|
std::fs::rename(oldConfigPath / "settings.json", newConfigPath / "settings.json");
|
|
|
|
wolv::io::File oldIniFile(oldConfigPath / "interface.ini", wolv::io::File::Mode::Read);
|
|
|
|
if (oldIniFile.isValid()) {
|
|
|
|
std::fs::rename(oldConfigPath / "interface.ini", newConfigPath / "interface.ini");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::fs::remove(oldConfigPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-14 09:25:37 +01:00
|
|
|
static bool loadFontsImpl(bool loadUnicode) {
|
2023-07-22 19:35:58 +02:00
|
|
|
const float defaultFontSize = ImHexApi::System::DefaultFontSize * std::round(ImHexApi::System::getGlobalScale());
|
|
|
|
|
2023-06-30 13:40:39 +02:00
|
|
|
// Load font related settings
|
|
|
|
{
|
2023-10-21 23:07:33 +02:00
|
|
|
std::fs::path fontFile = ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.font_path", "").get<std::string>();
|
2023-07-24 23:24:11 +02:00
|
|
|
if (!fontFile.empty()) {
|
|
|
|
if (!wolv::io::fs::exists(fontFile) || !wolv::io::fs::isRegularFile(fontFile)) {
|
|
|
|
log::warn("Custom font file {} not found! Falling back to default font.", wolv::util::toUTF8String(fontFile));
|
|
|
|
fontFile.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
log::info("Loading custom font from {}", wolv::util::toUTF8String(fontFile));
|
2023-07-24 17:25:36 +02:00
|
|
|
}
|
2023-06-30 13:40:39 +02:00
|
|
|
|
|
|
|
// If no custom font has been specified, search for a file called "font.ttf" in one of the resource folders
|
|
|
|
if (fontFile.empty()) {
|
|
|
|
for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Resources)) {
|
|
|
|
auto path = dir / "font.ttf";
|
|
|
|
if (wolv::io::fs::exists(path)) {
|
2023-07-24 23:24:11 +02:00
|
|
|
log::info("Loading custom font from {}", wolv::util::toUTF8String(path));
|
2023-06-30 13:40:39 +02:00
|
|
|
|
|
|
|
fontFile = path;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ImHexApi::System::impl::setCustomFontPath(fontFile);
|
|
|
|
|
|
|
|
// If a custom font has been loaded now, also load the font size
|
2023-07-22 19:35:58 +02:00
|
|
|
float fontSize = defaultFontSize;
|
2023-06-30 13:40:39 +02:00
|
|
|
if (!fontFile.empty()) {
|
2023-10-21 23:07:33 +02:00
|
|
|
fontSize = ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.font_size", 13).get<int>() * ImHexApi::System::getGlobalScale();
|
2023-06-30 13:40:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ImHexApi::System::impl::setFontSize(fontSize);
|
|
|
|
}
|
|
|
|
|
2022-11-08 18:09:48 +01:00
|
|
|
float fontSize = ImHexApi::System::getFontSize();
|
|
|
|
|
|
|
|
const auto &fontFile = ImHexApi::System::getCustomFontPath();
|
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Setup basic font configuration
|
2022-02-01 22:09:44 +01:00
|
|
|
auto fonts = IM_NEW(ImFontAtlas)();
|
2022-02-01 18:09:40 +01:00
|
|
|
ImFontConfig cfg = {};
|
2023-06-24 10:58:42 +02:00
|
|
|
cfg.OversampleH = cfg.OversampleV = 1, cfg.PixelSnapH = true;
|
2022-11-14 10:02:34 +01:00
|
|
|
cfg.SizePixels = fontSize;
|
2021-08-31 15:22:00 +02:00
|
|
|
|
2023-03-14 12:30:28 +01:00
|
|
|
fonts->Flags |= ImFontAtlasFlags_NoPowerOfTwoHeight;
|
2023-10-06 14:28:10 +02:00
|
|
|
fonts->TexDesiredWidth = 4096;
|
2023-03-14 12:30:28 +01:00
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Configure font glyph ranges that should be loaded from the default font and unifont
|
2023-06-24 10:58:42 +02:00
|
|
|
static ImVector<ImWchar> ranges;
|
2021-08-31 15:22:00 +02:00
|
|
|
{
|
|
|
|
ImFontGlyphRangesBuilder glyphRangesBuilder;
|
2022-12-25 10:01:21 +01:00
|
|
|
|
|
|
|
{
|
2023-03-14 12:30:28 +01:00
|
|
|
constexpr static ImWchar controlCodeRange[] = { 0x0001, 0x001F, 0 };
|
2022-12-25 10:01:21 +01:00
|
|
|
constexpr static ImWchar extendedAsciiRange[] = { 0x007F, 0x00FF, 0 };
|
2023-03-14 12:30:28 +01:00
|
|
|
|
2022-12-25 10:01:21 +01:00
|
|
|
glyphRangesBuilder.AddRanges(controlCodeRange);
|
2023-03-14 12:30:28 +01:00
|
|
|
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesDefault());
|
2022-12-25 10:01:21 +01:00
|
|
|
glyphRangesBuilder.AddRanges(extendedAsciiRange);
|
|
|
|
}
|
|
|
|
|
2023-03-14 12:30:28 +01:00
|
|
|
if (loadUnicode) {
|
|
|
|
constexpr static ImWchar fullRange[] = { 0x0100, 0xFFEF, 0 };
|
|
|
|
|
|
|
|
glyphRangesBuilder.AddRanges(fullRange);
|
|
|
|
} else {
|
|
|
|
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesJapanese());
|
|
|
|
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesChineseFull());
|
|
|
|
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesCyrillic());
|
|
|
|
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesKorean());
|
|
|
|
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesThai());
|
|
|
|
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesVietnamese());
|
|
|
|
}
|
|
|
|
|
2021-08-31 15:22:00 +02:00
|
|
|
glyphRangesBuilder.BuildRanges(&ranges);
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Glyph range for font awesome icons
|
2023-06-24 10:58:42 +02:00
|
|
|
static ImWchar fontAwesomeRange[] = {
|
2022-12-14 09:25:37 +01:00
|
|
|
ICON_MIN_FA, ICON_MAX_FA, 0
|
2021-08-31 15:22:00 +02:00
|
|
|
};
|
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Glyph range for codicons icons
|
2023-06-24 10:58:42 +02:00
|
|
|
static ImWchar codiconsRange[] = {
|
2022-12-14 09:25:37 +01:00
|
|
|
ICON_MIN_VS, ICON_MAX_VS, 0
|
2021-08-31 15:22:00 +02:00
|
|
|
};
|
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Load main font
|
|
|
|
// If a custom font has been specified, load it, otherwise load the default ImGui font
|
2021-09-22 23:42:52 +02:00
|
|
|
if (fontFile.empty()) {
|
2021-08-31 15:22:00 +02:00
|
|
|
fonts->Clear();
|
|
|
|
fonts->AddFontDefault(&cfg);
|
|
|
|
} else {
|
2023-07-22 19:35:58 +02:00
|
|
|
auto font = fonts->AddFontFromFileTTF(wolv::util::toUTF8String(fontFile).c_str(), 0, &cfg, ranges.Data);
|
2023-07-24 17:25:36 +02:00
|
|
|
if (font == nullptr) {
|
|
|
|
log::warn("Failed to load custom font! Falling back to default font.");
|
|
|
|
|
2023-07-22 19:35:58 +02:00
|
|
|
ImHexApi::System::impl::setFontSize(defaultFontSize);
|
|
|
|
cfg.SizePixels = defaultFontSize;
|
|
|
|
fonts->Clear();
|
|
|
|
fonts->AddFontDefault(&cfg);
|
|
|
|
}
|
2021-08-31 15:22:00 +02:00
|
|
|
}
|
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Merge all fonts into one big font atlas
|
2021-08-31 15:22:00 +02:00
|
|
|
cfg.MergeMode = true;
|
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Add font awesome and codicons icons to font atlas
|
2023-06-10 15:08:56 +02:00
|
|
|
cfg.GlyphOffset = ImVec2(0, 3_scaled);
|
2022-11-14 10:02:34 +01:00
|
|
|
fonts->AddFontFromMemoryCompressedTTF(font_awesome_compressed_data, font_awesome_compressed_size, 0, &cfg, fontAwesomeRange);
|
|
|
|
fonts->AddFontFromMemoryCompressedTTF(codicons_compressed_data, codicons_compressed_size, 0, &cfg, codiconsRange);
|
2022-10-27 13:11:09 +02:00
|
|
|
|
2023-06-09 00:30:12 +02:00
|
|
|
cfg.GlyphOffset = ImVec2(0, 0);
|
2023-02-17 12:03:53 +01:00
|
|
|
// Add unifont if unicode support is enabled
|
2023-06-24 14:55:10 +02:00
|
|
|
fonts->AddFontFromMemoryCompressedTTF(unifont_compressed_data, unifont_compressed_size, 0, &cfg, ranges.Data);
|
2022-12-14 09:25:37 +01:00
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Try to build the font atlas
|
2022-12-14 09:25:37 +01:00
|
|
|
if (!fonts->Build()) {
|
2023-02-17 12:03:53 +01:00
|
|
|
// The main reason the font atlas failed to build is that the font is too big for the GPU to handle
|
|
|
|
// If unicode support is enabled, therefor try to load the font atlas without unicode support
|
|
|
|
// If that still didn't work, there's probably something else going on with the graphics drivers
|
|
|
|
// Especially Intel GPU drivers are known to have various bugs
|
|
|
|
|
2022-12-14 09:25:37 +01:00
|
|
|
if (loadUnicode) {
|
|
|
|
log::error("Failed to build font atlas! Disabling Unicode support.");
|
|
|
|
IM_DELETE(fonts);
|
2023-02-17 12:03:53 +01:00
|
|
|
|
|
|
|
// Disable unicode support in settings
|
2023-10-21 23:07:33 +02:00
|
|
|
ContentRegistry::Settings::write("hex.builtin.setting.general", "hex.builtin.setting.font.load_all_unicode_chars", false);
|
2023-02-17 12:03:53 +01:00
|
|
|
|
|
|
|
// Try to load the font atlas again
|
2022-12-14 09:25:37 +01:00
|
|
|
return loadFontsImpl(false);
|
|
|
|
} else {
|
|
|
|
log::error("Failed to build font atlas! Check your Graphics driver!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-08-31 15:22:00 +02:00
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Configure ImGui to use the font atlas
|
2022-02-01 18:09:40 +01:00
|
|
|
View::setFontAtlas(fonts);
|
|
|
|
View::setFontConfig(cfg);
|
|
|
|
|
2021-08-31 15:22:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-14 09:25:37 +01:00
|
|
|
bool loadFonts() {
|
2023-10-06 15:33:55 +02:00
|
|
|
// Check if unicode support is enabled in the settings and that the user doesn't use the No GPU version on Windows
|
|
|
|
// The Mesa3D software renderer on Windows identifies itself as "VMware, Inc."
|
2023-06-22 17:56:09 +02:00
|
|
|
bool shouldLoadUnicode =
|
2023-10-21 23:07:33 +02:00
|
|
|
ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.font.load_all_unicode_chars", false) &&
|
2023-06-22 17:56:09 +02:00
|
|
|
ImHexApi::System::getGPUVendor() != "VMware, Inc.";
|
|
|
|
|
|
|
|
return loadFontsImpl(shouldLoadUnicode);
|
2022-12-14 09:25:37 +01:00
|
|
|
}
|
|
|
|
|
2021-07-31 17:11:10 +02:00
|
|
|
bool deleteSharedData() {
|
2023-02-17 12:03:53 +01:00
|
|
|
// This function is called when ImHex is closed. It deletes all shared data that was created by plugins
|
|
|
|
// This is a bit of a hack but necessary because when ImHex gets closed, all plugins are unloaded in order for
|
|
|
|
// destructors to be called correctly. To prevent crashes when ImHex exits, we need to delete all shared data
|
|
|
|
|
2023-04-17 16:18:48 +02:00
|
|
|
EventManager::post<EventImHexClosing>();
|
2022-08-03 10:45:50 +02:00
|
|
|
|
2023-06-10 15:08:56 +02:00
|
|
|
EventManager::clear();
|
|
|
|
|
2022-06-27 17:01:21 +02:00
|
|
|
while (ImHexApi::Provider::isValid())
|
|
|
|
ImHexApi::Provider::remove(ImHexApi::Provider::get());
|
2023-03-21 15:33:43 +01:00
|
|
|
ContentRegistry::Provider::impl::getEntries().clear();
|
2022-06-27 17:01:21 +02:00
|
|
|
|
2022-02-04 00:47:39 +01:00
|
|
|
ImHexApi::System::getInitArguments().clear();
|
2022-05-27 20:42:07 +02:00
|
|
|
ImHexApi::HexEditor::impl::getBackgroundHighlights().clear();
|
|
|
|
ImHexApi::HexEditor::impl::getForegroundHighlights().clear();
|
|
|
|
ImHexApi::HexEditor::impl::getBackgroundHighlightingFunctions().clear();
|
|
|
|
ImHexApi::HexEditor::impl::getForegroundHighlightingFunctions().clear();
|
|
|
|
ImHexApi::HexEditor::impl::getTooltips().clear();
|
2022-06-25 12:19:59 +02:00
|
|
|
ImHexApi::HexEditor::impl::getTooltipFunctions().clear();
|
2023-06-30 13:40:16 +02:00
|
|
|
ImHexApi::System::getAdditionalFolderPaths().clear();
|
|
|
|
ImHexApi::System::getCustomFontPath().clear();
|
2023-07-13 14:08:23 +02:00
|
|
|
ImHexApi::Messaging::impl::getHandlers().clear();
|
2021-07-31 17:11:10 +02:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
ContentRegistry::Settings::impl::getSettings().clear();
|
2023-03-21 15:33:43 +01:00
|
|
|
ContentRegistry::Settings::impl::getSettingsData().clear();
|
2021-07-31 17:11:10 +02:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
ContentRegistry::CommandPaletteCommands::impl::getEntries().clear();
|
|
|
|
ContentRegistry::CommandPaletteCommands::impl::getHandlers().clear();
|
2022-06-25 12:19:59 +02:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
ContentRegistry::PatternLanguage::impl::getFunctions().clear();
|
|
|
|
ContentRegistry::PatternLanguage::impl::getPragmas().clear();
|
2023-01-20 21:16:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::impl::getVisualizers().clear();
|
2023-07-04 22:18:06 +02:00
|
|
|
ContentRegistry::PatternLanguage::impl::getInlineVisualizers().clear();
|
2022-06-25 12:19:59 +02:00
|
|
|
|
2023-04-08 12:08:45 +02:00
|
|
|
ContentRegistry::Views::impl::getEntries().clear();
|
|
|
|
impl::PopupBase::getOpenPopups().clear();
|
2021-07-31 17:11:10 +02:00
|
|
|
|
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
ContentRegistry::Tools::impl::getEntries().clear();
|
|
|
|
ContentRegistry::DataInspector::impl::getEntries().clear();
|
2021-07-31 17:11:10 +02:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
ContentRegistry::Language::impl::getLanguages().clear();
|
|
|
|
ContentRegistry::Language::impl::getLanguageDefinitions().clear();
|
2022-02-01 18:09:40 +01:00
|
|
|
LangEntry::resetLanguageStrings();
|
2021-07-31 17:11:10 +02:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
ContentRegistry::Interface::impl::getWelcomeScreenEntries().clear();
|
|
|
|
ContentRegistry::Interface::impl::getFooterItems().clear();
|
|
|
|
ContentRegistry::Interface::impl::getToolbarItems().clear();
|
|
|
|
ContentRegistry::Interface::impl::getMainMenuItems().clear();
|
|
|
|
ContentRegistry::Interface::impl::getMenuItems().clear();
|
|
|
|
ContentRegistry::Interface::impl::getSidebarItems().clear();
|
|
|
|
ContentRegistry::Interface::impl::getTitleBarButtons().clear();
|
2021-07-31 17:11:10 +02:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
ShortcutManager::clearShortcuts();
|
2021-07-31 17:11:10 +02:00
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
TaskManager::getRunningTasks().clear();
|
2022-01-09 21:57:22 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
ContentRegistry::DataProcessorNode::impl::getEntries().clear();
|
2021-07-31 17:11:10 +02:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
ContentRegistry::DataFormatter::impl::getEntries().clear();
|
|
|
|
ContentRegistry::FileHandler::impl::getEntries().clear();
|
2022-08-16 11:48:37 +02:00
|
|
|
ContentRegistry::Hashes::impl::getHashes().clear();
|
2023-06-21 17:48:51 +02:00
|
|
|
ContentRegistry::HexEditor::impl::getVisualizers().clear();
|
2021-07-31 17:11:10 +02:00
|
|
|
|
2023-05-15 11:30:24 +02:00
|
|
|
ContentRegistry::BackgroundServices::impl::stopServices();
|
|
|
|
ContentRegistry::BackgroundServices::impl::getServices().clear();
|
|
|
|
|
|
|
|
ContentRegistry::CommunicationInterface::impl::getNetworkEndpoints().clear();
|
|
|
|
|
2023-05-11 23:21:52 +02:00
|
|
|
LayoutManager::reset();
|
|
|
|
|
2023-03-23 20:35:16 +01:00
|
|
|
ThemeManager::reset();
|
2023-01-05 09:29:16 +01:00
|
|
|
|
2023-08-06 21:33:15 +02:00
|
|
|
AchievementManager::getAchievements().clear();
|
|
|
|
|
2022-08-20 13:43:26 +02:00
|
|
|
ProjectFile::getHandlers().clear();
|
|
|
|
ProjectFile::getProviderHandlers().clear();
|
2023-06-23 21:39:22 +02:00
|
|
|
ProjectFile::setProjectFunctions(nullptr, nullptr);
|
2022-08-20 13:43:26 +02:00
|
|
|
|
2022-10-07 15:35:26 +02:00
|
|
|
fs::setFileBrowserErrorCallback(nullptr);
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool loadPlugins() {
|
2023-07-22 18:38:14 +02:00
|
|
|
// Load all plugins
|
|
|
|
for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Plugins)) {
|
|
|
|
PluginManager::load(dir);
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Get loaded plugins
|
2022-02-01 18:09:40 +01:00
|
|
|
auto &plugins = PluginManager::getPlugins();
|
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// If no plugins were loaded, ImHex wasn't installed properly. This will trigger an error popup later on
|
2022-02-01 18:09:40 +01:00
|
|
|
if (plugins.empty()) {
|
2022-01-13 14:34:27 +01:00
|
|
|
log::error("No plugins found!");
|
|
|
|
|
2022-09-19 16:54:19 +02:00
|
|
|
ImHexApi::System::impl::addInitArgument("no-plugins");
|
2021-04-20 21:46:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
const auto shouldLoadPlugin = [executablePath = wolv::io::fs::getExecutablePath()](const Plugin &plugin) {
|
2023-02-17 12:03:53 +01:00
|
|
|
// In debug builds, ignore all plugins that are not part of the executable directory
|
2022-11-25 10:28:05 +01:00
|
|
|
#if !defined(DEBUG)
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!executablePath.has_value())
|
|
|
|
return true;
|
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Check if the plugin is somewhere in the same directory tree as the executable
|
2022-11-25 10:28:05 +01:00
|
|
|
return !std::fs::relative(plugin.getPath(), executablePath->parent_path()).string().starts_with("..");
|
|
|
|
};
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
u32 builtinPlugins = 0;
|
2022-02-01 22:09:44 +01:00
|
|
|
u32 loadErrors = 0;
|
2023-02-17 12:03:53 +01:00
|
|
|
|
|
|
|
// Load the builtin plugin first, so it can initialize everything that's necessary for ImHex to work
|
2022-02-01 18:09:40 +01:00
|
|
|
for (const auto &plugin : plugins) {
|
2022-02-01 23:57:48 +01:00
|
|
|
if (!plugin.isBuiltinPlugin()) continue;
|
2022-11-25 10:28:05 +01:00
|
|
|
|
|
|
|
if (!shouldLoadPlugin(plugin)) {
|
|
|
|
log::debug("Skipping built-in plugin {}", plugin.getPath().string());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Make sure there's only one built-in plugin
|
2022-02-01 23:57:48 +01:00
|
|
|
if (builtinPlugins > 1) continue;
|
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Initialize the plugin
|
2022-02-01 23:57:48 +01:00
|
|
|
if (!plugin.initializePlugin()) {
|
2023-03-12 18:43:05 +01:00
|
|
|
log::error("Failed to initialize plugin {}", wolv::util::toUTF8String(plugin.getPath().filename()));
|
2022-02-01 23:57:48 +01:00
|
|
|
loadErrors++;
|
2023-08-06 21:33:15 +02:00
|
|
|
} else {
|
|
|
|
builtinPlugins++;
|
2022-02-01 23:57:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Load all other plugins
|
2022-02-01 23:57:48 +01:00
|
|
|
for (const auto &plugin : plugins) {
|
|
|
|
if (plugin.isBuiltinPlugin()) continue;
|
|
|
|
|
2022-11-25 10:28:05 +01:00
|
|
|
if (!shouldLoadPlugin(plugin)) {
|
|
|
|
log::debug("Skipping plugin {}", plugin.getPath().string());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Initialize the plugin
|
2022-02-01 18:09:40 +01:00
|
|
|
if (!plugin.initializePlugin()) {
|
2023-03-12 18:43:05 +01:00
|
|
|
log::error("Failed to initialize plugin {}", wolv::util::toUTF8String(plugin.getPath().filename()));
|
2022-02-01 18:09:40 +01:00
|
|
|
loadErrors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// If no plugins were loaded successfully, ImHex wasn't installed properly. This will trigger an error popup later on
|
2022-02-01 18:09:40 +01:00
|
|
|
if (loadErrors == plugins.size()) {
|
|
|
|
log::error("No plugins loaded successfully!");
|
2022-09-19 16:54:19 +02:00
|
|
|
ImHexApi::System::impl::addInitArgument("no-plugins");
|
2022-02-01 18:09:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// ImHex requires exactly one built-in plugin
|
|
|
|
// If no built-in plugin or more than one was found, something's wrong and we can't continue
|
2023-10-04 12:00:32 +02:00
|
|
|
#if !defined(EMSCRIPTEN)
|
|
|
|
if (builtinPlugins == 0) {
|
|
|
|
log::error("Built-in plugin not found!");
|
|
|
|
ImHexApi::System::impl::addInitArgument("no-builtin-plugin");
|
|
|
|
return false;
|
|
|
|
} else if (builtinPlugins > 1) {
|
|
|
|
log::error("Found more than one built-in plugin!");
|
|
|
|
ImHexApi::System::impl::addInitArgument("multiple-builtin-plugins");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
2021-04-20 21:46:48 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-05-16 14:45:24 +02:00
|
|
|
bool clearOldLogs() {
|
2023-06-11 09:44:34 +02:00
|
|
|
for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Logs)) {
|
2023-05-16 14:45:24 +02:00
|
|
|
std::vector<std::filesystem::directory_entry> files;
|
|
|
|
|
2023-07-24 15:36:29 +02:00
|
|
|
try {
|
|
|
|
for (const auto& file : std::filesystem::directory_iterator(path))
|
|
|
|
files.push_back(file);
|
2023-05-16 14:45:24 +02:00
|
|
|
|
2023-07-24 15:36:29 +02:00
|
|
|
if (files.size() <= 10)
|
|
|
|
return true;
|
2023-05-16 14:45:24 +02:00
|
|
|
|
2023-07-24 15:36:29 +02:00
|
|
|
std::sort(files.begin(), files.end(), [](const auto& a, const auto& b) {
|
|
|
|
return std::filesystem::last_write_time(a) > std::filesystem::last_write_time(b);
|
|
|
|
});
|
2023-05-16 14:45:24 +02:00
|
|
|
|
2023-07-24 15:36:29 +02:00
|
|
|
for (auto it = files.begin() + 10; it != files.end(); it++)
|
|
|
|
std::filesystem::remove(it->path());
|
|
|
|
} catch (std::filesystem::filesystem_error &e) {
|
|
|
|
log::error("Failed to clear old log! {}", e.what());
|
|
|
|
continue;
|
|
|
|
}
|
2023-05-16 14:45:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
bool unloadPlugins() {
|
|
|
|
PluginManager::unload();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool loadSettings() {
|
2021-05-21 23:46:36 +02:00
|
|
|
try {
|
2023-02-17 12:03:53 +01:00
|
|
|
// Try to load settings from file
|
2023-03-21 15:33:43 +01:00
|
|
|
ContentRegistry::Settings::impl::load();
|
2022-01-13 14:34:27 +01:00
|
|
|
} catch (std::exception &e) {
|
2023-02-17 12:03:53 +01:00
|
|
|
// If that fails, create a new settings file
|
|
|
|
|
2022-01-13 14:34:27 +01:00
|
|
|
log::error("Failed to load configuration! {}", e.what());
|
2022-07-30 11:19:56 +02:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
ContentRegistry::Settings::impl::clear();
|
|
|
|
ContentRegistry::Settings::impl::store();
|
2022-07-30 11:19:56 +02:00
|
|
|
|
2021-05-21 23:46:36 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-04-20 21:46:48 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-30 13:40:39 +02:00
|
|
|
bool configureUIScale() {
|
2023-10-21 23:58:51 +02:00
|
|
|
int interfaceScaleSetting = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.scaling", 0.0F).get<float>() * 10;
|
|
|
|
|
|
|
|
float interfaceScaling;
|
|
|
|
if (interfaceScaleSetting == 0)
|
2023-10-21 23:07:33 +02:00
|
|
|
interfaceScaling = ImHexApi::System::getNativeScale();
|
2023-10-21 23:58:51 +02:00
|
|
|
else
|
|
|
|
interfaceScaling = interfaceScaleSetting / 10.0F;
|
2023-06-30 13:40:39 +02:00
|
|
|
|
2023-10-21 23:58:51 +02:00
|
|
|
ImHexApi::System::impl::setGlobalScale(interfaceScaling);
|
2023-06-30 13:40:39 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
bool storeSettings() {
|
2021-05-21 23:46:36 +02:00
|
|
|
try {
|
2023-03-21 15:33:43 +01:00
|
|
|
ContentRegistry::Settings::impl::store();
|
2022-01-13 14:34:27 +01:00
|
|
|
} catch (std::exception &e) {
|
|
|
|
log::error("Failed to store configuration! {}", e.what());
|
2021-05-21 23:46:36 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-04-20 21:46:48 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
// Run all exit tasks, and print to console
|
2023-10-11 22:38:54 +02:00
|
|
|
void runExitTasks() {
|
|
|
|
for (const auto &[name, task, async] : init::getExitTasks()) {
|
|
|
|
task();
|
|
|
|
log::info("Finished exit task {}", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
std::vector<Task> getInitTasks() {
|
|
|
|
return {
|
2022-12-05 15:29:19 +01:00
|
|
|
{ "Setting up environment", setupEnvironment, false },
|
2022-10-20 08:28:29 +02:00
|
|
|
{ "Creating directories", createDirectories, false },
|
2023-03-26 11:02:23 +02:00
|
|
|
#if defined(OS_LINUX)
|
|
|
|
{ "Migrate config to .config", migrateConfig, false },
|
|
|
|
#endif
|
2022-10-20 08:28:29 +02:00
|
|
|
{ "Loading settings", loadSettings, false },
|
2023-06-30 13:40:39 +02:00
|
|
|
{ "Configuring UI scale", configureUIScale, false },
|
2023-06-27 00:52:13 +02:00
|
|
|
{ "Loading plugins", loadPlugins, true },
|
2023-10-26 23:45:43 +02:00
|
|
|
{ "Checking for updates", checkForUpdates, false },
|
2022-10-20 08:28:29 +02:00
|
|
|
{ "Loading fonts", loadFonts, true },
|
2021-04-20 21:46:48 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Task> getExitTasks() {
|
|
|
|
return {
|
2022-09-19 16:54:19 +02:00
|
|
|
{ "Saving settings...", storeSettings, false },
|
|
|
|
{ "Cleaning up shared data...", deleteSharedData, false },
|
|
|
|
{ "Unloading plugins...", unloadPlugins, false },
|
2023-06-06 11:29:58 +02:00
|
|
|
{ "Clearing old logs...", clearOldLogs, false },
|
2021-04-20 21:46:48 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-05-05 22:03:45 +02:00
|
|
|
}
|