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/api/content_registry.hpp>
|
2022-02-27 23:25:39 +01:00
|
|
|
#include <hex/ui/view.hpp>
|
|
|
|
#include <hex/helpers/net.hpp>
|
2022-03-04 11:36:37 +01:00
|
|
|
#include <hex/helpers/fs.hpp>
|
2022-02-27 23:25:39 +01:00
|
|
|
#include <hex/helpers/logger.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 = {
|
2022-03-04 11:36:37 +01:00
|
|
|
fs::ImHexPath::Patterns,
|
|
|
|
fs::ImHexPath::PatternsInclude,
|
|
|
|
fs::ImHexPath::Magic,
|
|
|
|
fs::ImHexPath::Plugins,
|
|
|
|
fs::ImHexPath::Resources,
|
|
|
|
fs::ImHexPath::Config,
|
|
|
|
fs::ImHexPath::Constants,
|
|
|
|
fs::ImHexPath::Yara,
|
|
|
|
fs::ImHexPath::Encodings,
|
|
|
|
fs::ImHexPath::Python,
|
|
|
|
fs::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-03-04 11:36:37 +01:00
|
|
|
for (auto &folder : fs::getDefaultPaths(path, true)) {
|
2021-05-21 23:46:36 +02:00
|
|
|
try {
|
2022-03-04 11:36:37 +01:00
|
|
|
fs::createDirectories(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
|
|
|
|
|
|
|
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-07-02 16:22:38 +02:00
|
|
|
auto fontFile = ImHexApi::System::getCustomFontPath();
|
|
|
|
float fontSize = ImHexApi::System::getFontSize();
|
2021-09-22 23:42:52 +02:00
|
|
|
if (fontFile.empty()) {
|
2022-02-21 21:46:25 +01:00
|
|
|
// Load default font if no custom one has been specified
|
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
|
|
|
|
|
|
|
|
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-06-27 17:01:21 +02:00
|
|
|
while (ImHexApi::Provider::isValid())
|
|
|
|
ImHexApi::Provider::remove(ImHexApi::Provider::get());
|
|
|
|
ContentRegistry::Provider::getEntries().clear();
|
|
|
|
|
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-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();
|
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();
|
2022-06-25 12:19:59 +02:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
ContentRegistry::PatternLanguage::getFunctions().clear();
|
2022-06-25 12:19:59 +02:00
|
|
|
ContentRegistry::PatternLanguage::getPragmas().clear();
|
|
|
|
|
|
|
|
{
|
|
|
|
auto &views = ContentRegistry::Views::getEntries();
|
|
|
|
for (auto &[name, view] : views)
|
|
|
|
delete view;
|
|
|
|
views.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
|
|
|
|
2022-06-25 12:19:59 +02:00
|
|
|
{
|
|
|
|
auto &visualizers = ContentRegistry::HexEditor::impl::getVisualizers();
|
|
|
|
for (auto &[name, visualizer] : visualizers)
|
|
|
|
delete visualizer;
|
|
|
|
visualizers.clear();
|
|
|
|
}
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool loadPlugins() {
|
2022-03-04 11:36:37 +01:00
|
|
|
for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Plugins)) {
|
2021-04-20 21:46:48 +02:00
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|