From 9b33d8c2c1bf859c31274da5c806feaa7c0fa1b0 Mon Sep 17 00:00:00 2001 From: Lucas Girouard-Stranks Date: Tue, 24 Nov 2020 19:22:47 -0500 Subject: [PATCH] Fix including x86 native libraries --- .../production/Ankura.FNA/Ankura.FNA.csproj | 20 ++++++++ .../production/Ankura/Ankura/Native/Native.cs | 47 ++++++++++--------- 2 files changed, 46 insertions(+), 21 deletions(-) diff --git a/src/dotnet/projects/production/Ankura.FNA/Ankura.FNA.csproj b/src/dotnet/projects/production/Ankura.FNA/Ankura.FNA.csproj index a32c63f..5998cbf 100644 --- a/src/dotnet/projects/production/Ankura.FNA/Ankura.FNA.csproj +++ b/src/dotnet/projects/production/Ankura.FNA/Ankura.FNA.csproj @@ -40,6 +40,11 @@ PreserveNewest runtimes/win-x64/native/FAudio.dll + + libs/win-x86/FAudio.dll + PreserveNewest + runtimes/win-x86/native/FAudio.dll + libs/osx-x64/libFAudio.0.dylib PreserveNewest @@ -58,6 +63,11 @@ PreserveNewest runtimes/win-x64/native/FNA3D.dll + + libs/win-x86/FNA3D.dll + PreserveNewest + runtimes/win-x86/native/FNA3D.dll + libs/osx-x64/libFNA3D.0.dylib PreserveNewest @@ -86,6 +96,11 @@ PreserveNewest runtimes/win-x64/native/SDL2.dll + + libs/win-x86/SDL2.dll + PreserveNewest + runtimes/win-x86/native/SDL2.dll + libs/osx-x64/libSDL2-2.0.0.dylib PreserveNewest @@ -104,6 +119,11 @@ PreserveNewest runtimes/win-x64/native/libtheorafile.dll + + libs/win-x86/libtheorafile.dll + PreserveNewest + runtimes/win-x86/native/libtheorafile.dll + libs/osx-x64/libtheorafile.dylib PreserveNewest diff --git a/src/dotnet/projects/production/Ankura/Ankura/Native/Native.cs b/src/dotnet/projects/production/Ankura/Ankura/Native/Native.cs index 3ed9e1d..ed6fc6c 100644 --- a/src/dotnet/projects/production/Ankura/Ankura/Native/Native.cs +++ b/src/dotnet/projects/production/Ankura/Ankura/Native/Native.cs @@ -13,7 +13,17 @@ namespace Ankura { public static class Native { - private static IEnumerable? _searchDirectories; + private static IEnumerable? _librarySearchDirectories; + private static RuntimePlatform? _platform; + + public static RuntimePlatform RuntimePlatform + { + get + { + _platform ??= GetRuntimePlatform(); + return _platform.Value; + } + } public static void SetDllImportResolver(Assembly assembly) { @@ -25,8 +35,7 @@ public static IntPtr LoadLibrary(string libraryFilePath) { EnsureIs64BitArchitecture(); - var platform = GetRuntimePlatform(); - return platform switch + return RuntimePlatform switch { RuntimePlatform.Linux => libdl.dlopen(libraryFilePath, 0x101), RuntimePlatform.Windows => Kernel32.LoadLibrary(libraryFilePath), @@ -37,8 +46,7 @@ public static IntPtr LoadLibrary(string libraryFilePath) public static bool FreeLibrary(IntPtr libraryHandle) { - var platform = GetRuntimePlatform(); - return platform switch + return RuntimePlatform switch { RuntimePlatform.Linux => libdl.dlclose(libraryHandle) == 0, RuntimePlatform.Windows => Kernel32.FreeLibrary(libraryHandle) != 0, @@ -49,8 +57,7 @@ public static bool FreeLibrary(IntPtr libraryHandle) public static IntPtr GetLibraryFunctionPointer(IntPtr libraryHandle, string functionName) { - var platform = GetRuntimePlatform(); - return platform switch + return RuntimePlatform switch { RuntimePlatform.Linux => libdl.dlsym(libraryHandle, functionName), RuntimePlatform.Windows => Kernel32.GetProcAddress(libraryHandle, functionName), @@ -84,7 +91,7 @@ public static T GetLibraryFunction(IntPtr libraryHandle, string functionName) return Marshal.GetDelegateForFunctionPointer(functionHandle); } - public static RuntimePlatform GetRuntimePlatform() + private static RuntimePlatform GetRuntimePlatform() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { @@ -105,7 +112,7 @@ public static RuntimePlatform GetRuntimePlatform() return RuntimePlatform.Unknown; } - public static string GetLibraryFileExtension(RuntimePlatform platform) + private static string GetLibraryFileExtension(RuntimePlatform platform) { return platform switch { @@ -119,31 +126,30 @@ public static string GetLibraryFileExtension(RuntimePlatform platform) }; } - public static string GetRuntimeIdentifier(RuntimePlatform platform) + private static string GetRuntimeIdentifier() { - return platform switch + return RuntimePlatform switch { - RuntimePlatform.Windows => "win-x64", + RuntimePlatform.Windows => Environment.Is64BitProcess ? "win-x64" : "win-x86", RuntimePlatform.macOS => "osx-x64", RuntimePlatform.Linux => "linux-x64", RuntimePlatform.Android => throw new NotImplementedException(), RuntimePlatform.iOS => throw new NotImplementedException(), RuntimePlatform.Unknown => throw new NotSupportedException(), - _ => throw new ArgumentOutOfRangeException(nameof(platform), platform, null) + _ => throw new ArgumentOutOfRangeException(nameof(RuntimePlatform), RuntimePlatform, null) }; } private static IEnumerable GetSearchDirectories() { - if (_searchDirectories != null) + if (_librarySearchDirectories != null) { - return _searchDirectories; + return _librarySearchDirectories; } - var platform = GetRuntimePlatform(); - var runtimeIdentifier = GetRuntimeIdentifier(platform); + var runtimeIdentifier = GetRuntimeIdentifier(); - return _searchDirectories = new[] + return _librarySearchDirectories = new[] { Environment.CurrentDirectory, AppDomain.CurrentDomain.BaseDirectory!, @@ -154,9 +160,8 @@ private static IEnumerable GetSearchDirectories() private static string GetLibraryPath(string libraryName) { - var platform = GetRuntimePlatform(); - var libraryPrefix = platform == RuntimePlatform.Windows ? string.Empty : "lib"; - var libraryFileExtension = GetLibraryFileExtension(platform); + var libraryPrefix = RuntimePlatform == RuntimePlatform.Windows ? string.Empty : "lib"; + var libraryFileExtension = GetLibraryFileExtension(RuntimePlatform); var libraryFileName = $"{libraryPrefix}{libraryName}"; var directories = GetSearchDirectories();