From f6953fd829f1427fdd5024d96c60c981d77d4e50 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Fri, 17 May 2024 21:01:35 +0200 Subject: [PATCH] impr: Merge in script loader structure improvements from python branch --- plugins/builtin/CMakeLists.txt | 10 +-- .../content/views/view_data_inspector.cpp | 2 +- .../content/views/view_pattern_editor.cpp | 2 +- plugins/script_loader/CMakeLists.txt | 69 ++++++++++--------- .../include/loaders/dotnet/dotnet_loader.hpp | 2 +- .../script_loader/include/loaders/loader.hpp | 61 +++++++++++++++- .../source/loaders/dotnet/dotnet_loader.cpp | 34 --------- .../source/plugin_script_loader.cpp | 8 +-- .../script_loader/support/c/CMakeLists.txt | 14 ++++ .../{ => support/c}/include/script_api.hpp | 0 .../c}/source/script_api/v1/bookmarks.cpp | 0 .../c}/source/script_api/v1/logger.cpp | 0 .../c}/source/script_api/v1/mem.cpp | 8 +++ .../c}/source/script_api/v1/ui.cpp | 0 .../dotnet/AssemblyLoader/.gitignore | 0 .../AssemblyLoader/AssemblyLoader.csproj | 0 .../dotnet/AssemblyLoader/Program.cs | 0 .../PublishProfiles/FolderProfile.pubxml | 0 .../PublishProfiles/FolderProfile.pubxml.user | 0 .../{ => support}/dotnet/CMakeLists.txt | 0 20 files changed, 130 insertions(+), 80 deletions(-) create mode 100644 plugins/script_loader/support/c/CMakeLists.txt rename plugins/script_loader/{ => support/c}/include/script_api.hpp (100%) rename plugins/script_loader/{ => support/c}/source/script_api/v1/bookmarks.cpp (100%) rename plugins/script_loader/{ => support/c}/source/script_api/v1/logger.cpp (100%) rename plugins/script_loader/{ => support/c}/source/script_api/v1/mem.cpp (95%) rename plugins/script_loader/{ => support/c}/source/script_api/v1/ui.cpp (100%) rename plugins/script_loader/{ => support}/dotnet/AssemblyLoader/.gitignore (100%) rename plugins/script_loader/{ => support}/dotnet/AssemblyLoader/AssemblyLoader.csproj (100%) rename plugins/script_loader/{ => support}/dotnet/AssemblyLoader/Program.cs (100%) rename plugins/script_loader/{ => support}/dotnet/AssemblyLoader/Properties/PublishProfiles/FolderProfile.pubxml (100%) rename plugins/script_loader/{ => support}/dotnet/AssemblyLoader/Properties/PublishProfiles/FolderProfile.pubxml.user (100%) rename plugins/script_loader/{ => support}/dotnet/CMakeLists.txt (100%) diff --git a/plugins/builtin/CMakeLists.txt b/plugins/builtin/CMakeLists.txt index 59a7573d1..c920116f8 100644 --- a/plugins/builtin/CMakeLists.txt +++ b/plugins/builtin/CMakeLists.txt @@ -2,11 +2,6 @@ cmake_minimum_required(VERSION 3.16) include(ImHexPlugin) -find_file(DEFAULT_MAGIC_FILE_PATH magic.mgc HINTS ${LIBMAGIC_INCLUDE_DIR}/../share/misc) -if (DEFAULT_MAGIC_FILE_PATH) - add_romfs_resource(${DEFAULT_MAGIC_FILE_PATH} always_auto_extract/magic/magic.mgc) -endif () - add_imhex_plugin( NAME builtin @@ -126,6 +121,11 @@ add_imhex_plugin( LLVMDemangle ) +find_file(DEFAULT_MAGIC_FILE_PATH magic.mgc HINTS ${LIBMAGIC_INCLUDE_DIR}/../share/misc) +if (DEFAULT_MAGIC_FILE_PATH) + add_romfs_resource(${DEFAULT_MAGIC_FILE_PATH} always_auto_extract/magic/magic.mgc) +endif () + if (WIN32) target_link_libraries(builtin PRIVATE setupapi) endif () \ No newline at end of file diff --git a/plugins/builtin/source/content/views/view_data_inspector.cpp b/plugins/builtin/source/content/views/view_data_inspector.cpp index c57a2d505..107332567 100644 --- a/plugins/builtin/source/content/views/view_data_inspector.cpp +++ b/plugins/builtin/source/content/views/view_data_inspector.cpp @@ -184,7 +184,7 @@ namespace hex::plugin::builtin { } } } else { - const auto& error = m_runtime.getError(); + const auto& error = m_runtime.getEvalError(); log::error("Failed to execute custom inspector file '{}'!", wolv::util::toUTF8String(filePath)); if (error.has_value()) diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index 62489a998..3925dc1e2 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -1662,7 +1662,7 @@ namespace hex::plugin::builtin { m_lastEvaluationResult = runtime.executeString(code, pl::api::Source::DefaultSource, envVars, inVariables); if (!m_lastEvaluationResult) { - *m_lastEvaluationError = runtime.getError(); + *m_lastEvaluationError = runtime.getEvalError(); *m_lastCompileError = runtime.getCompileErrors(); } diff --git a/plugins/script_loader/CMakeLists.txt b/plugins/script_loader/CMakeLists.txt index 88625d3e7..2c1c40d74 100644 --- a/plugins/script_loader/CMakeLists.txt +++ b/plugins/script_loader/CMakeLists.txt @@ -1,24 +1,11 @@ cmake_minimum_required(VERSION 3.16) include(ImHexPlugin) + find_package(CoreClrEmbed) - -add_imhex_plugin( - NAME - script_loader - - SOURCES - source/plugin_script_loader.cpp - - INCLUDES - include - - LIBRARIES - fonts - ui -) - if (CoreClrEmbed_FOUND) + set(IMHEX_DOTNET_SCRIPT_SUPPORT ON) + add_library(nethost SHARED IMPORTED) target_include_directories(nethost INTERFACE "${CoreClrEmbed_INCLUDE_DIRS}") get_filename_component(CoreClrEmbed_FOLDER ${CoreClrEmbed_SHARED_LIBRARIES} DIRECTORY) @@ -29,25 +16,43 @@ if (CoreClrEmbed_FOUND) BUILD_RPATH ${CoreClrEmbed_FOLDER} INSTALL_RPATH ${CoreClrEmbed_FOLDER}) - target_link_directories(script_loader PRIVATE ${CoreClrEmbed_FOLDER}) - target_include_directories(script_loader PRIVATE ${CoreClrEmbed_INCLUDE_DIRS}) - target_compile_definitions(script_loader PRIVATE DOTNET_PLUGINS=1) - target_sources(script_loader PRIVATE - source/loaders/dotnet/dotnet_loader.cpp - - source/script_api/v1/mem.cpp - source/script_api/v1/bookmarks.cpp - source/script_api/v1/ui.cpp - source/script_api/v1/logger.cpp - ) - set(EXTRA_BUNDLE_LIBRARY_PATHS "${CoreClrEmbed_FOLDER}" PARENT_SCOPE) if (IMHEX_BUNDLE_DOTNET) install(FILES ${CoreClrEmbed_SHARED_LIBRARIES} DESTINATION ${CMAKE_INSTALL_LIBDIR}) - endif () + endif() +endif() - add_subdirectory(dotnet) +add_subdirectory(support/c) + + + +add_imhex_plugin( + NAME + script_loader + + SOURCES + source/plugin_script_loader.cpp + INCLUDES + include + + LIBRARIES + c_api + fonts +) + + + +if (IMHEX_DOTNET_SCRIPT_SUPPORT) + message(STATUS "Enabling .NET Scripting support!") + + target_link_directories(script_loader PRIVATE ${CoreClrEmbed_FOLDER}) + target_include_directories(script_loader PRIVATE ${CoreClrEmbed_INCLUDE_DIRS}) + target_compile_definitions(script_loader PRIVATE IMHEX_DOTNET_SCRIPT_SUPPORT=1) + target_sources(script_loader PRIVATE + source/loaders/dotnet/dotnet_loader.cpp + ) + + add_subdirectory(support/dotnet) add_dependencies(script_loader AssemblyLoader) - -endif () \ No newline at end of file +endif() \ No newline at end of file diff --git a/plugins/script_loader/include/loaders/dotnet/dotnet_loader.hpp b/plugins/script_loader/include/loaders/dotnet/dotnet_loader.hpp index b3159916b..f0cb3467f 100644 --- a/plugins/script_loader/include/loaders/dotnet/dotnet_loader.hpp +++ b/plugins/script_loader/include/loaders/dotnet/dotnet_loader.hpp @@ -10,7 +10,7 @@ namespace hex::script::loader { class DotNetLoader : public ScriptLoader { public: - DotNetLoader() = default; + DotNetLoader() : ScriptLoader(".NET") {} ~DotNetLoader() override = default; bool initialize() override; diff --git a/plugins/script_loader/include/loaders/loader.hpp b/plugins/script_loader/include/loaders/loader.hpp index 52a30cc2b..77688230f 100644 --- a/plugins/script_loader/include/loaders/loader.hpp +++ b/plugins/script_loader/include/loaders/loader.hpp @@ -3,31 +3,45 @@ #include #include #include +#include + +#if defined(OS_WINDOWS) + #include +#else + #include +#endif namespace hex::script::loader { + class ScriptLoader; + struct Script { std::string name; bool background; std::function entryPoint; + const ScriptLoader *loader; }; class ScriptLoader { public: - ScriptLoader() = default; + ScriptLoader(std::string typeName) : m_typeName(std::move(typeName)) {} virtual ~ScriptLoader() = default; virtual bool initialize() = 0; virtual bool loadAll() = 0; void addScript(std::string name, bool background, std::function entryPoint) { - m_scripts.emplace_back(std::move(name), background, std::move(entryPoint)); + m_scripts.emplace_back(std::move(name), background, std::move(entryPoint), this); } const auto& getScripts() const { return m_scripts; } + const std::string& getTypeName() const { + return m_typeName; + } + protected: void clearScripts() { m_scripts.clear(); @@ -35,6 +49,49 @@ namespace hex::script::loader { private: std::vector