Refactor CoreCLR runtime into explicit dotnet api instead of mocking mono api

Required by platforms that will use mono under the hood for .Net 7
New `USE_CSHARP` define for C# ability
Engine doesn't use `mono_*` apis directly but via MCore/MClass/MMethod/ apis
This commit is contained in:
Wojtek Figat
2023-03-27 17:29:42 +02:00
parent eed2cdfe04
commit 510fc443e8
111 changed files with 17048 additions and 8765 deletions

View File

@@ -3,17 +3,15 @@
#include "DebugLog.h"
#include "Engine/Scripting/Scripting.h"
#include "Engine/Scripting/BinaryModule.h"
#include "Engine/Scripting/MainThreadManagedInvokeAction.h"
#include "Engine/Scripting/ManagedCLR/MCore.h"
#include "Engine/Scripting/ManagedCLR/MDomain.h"
#include "Engine/Scripting/ManagedCLR/MAssembly.h"
#include "Engine/Scripting/ManagedCLR/MClass.h"
#include "Engine/Scripting/Internal/MainThreadManagedInvokeAction.h"
#include "Engine/Threading/Threading.h"
#include "FlaxEngine.Gen.h"
#if USE_MONO
#include <mono/metadata/exception.h>
#include <mono/metadata/appdomain.h>
#if USE_CSHARP
namespace Impl
{
@@ -68,19 +66,19 @@ bool CacheMethods()
void DebugLog::Log(LogType type, const StringView& message)
{
#if USE_MONO
#if USE_CSHARP
if (CacheMethods())
return;
auto scriptsDomain = Scripting::GetScriptsDomain();
MainThreadManagedInvokeAction::ParamsBuilder params;
params.AddParam(type);
params.AddParam(message, scriptsDomain->GetNative());
params.AddParam(message, scriptsDomain);
#if BUILD_RELEASE
params.AddParam(StringView::Empty, scriptsDomain->GetNative());
params.AddParam(StringView::Empty, scriptsDomain);
#else
const String stackTrace = Platform::GetStackTrace(1);
params.AddParam(stackTrace, scriptsDomain->GetNative());
params.AddParam(stackTrace, scriptsDomain);
#endif
MainThreadManagedInvokeAction::Invoke(Internal_SendLog, params);
#endif
@@ -88,7 +86,7 @@ void DebugLog::Log(LogType type, const StringView& message)
void DebugLog::LogException(MObject* exceptionObject)
{
#if USE_MONO
#if USE_CSHARP
if (exceptionObject == nullptr || CacheMethods())
return;
@@ -101,11 +99,11 @@ void DebugLog::LogException(MObject* exceptionObject)
String DebugLog::GetStackTrace()
{
String result;
#if USE_MONO
#if USE_CSHARP
if (!CacheMethods())
{
auto stackTraceObj = Internal_GetStackTrace->Invoke(nullptr, nullptr, nullptr);
MUtils::ToString((MonoString*)stackTraceObj, result);
MUtils::ToString((MString*)stackTraceObj, result);
}
#endif
return result;
@@ -113,10 +111,9 @@ String DebugLog::GetStackTrace()
void DebugLog::ThrowException(const char* msg)
{
#if USE_MONO
// Throw exception to the C# world
auto ex = mono_exception_from_name_msg(mono_get_corlib(), "System", "Exception", msg);
mono_raise_exception(ex);
#if USE_CSHARP
auto ex = MCore::Exception::Get(msg);
MCore::Exception::Throw(ex);
#endif
}
@@ -125,45 +122,40 @@ void DebugLog::ThrowNullReference()
//LOG(Warning, "Invalid null reference.");
//LOG_STR(Warning, DebugLog::GetStackTrace());
#if USE_MONO
// Throw exception to the C# world
auto ex = mono_get_exception_null_reference();
mono_raise_exception(ex);
#if USE_CSHARP
auto ex = MCore::Exception::GetNullReference();
MCore::Exception::Throw(ex);
#endif
}
void DebugLog::ThrowArgument(const char* arg, const char* msg)
{
#if USE_MONO
// Throw exception to the C# world
auto ex = mono_get_exception_argument(arg, msg);
mono_raise_exception(ex);
#if USE_CSHARP
auto ex = MCore::Exception::GetArgument(arg, msg);
MCore::Exception::Throw(ex);
#endif
}
void DebugLog::ThrowArgumentNull(const char* arg)
{
#if USE_MONO
// Throw exception to the C# world
auto ex = mono_get_exception_argument_null(arg);
mono_raise_exception(ex);
#if USE_CSHARP
auto ex = MCore::Exception::GetArgumentNull(arg);
MCore::Exception::Throw(ex);
#endif
}
void DebugLog::ThrowArgumentOutOfRange(const char* arg)
{
#if USE_MONO
// Throw exception to the C# world
auto ex = mono_get_exception_argument_out_of_range(arg);
mono_raise_exception(ex);
#if USE_CSHARP
auto ex = MCore::Exception::GetArgumentOutOfRange(arg);
MCore::Exception::Throw(ex);
#endif
}
void DebugLog::ThrowNotSupported(const char* msg)
{
#if USE_MONO
// Throw exception to the C# world
auto ex = mono_get_exception_not_supported(msg);
mono_raise_exception(ex);
#if USE_CSHARP
auto ex = MCore::Exception::GetNotSupported(msg);
MCore::Exception::Throw(ex);
#endif
}