From 8433212bf256daa28b262128a4be06259a0a5a67 Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 2 Sep 2024 17:24:45 +0200 Subject: [PATCH] [Introspection] Change native library cache path Fixes: https://github.com/aardvark-platform/aardvark.base/issues/81 --- .../Introspection/Introspection.cs | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/src/Aardvark.Base/Introspection/Introspection.cs b/src/Aardvark.Base/Introspection/Introspection.cs index 254c9107..589b9976 100644 --- a/src/Aardvark.Base/Introspection/Introspection.cs +++ b/src/Aardvark.Base/Introspection/Introspection.cs @@ -1518,6 +1518,7 @@ private static void GetPlatformAndArch(out string platform, out string arch) } } + [Obsolete("Not used internally anymore.")] public static void UnpackNativeDependenciesToBaseDir(Assembly a, string baseDir) { if (a.IsDynamic) return; @@ -1643,6 +1644,7 @@ public static void UnpackNativeDependenciesToBaseDir(Assembly a, string baseDir) } } + [Obsolete("Not used internally anymore.")] public static void UnpackNativeDependencies(Assembly a) { var baseDir = IntrospectionProperties.CurrentEntryPath; @@ -1667,7 +1669,12 @@ public static void UnpackNativeDependencies(Assembly a) /// NOTE: When using global shared NativeLibraryPath, SeparateLibraryDirectories should not be set to false, as this there might be version conflicts public static bool SeparateLibraryDirectories = true; - private static readonly Dictionary s_nativePaths = new Dictionary(); + private static readonly Lazy s_nativeLibraryCacheDirectory = + new(() => Path.Combine(CachingProperties.CacheDirectory, "Native")); + + public static readonly string NativeLibraryCacheDirectory = s_nativeLibraryCacheDirectory.Value; + + private static readonly Dictionary s_nativePaths = new Dictionary(); private static string[] s_allPaths = null; public static string[] GetNativeLibraryPaths() @@ -1700,27 +1707,25 @@ public static bool TryGetNativeLibraryPath(Assembly assembly, out string path) } else { - using (var s = assembly.GetManifestResourceStream("native.zip")) + using var s = assembly.GetManifestResourceStream("native.zip"); + string dstFolder = NativeLibraryCacheDirectory; + + if (SeparateLibraryDirectories) { -#pragma warning disable CS0618 // Type or member is obsolete - string dstFolder = NativeLibraryPath; - if (SeparateLibraryDirectories) - { - var md5 = System.Security.Cryptography.SHA1.Create(); - var bytes = md5.ComputeHash(s); - Array.Resize(ref bytes, 16); - var hash = new Guid(bytes); - md5.Dispose(); - var bits = IntPtr.Size * 8; - var folderName = string.Format("{0}-{1}-{2}", assembly.GetName().Name, hash.ToString(), bits); - dstFolder = Path.Combine(NativeLibraryPath, folderName); - } -#pragma warning restore CS0618 // Type or member is obsolete - s_nativePaths[assembly] = dstFolder; - s_allPaths = null; - path = dstFolder; - return true; + var md5 = System.Security.Cryptography.SHA1.Create(); + var bytes = md5.ComputeHash(s); + Array.Resize(ref bytes, 16); + var hash = new Guid(bytes); + md5.Dispose(); + + GetPlatformAndArch(out var platform, out var arch); + dstFolder = Path.Combine(dstFolder, assembly.GetName().Name, hash.ToString(), platform, arch); } + + s_nativePaths[assembly] = dstFolder; + s_allPaths = null; + path = dstFolder; + return true; } } }