Пример #1
0
        public bool Handle()
        {
            var logger = LoggerManager.LoggerFactory.CreateLogger <FlacLibHandler>();

#if !LINUX
#endif
#if WINDOWS
            var libPath = Path.Combine(
                // ReSharper disable once AssignNullToNotNullAttribute
                Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath),
                Environment.Is64BitProcess ? "win-x64" : "win-x86");

#if NETSTANDARD2_0
            // On Full Framework, AssemblyLoadContext isn't available, so we add the directory to PATH
            if (RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework", StringComparison.Ordinal))
            {
                Environment.SetEnvironmentVariable("PATH",
                                                   $"{libPath}{Path.PathSeparator}{Environment.GetEnvironmentVariable("PATH")}");
            }
            else
            {
                AddUnmanagedLibraryPath(libPath);
            }
#else
            AddUnmanagedLibraryPath(libPath);
#endif

            var module = SafeNativeMethods.LoadLibrary(Path.Combine(libPath, "libFLAC.dll"));
#elif OSX
            var osVersion = GetOSVersion();

            var libPath = Path.Combine(
                // ReSharper disable once AssignNullToNotNullAttribute
                Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath),
                osVersion.StartsWith("10.12", StringComparison.Ordinal) ? "osx.10.12-x64" :
                osVersion.StartsWith("10.13", StringComparison.Ordinal) ? "osx.10.13-x64" :
                "osx.10.14-x64");

            AddUnmanagedLibraryPath(libPath);

            var module = SafeNativeMethods.DlOpen(Path.Combine(libPath, "libFLAC.dylib"), 2);
#else // LINUX
            if (!VerifyLibrary("libFLAC.so.8"))
            {
                logger.LogWarning(
                    GetDistribution().Equals("Ubuntu", StringComparison.OrdinalIgnoreCase)
                        ? "Missing libFLAC.so.8. Run 'sudo apt-get install -y libflac8 && sudo updatedb' then restart AudioWorks."
                        : "Missing libFLAC.so.8.");
                return(false);
            }

            var module = SafeNativeMethods.DlOpen("libFLAC.so.8", 2);
#endif

            try
            {
                logger.LogInformation("Using libFLAC version {0}.",
                                      Marshal.PtrToStringAnsi(
                                          Marshal.PtrToStructure <IntPtr>(
#if WINDOWS
                                              SafeNativeMethods.GetProcAddress(module, "FLAC__VENDOR_STRING"))));
#else
                                              SafeNativeMethods.DlSym(module, "FLAC__VENDOR_STRING"))));
#endif
            }
            finally
            {
#if WINDOWS
                SafeNativeMethods.FreeLibrary(module);
#else
                SafeNativeMethods.DlClose(module);
#endif
            }

            return(true);
        }
Пример #2
0
        public bool Handle()
        {
            var logger = LoggerManager.LoggerFactory.CreateLogger <FlacLibHandler>();

#if !LINUX
#endif
#if WINDOWS
            var libPath = Path.Combine(
                Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().Location).LocalPath) !,
                Environment.Is64BitProcess ? "win-x64" : "win-x86");

#if NETSTANDARD2_0
            // On Full Framework, AssemblyLoadContext isn't available, so we add the directory to PATH
            if (RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework", StringComparison.Ordinal))
            {
                Environment.SetEnvironmentVariable("PATH",
                                                   $"{libPath}{Path.PathSeparator}{Environment.GetEnvironmentVariable("PATH")}");
            }
            else
            {
                AddUnmanagedLibraryPath(libPath);
            }
#else
            AddUnmanagedLibraryPath(libPath);
#endif
#elif OSX
            var osVersion = GetOSVersion();
            var libPath   = Path.Combine(
                Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().Location).LocalPath) !,
                osVersion.StartsWith("10.13", StringComparison.Ordinal) ? "osx.10.13-x64" :
                osVersion.StartsWith("10.14", StringComparison.Ordinal) ? "osx.10.14-x64" :
                osVersion.StartsWith("10.15", StringComparison.Ordinal) ? "osx.10.15-x64" :
                "osx.11");

            AddUnmanagedLibraryPath(libPath);
#endif

            try
            {
                foreach (var methodInfo in typeof(SafeNativeMethods).GetMethods(
                             BindingFlags.NonPublic | BindingFlags.Static))
                {
                    Marshal.Prelink(methodInfo);
                }
            }
            catch (DllNotFoundException e)
            {
                logger.LogWarning(e.Message);
                return(false);
            }
            catch (EntryPointNotFoundException e)
            {
                logger.LogWarning(e.Message);
                return(false);
            }

            var module = IntPtr.Zero;
            try
            {
#if WINDOWS
                module = SafeNativeMethods.LoadLibrary(Path.Combine(libPath, "FLAC.dll"));
#elif OSX
                module = SafeNativeMethods.DlOpen(Path.Combine(libPath, "libFLAC.dylib"), 2);
#else // LINUX
                module = SafeNativeMethods.DlOpen("libFLAC.so.8", 2);
#endif
                logger.LogInformation("Using FLAC version {0}.",
                                      Marshal.PtrToStringAnsi(
                                          Marshal.PtrToStructure <IntPtr>(
#if WINDOWS
                                              SafeNativeMethods.GetProcAddress(module, "FLAC__VENDOR_STRING"))));
#else
                                              SafeNativeMethods.DlSym(module, "FLAC__VENDOR_STRING"))));
#endif
            }
            finally
            {
                if (module != IntPtr.Zero)
#if WINDOWS
                { SafeNativeMethods.FreeLibrary(module); }
#else
                { SafeNativeMethods.DlClose(module); }
#endif
            }

            return(true);
        }