2021-04-20 21:46:48 +02:00
|
|
|
#include "init/tasks.hpp"
|
2023-12-05 10:49:51 +01:00
|
|
|
#include "misc/freetype/imgui_freetype.h"
|
2021-04-20 21:46:48 +02:00
|
|
|
|
2021-08-31 15:22:00 +02:00
|
|
|
#include <imgui.h>
|
2023-11-24 11:29:05 +01:00
|
|
|
|
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>
|
|
|
|
|
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
|
|
|
|
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>
|
|
|
|
|
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;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-12-08 10:29:44 +01:00
|
|
|
EventImHexClosing::post();
|
2023-06-10 15:08:56 +02:00
|
|
|
EventManager::clear();
|
|
|
|
|
2023-12-06 11:04:35 +01:00
|
|
|
// Terminate all asynchronous tasks
|
|
|
|
TaskManager::exit();
|
|
|
|
|
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();
|
2023-07-13 14:08:23 +02:00
|
|
|
ImHexApi::Messaging::impl::getHandlers().clear();
|
2023-11-29 23:47:37 +01:00
|
|
|
ImHexApi::Fonts::getCustomFontPath().clear();
|
2023-11-28 09:49:38 +01:00
|
|
|
ImHexApi::Fonts::impl::getFonts().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();
|
2023-11-21 14:38:01 +01:00
|
|
|
LocalizationManager::impl::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
|
|
|
|
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-11-22 08:26:31 +01:00
|
|
|
ContentRegistry::Experiments::impl::getExperiments().clear();
|
|
|
|
ContentRegistry::Reports::impl::getGenerators().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);
|
|
|
|
|
2023-12-02 11:09:32 +01:00
|
|
|
// Unlock font atlas so it can be deleted in case of a crash
|
|
|
|
if (ImGui::GetCurrentContext() != nullptr)
|
|
|
|
ImGui::GetIO().Fonts->Locked = false;
|
|
|
|
|
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-11-30 11:23:12 +01:00
|
|
|
bool result = true;
|
2023-06-11 09:44:34 +02:00
|
|
|
for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Logs)) {
|
2023-07-24 15:36:29 +02:00
|
|
|
try {
|
2023-11-10 20:47:08 +01:00
|
|
|
std::vector<std::filesystem::directory_entry> files;
|
|
|
|
|
2023-07-24 15:36:29 +02:00
|
|
|
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-11-10 20:47:08 +01:00
|
|
|
for (auto it = files.begin() + 10; it != files.end(); it += 1)
|
2023-07-24 15:36:29 +02:00
|
|
|
std::filesystem::remove(it->path());
|
|
|
|
} catch (std::filesystem::filesystem_error &e) {
|
|
|
|
log::error("Failed to clear old log! {}", e.what());
|
2023-11-30 11:23:12 +01:00
|
|
|
result = false;
|
2023-07-24 15:36:29 +02:00
|
|
|
}
|
2023-05-16 14:45:24 +02:00
|
|
|
}
|
|
|
|
|
2023-11-30 11:23:12 +01:00
|
|
|
return result;
|
2023-05-16 14:45:24 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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()) {
|
2023-11-10 20:47:08 +01:00
|
|
|
bool result = task();
|
2023-11-11 00:54:16 +01:00
|
|
|
log::info("Exit task '{0}' finished {1}", name, result ? "successfully" : "unsuccessfully");
|
2023-10-11 22:38:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 },
|
|
|
|
{ "Loading settings", loadSettings, false },
|
2023-11-28 00:47:03 +01:00
|
|
|
{ "Loading plugins", loadPlugins, false },
|
2021-04-20 21:46:48 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Task> getExitTasks() {
|
|
|
|
return {
|
2023-11-10 23:25:02 +01:00
|
|
|
{ "Saving settings", storeSettings, false },
|
|
|
|
{ "Cleaning up shared data", deleteSharedData, false },
|
|
|
|
{ "Unloading plugins", unloadPlugins, false },
|
|
|
|
{ "Clearing old logs", clearOldLogs, false },
|
2021-04-20 21:46:48 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-05-05 22:03:45 +02:00
|
|
|
}
|