From 4020ac98434233ad918c7619766e74e1e879f087 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 2 Jul 2024 23:15:54 +0200 Subject: [PATCH] impr: Added nicer console warning when .NET runtime isn't installed --- cmake/modules/FindCoreClrEmbed.cmake | 1 + plugins/script_loader/CMakeLists.txt | 4 ++++ .../source/loaders/dotnet/dotnet_loader.cpp | 24 ++++++++++++------- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/cmake/modules/FindCoreClrEmbed.cmake b/cmake/modules/FindCoreClrEmbed.cmake index c61acd165..6bea2c57b 100644 --- a/cmake/modules/FindCoreClrEmbed.cmake +++ b/cmake/modules/FindCoreClrEmbed.cmake @@ -89,4 +89,5 @@ if (CoreClrEmbed_INCLUDE_DIR AND CoreClrEmbed_LIBRARY) set(CoreClrEmbed_LIBRARIES "${CoreClrEmbed_LIBRARY}" CACHE STRING "CoreClrEmbed libraries" FORCE) set(CoreClrEmbed_SHARED_LIBRARIES "${CoreClrEmbed_SHARED_LIBRARY}" CACHE STRING "CoreClrEmbed shared libraries" FORCE) set(CoreClrEmbed_INCLUDE_DIRS "${CoreClrEmbed_INCLUDE_DIR}" CACHE STRING "CoreClrEmbed include directories" FORCE) + set(CoreClrEmbed_VERSION "${CORECLR_RUNTIME_VERSION_FULL}" CACHE STRING "CoreClrEmbed version" FORCE) endif() \ No newline at end of file diff --git a/plugins/script_loader/CMakeLists.txt b/plugins/script_loader/CMakeLists.txt index 0238c38ac..7c162f88d 100644 --- a/plugins/script_loader/CMakeLists.txt +++ b/plugins/script_loader/CMakeLists.txt @@ -3,9 +3,12 @@ cmake_minimum_required(VERSION 3.16) include(ImHexPlugin) find_package(CoreClrEmbed) +add_library(dotnet INTERFACE) if (CoreClrEmbed_FOUND) set(IMHEX_DOTNET_SCRIPT_SUPPORT ON) + target_compile_definitions(dotnet INTERFACE IMHEX_DOTNET_RUNTIME_VERSION="${CoreClrEmbed_VERSION}") + add_library(nethost SHARED IMPORTED) target_include_directories(nethost INTERFACE "${CoreClrEmbed_INCLUDE_DIRS}") get_filename_component(CoreClrEmbed_FOLDER ${CoreClrEmbed_SHARED_LIBRARIES} DIRECTORY) @@ -40,6 +43,7 @@ add_imhex_plugin( c_api fonts ui + dotnet ) diff --git a/plugins/script_loader/source/loaders/dotnet/dotnet_loader.cpp b/plugins/script_loader/source/loaders/dotnet/dotnet_loader.cpp index 9e8abb014..1fffef1e2 100644 --- a/plugins/script_loader/source/loaders/dotnet/dotnet_loader.cpp +++ b/plugins/script_loader/source/loaders/dotnet/dotnet_loader.cpp @@ -13,6 +13,7 @@ #include #include #include + #include #include @@ -110,13 +111,7 @@ namespace hex::script::loader { = getExport(hostfxrLibrary, "hostfxr_set_error_writer"); } - hostfxr_set_error_writer([] HOSTFXR_CALLTYPE (const char_t *message) { - #if defined(OS_WINDOWS) - log::error("{}", utf16ToUtf8(message)); - #else - log::error("{}", message); - #endif - }); + hostfxr_set_error_writer([] HOSTFXR_CALLTYPE (const char_t *) { }); return hostfxr_initialize_for_runtime_config != nullptr && @@ -137,7 +132,12 @@ namespace hex::script::loader { }; if (result > 2 || ctx == nullptr) { - throw std::runtime_error(hex::format("Failed to initialize command line 0x{:X}", result)); + if (result == /* FrameworkMissingFailure */ 0x80008096) { + log::warn("ImHex has built-in support for .NET scripts and extensions. However, these can only be used when the .NET runtime is installed."); + log::warn("Please install version {} or later of the .NET runtime if you plan to use them. Otherwise this error can be safely ignored.", IMHEX_DOTNET_RUNTIME_VERSION); + } + + throw std::runtime_error(hex::format("Command line init failed 0x{:X}", result)); } #if defined (OS_WINDOWS) @@ -146,6 +146,14 @@ namespace hex::script::loader { hostfxr_set_runtime_property_value(ctx, STRING("PINVOKE_OVERRIDE"), hex::format("{}", (void*)pInvokeOverride).c_str()); #endif + hostfxr_set_error_writer([] HOSTFXR_CALLTYPE (const char_t *message) { + #if defined(OS_WINDOWS) + log::error("{}", utf16ToUtf8(message)); + #else + log::error("{}", message); + #endif + }); + result = hostfxr_get_runtime_delegate( ctx, hostfxr_delegate_type::hdt_load_assembly_and_get_function_pointer,