Remove mono from Glue functions

This commit is contained in:
Simon Lübeß
2025-07-17 20:07:19 +02:00
parent f77b153512
commit 271286fb7e
19 changed files with 546 additions and 742 deletions
+1 -1
View File
@@ -21,7 +21,7 @@ abstract class Asset : RefCounter
/// This identifier can be used to request the Asset from the content manager.
public StringView Identifier
{
get => _identifier;
get => _identifier..EnsureNullTerminator();
set => _identifier.Set(value);
}
@@ -36,6 +36,12 @@ static class CoreClrHelper
private function void CreateScriptInstanceFunc(UUID entityId, char8* scriptClassName);
static CreateScriptInstanceFunc _createScriptInstance;
private function void ThrowExceptionFunc(char8* message);
static ThrowExceptionFunc _throwException;
private function void RegisterComponentTypeFunc(StringView fullComponentTypeName, function void(UUID entityId) addComponent, function bool(UUID entityId) hasComponent, function void(UUID entityId) removeComponent);
static RegisterComponentTypeFunc _registerComponentType;
public static ScriptFunctionPointers _entityScriptFunctions;
public static void Init(StringView coreAssemblyPath)
@@ -106,6 +112,9 @@ static class CoreClrHelper
GetFunctionPointerUnmanagedCallersOnly("GlitchyEngine.ScriptGlue, ScriptCore", "CreateScriptInstance", out _createScriptInstance);
GetFunctionPointerUnmanagedCallersOnly("GlitchyEngine.ScriptGlue, ScriptCore", "ThrowException", out _throwException);
GetFunctionPointerUnmanagedCallersOnly("GlitchyEngine.ScriptGlue, ScriptCore", "RegisterComponentType", out _registerComponentType);
InitEntityFunctions();
}
@@ -182,4 +191,14 @@ static class CoreClrHelper
return rc;
}
public static void ThrowException(StringView message)
{
_throwException(message.Ptr);
}
public static void RegisterComponent(StringView fullComponentTypeName, function void(UUID entityId) addComponent, function bool(UUID entityId) hasComponent, function void(UUID entityId) removeComponent)
{
_registerComponentType(fullComponentTypeName, addComponent, hasComponent, removeComponent);
}
}
@@ -1,21 +1,21 @@
using System;
using GlitchyEngine.Core;
using Mono;
namespace GlitchyEngine.Scripting;
public class MonoExceptionHelper : RefCounter
public class ScriptException : RefCounter
{
private String _fullName ~ delete _;
private String _fullName ~ delete:append _;
private String _message ~ delete _;
private String _message ~ delete:append _;
private String _stackTrace ~ delete _;
private String _stackTrace ~ delete:append _;
/// The clean stack trace only contains the Managed Stack (the full trace contains one line for the native-to-managed entry)
private StringView _cleanStackTrace;
private MonoExceptionHelper _innerException ~ _?.ReleaseRef();
//TODO
//private ScriptException _innerException ~ _?.ReleaseRef();
public StringView FullName => _fullName;
public StringView Message => _message;
@@ -23,59 +23,22 @@ public class MonoExceptionHelper : RefCounter
public StringView StackTrace => _stackTrace;
public StringView CleanStackTrace => _cleanStackTrace;
public MonoExceptionHelper InnerException => _innerException;
//public ScriptException InnerException => _innerException;
public UUID Instance { get; set; }
public UUID EntityId { get; set; }
public this(MonoException* exception)
[AllowAppend]
public this(UUID entityId, StringView fullExceptionClassName, StringView message, StringView stackTrace)
{
MonoObject* exObject = (MonoObject*)exception;
String allocFullExceptionClassName = append String(fullExceptionClassName);
String allocMessage = append String(fullExceptionClassName);
String allocStackTrace = append String(fullExceptionClassName);
MonoClass* monoClass = Mono.mono_object_get_class(exObject);
_fullName = allocFullExceptionClassName;
_message = allocMessage;
_stackTrace = allocStackTrace;
StringView classNamespace = .(Mono.mono_class_get_namespace(monoClass));
StringView className = .(Mono.mono_class_get_name(monoClass));
_fullName = new $"{classNamespace}.{className}";
GetMessage(exObject, monoClass);
GetStackTrace(exception);
GetInnerException(exObject, monoClass);
}
private void GetMessage(MonoObject* exceptionObject, MonoClass* monoClass)
{
var messageProperty = Mono.mono_class_get_property_from_name(monoClass, "Message");
MonoObject* message = Mono.mono_property_get_value(messageProperty, exceptionObject, null, null);
char8* exMessage = Mono.mono_string_to_utf8((.)message);
_message = new String(exMessage);
Mono.mono_free(exMessage);
}
private void GetStackTrace(MonoException* exception)
{
char8* stacktracePtr = Mono.mono_exception_get_managed_backtrace(exception);
_stackTrace = new String(stacktracePtr);
int entryIndex = _stackTrace.IndexOf("at (wrapper native-to-managed)");
if (entryIndex != -1)
_cleanStackTrace = _stackTrace.Substring(0, entryIndex);
else
_cleanStackTrace = _stackTrace;
}
private void GetInnerException(MonoObject* exceptionObject, MonoClass* monoClass)
{
MonoProperty* innerExceptionProperty = Mono.mono_class_get_property_from_name(monoClass, "InnerException");
MonoObject* innerException = Mono.mono_property_get_value(innerExceptionProperty, exceptionObject, null, null);
if (innerException != null)
_innerException = new MonoExceptionHelper((MonoException*)innerException);
// TODO
_cleanStackTrace = _stackTrace;
}
}
+10 -8
View File
@@ -507,15 +507,18 @@ static class ScriptEngine
return scriptClass;
}
internal static void HandleMonoException(MonoException* exception, UUID entityId)
internal static void HandleException()
{
MonoExceptionHelper wrappedException = new MonoExceptionHelper(exception);
}
internal static void LogScriptException(ScriptException exception, UUID entityId)
{
String entityInfo = scope .();
if (entityId != .Zero)
{
wrappedException.Instance = entityId;
exception.EntityId = entityId;
Result<Entity> sourceEntity = Context.GetEntityByID(entityId);
@@ -525,14 +528,13 @@ static class ScriptEngine
}
}
Log.ClientLogger.Error($"Mono Exception \"{wrappedException.FullName}\": \"{wrappedException.Message}\"{entityInfo}\nStackTrace:\n{wrappedException.StackTrace}", wrappedException);
wrappedException.ReleaseRef();
Log.ClientLogger.Error($"Mono Exception \"{exception.FullName}\": \"{exception.Message}\"{entityInfo}\nStackTrace:\n{exception.StackTrace}", exception);
}
// TODO: This can go soon?
internal static void HandleMonoException(MonoException* exception, ScriptInstance sourceInstance = null)
{
HandleMonoException(exception, sourceInstance?.EntityId ?? .Zero);
//LogScriptException(exception, sourceInstance?.EntityId ?? .Zero);
}
public static void ShowScriptEditor(Entity entity, ScriptComponent* scriptComponent)
File diff suppressed because it is too large Load Diff
@@ -90,7 +90,7 @@ class SerializedObject
Fields.Add(nameCopy, (fieldType, data));
}
public void AddField(StringView name, SerializationType primitiveType, MonoObject* value, MonoString* fullTypeName)
public void AddField(StringView name, SerializationType primitiveType, void* value, StringView fullTypeName)
{
FieldData data = .();
@@ -102,15 +102,10 @@ class SerializedObject
if (value != null)
{
MonoString* string = (.)value;
char8* rawStringValue = Mono.mono_string_to_utf8(string);
String stringValue = new String(rawStringValue);
String stringValue = new String((char8*)value);
_ownedString.Add(stringValue);
Mono.mono_free(rawStringValue);
valueView = stringValue;
}
@@ -118,21 +113,14 @@ class SerializedObject
case .EngineObjectReference:
String typeName = null;
if (fullTypeName != null)
if (!fullTypeName.IsEmpty)
{
char8* rawTypeName = Mono.mono_string_to_utf8(fullTypeName);
typeName = new String(rawTypeName);
_ownedString.Add(typeName);
Mono.mono_free(rawTypeName);
_ownedString.Add(new String(fullTypeName));
}
data.EngineObject = (FullTypeName: typeName, ID: *(UUID*)Mono.mono_object_unbox(value));
data.EngineObject = (FullTypeName: typeName, ID: *(UUID*)value);
default:
void* rawValue = Mono.mono_object_unbox(value);
SetDataSimple(primitiveType, rawValue, ref data);
SetDataSimple(primitiveType, value, ref data);
}
AddField(name, primitiveType, data);
+2 -2
View File
@@ -686,7 +686,7 @@ namespace GlitchyEngine.World
private append List<Entity> _destroyQueue = .();
private append List<ScriptInstance> _destroyScriptQueue = .();
private append List<NewScriptInstance> _destroyScriptQueue = .();
float physicsDelta = 0;
@@ -926,7 +926,7 @@ namespace GlitchyEngine.World
* @param scriptInstance The script instance to destroy.
* @param removeComponent If set to true the ScriptComponent will be removed from the entity.
*/
public void DestroyScriptDeferred(ScriptInstance scriptInstance, bool removeComponent)
public void DestroyScriptDeferred(NewScriptInstance scriptInstance, bool removeComponent)
{
_destroyScriptQueue.Add(scriptInstance..AddRef());