mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Try catch in C# Glue Functions called by engine, removed test ThrowException method
This commit is contained in:
@@ -113,7 +113,6 @@ static class CoreClrHelper
|
|||||||
|
|
||||||
GetFunctionPointerUnmanagedCallersOnly("GlitchyEngine.ScriptGlue, ScriptCore", "CreateScriptInstance", out _createScriptInstance);
|
GetFunctionPointerUnmanagedCallersOnly("GlitchyEngine.ScriptGlue, ScriptCore", "CreateScriptInstance", out _createScriptInstance);
|
||||||
|
|
||||||
GetFunctionPointerUnmanagedCallersOnly("GlitchyEngine.ScriptGlue, ScriptCore", "ThrowException", out _throwException);
|
|
||||||
GetFunctionPointerUnmanagedCallersOnly("GlitchyEngine.ScriptGlue, ScriptCore", "RegisterComponentType", out _registerComponentType);
|
GetFunctionPointerUnmanagedCallersOnly("GlitchyEngine.ScriptGlue, ScriptCore", "RegisterComponentType", out _registerComponentType);
|
||||||
|
|
||||||
InitEntityFunctions();
|
InitEntityFunctions();
|
||||||
|
|||||||
+95
-16
@@ -73,10 +73,15 @@ internal static unsafe partial class ScriptGlue
|
|||||||
/// <param name="engineFunctions">The struct containing the function pointers to the engine functions.</param>
|
/// <param name="engineFunctions">The struct containing the function pointers to the engine functions.</param>
|
||||||
[UnmanagedCallersOnly]
|
[UnmanagedCallersOnly]
|
||||||
public static void SetEngineFunctions(EngineFunctions* engineFunctions)
|
public static void SetEngineFunctions(EngineFunctions* engineFunctions)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
_engineFunctions = *engineFunctions;
|
_engineFunctions = *engineFunctions;
|
||||||
|
}
|
||||||
Log.Info("Yeah");
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Exception(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -84,6 +89,8 @@ internal static unsafe partial class ScriptGlue
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
[UnmanagedCallersOnly]
|
[UnmanagedCallersOnly]
|
||||||
public static void LoadScriptAssembly(byte* assemblyData, long assemblyLength, byte* pdbData, long pdbLength)
|
public static void LoadScriptAssembly(byte* assemblyData, long assemblyLength, byte* pdbData, long pdbLength)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
using UnmanagedMemoryStream assemblyStream = new(assemblyData, assemblyLength);
|
using UnmanagedMemoryStream assemblyStream = new(assemblyData, assemblyLength);
|
||||||
|
|
||||||
@@ -91,10 +98,13 @@ internal static unsafe partial class ScriptGlue
|
|||||||
|
|
||||||
LoadAssembly(assemblyStream, pdbStream);
|
LoadAssembly(assemblyStream, pdbStream);
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Exception(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void LoadAssembly(Stream assemblyStream, Stream? pdbStream)
|
private static void LoadAssembly(Stream assemblyStream, Stream? pdbStream)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
_scriptAssemblyContext ??= new AssemblyLoadContext("ScriptContext", true);
|
_scriptAssemblyContext ??= new AssemblyLoadContext("ScriptContext", true);
|
||||||
|
|
||||||
@@ -102,21 +112,23 @@ internal static unsafe partial class ScriptGlue
|
|||||||
|
|
||||||
Debug.Assert(_appAssembly != null);
|
Debug.Assert(_appAssembly != null);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Fehler: {e}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Called by the engine to unload the assembly (containing user scripts).
|
/// Called by the engine to unload the assembly (containing user scripts).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[UnmanagedCallersOnly]
|
[UnmanagedCallersOnly]
|
||||||
public static void UnloadAssemblies()
|
public static void UnloadAssemblies()
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
_scriptAssemblyContext?.Unload();
|
_scriptAssemblyContext?.Unload();
|
||||||
_scriptAssemblyContext = null;
|
_scriptAssemblyContext = null;
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Exception(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static NativeScriptClassInfo[]? _unsafeClasses;
|
private static NativeScriptClassInfo[]? _unsafeClasses;
|
||||||
|
|
||||||
@@ -147,6 +159,8 @@ internal static unsafe partial class ScriptGlue
|
|||||||
/// </remarks>
|
/// </remarks>
|
||||||
[UnmanagedCallersOnly]
|
[UnmanagedCallersOnly]
|
||||||
public static void GetScriptClasses(NativeScriptClassInfo** outBuffer, long* length)
|
public static void GetScriptClasses(NativeScriptClassInfo** outBuffer, long* length)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
using var contextualReflection = AssemblyLoadContext.EnterContextualReflection(_appAssembly);
|
using var contextualReflection = AssemblyLoadContext.EnterContextualReflection(_appAssembly);
|
||||||
|
|
||||||
@@ -204,15 +218,27 @@ internal static unsafe partial class ScriptGlue
|
|||||||
*outBuffer = (NativeScriptClassInfo*)Marshal.UnsafeAddrOfPinnedArrayElement(_unsafeClasses, 0);
|
*outBuffer = (NativeScriptClassInfo*)Marshal.UnsafeAddrOfPinnedArrayElement(_unsafeClasses, 0);
|
||||||
*length = _unsafeClasses.Length;
|
*length = _unsafeClasses.Length;
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Exception(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Called by the engine to free the data allocated by <see cref="GetScriptClasses"/>
|
/// Called by the engine to free the data allocated by <see cref="GetScriptClasses"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[UnmanagedCallersOnly]
|
[UnmanagedCallersOnly]
|
||||||
public static void FreeScriptClassNames()
|
public static void FreeScriptClassNames()
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
Internal_FreeScriptClassNames();
|
Internal_FreeScriptClassNames();
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Exception(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void Internal_FreeScriptClassNames()
|
private static void Internal_FreeScriptClassNames()
|
||||||
{
|
{
|
||||||
@@ -237,8 +263,7 @@ internal static unsafe partial class ScriptGlue
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Console.WriteLine(e);
|
Log.Exception(e);
|
||||||
// TODO: Log exceptions to console
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -304,6 +329,8 @@ internal static unsafe partial class ScriptGlue
|
|||||||
|
|
||||||
[UnmanagedCallersOnly]
|
[UnmanagedCallersOnly]
|
||||||
public static void CreateScriptInstance(UUID entityId, byte* scriptClassName)
|
public static void CreateScriptInstance(UUID entityId, byte* scriptClassName)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
Type? scriptType = GetTypeFromNativeString(scriptClassName);
|
Type? scriptType = GetTypeFromNativeString(scriptClassName);
|
||||||
|
|
||||||
@@ -315,6 +342,11 @@ internal static unsafe partial class ScriptGlue
|
|||||||
|
|
||||||
EntityScriptInstances.Add(entityId, (scriptInstance!, scriptType));
|
EntityScriptInstances.Add(entityId, (scriptInstance!, scriptType));
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Exception(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct ComponentFunctionPointers
|
struct ComponentFunctionPointers
|
||||||
{
|
{
|
||||||
@@ -330,6 +362,8 @@ internal static unsafe partial class ScriptGlue
|
|||||||
delegate* unmanaged[Cdecl]<UUID, void> addComponent,
|
delegate* unmanaged[Cdecl]<UUID, void> addComponent,
|
||||||
delegate* unmanaged[Cdecl]<UUID, EngineResult> hasComponent,
|
delegate* unmanaged[Cdecl]<UUID, EngineResult> hasComponent,
|
||||||
delegate* unmanaged[Cdecl]<UUID, void> removeComponent)
|
delegate* unmanaged[Cdecl]<UUID, void> removeComponent)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
string? beefComponentTypeName = Marshal.PtrToStringUTF8((IntPtr)fullComponentTypeName);
|
string? beefComponentTypeName = Marshal.PtrToStringUTF8((IntPtr)fullComponentTypeName);
|
||||||
|
|
||||||
@@ -353,62 +387,103 @@ internal static unsafe partial class ScriptGlue
|
|||||||
|
|
||||||
Log.Error($"Failed to register component type \"{beefComponentTypeName}\": No matching component class found.");
|
Log.Error($"Failed to register component type \"{beefComponentTypeName}\": No matching component class found.");
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
[UnmanagedCallersOnly]
|
|
||||||
internal static void ThrowException(IntPtr message)
|
|
||||||
{
|
{
|
||||||
throw new Exception(Marshal.PtrToStringUTF8(message));
|
Log.Exception(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#region EntitySerializer
|
#region EntitySerializer
|
||||||
|
|
||||||
[UnmanagedCallersOnly]
|
[UnmanagedCallersOnly]
|
||||||
public static void CreateSerializationContext(IntPtr engineSerializer)
|
public static void CreateSerializationContext(IntPtr engineSerializer)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
EntitySerializer.CreateSerializationContext(engineSerializer);
|
EntitySerializer.CreateSerializationContext(engineSerializer);
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Exception(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[UnmanagedCallersOnly]
|
[UnmanagedCallersOnly]
|
||||||
public static void DestroySerializationContext(IntPtr engineSerializer)
|
public static void DestroySerializationContext(IntPtr engineSerializer)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
EntitySerializer.DestroySerializationContext(engineSerializer);
|
EntitySerializer.DestroySerializationContext(engineSerializer);
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Exception(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[UnmanagedCallersOnly]
|
[UnmanagedCallersOnly]
|
||||||
public static void EntitySerializer_Serialize(UUID entityId, IntPtr engineObject, IntPtr engineSerializer)
|
public static void EntitySerializer_Serialize(UUID entityId, IntPtr engineObject, IntPtr engineSerializer)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (!EntityScriptInstances.TryGetValue(entityId, out var match))
|
if (!EntityScriptInstances.TryGetValue(entityId, out var match))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
EntitySerializer.Serialize(match.Entity, engineObject, engineSerializer);
|
EntitySerializer.Serialize(match.Entity, engineObject, engineSerializer);
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Exception(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[UnmanagedCallersOnly]
|
[UnmanagedCallersOnly]
|
||||||
public static void EntitySerializer_Deserialize(UUID entityId, IntPtr engineObject, IntPtr engineSerializer)
|
public static void EntitySerializer_Deserialize(UUID entityId, IntPtr engineObject, IntPtr engineSerializer)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (!EntityScriptInstances.TryGetValue(entityId, out var match))
|
if (!EntityScriptInstances.TryGetValue(entityId, out var match))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
EntitySerializer.Deserialize(match.Entity, engineObject, engineSerializer);
|
EntitySerializer.Deserialize(match.Entity, engineObject, engineSerializer);
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Exception(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[UnmanagedCallersOnly]
|
[UnmanagedCallersOnly]
|
||||||
public static void EntitySerializer_SerializeStaticFields(byte* fullTypeName, IntPtr engineObject, IntPtr engineSerializer)
|
public static void EntitySerializer_SerializeStaticFields(byte* fullTypeName, IntPtr engineObject, IntPtr engineSerializer)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
Type? type = GetTypeFromNativeString(fullTypeName);
|
Type? type = GetTypeFromNativeString(fullTypeName);
|
||||||
Debug.Assert(type != null);
|
Debug.Assert(type != null);
|
||||||
|
|
||||||
EntitySerializer.SerializeStaticFields(type, engineObject, engineSerializer);
|
EntitySerializer.SerializeStaticFields(type, engineObject, engineSerializer);
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Exception(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[UnmanagedCallersOnly]
|
[UnmanagedCallersOnly]
|
||||||
public static void EntitySerializer_DeserializeStaticFields(byte* fullTypeName, IntPtr engineObject, IntPtr engineSerializer)
|
public static void EntitySerializer_DeserializeStaticFields(byte* fullTypeName, IntPtr engineObject, IntPtr engineSerializer)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
Type? type = GetTypeFromNativeString(fullTypeName);
|
Type? type = GetTypeFromNativeString(fullTypeName);
|
||||||
Debug.Assert(type != null);
|
Debug.Assert(type != null);
|
||||||
|
|
||||||
EntitySerializer.DeserializeStaticFields(type, engineObject, engineSerializer);
|
EntitySerializer.DeserializeStaticFields(type, engineObject, engineSerializer);
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Exception(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endregion EntitySerializer
|
#endregion EntitySerializer
|
||||||
|
|
||||||
@@ -454,8 +529,12 @@ internal static unsafe partial class ScriptGlue
|
|||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
case SerializationType.String:
|
case SerializationType.String:
|
||||||
valueObjectConverted = (void*)Marshal.StringToCoTaskMemUTF8(fullTypeName);
|
case SerializationType.Enum:
|
||||||
|
if (valueObject is String stringValue)
|
||||||
|
{
|
||||||
|
valueObjectConverted = (void*)Marshal.StringToCoTaskMemUTF8(stringValue);
|
||||||
deleteValueObject = true;
|
deleteValueObject = true;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (valueObject is not null)
|
if (valueObject is not null)
|
||||||
|
|||||||
Reference in New Issue
Block a user