Provides a Mono-like API for native code to access managed runtime.
| 25 | /// Provides a Mono-like API for native code to access managed runtime. |
| 26 | /// </summary> |
| 27 | [HideInEditor] |
| 28 | public static unsafe partial class NativeInterop |
| 29 | { |
| 30 | internal static Dictionary<string, string> AssemblyLocations = new(); |
| 31 | |
| 32 | private static bool firstAssemblyLoaded = false; |
| 33 | |
| 34 | private static IntPtr boolTruePtr = ManagedHandle.ToIntPtr(ManagedHandle.Alloc((int)1, GCHandleType.Pinned)); |
| 35 | private static IntPtr boolFalsePtr = ManagedHandle.ToIntPtr(ManagedHandle.Alloc((int)0, GCHandleType.Pinned)); |
| 36 | |
| 37 | private static List<ManagedHandle> methodHandles = new(); |
| 38 | private static ConcurrentDictionary<IntPtr, Delegate> cachedDelegates = new(); |
| 39 | private static Dictionary<Type, (TypeHolder typeHolder, ManagedHandle handle)> managedTypes = new(new TypeComparer()); |
| 40 | private static List<ManagedHandle> fieldHandleCache = new(); |
| 41 | private static List<ManagedHandle> propertyHandleCache = new(); |
| 42 | #if FLAX_EDITOR |
| 43 | private static List<ManagedHandle> methodHandlesCollectible = new(); |
| 44 | private static ConcurrentDictionary<IntPtr, Delegate> cachedDelegatesCollectible = new(); |
| 45 | private static Dictionary<Type, (TypeHolder typeHolder, ManagedHandle handle)> managedTypesCollectible = new(new TypeComparer()); |
| 46 | private static List<ManagedHandle> fieldHandleCacheCollectible = new(); |
| 47 | private static List<ManagedHandle> propertyHandleCacheCollectible = new(); |
| 48 | #endif |
| 49 | private static Dictionary<object, ManagedHandle> classAttributesCacheCollectible = new(); |
| 50 | private static Dictionary<Assembly, ManagedHandle> assemblyHandles = new(); |
| 51 | private static ConcurrentDictionary<Type, int> _typeSizeCache = new(); |
| 52 | |
| 53 | private static Dictionary<string, IntPtr> loadedNativeLibraries = new(); |
| 54 | internal static Dictionary<string, string> libraryPaths = new(); |
| 55 | private static Dictionary<Assembly, string> assemblyOwnedNativeLibraries = new(); |
| 56 | internal static AssemblyLoadContext scriptingAssemblyLoadContext; |
| 57 | |
| 58 | private delegate TInternal ToNativeDelegate<T, TInternal>(T value); |
| 59 | private delegate T ToManagedDelegate<T, TInternal>(TInternal value); |
| 60 | |
| 61 | [System.Diagnostics.DebuggerStepThrough] |
| 62 | private static IntPtr NativeLibraryImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? dllImportSearchPath) |
| 63 | { |
| 64 | if (!loadedNativeLibraries.TryGetValue(libraryName, out IntPtr nativeLibrary)) |
| 65 | { |
| 66 | if (!libraryPaths.TryGetValue(libraryName, out var nativeLibraryPath)) |
| 67 | nativeLibraryPath = libraryName; |
| 68 | |
| 69 | nativeLibrary = NativeLibrary.Load(nativeLibraryPath, assembly, dllImportSearchPath); |
| 70 | loadedNativeLibraries.Add(libraryName, nativeLibrary); |
| 71 | assemblyOwnedNativeLibraries.Add(assembly, libraryName); |
| 72 | } |
| 73 | return nativeLibrary; |
| 74 | } |
| 75 | |
| 76 | [UnmanagedCallersOnly] |
| 77 | internal static unsafe void Init() |
| 78 | { |
| 79 | NativeLibrary.SetDllImportResolver(Assembly.GetExecutingAssembly(), NativeLibraryImportResolver); |
| 80 | |
| 81 | // Change default culture to match with Mono runtime default culture |
| 82 | var culture = CultureInfo.InvariantCulture; |
| 83 | CultureInfo.DefaultThreadCurrentCulture = culture; |
| 84 | CultureInfo.DefaultThreadCurrentUICulture = culture; |