From 4f719769582a01e49531a926839321b36616bd03 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Fri, 27 Jan 2023 14:24:53 +0100 Subject: [PATCH] CoreCRL fixes and tweaks --- Source/Engine/Scripting/DotNet/CoreCLR.cpp | 62 +++++++++---------- Source/Engine/Scripting/DotNet/CoreCLR.h | 4 +- Source/Engine/Scripting/ManagedCLR/MCore.cpp | 10 +-- .../VisualStudioCodeProjectGenerator.cs | 1 - 4 files changed, 33 insertions(+), 44 deletions(-) diff --git a/Source/Engine/Scripting/DotNet/CoreCLR.cpp b/Source/Engine/Scripting/DotNet/CoreCLR.cpp index bd65a9af8..959273146 100644 --- a/Source/Engine/Scripting/DotNet/CoreCLR.cpp +++ b/Source/Engine/Scripting/DotNet/CoreCLR.cpp @@ -34,28 +34,27 @@ hostfxr_set_error_writer_fn hostfxr_set_error_writer; hostfxr_get_dotnet_environment_info_result_fn hostfxr_get_dotnet_environment_info_result; hostfxr_run_app_fn hostfxr_run_app; -bool CoreCLR::LoadHostfxr(const String& library_path_) +bool CoreCLR::InitHostfxr(const String& configPath, const String& libraryPath) { - const FLAX_CORECLR_STRING& library_path = FLAX_CORECLR_STRING(library_path_); + const FLAX_CORECLR_STRING& library_path = FLAX_CORECLR_STRING(libraryPath); + // Get path to hostfxr library + get_hostfxr_parameters get_hostfxr_params; + get_hostfxr_params.size = sizeof(hostfxr_initialize_parameters); + get_hostfxr_params.assembly_path = library_path.Get(); + get_hostfxr_params.dotnet_root = nullptr;//dotnetRoot.Get(); char_t hostfxrPath[1024]; size_t hostfxrPathSize = sizeof(hostfxrPath) / sizeof(char_t); - - get_hostfxr_parameters params; - params.size = sizeof(hostfxr_initialize_parameters); - params.assembly_path = library_path.Get(); - params.dotnet_root = nullptr;//dotnetRoot.Get(); - - int rc = get_hostfxr_path(hostfxrPath, &hostfxrPathSize, ¶ms); + int rc = get_hostfxr_path(hostfxrPath, &hostfxrPathSize, &get_hostfxr_params); if (rc != 0) { LOG(Error, "Failed to find hostfxr: {0:x}", (unsigned int)rc); - return false; + return true; } - String path(hostfxrPath); + const String path(hostfxrPath); LOG(Info, "Found hostfxr in {0}", path); - void *hostfxr = Platform::LoadLibrary(path.Get()); + void* hostfxr = Platform::LoadLibrary(path.Get()); hostfxr_initialize_for_runtime_config = (hostfxr_initialize_for_runtime_config_fn)Platform::GetProcAddress(hostfxr, "hostfxr_initialize_for_runtime_config"); hostfxr_initialize_for_dotnet_command_line = (hostfxr_initialize_for_dotnet_command_line_fn)Platform::GetProcAddress(hostfxr, "hostfxr_initialize_for_dotnet_command_line"); hostfxr_get_runtime_delegate = (hostfxr_get_runtime_delegate_fn)Platform::GetProcAddress(hostfxr, "hostfxr_get_runtime_delegate"); @@ -63,42 +62,39 @@ bool CoreCLR::LoadHostfxr(const String& library_path_) hostfxr_set_error_writer = (hostfxr_set_error_writer_fn)Platform::GetProcAddress(hostfxr, "hostfxr_set_error_writer"); hostfxr_get_dotnet_environment_info_result = (hostfxr_get_dotnet_environment_info_result_fn)Platform::GetProcAddress(hostfxr, "hostfxr_get_dotnet_environment_info_result"); hostfxr_run_app = (hostfxr_run_app_fn)Platform::GetProcAddress(hostfxr, "hostfxr_run_app"); + if (!hostfxr_get_runtime_delegate || !hostfxr_run_app) + { + LOG(Error, "Failed to setup hostfxr API"); + return true; + } - return true; -} - -bool CoreCLR::InitHostfxr(const String& config_path, const String& library_path_) -{ - const FLAX_CORECLR_STRING& library_path = FLAX_CORECLR_STRING(library_path_); + // Initialize hosting component const char_t* argv[1] = { library_path.Get() }; - - hostfxr_initialize_parameters params; - params.size = sizeof(hostfxr_initialize_parameters); - params.host_path = library_path.Get(); - params.dotnet_root = nullptr;//dotnetRoot.Get(); // This probably must be set - + hostfxr_initialize_parameters init_params; + init_params.size = sizeof(hostfxr_initialize_parameters); + init_params.host_path = library_path.Get(); + init_params.dotnet_root = nullptr;//dotnetRoot.Get(); // This probably must be set hostfxr_handle handle = nullptr; - - // Initialize hosting component, hostfxr_initialize_for_dotnet_command_line is used here - // to allow self-contained engine installation to be used when needed. - - int rc = hostfxr_initialize_for_dotnet_command_line(1, argv, ¶ms, &handle); + rc = hostfxr_initialize_for_dotnet_command_line(ARRAY_COUNT(argv), argv, &init_params, &handle); if (rc != 0 || handle == nullptr) { LOG(Error, "Failed to initialize hostfxr: {0:x}", (unsigned int)rc); hostfxr_close(handle); - return false; + return true; } void* pget_function_pointer = nullptr; rc = hostfxr_get_runtime_delegate(handle, hdt_get_function_pointer, &pget_function_pointer); if (rc != 0 || pget_function_pointer == nullptr) - LOG(Error, "Failed to get runtime delegate hdt_get_function_pointer: {0:x}", (unsigned int)rc); + { + LOG(Error, "Failed to get runtime delegate hdt_get_function_pointer: 0x{0:x}", (unsigned int)rc); + hostfxr_close(handle); + return true; + } hostfxr_close(handle); get_function_pointer = (get_function_pointer_fn)pget_function_pointer; - - return true; + return false; } void* CoreCLR::GetStaticMethodPointer(const String& methodName) diff --git a/Source/Engine/Scripting/DotNet/CoreCLR.h b/Source/Engine/Scripting/DotNet/CoreCLR.h index dd0c960d3..60ad1742e 100644 --- a/Source/Engine/Scripting/DotNet/CoreCLR.h +++ b/Source/Engine/Scripting/DotNet/CoreCLR.h @@ -18,10 +18,8 @@ class CoreCLR { -private: public: - static bool LoadHostfxr(const String& library_path); - static bool InitHostfxr(const String& config_path, const String& library_path); + static bool InitHostfxr(const String& configPath, const String& libraryPath); /// /// Returns the function pointer to the managed static method in NativeInterop class. diff --git a/Source/Engine/Scripting/ManagedCLR/MCore.cpp b/Source/Engine/Scripting/ManagedCLR/MCore.cpp index d65c04a0e..a96eb5bff 100644 --- a/Source/Engine/Scripting/ManagedCLR/MCore.cpp +++ b/Source/Engine/Scripting/ManagedCLR/MCore.cpp @@ -126,13 +126,9 @@ bool MCore::LoadEngine() if (!FileSystem::FileExists(csharpRuntimeConfigPath)) LOG(Fatal, "Failed to initialize managed runtime, FlaxEngine.CSharp.runtimeconfig.json is missing."); - // Locate hostfxr and load it - if (!CoreCLR::LoadHostfxr(csharpLibraryPath)) - return false; - - // Initialize hosting component - if (!CoreCLR::InitHostfxr(csharpRuntimeConfigPath, csharpLibraryPath)) - return false; + // Initialize hostfxr + if (CoreCLR::InitHostfxr(csharpRuntimeConfigPath, csharpLibraryPath)) + return true; // Prepare managed side CoreCLR::CallStaticMethodByName(TEXT("Init")); diff --git a/Source/Tools/Flax.Build/Projects/VisualStudioCode/VisualStudioCodeProjectGenerator.cs b/Source/Tools/Flax.Build/Projects/VisualStudioCode/VisualStudioCodeProjectGenerator.cs index 81c8d2893..eb6c2bb24 100644 --- a/Source/Tools/Flax.Build/Projects/VisualStudioCode/VisualStudioCodeProjectGenerator.cs +++ b/Source/Tools/Flax.Build/Projects/VisualStudioCode/VisualStudioCodeProjectGenerator.cs @@ -480,7 +480,6 @@ namespace Flax.Build.Projects.VisualStudioCode case TargetPlatform.Linux: break; } - json.AddField("visualizerFile", Path.Combine(Globals.EngineRoot, "Source", "flax.natvis")); } json.EndObject(); }