From 6456f1cad33be9f6564d7fa48eacbe4d1267fe26 Mon Sep 17 00:00:00 2001 From: Ben Olden-Cooligan Date: Wed, 4 Sep 2024 21:37:08 -0700 Subject: [PATCH] Fix mac build --- .../Platform/Windows/Win32MessagePump.cs | 1 + .../Windows/Win32TwainHandleManager.cs | 5 +++- NAPS2.Sdk/Remoting/Worker/WorkerServer.cs | 30 +++++++++++++++---- .../Scan/Internal/Twain/TwainHandleManager.cs | 4 ++- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/NAPS2.Sdk/Platform/Windows/Win32MessagePump.cs b/NAPS2.Sdk/Platform/Windows/Win32MessagePump.cs index 56d7122c27..f0487d6e77 100644 --- a/NAPS2.Sdk/Platform/Windows/Win32MessagePump.cs +++ b/NAPS2.Sdk/Platform/Windows/Win32MessagePump.cs @@ -5,6 +5,7 @@ namespace NAPS2.Platform.Windows; +[System.Runtime.Versioning.SupportedOSPlatform("windows")] internal class Win32MessagePump : IInvoker, IDisposable { private const string WND_CLASS_NAME = "MPWndClass"; diff --git a/NAPS2.Sdk/Platform/Windows/Win32TwainHandleManager.cs b/NAPS2.Sdk/Platform/Windows/Win32TwainHandleManager.cs index ba29f0a84e..ef3acd0e46 100644 --- a/NAPS2.Sdk/Platform/Windows/Win32TwainHandleManager.cs +++ b/NAPS2.Sdk/Platform/Windows/Win32TwainHandleManager.cs @@ -1,8 +1,10 @@ +#if !MAC using NAPS2.Scan.Internal.Twain; using NTwain; namespace NAPS2.Platform.Windows; +[System.Runtime.Versioning.SupportedOSPlatform("windows")] internal class Win32TwainHandleManager : TwainHandleManager { private readonly Win32MessagePump _messagePump; @@ -72,4 +74,5 @@ public override void Dispose() Win32.EnableWindow(_disabledWindow, true); } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/NAPS2.Sdk/Remoting/Worker/WorkerServer.cs b/NAPS2.Sdk/Remoting/Worker/WorkerServer.cs index 880864ba79..5aeee414c1 100644 --- a/NAPS2.Sdk/Remoting/Worker/WorkerServer.cs +++ b/NAPS2.Sdk/Remoting/Worker/WorkerServer.cs @@ -13,14 +13,32 @@ namespace NAPS2.Remoting.Worker; /// public static class WorkerServer { +#pragma warning disable CS1998 public static async Task Run(ScanningContext scanningContext, CancellationToken cancellationToken = default) { try { - var messagePump = Win32MessagePump.Create(); - messagePump.Logger = scanningContext.Logger; - Invoker.Current = messagePump; - TwainHandleManager.Factory = () => new Win32TwainHandleManager(messagePump); + var tcs = new TaskCompletionSource(); + var run = async () => { await tcs.Task; }; + var stop = () => tcs.SetResult(true); + +#if !MAC +#if NET6_0_OR_GREATER + if (OperatingSystem.IsWindows()) + { +#endif + var messagePump = Win32MessagePump.Create(); + messagePump.Logger = scanningContext.Logger; + Invoker.Current = messagePump; +#pragma warning disable CA1416 + TwainHandleManager.Factory = () => new Win32TwainHandleManager(messagePump); + run = async () => messagePump.RunMessageLoop(); + stop = () => messagePump.Dispose(); +#pragma warning restore CA1416 +#if NET6_0_OR_GREATER + } +#endif +#endif var server = new NamedPipeServer(string.Format(WorkerFactory.PIPE_NAME_FORMAT, Process.GetCurrentProcess().Id)); @@ -31,14 +49,14 @@ public static async Task Run(ScanningContext scanningContext, CancellationToken #else new LocalTwainController(scanningContext)); #endif - serviceImpl.OnStop += (_, _) => messagePump.Dispose(); + serviceImpl.OnStop += (_, _) => stop(); WorkerService.BindService(server.ServiceBinder, serviceImpl); cancellationToken.Register(() => serviceImpl.Stop()); server.Start(); try { Console.WriteLine(@"ready"); - messagePump.RunMessageLoop(); + await run(); } finally { diff --git a/NAPS2.Sdk/Scan/Internal/Twain/TwainHandleManager.cs b/NAPS2.Sdk/Scan/Internal/Twain/TwainHandleManager.cs index 7b2bc8fe8c..33f362b50f 100644 --- a/NAPS2.Sdk/Scan/Internal/Twain/TwainHandleManager.cs +++ b/NAPS2.Sdk/Scan/Internal/Twain/TwainHandleManager.cs @@ -1,3 +1,4 @@ +#if !MAC using NTwain; namespace NAPS2.Scan.Internal.Twain; @@ -20,4 +21,5 @@ public virtual MessageLoopHook CreateMessageLoopHook(IntPtr dialogParent = defau public virtual void Dispose() { } -} \ No newline at end of file +} +#endif \ No newline at end of file