Skip to content

Commit

Permalink
[Introspection] Change native library cache path
Browse files Browse the repository at this point in the history
Fixes: #81
  • Loading branch information
hyazinthh committed Sep 2, 2024
1 parent 772f248 commit 8433212
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions src/Aardvark.Base/Introspection/Introspection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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<Assembly, string> s_nativePaths = new Dictionary<Assembly, string>();
private static readonly Lazy<string> s_nativeLibraryCacheDirectory =
new(() => Path.Combine(CachingProperties.CacheDirectory, "Native"));

public static readonly string NativeLibraryCacheDirectory = s_nativeLibraryCacheDirectory.Value;

private static readonly Dictionary<Assembly, string> s_nativePaths = new Dictionary<Assembly, string>();
private static string[] s_allPaths = null;

public static string[] GetNativeLibraryPaths()
Expand Down Expand Up @@ -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;
}
}
}
Expand Down

0 comments on commit 8433212

Please sign in to comment.