2021-04-20 21:46:48 +02:00
|
|
|
#include "init/tasks.hpp"
|
|
|
|
|
2021-08-31 15:22:00 +02:00
|
|
|
#include <imgui.h>
|
|
|
|
#include <imgui_freetype.h>
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
#include <hex/helpers/net.hpp>
|
|
|
|
#include <hex/api/content_registry.hpp>
|
2021-09-08 15:18:24 +02:00
|
|
|
#include <hex/pattern_language/pattern_data.hpp>
|
2021-12-07 22:47:41 +01:00
|
|
|
#include <hex/helpers/paths.hpp>
|
2021-04-20 21:46:48 +02:00
|
|
|
|
2021-08-31 15:22:00 +02:00
|
|
|
#include <fontawesome_font.h>
|
|
|
|
#include <codicons_font.h>
|
|
|
|
#include <unifont_font.h>
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/api/plugin_manager.hpp>
|
2021-04-20 21:46:48 +02:00
|
|
|
|
|
|
|
#include <filesystem>
|
|
|
|
|
2021-08-29 14:18:45 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
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;
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
static bool checkForUpdates() {
|
|
|
|
hex::Net net;
|
|
|
|
|
2022-01-13 14:33:30 +01:00
|
|
|
auto releases = net.getJson(GitHubApiURL + "/releases/latest"s, 2000).get();
|
2021-04-20 21:46:48 +02:00
|
|
|
if (releases.code != 200)
|
2021-05-24 23:34:17 +02:00
|
|
|
return false;
|
2021-04-20 21:46:48 +02:00
|
|
|
|
2021-08-28 00:45:59 +02:00
|
|
|
if (!releases.body.contains("tag_name") || !releases.body["tag_name"].is_string())
|
2021-04-20 21:46:48 +02:00
|
|
|
return false;
|
|
|
|
|
2021-09-22 12:57:40 +02:00
|
|
|
auto versionString = std::string(IMHEX_VERSION);
|
2022-02-01 22:09:44 +01:00
|
|
|
auto currVersion = "v" + versionString.substr(0, versionString.find_first_of('-'));
|
2021-08-28 00:45:59 +02:00
|
|
|
auto latestVersion = releases.body["tag_name"].get<std::string_view>();
|
2021-04-20 21:46:48 +02:00
|
|
|
|
|
|
|
if (latestVersion != currVersion)
|
2022-02-01 18:09:40 +01:00
|
|
|
ImHexApi::System::getInitArguments().insert({ "update-available", latestVersion.data() });
|
2021-04-20 21:46:48 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-22 21:11:01 +02:00
|
|
|
static bool downloadInformation() {
|
|
|
|
hex::Net net;
|
|
|
|
|
2021-12-17 09:52:58 +01:00
|
|
|
auto tip = net.getString(ImHexApiURL + "/tip"s, 200).get();
|
2021-08-22 21:11:01 +02:00
|
|
|
if (tip.code != 200)
|
|
|
|
return false;
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
ImHexApi::System::getInitArguments().insert({ "tip-of-the-day", tip.body });
|
2021-08-22 21:11:01 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
bool createDirectories() {
|
2021-05-21 23:46:36 +02:00
|
|
|
bool result = true;
|
|
|
|
|
2022-01-17 20:06:00 +01:00
|
|
|
constexpr std::array paths = {
|
2021-08-28 16:29:15 +02:00
|
|
|
ImHexPath::Patterns,
|
|
|
|
ImHexPath::PatternsInclude,
|
|
|
|
ImHexPath::Magic,
|
|
|
|
ImHexPath::Plugins,
|
|
|
|
ImHexPath::Resources,
|
|
|
|
ImHexPath::Config,
|
|
|
|
ImHexPath::Constants,
|
|
|
|
ImHexPath::Yara,
|
2022-01-23 21:52:24 +01:00
|
|
|
ImHexPath::Encodings,
|
2022-01-17 20:06:00 +01:00
|
|
|
ImHexPath::Python,
|
|
|
|
ImHexPath::Logs
|
2021-05-21 23:46:36 +02:00
|
|
|
};
|
2021-04-20 21:46:48 +02:00
|
|
|
|
2021-05-21 23:46:36 +02:00
|
|
|
for (auto path : paths) {
|
2022-01-12 09:02:03 +01:00
|
|
|
for (auto &folder : hex::getPath(path, true)) {
|
2021-05-21 23:46:36 +02:00
|
|
|
try {
|
2022-01-13 14:33:30 +01:00
|
|
|
fs::create_directories(folder);
|
2021-05-21 23:46:36 +02:00
|
|
|
} catch (...) {
|
2022-01-13 14:34:27 +01:00
|
|
|
log::error("Failed to create folder {}!", folder.string());
|
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-02-01 18:09:40 +01:00
|
|
|
ImHexApi::System::getInitArguments().insert({ "folder-creation-error", {} });
|
2021-05-21 23:46:36 +02:00
|
|
|
|
|
|
|
return result;
|
2021-04-20 21:46:48 +02:00
|
|
|
}
|
|
|
|
|
2021-08-31 15:22:00 +02:00
|
|
|
bool loadFonts() {
|
2022-02-01 22:09:44 +01:00
|
|
|
auto fonts = IM_NEW(ImFontAtlas)();
|
2022-02-01 18:09:40 +01:00
|
|
|
ImFontConfig cfg = {};
|
2021-08-31 15:22:00 +02:00
|
|
|
|
2022-01-13 14:33:30 +01:00
|
|
|
fs::path fontFile;
|
2021-08-31 15:22:00 +02:00
|
|
|
for (const auto &dir : hex::getPath(ImHexPath::Resources)) {
|
2022-01-13 14:33:30 +01:00
|
|
|
auto path = dir / "font.ttf";
|
|
|
|
if (fs::exists(path)) {
|
2022-01-13 14:34:27 +01:00
|
|
|
log::info("Loading custom front from {}", path.string());
|
|
|
|
|
2021-09-22 23:42:52 +02:00
|
|
|
fontFile = path;
|
2021-08-31 15:22:00 +02:00
|
|
|
break;
|
2021-09-22 23:42:52 +02:00
|
|
|
}
|
2021-08-31 15:22:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ImVector<ImWchar> ranges;
|
|
|
|
{
|
|
|
|
ImFontGlyphRangesBuilder glyphRangesBuilder;
|
|
|
|
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesDefault());
|
|
|
|
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesJapanese());
|
|
|
|
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesChineseFull());
|
|
|
|
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesCyrillic());
|
|
|
|
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesKorean());
|
|
|
|
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesThai());
|
|
|
|
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesVietnamese());
|
|
|
|
glyphRangesBuilder.BuildRanges(&ranges);
|
|
|
|
}
|
|
|
|
|
|
|
|
ImWchar fontAwesomeRange[] = {
|
2022-01-24 20:53:17 +01:00
|
|
|
ICON_MIN_FA, ICON_MAX_FA, 0
|
2021-08-31 15:22:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ImWchar codiconsRange[] = {
|
2022-01-24 20:53:17 +01:00
|
|
|
ICON_MIN_VS, ICON_MAX_VS, 0
|
2021-08-31 15:22:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ImWchar unifontRange[] = {
|
2022-02-18 16:19:12 +01:00
|
|
|
0x0100, 0xFFF0, 0
|
2021-08-31 15:22:00 +02:00
|
|
|
};
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
float fontSize = 13.0F * ImHexApi::System::getGlobalScale();
|
2021-09-22 23:42:52 +02:00
|
|
|
if (fontFile.empty()) {
|
2021-08-31 15:22:00 +02:00
|
|
|
// Load default font
|
2021-09-22 23:42:52 +02:00
|
|
|
|
2021-08-31 15:22:00 +02:00
|
|
|
fonts->Clear();
|
|
|
|
|
|
|
|
cfg.OversampleH = cfg.OversampleV = 1, cfg.PixelSnapH = true;
|
2022-02-01 18:09:40 +01:00
|
|
|
cfg.SizePixels = fontSize;
|
2021-08-31 15:22:00 +02:00
|
|
|
fonts->AddFontDefault(&cfg);
|
|
|
|
} else {
|
|
|
|
// Load custom font
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
fontSize = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.font_size", 14) * ImHexApi::System::getGlobalScale();
|
2021-09-22 23:42:52 +02:00
|
|
|
|
2021-08-31 15:22:00 +02:00
|
|
|
cfg.OversampleH = cfg.OversampleV = 1, cfg.PixelSnapH = true;
|
2022-02-01 18:09:40 +01:00
|
|
|
cfg.SizePixels = fontSize;
|
2021-08-31 15:22:00 +02:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
fonts->AddFontFromFileTTF(fontFile.string().c_str(), std::floor(fontSize), &cfg, ranges.Data); // Needs conversion to char for Windows
|
2021-08-31 15:22:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.MergeMode = true;
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
fonts->AddFontFromMemoryCompressedTTF(font_awesome_compressed_data, font_awesome_compressed_size, fontSize, &cfg, fontAwesomeRange);
|
|
|
|
fonts->AddFontFromMemoryCompressedTTF(codicons_compressed_data, codicons_compressed_size, fontSize, &cfg, codiconsRange);
|
|
|
|
fonts->AddFontFromMemoryCompressedTTF(unifont_compressed_data, unifont_compressed_size, fontSize, &cfg, unifontRange);
|
2021-08-31 15:22:00 +02:00
|
|
|
|
|
|
|
ImGuiFreeType::BuildFontAtlas(fonts);
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
View::setFontAtlas(fonts);
|
|
|
|
View::setFontConfig(cfg);
|
|
|
|
|
2021-08-31 15:22:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-31 17:11:10 +02:00
|
|
|
bool deleteSharedData() {
|
2022-02-04 00:47:39 +01:00
|
|
|
ImHexApi::System::getInitArguments().clear();
|
2022-02-01 18:09:40 +01:00
|
|
|
ImHexApi::Tasks::getDeferredCalls().clear();
|
2022-02-16 14:57:13 +01:00
|
|
|
ImHexApi::HexEditor::impl::getHighlights().clear();
|
|
|
|
ImHexApi::HexEditor::impl::getHighlightingFunctions().clear();
|
2021-07-31 17:11:10 +02:00
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
while (ImHexApi::Provider::isValid())
|
|
|
|
ImHexApi::Provider::remove(ImHexApi::Provider::get());
|
2022-02-01 18:09:40 +01:00
|
|
|
ContentRegistry::Provider::getEntries().clear();
|
2021-07-31 17:11:10 +02:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
ContentRegistry::Settings::getEntries().clear();
|
|
|
|
ContentRegistry::Settings::getSettingsData().clear();
|
2021-07-31 17:11:10 +02:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
ContentRegistry::CommandPaletteCommands::getEntries().clear();
|
|
|
|
ContentRegistry::PatternLanguage::getFunctions().clear();
|
|
|
|
ContentRegistry::PatternLanguage::getPalettes().clear();
|
2021-07-31 17:11:10 +02:00
|
|
|
|
2022-01-18 00:10:10 +01:00
|
|
|
for (auto &[name, view] : ContentRegistry::Views::getEntries())
|
2021-04-20 21:46:48 +02:00
|
|
|
delete view;
|
2022-02-01 18:09:40 +01:00
|
|
|
ContentRegistry::Views::getEntries().clear();
|
2021-07-31 17:11:10 +02:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
ContentRegistry::Tools::getEntries().clear();
|
|
|
|
ContentRegistry::DataInspector::getEntries().clear();
|
2021-07-31 17:11:10 +02:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
ContentRegistry::Language::getLanguages().clear();
|
|
|
|
ContentRegistry::Language::getLanguageDefinitions().clear();
|
|
|
|
LangEntry::resetLanguageStrings();
|
2021-07-31 17:11:10 +02:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
ContentRegistry::Interface::getWelcomeScreenEntries().clear();
|
|
|
|
ContentRegistry::Interface::getFooterItems().clear();
|
|
|
|
ContentRegistry::Interface::getToolbarItems().clear();
|
|
|
|
ContentRegistry::Interface::getMainMenuItems().clear();
|
|
|
|
ContentRegistry::Interface::getMenuItems().clear();
|
2022-02-02 16:03:18 +01:00
|
|
|
ContentRegistry::Interface::getSidebarItems().clear();
|
2022-02-02 00:36:09 +01:00
|
|
|
ContentRegistry::Interface::getTitleBarButtons().clear();
|
2022-02-02 16:03:18 +01:00
|
|
|
ContentRegistry::Interface::getLayouts().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-02-01 18:09:40 +01:00
|
|
|
hex::Task::getRunningTasks().clear();
|
2022-01-09 21:57:22 +01:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
ContentRegistry::DataProcessorNode::getEntries().clear();
|
2021-07-31 17:11:10 +02:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
ContentRegistry::DataFormatter::getEntries().clear();
|
|
|
|
ContentRegistry::FileHandler::getEntries().clear();
|
2021-07-31 17:11:10 +02:00
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool loadPlugins() {
|
|
|
|
for (const auto &dir : hex::getPath(ImHexPath::Plugins)) {
|
|
|
|
PluginManager::load(dir);
|
|
|
|
}
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
auto &plugins = PluginManager::getPlugins();
|
|
|
|
|
|
|
|
if (plugins.empty()) {
|
2022-01-13 14:34:27 +01:00
|
|
|
log::error("No plugins found!");
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
ImHexApi::System::getInitArguments().insert({ "no-plugins", {} });
|
2021-04-20 21:46:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
u32 builtinPlugins = 0;
|
2022-02-01 22:09:44 +01:00
|
|
|
u32 loadErrors = 0;
|
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;
|
|
|
|
builtinPlugins++;
|
|
|
|
if (builtinPlugins > 1) continue;
|
|
|
|
|
|
|
|
if (!plugin.initializePlugin()) {
|
|
|
|
log::error("Failed to initialize plugin {}", plugin.getPath().filename().string());
|
|
|
|
loadErrors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &plugin : plugins) {
|
|
|
|
if (plugin.isBuiltinPlugin()) continue;
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
if (!plugin.initializePlugin()) {
|
2022-01-17 20:06:00 +01:00
|
|
|
log::error("Failed to initialize plugin {}", plugin.getPath().filename().string());
|
2022-02-01 18:09:40 +01:00
|
|
|
loadErrors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loadErrors == plugins.size()) {
|
|
|
|
log::error("No plugins loaded successfully!");
|
|
|
|
ImHexApi::System::getInitArguments().insert({ "no-plugins", {} });
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (builtinPlugins == 0) {
|
|
|
|
log::error("Built-in plugin not found!");
|
2022-02-01 23:57:48 +01:00
|
|
|
ImHexApi::System::getInitArguments().insert({ "no-builtin-plugin", {} });
|
2022-02-01 18:09:40 +01:00
|
|
|
return false;
|
|
|
|
} else if (builtinPlugins > 1) {
|
|
|
|
log::error("Found more than one built-in plugin!");
|
2022-02-01 23:57:48 +01:00
|
|
|
ImHexApi::System::getInitArguments().insert({ "multiple-builtin-plugins", {} });
|
2022-02-01 18:09:40 +01:00
|
|
|
return false;
|
2021-04-20 21:46:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool unloadPlugins() {
|
|
|
|
PluginManager::unload();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool loadSettings() {
|
2021-05-21 23:46:36 +02:00
|
|
|
try {
|
|
|
|
ContentRegistry::Settings::load();
|
2022-01-13 14:34:27 +01:00
|
|
|
} catch (std::exception &e) {
|
|
|
|
log::error("Failed to load configuration! {}", e.what());
|
2021-05-21 23:46:36 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-04-20 21:46:48 +02:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
float interfaceScaling = 1.0F;
|
2021-08-31 15:22:00 +02:00
|
|
|
switch (ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.scaling", 0)) {
|
2022-02-01 22:09:44 +01:00
|
|
|
default:
|
|
|
|
case 0:
|
|
|
|
// Native scaling
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
interfaceScaling = 0.5F;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
interfaceScaling = 1.0F;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
interfaceScaling = 1.5F;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
interfaceScaling = 2.0F;
|
|
|
|
break;
|
2021-08-31 15:22:00 +02:00
|
|
|
}
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
ImHexApi::System::impl::setGlobalScale(interfaceScaling);
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool storeSettings() {
|
2021-05-21 23:46:36 +02:00
|
|
|
try {
|
|
|
|
ContentRegistry::Settings::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Task> getInitTasks() {
|
|
|
|
return {
|
2022-02-01 22:09:44 +01:00
|
|
|
{"Checking for updates...", checkForUpdates },
|
2022-01-24 20:53:17 +01:00
|
|
|
{ "Downloading information...", downloadInformation},
|
|
|
|
{ "Creating directories...", createDirectories },
|
|
|
|
{ "Loading settings...", loadSettings },
|
|
|
|
{ "Loading plugins...", loadPlugins },
|
|
|
|
{ "Loading fonts...", loadFonts },
|
2021-04-20 21:46:48 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Task> getExitTasks() {
|
|
|
|
return {
|
2022-02-01 22:09:44 +01:00
|
|
|
{"Saving settings...", storeSettings },
|
|
|
|
{ "Cleaning up shared data...", deleteSharedData},
|
|
|
|
{ "Unloading plugins...", unloadPlugins },
|
2021-04-20 21:46:48 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|