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

@@ -1,13 +1,11 @@
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
#include "UICanvas.h"
#include "Engine/Scripting/MException.h"
#include "Engine/Scripting/ManagedCLR/MException.h"
#include "Engine/Scripting/ManagedCLR/MMethod.h"
#include "Engine/Scripting/ManagedCLR/MClass.h"
#include "Engine/Scripting/ManagedCLR/MUtils.h"
#include "Engine/Serialization/Serialization.h"
#if USE_MONO
#include <mono/metadata/appdomain.h>
#endif
#if COMPILE_WITHOUT_CSHARP
#define UICANVAS_INVOKE(event)
@@ -85,7 +83,7 @@ void UICanvas::Serialize(SerializeStream& stream, const void* otherObj)
params[0] = other ? other->GetOrCreateManagedInstance() : nullptr;
MObject* exception = nullptr;
auto method = other ? UICanvas_SerializeDiff : UICanvas_Serialize;
auto invokeResultStr = (MonoString*)method->Invoke(GetOrCreateManagedInstance(), params, &exception);
auto invokeResultStr = (MString*)method->Invoke(GetOrCreateManagedInstance(), params, &exception);
if (exception)
{
MException ex(exception);
@@ -98,9 +96,7 @@ void UICanvas::Serialize(SerializeStream& stream, const void* otherObj)
else
{
// Write result data
auto invokeResultChars = mono_string_to_utf8(invokeResultStr);
stream.RawValue(invokeResultChars);
mono_free(invokeResultChars);
stream.RawValue(MCore::String::GetChars(invokeResultStr));
}
#endif
}
@@ -118,9 +114,8 @@ void UICanvas::Deserialize(DeserializeStream& stream, ISerializeModifier* modifi
rapidjson_flax::StringBuffer buffer;
rapidjson_flax::Writer<rapidjson_flax::StringBuffer> writer(buffer);
dataMember->value.Accept(writer);
const auto str = buffer.GetString();
void* args[1];
args[0] = mono_string_new(mono_domain_get(), str);
args[0] = MUtils::ToString(StringAnsiView(buffer.GetString(), (int32)buffer.GetSize()));
MObject* exception = nullptr;
UICanvas_Deserialize->Invoke(GetOrCreateManagedInstance(), args, &exception);
if (exception)

View File

@@ -1,14 +1,12 @@
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
#include "UIControl.h"
#include "Engine/Scripting/MException.h"
#include "Engine/Scripting/Scripting.h"
#include "Engine/Scripting/ManagedCLR/MException.h"
#include "Engine/Scripting/ManagedCLR/MMethod.h"
#include "Engine/Scripting/ManagedCLR/MClass.h"
#include "Engine/Scripting/Scripting.h"
#include "Engine/Scripting/ManagedCLR/MCore.h"
#include "Engine/Serialization/Serialization.h"
#if USE_MONO
#include <mono/metadata/appdomain.h>
#endif
#if COMPILE_WITHOUT_CSHARP
#define UICONTROL_INVOKE(event)
@@ -80,12 +78,12 @@ void UIControl::Serialize(SerializeStream& stream, const void* otherObj)
#if !COMPILE_WITHOUT_CSHARP
void* params[2];
MonoString* controlType = nullptr;
MString* controlType = nullptr;
params[0] = &controlType;
params[1] = other ? other->GetOrCreateManagedInstance() : nullptr;
MObject* exception = nullptr;
const auto method = other ? UIControl_SerializeDiff : UIControl_Serialize;
const auto invokeResultStr = (MonoString*)method->Invoke(GetOrCreateManagedInstance(), params, &exception);
const auto invokeResultStr = (MString*)method->Invoke(GetOrCreateManagedInstance(), params, &exception);
if (exception)
{
MException ex(exception);
@@ -107,19 +105,16 @@ void UIControl::Serialize(SerializeStream& stream, const void* otherObj)
return;
}
const auto controlTypeLength = mono_string_length(controlType);
if (controlTypeLength != 0)
const StringView controlTypeChars = MCore::String::GetChars(controlType);
if (controlTypeChars.Length() != 0)
{
stream.JKEY("Control");
const auto controlTypeChars = mono_string_to_utf8(controlType);
stream.String(controlTypeChars);
mono_free(controlTypeChars);
}
const StringView invokeResultStrChars = MCore::String::GetChars(invokeResultStr);
stream.JKEY("Data");
const auto invokeResultChars = mono_string_to_utf8(invokeResultStr);
stream.RawValue(invokeResultChars);
mono_free(invokeResultChars);
stream.RawValue(invokeResultStrChars);
#endif
}
@@ -134,15 +129,15 @@ void UIControl::Deserialize(DeserializeStream& stream, ISerializeModifier* modif
DESERIALIZE_MEMBER(NavTargetRight, _navTargetRight);
#if !COMPILE_WITHOUT_CSHARP
MonoReflectionType* typeObj = nullptr;
MTypeObject* typeObj = nullptr;
const auto controlMember = stream.FindMember("Control");
if (controlMember != stream.MemberEnd())
{
const StringAnsiView controlType(controlMember->value.GetStringAnsiView());
const auto type = Scripting::FindClass(controlType);
const MClass* type = Scripting::FindClass(controlType);
if (type != nullptr)
{
typeObj = mono_type_get_object(mono_domain_get(), mono_class_get_type(type->GetNative()));
typeObj = INTERNAL_TYPE_GET_OBJECT(type->GetType());
}
else
{
@@ -156,9 +151,8 @@ void UIControl::Deserialize(DeserializeStream& stream, ISerializeModifier* modif
rapidjson_flax::StringBuffer buffer;
rapidjson_flax::Writer<rapidjson_flax::StringBuffer> writer(buffer);
dataMember->value.Accept(writer);
const auto str = buffer.GetString();
void* args[2];
args[0] = mono_string_new(mono_domain_get(), str);
args[0] = MCore::String::New(buffer.GetString(), (int32)buffer.GetSize());
args[1] = typeObj;
MObject* exception = nullptr;
UIControl_Deserialize->Invoke(GetOrCreateManagedInstance(), args, &exception);