Browse Source

Fix: Update logic to find native files (icudtl.dat & QuestPdfSkia.dll) (#813)

* Fix: Update logic to find native files (icudtl.dat & QuestPdfSkia.dll)

* Enhanced handling of native dependencies

---------

Co-authored-by: CT2003\sam <[email protected]>
Co-authored-by: Marcin Ziąbek <[email protected]>
samceustermanscoteng 1 year ago
parent
commit
67e180ef19
1 changed files with 34 additions and 15 deletions
  1. 34 15
      Source/QuestPDF/Skia/SkNativeDependencyProvider.cs

+ 34 - 15
Source/QuestPDF/Skia/SkNativeDependencyProvider.cs

@@ -9,14 +9,21 @@ internal static class SkNativeDependencyProvider
 {
     public static void EnsureNativeFileAvailability()
     {
-        var nativeFiles = Directory.GetFiles(GetNativeFileSourcePath());
+        var nativeFilesPath = GetNativeFileSourcePath();
+        
+        if (nativeFilesPath == null)
+            return;
 
-        foreach (var nativeFileSourcePath in nativeFiles)
+        foreach (var nativeFilePath in Directory.GetFiles(nativeFilesPath))
         {
-            var nativeFileName = Path.GetFileName(nativeFileSourcePath);
-            var runtimePath = GetNativeFileRuntimePath(nativeFileName);
-
-            CopyFileIfNewer(nativeFileSourcePath, runtimePath);
+            var targetPath = new FileInfo(nativeFilePath)
+                .Directory
+                .Parent // native
+                .Parent // platform
+                .Parent // runtimes
+                .FullName;
+            
+            CopyFileIfNewer(nativeFilePath, targetPath);
         }
     }
     
@@ -33,11 +40,29 @@ internal static class SkNativeDependencyProvider
         }
     }
     
-    static string GetNativeFileSourcePath()
+    static string? GetNativeFileSourcePath()
     {
-        var applicationDirectory = AppDomain.CurrentDomain.BaseDirectory;
         var platform = GetRuntimePlatform();
-        return Path.Combine(applicationDirectory, "runtimes", platform, "native");
+
+        var availableLocations = new[]
+        {
+            AppDomain.CurrentDomain.RelativeSearchPath, 
+            AppDomain.CurrentDomain.BaseDirectory,
+            new FileInfo(typeof(SkNativeDependencyProvider).Assembly.Location).Directory?.FullName
+        };
+        
+        foreach (var location in availableLocations)
+        {
+            if (string.IsNullOrEmpty(location))
+                continue;
+
+            var nativeFileSourcePath = Path.Combine(location, "runtimes", platform, "native");
+
+            if (Directory.Exists(nativeFileSourcePath))
+                return nativeFileSourcePath;
+        }
+
+        return null;
     }
         
     static string GetRuntimePlatform()
@@ -62,12 +87,6 @@ internal static class SkNativeDependencyProvider
 
         throw new InitializationException("Your runtime is currently not supported by QuestPDF.");
     }
-        
-    static string GetNativeFileRuntimePath(string fileName)
-    {
-        var applicationDirectory = AppDomain.CurrentDomain.BaseDirectory;
-        return Path.Combine(applicationDirectory, fileName);
-    }
 
     static void CopyFileIfNewer(string sourcePath, string targetPath)
     {