diff --git a/.idea/vcs.xml b/.idea/vcs.xml index deea3b61b..92c44cb26 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -2,6 +2,7 @@ + diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c37713c2..b4abab834 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,10 +40,13 @@ add_executable(imhex ${application_type} source/main.cpp source/window.cpp + source/init/splash_window.cpp + source/init/tasks.cpp + source/helpers/patches.cpp source/helpers/project_file_handler.cpp source/helpers/loader_script_handler.cpp - source/helpers/plugin_handler.cpp + source/helpers/plugin_manager.cpp source/helpers/encoding_file.cpp source/providers/file_provider.cpp @@ -68,7 +71,6 @@ add_executable(imhex ${application_type} ${imhex_icon} ) -set_target_properties(imhex PROPERTIES OUTPUT_NAME "imhexg") set_target_properties(imhex PROPERTIES CXX_VISIBILITY_PRESET hidden) target_link_directories(imhex PRIVATE ${CAPSTONE_LIBRARY_DIRS} ${MAGIC_LIBRARY_DIRS}) @@ -78,8 +80,4 @@ else () target_link_libraries(imhex magic ${CMAKE_DL_LIBS} capstone LLVMDemangle libimhex ${Python_LIBRARIES} dl pthread libyara) endif () -set_target_properties(imhex PROPERTIES VERSION ${IMHEX_VERSION}) -add_subdirectory(splash) -set_target_properties(splash PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) - createPackage() diff --git a/cmake/build_helpers.cmake b/cmake/build_helpers.cmake index 7151c2468..646a70f6e 100644 --- a/cmake/build_helpers.cmake +++ b/cmake/build_helpers.cmake @@ -252,9 +252,9 @@ macro(createPackage) # Enforce DragNDrop packaging. set(CPACK_GENERATOR "DragNDrop") - install(TARGETS splash imhex BUNDLE DESTINATION .) + install(TARGETS imhex BUNDLE DESTINATION .) else() - install(TARGETS splash imhex RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) + install(TARGETS imhex RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) endif() diff --git a/include/helpers/plugin_handler.hpp b/include/helpers/plugin_manager.hpp similarity index 91% rename from include/helpers/plugin_handler.hpp rename to include/helpers/plugin_manager.hpp index 5375c4600..7840c4b70 100644 --- a/include/helpers/plugin_handler.hpp +++ b/include/helpers/plugin_manager.hpp @@ -44,16 +44,16 @@ namespace hex { }; }; - class PluginHandler { + class PluginManager { public: - PluginHandler() = delete; + PluginManager() = delete; - static void load(std::string_view pluginFolder); + static bool load(std::string_view pluginFolder); static void unload(); static void reload(); static const auto& getPlugins() { - return PluginHandler::s_plugins; + return PluginManager::s_plugins; } private: diff --git a/splash/include/splash_window.hpp b/include/init/splash_window.hpp similarity index 87% rename from splash/include/splash_window.hpp rename to include/init/splash_window.hpp index 10d14ff26..0d026cf8b 100644 --- a/splash/include/splash_window.hpp +++ b/include/init/splash_window.hpp @@ -8,11 +8,11 @@ struct GLFWwindow; -namespace hex::pre { +namespace hex::init { class WindowSplash { public: - WindowSplash(); + WindowSplash(int &argc, char **&argv); ~WindowSplash(); bool loop(); @@ -24,7 +24,7 @@ namespace hex::pre { private: GLFWwindow *m_window; std::mutex m_progressMutex; - float m_progress; + float m_progress = 0; std::string m_currTaskName; void initGLFW(); diff --git a/include/init/tasks.hpp b/include/init/tasks.hpp new file mode 100644 index 000000000..6e37873a9 --- /dev/null +++ b/include/init/tasks.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include +#include +#include + +namespace hex::init { + + struct Task { + std::string name; + std::function function; + }; + + struct Argument { + std::string name, value; + }; + + std::vector getInitTasks(); + std::vector getExitTasks(); + + std::vector& getInitArguments(); +} \ No newline at end of file diff --git a/include/views/view_hexeditor.hpp b/include/views/view_hexeditor.hpp index 8067bf6d2..77a61551a 100644 --- a/include/views/view_hexeditor.hpp +++ b/include/views/view_hexeditor.hpp @@ -21,7 +21,7 @@ namespace hex { class ViewHexEditor : public View { public: - ViewHexEditor(std::vector &patternData); + ViewHexEditor(); ~ViewHexEditor() override; void drawContent() override; @@ -32,8 +32,6 @@ namespace hex { private: MemoryEditor m_memoryEditor; - std::vector &m_patternData; - std::map m_highlightedBytes; std::vector m_searchStringBuffer; diff --git a/include/views/view_pattern.hpp b/include/views/view_pattern.hpp index d5a16cf4f..34739fb52 100644 --- a/include/views/view_pattern.hpp +++ b/include/views/view_pattern.hpp @@ -18,7 +18,7 @@ namespace hex { class ViewPattern : public View { public: - explicit ViewPattern(std::vector &patternData); + ViewPattern(); ~ViewPattern() override; void drawMenu() override; @@ -27,7 +27,6 @@ namespace hex { private: lang::PatternLanguage *m_patternLanguageRuntime; - std::vector &m_patternData; std::vector m_possiblePatternFiles; int m_selectedPatternFile = 0; bool m_runAutomatically = false; diff --git a/include/views/view_pattern_data.hpp b/include/views/view_pattern_data.hpp index 489453773..2aea0372d 100644 --- a/include/views/view_pattern_data.hpp +++ b/include/views/view_pattern_data.hpp @@ -16,15 +16,13 @@ namespace hex { class ViewPatternData : public View { public: - ViewPatternData(std::vector &patternData); + ViewPatternData(); ~ViewPatternData() override; void drawContent() override; void drawMenu() override; private: - - std::vector &m_patternData; std::vector m_sortedPatternData; }; diff --git a/include/window.hpp b/include/window.hpp index f518a9316..98a462113 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -15,7 +15,7 @@ namespace hex { class Window { public: - Window(int &argc, char **&argv); + Window(); ~Window(); void loop(); @@ -26,9 +26,6 @@ namespace hex { friend void ImHexSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandler *handler, ImGuiTextBuffer *buf); bool setFont(const std::filesystem::path &font_path); - - void initPlugins(); - void deinitPlugins(); private: void frameBegin(); void frameEnd(); diff --git a/plugins/libimhex/include/hex.hpp b/plugins/libimhex/include/hex.hpp index d2039cb3b..7af5f6ac9 100644 --- a/plugins/libimhex/include/hex.hpp +++ b/plugins/libimhex/include/hex.hpp @@ -4,6 +4,7 @@ #include #include +#include using namespace hex::lang_literals; using u8 = std::uint8_t; diff --git a/plugins/libimhex/include/hex/helpers/logger.hpp b/plugins/libimhex/include/hex/helpers/logger.hpp new file mode 100644 index 000000000..847939258 --- /dev/null +++ b/plugins/libimhex/include/hex/helpers/logger.hpp @@ -0,0 +1,40 @@ +#pragma once + +#include +#include + +namespace hex::log { + + void debug(std::string_view fmt, auto ... args) { + #if defined(DEBUG) + fmt::print(fg(fmt::color::green_yellow) | fmt::emphasis::bold, "[DEBUG] "); + fmt::print(fmt, args...); + fmt::print("\n"); + #endif + } + + void info(std::string_view fmt, auto ... args) { + fmt::print(fg(fmt::color::cadet_blue) | fmt::emphasis::bold, "[INFO] "); + fmt::print(fmt, args...); + fmt::print("\n"); + } + + void warn(std::string_view fmt, auto ... args) { + fmt::print(fg(fmt::color::light_golden_rod_yellow) | fmt::emphasis::bold, "[WARN] "); + fmt::print(fmt, args...); + fmt::print("\n"); + } + + void error(std::string_view fmt, auto ... args) { + fmt::print(fg(fmt::color::red) | fmt::emphasis::bold, "[ERROR] "); + fmt::print(fmt, args...); + fmt::print("\n"); + } + + void fatal(std::string_view fmt, auto ... args) { + fmt::print(fg(fmt::color::purple) | fmt::emphasis::bold, "[FATAL] "); + fmt::print(fmt, args...); + fmt::print("\n"); + } + +} \ No newline at end of file diff --git a/plugins/libimhex/include/hex/helpers/shared_data.hpp b/plugins/libimhex/include/hex/helpers/shared_data.hpp index 5101766a9..0cd2b141a 100644 --- a/plugins/libimhex/include/hex/helpers/shared_data.hpp +++ b/plugins/libimhex/include/hex/helpers/shared_data.hpp @@ -25,6 +25,8 @@ namespace hex { namespace prv { class Provider; } namespace dp { class Node; } + namespace lang { class PatternData; } + class View; class SharedData { @@ -58,6 +60,7 @@ namespace hex { static u32 patternPaletteOffset; static std::string errorPopupMessage; static std::list bookmarkEntries; + static std::vector patternData; static std::map languageNames; static std::map> languageDefinitions; diff --git a/plugins/libimhex/source/helpers/shared_data.cpp b/plugins/libimhex/source/helpers/shared_data.cpp index 99a00b678..50a2d3fa8 100644 --- a/plugins/libimhex/source/helpers/shared_data.cpp +++ b/plugins/libimhex/source/helpers/shared_data.cpp @@ -14,6 +14,7 @@ namespace hex { u32 SharedData::patternPaletteOffset; std::string SharedData::errorPopupMessage; std::list SharedData::bookmarkEntries; + std::vector SharedData::patternData; std::map SharedData::languageNames; std::map> SharedData::languageDefinitions; diff --git a/source/helpers/plugin_handler.cpp b/source/helpers/plugin_manager.cpp similarity index 84% rename from source/helpers/plugin_handler.cpp rename to source/helpers/plugin_manager.cpp index 0159bbb3b..adb627e19 100644 --- a/source/helpers/plugin_handler.cpp +++ b/source/helpers/plugin_manager.cpp @@ -1,4 +1,4 @@ -#include "helpers/plugin_handler.hpp" +#include "helpers/plugin_manager.hpp" #include @@ -71,26 +71,31 @@ namespace hex { return ""; } - void PluginHandler::load(std::string_view pluginFolder) { + bool PluginManager::load(std::string_view pluginFolder) { if (!std::filesystem::exists(pluginFolder)) - throw std::runtime_error("Failed to find plugin folder"); + return false; - PluginHandler::s_pluginFolder = pluginFolder; + PluginManager::s_pluginFolder = pluginFolder; for (auto& pluginPath : std::filesystem::directory_iterator(pluginFolder)) { if (pluginPath.is_regular_file() && pluginPath.path().extension() == ".hexplug") - PluginHandler::s_plugins.emplace_back(pluginPath.path().string()); + PluginManager::s_plugins.emplace_back(pluginPath.path().string()); } + + if (PluginManager::s_plugins.empty()) + return false; + + return true; } - void PluginHandler::unload() { - PluginHandler::s_plugins.clear(); - PluginHandler::s_pluginFolder.clear(); + void PluginManager::unload() { + PluginManager::s_plugins.clear(); + PluginManager::s_pluginFolder.clear(); } - void PluginHandler::reload() { - PluginHandler::unload(); - PluginHandler::load(PluginHandler::s_pluginFolder); + void PluginManager::reload() { + PluginManager::unload(); + PluginManager::load(PluginManager::s_pluginFolder); } } diff --git a/splash/source/splash_window.cpp b/source/init/splash_window.cpp similarity index 84% rename from splash/source/splash_window.cpp rename to source/init/splash_window.cpp index f828f1506..e7bd400bd 100644 --- a/splash/source/splash_window.cpp +++ b/source/init/splash_window.cpp @@ -1,6 +1,7 @@ -#include "splash_window.hpp" +#include "init/splash_window.hpp" #include +#include #include #include @@ -17,28 +18,12 @@ using namespace std::literals::chrono_literals; -namespace hex::pre { +namespace hex::init { - static void centerWindow(GLFWwindow *window) { - GLFWmonitor *monitor = glfwGetPrimaryMonitor(); - if (!monitor) - return; + WindowSplash::WindowSplash(int &argc, char **&argv) { + SharedData::mainArgc = argc; + SharedData::mainArgv = argv; - const GLFWvidmode *mode = glfwGetVideoMode(monitor); - if (!mode) - return; - - int monitorX, monitorY; - glfwGetMonitorPos(monitor, &monitorX, &monitorY); - - int windowWidth, windowHeight; - glfwGetWindowSize(window, &windowWidth, &windowHeight); - - glfwSetWindowPos(window, monitorX + (mode->width - windowWidth) / 2, monitorY + (mode->height - windowHeight) / 2); - } - - - WindowSplash::WindowSplash() { this->initGLFW(); this->initImGui(); } @@ -84,13 +69,14 @@ namespace hex::pre { break; } - if (splashTexture == nullptr) + if (splashTexture == nullptr) { + log::fatal("Could not load splash screen image!"); exit(EXIT_FAILURE); + } ON_SCOPE_EXIT { ImGui::UnloadImage(splashTexture); }; - - auto done = processTasksAsync(); + auto tasksSucceeded = processTasksAsync(); while (!glfwWindowShouldClose(this->m_window)) { glfwPollEvents(); @@ -120,22 +106,42 @@ namespace hex::pre { glfwSwapBuffers(this->m_window); - if (done.wait_for(0s) == std::future_status::ready) { - return done.get(); + if (tasksSucceeded.wait_for(0s) == std::future_status::ready) { + return tasksSucceeded.get(); } } return false; } + static void centerWindow(GLFWwindow *window) { + GLFWmonitor *monitor = glfwGetPrimaryMonitor(); + if (!monitor) + return; + + const GLFWvidmode *mode = glfwGetVideoMode(monitor); + if (!mode) + return; + + int monitorX, monitorY; + glfwGetMonitorPos(monitor, &monitorX, &monitorY); + + int windowWidth, windowHeight; + glfwGetWindowSize(window, &windowWidth, &windowHeight); + + glfwSetWindowPos(window, monitorX + (mode->width - windowWidth) / 2, monitorY + (mode->height - windowHeight) / 2); + } + void WindowSplash::initGLFW() { glfwSetErrorCallback([](int error, const char *description) { - printf("GLFW Error: %d - %s\n", error, description); + log::fatal("GLFW Error: {0} - {0}", error, description); exit(EXIT_FAILURE); }); - if (!glfwInit()) + if (!glfwInit()) { + log::fatal("Failed to initialize GLFW!"); exit(EXIT_FAILURE); + } glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); @@ -145,16 +151,20 @@ namespace hex::pre { glfwWindowHint(GLFW_FLOATING, GLFW_TRUE); this->m_window = glfwCreateWindow(640, 400, "ImHex", nullptr, nullptr); - if (this->m_window == nullptr) + if (this->m_window == nullptr) { + log::fatal("Failed to create GLFW window!"); exit(EXIT_FAILURE); + } centerWindow(this->m_window); glfwMakeContextCurrent(this->m_window); glfwSwapInterval(1); - if (gladLoadGL() == 0) + if (gladLoadGL() == 0) { + log::fatal("Failed to load OpenGL Context!"); exit(EXIT_FAILURE); + } } void WindowSplash::initImGui() { diff --git a/source/init/tasks.cpp b/source/init/tasks.cpp new file mode 100644 index 000000000..ccb46e257 --- /dev/null +++ b/source/init/tasks.cpp @@ -0,0 +1,150 @@ +#include "init/tasks.hpp" + +#include +#include +#include + +#include "views/view_hexeditor.hpp" +#include "views/view_pattern.hpp" +#include "views/view_pattern_data.hpp" +#include "views/view_hashes.hpp" +#include "views/view_information.hpp" +#include "views/view_help.hpp" +#include "views/view_tools.hpp" +#include "views/view_strings.hpp" +#include "views/view_data_inspector.hpp" +#include "views/view_disassembler.hpp" +#include "views/view_bookmarks.hpp" +#include "views/view_patches.hpp" +#include "views/view_command_palette.hpp" +#include "views/view_settings.hpp" +#include "views/view_data_processor.hpp" +#include "views/view_yara.hpp" + +#include "helpers/plugin_manager.hpp" + +#include + +namespace hex::init { + + static bool checkForUpdates() { + hex::Net net; + + auto releases = net.getJson("https://api.github.com/repos/WerWolv/ImHex/releases/latest"); + if (releases.code != 200) + return false; + + if (!releases.response.contains("tag_name") || !releases.response["tag_name"].is_string()) + return false; + + auto currVersion = "v" + std::string(IMHEX_VERSION).substr(0, 5); + auto latestVersion = releases.response["tag_name"].get(); + + if (latestVersion != currVersion) + getInitArguments().push_back({ "update-available", latestVersion.data() }); + + return true; + } + + bool createDirectories() { + + std::filesystem::create_directories(hex::getPath(ImHexPath::Patterns)[0]); + std::filesystem::create_directories(hex::getPath(ImHexPath::PatternsInclude)[0]); + std::filesystem::create_directories(hex::getPath(ImHexPath::Magic)[0]); + std::filesystem::create_directories(hex::getPath(ImHexPath::Plugins)[0]); + std::filesystem::create_directories(hex::getPath(ImHexPath::Resources)[0]); + std::filesystem::create_directories(hex::getPath(ImHexPath::Config)[0]); + + return true; + } + + bool loadDefaultViews() { + + ContentRegistry::Views::add(); + ContentRegistry::Views::add(); + ContentRegistry::Views::add(); + ContentRegistry::Views::add(); + ContentRegistry::Views::add(); + ContentRegistry::Views::add(); + ContentRegistry::Views::add(); + ContentRegistry::Views::add(); + ContentRegistry::Views::add(); + ContentRegistry::Views::add(); + ContentRegistry::Views::add(); + ContentRegistry::Views::add(); + ContentRegistry::Views::add(); + ContentRegistry::Views::add(); + ContentRegistry::Views::add(); + ContentRegistry::Views::add(); + + return true; + } + + bool deleteViews() { + for (auto &view : SharedData::views) + delete view; + SharedData::views.clear(); + + return true; + } + + bool loadPlugins() { + for (const auto &dir : hex::getPath(ImHexPath::Plugins)) { + PluginManager::load(dir); + } + + if (PluginManager::getPlugins().empty()) { + getInitArguments().push_back({ "no-plugins", { } }); + return false; + } + + for (const auto &plugin : PluginManager::getPlugins()) { + plugin.initializePlugin(); + } + + return true; + } + + bool unloadPlugins() { + PluginManager::unload(); + + return true; + } + + bool loadSettings() { + ContentRegistry::Settings::load(); + + return true; + } + + bool storeSettings() { + ContentRegistry::Settings::store(); + + return true; + } + + std::vector getInitTasks() { + return { + { "Checking for updates...", checkForUpdates }, + { "Creating directories...", createDirectories }, + { "Loading default views...", loadDefaultViews }, + { "Loading plugins...", loadPlugins }, + { "Loading settings...", loadSettings } + }; + } + + std::vector getExitTasks() { + return { + { "Unloading plugins...", unloadPlugins }, + { "Saving settings...", storeSettings }, + { "Cleaning up views...", deleteViews } + }; + } + + std::vector& getInitArguments() { + static std::vector initArguments; + + return initArguments; + } + +} \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index 8941ff839..9945ee86d 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -2,61 +2,47 @@ #include "window.hpp" -#include -#include -#include -#include +#include "init/splash_window.hpp" +#include "init/tasks.hpp" -#include "views/view_hexeditor.hpp" -#include "views/view_pattern.hpp" -#include "views/view_pattern_data.hpp" -#include "views/view_hashes.hpp" -#include "views/view_information.hpp" -#include "views/view_help.hpp" -#include "views/view_tools.hpp" -#include "views/view_strings.hpp" -#include "views/view_data_inspector.hpp" -#include "views/view_disassembler.hpp" -#include "views/view_bookmarks.hpp" -#include "views/view_patches.hpp" -#include "views/view_command_palette.hpp" -#include "views/view_settings.hpp" -#include "views/view_data_processor.hpp" -#include "views/view_yara.hpp" - -#include int main(int argc, char **argv) { using namespace hex; - Window window(argc, argv); + // Initialization + { + init::WindowSplash splashWindow(argc, argv); - // Shared Data - std::vector patternData; + for (const auto &[name, task] : init::getInitTasks()) + splashWindow.addStartupTask(name, task); - // Create views - ContentRegistry::Views::add(patternData); - ContentRegistry::Views::add(patternData); - ContentRegistry::Views::add(patternData); - ContentRegistry::Views::add(); - ContentRegistry::Views::add(); - ContentRegistry::Views::add(); - ContentRegistry::Views::add(); - ContentRegistry::Views::add(); - ContentRegistry::Views::add(); - ContentRegistry::Views::add(); - ContentRegistry::Views::add(); - ContentRegistry::Views::add(); - ContentRegistry::Views::add(); - ContentRegistry::Views::add(); - ContentRegistry::Views::add(); - ContentRegistry::Views::add(); + if (!splashWindow.loop()) + init::getInitArguments().push_back({ "tasks-failed", { } }); + } - if (argc > 2) - EventManager::post(argv[1]); + // Clean up + ON_SCOPE_EXIT { + for (const auto &[name, task] : init::getExitTasks()) + task(); + }; - window.loop(); + // Main window + { + Window window; + if (argc == 1) + ; // No arguments provided + else if (argc == 2) + EventManager::post(argv[1]); + else { + hex::log::fatal("Usage: imhex [file_name]"); + return EXIT_SUCCESS; + } + + hex::log::info("Welcome to ImHex!"); + + window.loop(); + } return EXIT_SUCCESS; } diff --git a/source/views/view_hexeditor.cpp b/source/views/view_hexeditor.cpp index 9b83516e1..65d209dc8 100644 --- a/source/views/view_hexeditor.cpp +++ b/source/views/view_hexeditor.cpp @@ -18,8 +18,7 @@ namespace hex { - ViewHexEditor::ViewHexEditor(std::vector &patternData) - : View("hex.view.hexeditor.name"_lang), m_patternData(patternData) { + ViewHexEditor::ViewHexEditor() : View("hex.view.hexeditor.name"_lang) { this->m_searchStringBuffer.resize(0xFFF, 0x00); this->m_searchHexBuffer.resize(0xFFF, 0x00); @@ -162,7 +161,7 @@ namespace hex { EventManager::subscribe(this, [this]() { this->m_highlightedBytes.clear(); - for (const auto &pattern : this->m_patternData) + for (const auto &pattern : SharedData::patternData) this->m_highlightedBytes.merge(pattern->getHighlightedAddresses()); }); diff --git a/source/views/view_pattern.cpp b/source/views/view_pattern.cpp index 3dca99d42..128d0c6f3 100644 --- a/source/views/view_pattern.cpp +++ b/source/views/view_pattern.cpp @@ -76,7 +76,7 @@ namespace hex { } - ViewPattern::ViewPattern(std::vector &patternData) : View("hex.view.pattern.name"), m_patternData(patternData) { + ViewPattern::ViewPattern() : View("hex.view.pattern.name") { this->m_patternLanguageRuntime = new lang::PatternLanguage(); this->m_textEditor.SetLanguageDefinition(PatternLanguage()); @@ -345,10 +345,10 @@ namespace hex { } void ViewPattern::clearPatternData() { - for (auto &data : this->m_patternData) + for (auto &data : SharedData::patternData) delete data; - this->m_patternData.clear(); + SharedData::patternData.clear(); lang::PatternData::resetPalette(); } @@ -371,7 +371,7 @@ namespace hex { this->m_console = this->m_patternLanguageRuntime->getConsoleLog(); if (result.has_value()) { - this->m_patternData = std::move(result.value()); + SharedData::patternData = std::move(result.value()); View::doLater([]{ EventManager::post(); }); diff --git a/source/views/view_pattern_data.cpp b/source/views/view_pattern_data.cpp index ccda1d767..c34b949a7 100644 --- a/source/views/view_pattern_data.cpp +++ b/source/views/view_pattern_data.cpp @@ -5,8 +5,7 @@ namespace hex { - ViewPatternData::ViewPatternData(std::vector &patternData) - : View("hex.view.pattern_data.name"), m_patternData(patternData) { + ViewPatternData::ViewPatternData() : View("hex.view.pattern_data.name") { EventManager::subscribe(this, [this]() { this->m_sortedPatternData.clear(); @@ -53,7 +52,7 @@ namespace hex { auto provider = SharedData::currentProvider; if (provider != nullptr && provider->isReadable()) { - if (beginPatternDataTable(provider, this->m_patternData, this->m_sortedPatternData)) { + if (beginPatternDataTable(provider, SharedData::patternData, this->m_sortedPatternData)) { ImGui::TableHeadersRow(); if (this->m_sortedPatternData.size() > 0) { diff --git a/source/window.cpp b/source/window.cpp index 11b4c57be..05d2c3e49 100644 --- a/source/window.cpp +++ b/source/window.cpp @@ -21,7 +21,8 @@ #include -#include "helpers/plugin_handler.hpp" +#include "helpers/plugin_manager.hpp" +#include "init/tasks.hpp" #include #include @@ -53,64 +54,19 @@ namespace hex { buf->append("\n"); } - Window::Window(int &argc, char **&argv) { - SharedData::mainArgc = argc; - SharedData::mainArgv = argv; + Window::Window() { SharedData::currentProvider = nullptr; - #if defined(RELEASE) + #if !defined(RELEASE) { - if (argc < 2) { - View::showFatalPopup("No launch arguments supplied! Please launch imhex instead of imhexg!"); - } - - std::string launchArguments = argv[argc - 1]; - - if (launchArguments.find("--args=") != 0) { - View::showFatalPopup("No launch arguments supplied! Please launch imhex instead of imhexg!"); - } - - bool commitChecked = false; - bool branchChecked = false; - - for (const auto& arg : hex::splitString(launchArguments.substr(7), "|")) { - auto splitArg = hex::splitString(arg, "="); - - // Handle flags - if (splitArg.size() == 1) { - auto &flag = splitArg[0]; - - if (flag == "tasks-failed") { - - } else if (flag == "splash-skipped") { - - } - } - // Handle arguments - else if (splitArg.size() == 2) { - auto &name = splitArg[0]; - auto &value = splitArg[1]; - - if (name == "git-hash") { - if (value != GIT_COMMIT_HASH) - View::showFatalPopup("Launcher and ImHex version commit mismatch. Please fully update ImHex before using it!"); - commitChecked = true; - } else if (name == "git-branch") { - if (value != GIT_BRANCH) - View::showFatalPopup("Launcher and ImHex version branch mismatch. Please fully update ImHex before using it!"); - branchChecked = true; - } else if (name == "update") { - this->m_availableUpdate = value; - } - } - // Handle others - else { - View::showFatalPopup("Invalid launch arguments supplied! Please launch imhex instead of imhexg!"); + for (const auto &[argument, value] : init::getInitArguments()) { + if (argument == "update-available") { + this->m_availableUpdate = value; + } else if (argument == "no-plugins") { + View::showErrorPopup("No plugins are loaded, including the built-in functions plugin!\n" + "Make sure you got at least builtin.hexplug in your plugins folder."); } } - - if (!commitChecked || !branchChecked) - View::showFatalPopup("No commit information available! Please launch imhex instead of imhexg!"); } #endif @@ -207,9 +163,6 @@ namespace hex { glfwSetWindowTitle(this->m_window, ("ImHex - " + windowTitle).c_str()); }); - this->initPlugins(); - - ContentRegistry::Settings::load(); EventManager::post(); for (const auto &path : ContentRegistry::Settings::read("hex.builtin.setting.imhex", "hex.builtin.setting.imhex.recent_files")) @@ -221,13 +174,6 @@ namespace hex { this->deinitImGui(); this->deinitGLFW(); - ContentRegistry::Settings::store(); - - for (auto &view : SharedData::views) - delete view; - SharedData::views.clear(); - - this->deinitPlugins(); EventManager::unsubscribe(this); EventManager::unsubscribe(this); @@ -236,7 +182,7 @@ namespace hex { } void Window::loop() { - bool pressedKeys[512] = { 0 }; + bool pressedKeys[512] = { false }; this->m_lastFrameTime = glfwGetTime(); while (!glfwWindowShouldClose(this->m_window)) { @@ -513,7 +459,7 @@ namespace hex { ImGui::TableNextColumn(); ImGui::TextUnformatted("hex.welcome.header.plugins"_lang); { - const auto &plugins = PluginHandler::getPlugins(); + const auto &plugins = PluginManager::getPlugins(); if (plugins.empty()) { // Intentionally left untranslated so it will be readable even if no plugin with translations is loaded @@ -817,20 +763,6 @@ namespace hex { ImGui_ImplOpenGL3_Init("#version 150"); } - void Window::initPlugins() { - for (const auto &dir : hex::getPath(ImHexPath::Plugins)) { - try { - PluginHandler::load(dir); - } catch (std::runtime_error &e) { - // Plugin folder not found. Not a problem. - } - } - - for (const auto &plugin : PluginHandler::getPlugins()) { - plugin.initializePlugin(); - } - } - void Window::deinitGLFW() { glfwDestroyWindow(this->m_window); glfwTerminate(); @@ -845,8 +777,4 @@ namespace hex { ImGui::DestroyContext(); } - void Window::deinitPlugins() { - PluginHandler::unload(); - } - } \ No newline at end of file diff --git a/splash/CMakeLists.txt b/splash/CMakeLists.txt deleted file mode 100644 index 150d4c5ba..000000000 --- a/splash/CMakeLists.txt +++ /dev/null @@ -1,34 +0,0 @@ -cmake_minimum_required(VERSION 3.16) -include("${CMAKE_SOURCE_DIR}/cmake/build_helpers.cmake") -include("${CMAKE_SOURCE_DIR}/cmake/modules/PostprocessBundle.cmake") - -get_target_property(IMHEX_VERSION imhex VERSION) -project(splash VERSION ${IMHEX_VERSION}) - -addVersionDefines() -configurePackageCreation() - -if (WIN32) - set(application_type "WIN32") -else () - set(application_type) -endif () - -add_executable(splash ${application_type} - source/main.cpp - source/splash_window.cpp - ${imhex_icon} -) - -target_include_directories(splash PUBLIC include) - -add_compile_definitions(IMHEX_FILE_NAME=\"$\") -set_target_properties(splash PROPERTIES OUTPUT_NAME "imhex") -add_dependencies(splash imhex) - - -if (WIN32) - target_link_libraries(splash libimhex winpthread) -else () - target_link_libraries(splash libimhex pthread) -endif () \ No newline at end of file diff --git a/splash/source/main.cpp b/splash/source/main.cpp deleted file mode 100644 index 0730c419b..000000000 --- a/splash/source/main.cpp +++ /dev/null @@ -1,94 +0,0 @@ -#include - -#include "splash_window.hpp" - -#include -#include -#include - -#include - -#include -#include - -using namespace std::literals::chrono_literals; - -constexpr auto ImHexPath = IMHEX_FILE_NAME; - -int main(int argc, char **argv) { - bool skipSplash = false; - std::string fileToOpen; - std::vector imHexArgs; - - // Handle command line arguments - { - if (argc == 1) { - // No optional arguments used, use default - } else if (argc == 2) { - if (argv[1] == std::string("--no-splash")) skipSplash = true; - else fileToOpen = argv[1]; - } else if (argc == 3) { - fileToOpen = argv[1]; - - if (argv[2] == std::string("--no-splash")) skipSplash = true; - else { - printf("Invalid argument '%s'\n", argv[2]); - return EXIT_FAILURE; - } - } else { - printf("Usage: imhex [file_path] [--no-splash] "); - return EXIT_FAILURE; - } - } - - imHexArgs.emplace_back("git-hash=" GIT_COMMIT_HASH); - imHexArgs.emplace_back("git-branch=" GIT_BRANCH); - - if (!skipSplash) { - hex::pre::WindowSplash window; - - window.addStartupTask("Checking for updates...", [&]{ - hex::Net net; - - auto releases = net.getJson("https://api.github.com/repos/WerWolv/ImHex/releases/latest"); - if (releases.code != 200) - return false; - - if (!releases.response.contains("tag_name") || !releases.response["tag_name"].is_string()) - return false; - - auto currVersion = "v" + std::string(IMHEX_VERSION).substr(0, 5); - auto latestVersion = releases.response["tag_name"].get(); - - if (latestVersion != currVersion) - imHexArgs.push_back(std::string("update=") + latestVersion.data()); - - return true; - }); - - window.addStartupTask("Creating directories...", []{ - std::filesystem::create_directories(hex::getPath(hex::ImHexPath::Patterns)[0]); - std::filesystem::create_directories(hex::getPath(hex::ImHexPath::PatternsInclude)[0]); - std::filesystem::create_directories(hex::getPath(hex::ImHexPath::Magic)[0]); - std::filesystem::create_directories(hex::getPath(hex::ImHexPath::Plugins)[0]); - std::filesystem::create_directories(hex::getPath(hex::ImHexPath::Resources)[0]); - std::filesystem::create_directories(hex::getPath(hex::ImHexPath::Config)[0]); - - return true; - }); - - if (!window.loop()) - imHexArgs.emplace_back("tasks-failed"); - - } else { - imHexArgs.emplace_back("splash-skipped"); - } - - // Launch ImHex - std::string packedArgs = "--args=" + hex::combineStrings(imHexArgs, "|"); - - if (fileToOpen.empty()) - return execl(ImHexPath, ImHexPath, packedArgs.c_str(), nullptr); - else - return execl(ImHexPath, ImHexPath, fileToOpen.c_str(), packedArgs.c_str(), nullptr); -} \ No newline at end of file