Add NativeInteropException for native interop crashes

This commit is contained in:
Wojtek Figat
2023-05-19 13:52:30 +02:00
parent ed70eb24c7
commit 3e792e6cd7
3 changed files with 21 additions and 8 deletions

View File

@@ -87,7 +87,7 @@ namespace FlaxEngine.Interop
#endif
scriptingAssemblyLoadContext = new AssemblyLoadContext("Flax", isCollectible);
#if FLAX_EDITOR
scriptingAssemblyLoadContext.Resolving += ScriptingAssemblyLoadContext_Resolving;
scriptingAssemblyLoadContext.Resolving += OnScriptingAssemblyLoadContextResolving;
#endif
}
@@ -106,7 +106,8 @@ namespace FlaxEngine.Interop
DelegateHelpers.InitMethods();
}
private static Assembly? ScriptingAssemblyLoadContext_Resolving(AssemblyLoadContext assemblyLoadContext, AssemblyName assemblyName)
#if FLAX_EDITOR
private static Assembly? OnScriptingAssemblyLoadContextResolving(AssemblyLoadContext assemblyLoadContext, AssemblyName assemblyName)
{
// FIXME: There should be a better way to resolve the path to EditorTargetPath where the dependencies are stored
string editorTargetPath = Path.GetDirectoryName(nativeLibraryPaths.Keys.First(x => x != "FlaxEngine"));
@@ -116,6 +117,7 @@ namespace FlaxEngine.Interop
return assemblyLoadContext.LoadFromAssemblyPath(assemblyPath);
return null;
}
#endif
[UnmanagedCallersOnly]
internal static unsafe void Exit()
@@ -523,7 +525,7 @@ namespace FlaxEngine.Interop
}
}
throw new Exception($"Invalid field {field.Name} to marshal for type {typeof(T).Name}");
throw new NativeInteropException($"Invalid field {field.Name} to marshal for type {typeof(T).Name}");
}
private static void ToManagedFieldPointer(FieldInfo field, ref T fieldOwner, IntPtr fieldPtr, out int fieldOffset)
@@ -1271,6 +1273,17 @@ namespace FlaxEngine.Interop
}
#endif
}
internal class NativeInteropException : Exception
{
public NativeInteropException(string message)
: base(message)
{
#if !BUILD_RELEASE
Debug.Logger.LogHandler.LogWrite(LogType.Error, "Native interop exception!");
#endif
}
}
}
#endif