SkNativeDependencyCompatibilityChecker.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Linq;
  3. using System.Runtime.InteropServices;
  4. using QuestPDF.Helpers;
  5. namespace QuestPDF.Skia;
  6. internal static class SkNativeDependencyCompatibilityChecker
  7. {
  8. private const int ExpectedNativeLibraryVersion = 4;
  9. private static NativeDependencyCompatibilityChecker Instance { get; } = new()
  10. {
  11. ExecuteNativeCode = ExecuteNativeCode,
  12. CheckNativeLibraryVersion = CheckNativeLibraryVersion
  13. };
  14. public static void Test()
  15. {
  16. Instance.Test();
  17. }
  18. private static bool CheckNativeLibraryVersion()
  19. {
  20. try
  21. {
  22. return API.get_questpdf_version() == ExpectedNativeLibraryVersion;
  23. }
  24. catch
  25. {
  26. return false;
  27. }
  28. }
  29. private static void ExecuteNativeCode()
  30. {
  31. var random = new Random();
  32. var a = random.Next();
  33. var b = random.Next();
  34. var expected = a + b;
  35. var returned = API.check_compatibility_by_calculating_sum(a, b);
  36. if (expected != returned)
  37. throw new Exception();
  38. }
  39. private static class API
  40. {
  41. [DllImport(SkiaAPI.LibraryName, CallingConvention = CallingConvention.Cdecl)]
  42. public static extern int get_questpdf_version();
  43. [DllImport(SkiaAPI.LibraryName, CallingConvention = CallingConvention.Cdecl)]
  44. public static extern int check_compatibility_by_calculating_sum(int a, int b);
  45. }
  46. }