瀏覽代碼

Generalized loading assemblies

Marcin Ziąbek 1 年之前
父節點
當前提交
243be54e94

+ 73 - 5
Source/QuestPDF/Helpers/NativeDependencyCompatibilityChecker.cs

@@ -1,7 +1,6 @@
 using System;
-using System.Collections.Generic;
+using System.Linq;
 using System.Runtime.InteropServices;
-using QuestPDF.Drawing.Exceptions;
 using QuestPDF.Skia;
 
 namespace QuestPDF.Helpers
@@ -9,14 +8,83 @@ namespace QuestPDF.Helpers
     internal static class NativeDependencyCompatibilityChecker
     {
         private static bool IsCompatibilityChecked = false;
-        
+            
         public static void Test()
         {
+            const string exceptionBaseMessage = "The QuestPDF library has encountered an issue while loading one of its dependencies.";
+            const string paragraph = "\n\n";
+            
             if (IsCompatibilityChecked)
                 return;
+                
+            // test with dotnet-based mechanism where native files are provided
+            // in the "runtimes/{rid}/native" folder on Core, or by the targets file on .NET Framework
+            var innerException = CheckIfExceptionIsThrownWhenLoadingNativeDependencies();
+
+            if (innerException == null)
+            {
+                IsCompatibilityChecked = true;
+                return;
+            }
+
+            if (!NativeDependencyProvider.IsCurrentPlatformSupported())
+                ThrowCompatibilityException(innerException);
+            
+            // detect platform, copy appropriate native files and test compatibility again
+            NativeDependencyProvider.EnsureNativeFileAvailability();
             
-            IsCompatibilityChecked = true;
-            SkNativeDependencyCompatibilityChecker.Test();
+            innerException = CheckIfExceptionIsThrownWhenLoadingNativeDependencies();
+
+            if (innerException == null)
+            {
+                IsCompatibilityChecked = true;
+                return;
+            }
+
+            ThrowCompatibilityException(innerException);
+            
+            static void ThrowCompatibilityException(Exception innerException)
+            {
+                var supportedRuntimes = string.Join(", ", NativeDependencyProvider.SupportedPlatforms);
+                var currentRuntime = NativeDependencyProvider.GetRuntimePlatform();
+                
+                var message = 
+                    $"{exceptionBaseMessage}{paragraph}" +
+                    "Your runtime is currently not supported by QuestPDF. " +
+                    $"Currently supported runtimes are: {supportedRuntimes}. ";
+
+                if (NativeDependencyProvider.SupportedPlatforms.Contains(currentRuntime))
+                {
+                    message += $"{paragraph}It appears that your current operating system distribution may be outdated. For optimal compatibility, please consider updating it to a more recent version.";
+                }
+                else
+                {
+                    message += $"{paragraph}Your current runtime is detected as '{currentRuntime}'.";
+                }
+
+                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+                    message += $"{paragraph}Please always set the 'Platform target' to either 'X86' or 'X64' in your startup project settings. Please do not use the 'Any CPU' option.";
+                
+                if (RuntimeInformation.ProcessArchitecture is Architecture.Arm)
+                    message += $"{paragraph}Please consider setting the 'Platform target' property to 'Arm64' in your project settings.";
+                
+                throw new Exception(message, innerException);
+            }
+        }
+    
+        private static Exception? CheckIfExceptionIsThrownWhenLoadingNativeDependencies()
+        {
+            try
+            {
+                SkNativeDependencyCompatibilityChecker.CheckIfExceptionIsThrownWhenLoadingNativeDependencies();
+                QpdfNativeDependencyCompatibilityChecker.CheckIfExceptionIsThrownWhenLoadingNativeDependencies();
+
+                return null;
+            }
+            catch (Exception exception)
+            {
+                return exception;
+            }
         }
     }
 }

+ 3 - 3
Source/QuestPDF/Skia/SkNativeDependencyProvider.cs → Source/QuestPDF/Helpers/NativeDependencyProvider.cs

@@ -4,9 +4,9 @@ using System.IO;
 using System.Linq;
 using System.Runtime.InteropServices;
 
-namespace QuestPDF.Skia;
+namespace QuestPDF.Helpers;
 
