From 839eb3d4934063cc72686ed631045679cee2f81e Mon Sep 17 00:00:00 2001 From: praydog Date: Mon, 18 Mar 2024 15:55:10 -0700 Subject: [PATCH] .NET: Don't fail outright if compiler failed before loading DLLs --- csharp-api/REFrameworkNET/PluginManager.cpp | 28 +++++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/csharp-api/REFrameworkNET/PluginManager.cpp b/csharp-api/REFrameworkNET/PluginManager.cpp index 1f8b2fd29..25c82fda4 100644 --- a/csharp-api/REFrameworkNET/PluginManager.cpp +++ b/csharp-api/REFrameworkNET/PluginManager.cpp @@ -100,7 +100,25 @@ namespace REFrameworkNET { PluginManager::s_api_instance = gcnew REFrameworkNET::API(param_raw); } - LoadPlugins_FromSourceCode(param_raw); + // Try-catch because the user might not have the compiler + // dependencies in the plugins directory + try { + LoadPlugins_FromSourceCode(param_raw); + } catch (System::Exception^ e) { + REFrameworkNET::API::LogError("Could not load plugins from source code: " + e->Message); + + auto ex = e; + while (ex != nullptr) { + REFrameworkNET::API::LogError(ex->StackTrace); + ex = ex->InnerException; + } + } catch (const std::exception& e) { + REFrameworkNET::API::LogError("Could not load plugins from source code: " + gcnew System::String(e.what())); + } catch (...) { + REFrameworkNET::API::LogError("Could not load plugins from source code: Unknown exception caught"); + } + + System::Console::WriteLine("Continue with managed plugins..."); const auto managed_path = std::filesystem::current_path() / "reframework" / "plugins" / "managed"; std::filesystem::create_directories(managed_path); @@ -111,7 +129,7 @@ namespace REFrameworkNET { auto files = System::IO::Directory::GetFiles(managed_dir, "*.dll"); if (files->Length == 0) { - //API::get()->log_error("No DLLs found in %s", managedDir); + REFrameworkNET::API::LogInfo("No DLLs found in " + managed_dir); return false; } @@ -120,7 +138,7 @@ namespace REFrameworkNET { System::Reflection::Assembly^ assem = System::Reflection::Assembly::LoadFrom(file); if (assem == nullptr) { - Console::WriteLine("Failed to load assembly from " + file); + REFrameworkNET::API::LogError("Failed to load assembly from " + file); continue; } @@ -133,7 +151,7 @@ namespace REFrameworkNET { gcnew array{REFrameworkNET::API::typeid}); if (mainMethod != nullptr) { - Console::WriteLine("Found Main method in " + file); + REFrameworkNET::API::LogInfo("Found Main method in " + file); array^ args = gcnew array{PluginManager::s_api_instance}; mainMethod->Invoke(nullptr, args); @@ -143,7 +161,7 @@ namespace REFrameworkNET { } if (!ever_found) { - Console::WriteLine("No Main method found in any DLLs in " + managed_dir); + REFrameworkNET::API::LogInfo("No Main method found in any DLLs in " + managed_dir); } return true;