From 9dea00890c13151d22e77f7488efed5651f4884c Mon Sep 17 00:00:00 2001 From: Salman Alshamrani Date: Sat, 28 Dec 2024 14:49:16 -0500 Subject: [PATCH] Add macOS implementation --- osu.Framework/Platform/MacOS/MacOSGameHost.cs | 5 +++ .../Platform/MacOS/MacOSTextureLoaderStore.cs | 37 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 osu.Framework/Platform/MacOS/MacOSTextureLoaderStore.cs diff --git a/osu.Framework/Platform/MacOS/MacOSGameHost.cs b/osu.Framework/Platform/MacOS/MacOSGameHost.cs index 67100d46b3..234976426f 100644 --- a/osu.Framework/Platform/MacOS/MacOSGameHost.cs +++ b/osu.Framework/Platform/MacOS/MacOSGameHost.cs @@ -7,10 +7,12 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using osu.Framework.Graphics.Textures; using osu.Framework.Input; using osu.Framework.Input.Bindings; using osu.Framework.Input.Handlers; using osu.Framework.Input.Handlers.Mouse; +using osu.Framework.IO.Stores; using osu.Framework.Logging; using osu.Framework.Platform.MacOS.Native; @@ -44,6 +46,9 @@ public override IEnumerable UserStoragePaths protected override ReadableKeyCombinationProvider CreateReadableKeyCombinationProvider() => new MacOSReadableKeyCombinationProvider(); + public override IResourceStore CreateTextureLoaderStore(IResourceStore underlyingStore) + => new MacOSTextureLoaderStore(underlyingStore); + protected override void Swap() { base.Swap(); diff --git a/osu.Framework/Platform/MacOS/MacOSTextureLoaderStore.cs b/osu.Framework/Platform/MacOS/MacOSTextureLoaderStore.cs new file mode 100644 index 0000000000..a65aa65b94 --- /dev/null +++ b/osu.Framework/Platform/MacOS/MacOSTextureLoaderStore.cs @@ -0,0 +1,37 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using System.IO; +using osu.Framework.IO.Stores; +using osu.Framework.Platform.Apple; +using osu.Framework.Platform.Apple.Native; +using osu.Framework.Platform.MacOS.Native; +using SixLabors.ImageSharp; + +namespace osu.Framework.Platform.MacOS +{ + internal class MacOSTextureLoaderStore : AppleTextureLoaderStore + { + public MacOSTextureLoaderStore(IResourceStore store) + : base(store) + { + } + + protected override unsafe Image ImageFromStream(Stream stream) + { + int length = (int)(stream.Length - stream.Position); + using var nativeData = NSMutableData.FromLength(length); + + var bytesSpan = new Span(nativeData.MutableBytes, length); + stream.ReadExactly(bytesSpan); + + using var nsImage = NSImage.LoadFromData(nativeData); + if (nsImage.Handle == IntPtr.Zero) + throw new ArgumentException($"{nameof(Image)} could not be created from {nameof(stream)}."); + + var cgImage = nsImage.CGImage; + return ImageFromCGImage(cgImage); + } + } +}