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-08-20 13:43:26 +02:00
|
|
|
#include <hex/api/project_file_manager.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
|
|
|
|
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
|
|
|
|
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-08-05 08:10:35 +02:00
|
|
|
size_t versionLength = std::min(versionString.find_first_of('-'), versionString.length());
|
|
|
|
auto currVersion = "v" + versionString.substr(0, versionLength);
|
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-09-19 16:54:19 +02:00
|
|
|
ImHexApi::System::impl::addInitArgument("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-09-19 16:54:19 +02:00
|
|
|
ImHexApi::System::impl::addInitArgument("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-08-14 10:07:45 +02:00
|
|
|
using enum fs::ImHexPath;
|
2021-04-20 21:46:48 +02:00
|
|
|
|
2022-08-01 14:51:08 +02:00
|
|
|
// Check if ImHex is installed in portable mode
|
|
|
|
{
|
|
|
|
if (const auto executablePath = fs::getExecutablePath(); executablePath.has_value()) {
|
2022-08-01 23:25:13 +02:00
|
|
|
const auto flagFile = executablePath->parent_path() / "PORTABLE";
|
2022-08-01 14:51:08 +02:00
|
|
|
|
|
|
|
if (fs::exists(flagFile) && fs::isRegularFile(flagFile))
|
|
|
|
ImHexApi::System::impl::setPortableVersion(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create all folders
|
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 {
|
2022-03-04 11:36:37 +01:00
|
|
|
fs::createDirectories(folder);
|
2021-05-21 23:46:36 +02:00
|
|
|
} catch (...) {
|
2022-10-04 09:10:58 +02:00
|
|
|
log::error("Failed to create folder {}!", hex::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
|
|
|
}
|
|
|
|
|
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-09-20 15:47:28 +02:00
|
|
|
const auto &fontFile = ImHexApi::System::getCustomFontPath();
|
2022-07-02 16:22:38 +02:00
|
|
|
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-10-04 09:10:58 +02:00
|
|
|
fonts->AddFontFromFileTTF(hex::toUTF8String(fontFile).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
|
|
|
|
2022-08-03 23:32:34 +02:00
|
|
|
fonts->Build();
|
2021-08-31 15:22:00 +02:00
|
|
|
|
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-08-03 10:45:50 +02:00
|
|
|
EventManager::clear();
|
|
|
|
|
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-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-08-17 16:15:36 +02:00
|
|
|
TaskManager::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();
|
2022-08-16 11:48:37 +02:00
|
|
|
ContentRegistry::Hashes::impl::getHashes().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();
|
|
|
|
}
|
|
|
|
|
2022-08-20 13:43:26 +02:00
|
|
|
ProjectFile::getHandlers().clear();
|
|
|
|
ProjectFile::getProviderHandlers().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-09-19 16:54:19 +02:00
|
|
|
ImHexApi::System::impl::addInitArgument("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()) {
|
2022-10-04 09:10:58 +02:00
|
|
|
log::error("Failed to initialize plugin {}", hex::toUTF8String(plugin.getPath().filename()));
|
2022-02-01 23:57:48 +01:00
|
|
|
loadErrors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &plugin : plugins) {
|
|
|
|
if (plugin.isBuiltinPlugin()) continue;
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
if (!plugin.initializePlugin()) {
|
2022-10-04 09:10:58 +02:00
|
|
|
log::error("Failed to initialize plugin {}", hex::toUTF8String(plugin.getPath().filename()));
|
2022-02-01 18:09:40 +01:00
|
|
|
loadErrors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (builtinPlugins == 0) {
|
|
|
|
log::error("Built-in plugin not found!");
|
2022-09-19 16:54:19 +02:00
|
|
|
ImHexApi::System::impl::addInitArgument("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-09-19 16:54:19 +02:00
|
|
|
ImHexApi::System::impl::addInitArgument("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());
|
2022-07-30 11:19:56 +02:00
|
|
|
|
|
|
|
ContentRegistry::Settings::clear();
|
|
|
|
ContentRegistry::Settings::store();
|
|
|
|
|
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-09-19 16:54:19 +02:00
|
|
|
{ "Creating directories...", createDirectories, false },
|
|
|
|
{ "Loading settings...", loadSettings, false },
|
|
|
|
{ "Loading plugins...", loadPlugins, false },
|
2022-09-20 14:09:41 +02:00
|
|
|
{ "Checking for updates...", checkForUpdates, true },
|
|
|
|
{ "Downloading information...", downloadInformation, true },
|
|
|
|
{ "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 },
|
2021-04-20 21:46:48 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|