-internal static class SkNativeDependencyProvider
+internal static class NativeDependencyProvider
 {
     public static readonly string[] SupportedPlatforms =
     {
@@ -57,7 +57,7 @@ internal static class SkNativeDependencyProvider
             Environment.CurrentDirectory,
             AppContext.BaseDirectory,
             Directory.GetCurrentDirectory(),
-            new FileInfo(typeof(SkNativeDependencyProvider).Assembly.Location).Directory?.FullName
+            new FileInfo(typeof(NativeDependencyProvider).Assembly.Location).Directory?.FullName
         };
         
         foreach (var location in availableLocations)

+ 17 - 0
Source/QuestPDF/Qpdf/QpdfNativeDependencyCompatibilityChecker.cs

@@ -0,0 +1,17 @@
+using System;
+using QuestPDF.Qpdf;
+
+namespace QuestPDF.Skia;
+
+internal static class QpdfNativeDependencyCompatibilityChecker
+{
+    public static void CheckIfExceptionIsThrownWhenLoadingNativeDependencies()
+    {
+        QpdfAPI.Initialize();
+        
+        var qpdfVersion = QpdfAPI.GetQpdfVersion();
+        
+        if (string.IsNullOrEmpty(qpdfVersion))
+            throw new Exception();
+    }
+}

+ 9 - 82
Source/QuestPDF/Skia/SkNativeDependencyCompatibilityChecker.cs

@@ -1,97 +1,24 @@
 using System;
 using System.Linq;
 using System.Runtime.InteropServices;
+using QuestPDF.Helpers;
 
 namespace QuestPDF.Skia;
 
 internal static class SkNativeDependencyCompatibilityChecker
 {
-    private static bool IsCompatibilityChecked = false;
-        
-    public static void Test()
+    public static void CheckIfExceptionIsThrownWhenLoadingNativeDependencies()
     {
-        const string exceptionBaseMessage = "The QuestPDF library has encountered an issue while loading one of its dependencies.";
-        const string paragraph = "\n\n";
-        
-        if (IsCompatibilityChecked)
-            return;
+        var random = new Random();
             
-        // test with dotnet-based mechanism where native files are provided
-        // in the "runtimes/{rid}/native" folder on Core, or by the targets file on .NET Framework
-        var innerException = CheckIfExceptionIsThrownWhenLoadingNativeDependencies();
-
-        if (innerException == null)
-        {
-            IsCompatibilityChecked = true;
-            return;
-        }
-
-        if (!SkNativeDependencyProvider.IsCurrentPlatformSupported())
-            ThrowCompatibilityException(innerException);
-        
-        // detect platform, copy appropriate native files and test compatibility again
-        SkNativeDependencyProvider.EnsureNativeFileAvailability();
+        var a = random.Next();
+        var b = random.Next();
         
-        innerException = CheckIfExceptionIsThrownWhenLoadingNativeDependencies();
-
-        if (innerException == null)
-        {
-            IsCompatibilityChecked = true;
-            return;
-        }
-
-        ThrowCompatibilityException(innerException);
+        var expected = a + b;
+        var returned = API.check_compatibility_by_calculating_sum(a, b);
         
-        static void ThrowCompatibilityException(Exception innerException)
-        {
-            var supportedRuntimes = string.Join(", ", SkNativeDependencyProvider.SupportedPlatforms);
-            var currentRuntime = SkNativeDependencyProvider.GetRuntimePlatform();
-            
-            var message = 
-                $"{exceptionBaseMessage}{paragraph}" +
-                "Your runtime is currently not supported by QuestPDF. " +
-                $"Currently supported runtimes are: {supportedRuntimes}. ";
-
-            if (SkNativeDependencyProvider.SupportedPlatforms.Contains(currentRuntime))
-            {
-                message += $"{paragraph}It appears that your current operating system distribution may be outdated. For optimal compatibility, please consider updating it to a more recent version.";
-            }
-            else
-            {
-                message += $"{paragraph}Your current runtime is detected as '{currentRuntime}'.";
-            }
-
-            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
-                message += $"{paragraph}Please always set the 'Platform target' to either 'X86' or 'X64' in your startup project settings. Please do not use the 'Any CPU' option.";
-            
-            if (RuntimeInformation.ProcessArchitecture is Architecture.Arm)
-                message += $"{paragraph}Please consider setting the 'Platform target' property to 'Arm64' in your project settings.";
-            
-            throw new Exception(message, innerException);
-        }
-    }
-    
-    private static Exception? CheckIfExceptionIsThrownWhenLoadingNativeDependencies()
-    {
-        try
-        {
-            var random = new Random();
-            
-            var a = random.Next();
-            var b = random.Next();
-        
-            var expected = a + b;
-            var returned = API.check_compatibility_by_calculating_sum(a, b);
-        
-            if (expected != returned)
-                throw new Exception();
-
-            return null;
-        }
-        catch (Exception exception)
-        {
-            return exception;
-        }
+        if (expected != returned)
+            throw new Exception();
     }
     
     private static class API