From 6f1b8b8286a079b332716fb3432db0a585e8c052 Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Fri, 14 Jun 2024 09:42:03 +0200 Subject: [PATCH] Use SourceGenerator for native code of PdfInfo. --- .../NativeInterop/NativeInteropGenerator.cs | 4 +- .../NativeInterop/NativeInteropInfo.cs | 3 + src/Magick.NET/Formats/Pdf/PdfInfo.cs | 3 +- src/Magick.NET/Native/Formats/Pdf/PdfInfo.cs | 65 ++----------------- 4 files changed, 13 insertions(+), 62 deletions(-) diff --git a/src/Magick.NET.SourceGenerator/NativeInterop/NativeInteropGenerator.cs b/src/Magick.NET.SourceGenerator/NativeInterop/NativeInteropGenerator.cs index 28edf8b98c..19cf44fbef 100644 --- a/src/Magick.NET.SourceGenerator/NativeInterop/NativeInteropGenerator.cs +++ b/src/Magick.NET.SourceGenerator/NativeInterop/NativeInteropGenerator.cs @@ -33,7 +33,9 @@ private static void GenerateCode(SourceProductionContext context, NativeInteropI codeBuilder.AppendLine("using System.Runtime.InteropServices;"); codeBuilder.AppendLine(); - codeBuilder.AppendLine("namespace ImageMagick;"); + codeBuilder.Append("namespace "); + codeBuilder.Append(info.Namespace); + codeBuilder.AppendLine(";"); codeBuilder.AppendLine(); if (info.UsesQuantumType) diff --git a/src/Magick.NET.SourceGenerator/NativeInterop/NativeInteropInfo.cs b/src/Magick.NET.SourceGenerator/NativeInterop/NativeInteropInfo.cs index 3285d8ce99..6587240692 100644 --- a/src/Magick.NET.SourceGenerator/NativeInterop/NativeInteropInfo.cs +++ b/src/Magick.NET.SourceGenerator/NativeInterop/NativeInteropInfo.cs @@ -26,6 +26,7 @@ public NativeInteropInfo(SemanticModel semanticModel, SyntaxNode syntaxNode) var parentClass = (ClassDeclarationSyntax)_class.Parent!; ParentClassName = parentClass.Identifier.Text; IsInternal = parentClass.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.InternalKeyword)); + Namespace = ((FileScopedNamespaceDeclarationSyntax)parentClass.Parent!).Name.ToString(); var nativeInteropAttribute = _class.AttributeLists .SelectMany(list => list.Attributes) @@ -63,6 +64,8 @@ public string ClassName public List Methods { get; } + public string Namespace { get; } + public bool NativeToManaged { get; } public string ParentClassName { get; } diff --git a/src/Magick.NET/Formats/Pdf/PdfInfo.cs b/src/Magick.NET/Formats/Pdf/PdfInfo.cs index 7bcbeb9f6e..d87bf45184 100644 --- a/src/Magick.NET/Formats/Pdf/PdfInfo.cs +++ b/src/Magick.NET/Formats/Pdf/PdfInfo.cs @@ -62,8 +62,7 @@ public static PdfInfo Create(string fileName, string password) Throw.IfNull(nameof(password), password); - var nativePdfInfo = new NativePdfInfo(); - var pageCount = nativePdfInfo.PageCount(filePath, password); + var pageCount = NativePdfInfo.PageCount(filePath, password); if (pageCount == 0) throw new MagickErrorException("Unable to determine the page count."); diff --git a/src/Magick.NET/Native/Formats/Pdf/PdfInfo.cs b/src/Magick.NET/Native/Formats/Pdf/PdfInfo.cs index c48c70adde..822c05dff6 100644 --- a/src/Magick.NET/Native/Formats/Pdf/PdfInfo.cs +++ b/src/Magick.NET/Native/Formats/Pdf/PdfInfo.cs @@ -1,70 +1,17 @@ // Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET. // Licensed under the Apache License, Version 2.0. -// -#nullable enable -using System; -using System.Security; -using System.Runtime.InteropServices; +using ImageMagick.SourceGenerator; namespace ImageMagick.Formats; +/// public partial class PdfInfo { - [SuppressUnmanagedCodeSecurity] - private static unsafe class NativeMethods + [NativeInterop] + private partial class NativePdfInfo { - #if PLATFORM_x64 || PLATFORM_AnyCPU - public static class X64 - { - [DllImport(NativeLibrary.X64Name, CallingConvention = CallingConvention.Cdecl)] - public static extern UIntPtr PdfInfo_PageCount(IntPtr fileName, IntPtr password, out IntPtr exception); - } - #endif - #if PLATFORM_arm64 || PLATFORM_AnyCPU - public static class ARM64 - { - [DllImport(NativeLibrary.ARM64Name, CallingConvention = CallingConvention.Cdecl)] - public static extern UIntPtr PdfInfo_PageCount(IntPtr fileName, IntPtr password, out IntPtr exception); - } - #endif - #if PLATFORM_x86 || PLATFORM_AnyCPU - public static class X86 - { - [DllImport(NativeLibrary.X86Name, CallingConvention = CallingConvention.Cdecl)] - public static extern UIntPtr PdfInfo_PageCount(IntPtr fileName, IntPtr password, out IntPtr exception); - } - #endif - } - private unsafe sealed partial class NativePdfInfo : NativeHelper - { - static NativePdfInfo() { Environment.Initialize(); } - public int PageCount(string fileName, string password) - { - using var fileNameNative = UTF8Marshaler.CreateInstance(fileName); - using var passwordNative = UTF8Marshaler.CreateInstance(password); - IntPtr exception = IntPtr.Zero; - UIntPtr result; - #if PLATFORM_AnyCPU - if (Runtime.IsArm64) - #endif - #if PLATFORM_arm64 || PLATFORM_AnyCPU - result = NativeMethods.ARM64.PdfInfo_PageCount(fileNameNative.Instance, passwordNative.Instance, out exception); - #endif - #if PLATFORM_AnyCPU - else if (Runtime.Is64Bit) - #endif - #if PLATFORM_x64 || PLATFORM_AnyCPU - result = NativeMethods.X64.PdfInfo_PageCount(fileNameNative.Instance, passwordNative.Instance, out exception); - #endif - #if PLATFORM_AnyCPU - else - #endif - #if PLATFORM_x86 || PLATFORM_AnyCPU - result = NativeMethods.X86.PdfInfo_PageCount(fileNameNative.Instance, passwordNative.Instance, out exception); - #endif - CheckException(exception); - return (int)result; - } + [Throws] + public static partial int PageCount(string fileName, string password); } }