Skip to content

Commit

Permalink
refactor: adapt interop to framework 2.0.0-beta.1
Browse files Browse the repository at this point in the history
  • Loading branch information
moomiji committed Sep 19, 2024
1 parent 7e09cbb commit 72c08cb
Show file tree
Hide file tree
Showing 117 changed files with 3,270 additions and 3,828 deletions.
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@

<!-- NuGet Package Metadata -->
<PropertyGroup>
<Version Condition="'$(Version)' == ''">1.0.0-dev</Version>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageIcon>maa-logo_128x128.png</PackageIcon>
Expand Down
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Maa.AgentBinary" Version="1.0.0" />
<PackageVersion Include="Maa.Framework.Runtimes" Version="1.8.9" />
<PackageVersion Include="Maa.Framework.Runtimes" Version="2.0.0-beta.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageVersion Include="MSTest.TestAdapter" Version="3.0.2" />
<PackageVersion Include="MSTest.TestFramework" Version="3.0.2" />
<PackageVersion Include="SixLabors.ImageSharp" Version="3.1.5" />
<PackageVersion Include="SonarAnalyzer.CSharp" Version="9.21.0.86780" />
</ItemGroup>
</Project>
</Project>
2 changes: 2 additions & 0 deletions MaaFramework.Binding.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Screencap/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
18 changes: 9 additions & 9 deletions src/MaaFramework.Binding.Native/Abstractions/MaaCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace MaaFramework.Binding.Abstractions.Native;

/// <summary>
/// An abstract class providing common members for <see cref="MaaController"/>, <see cref="MaaInstance"/> and <see cref="MaaResource"/>.
/// An abstract class providing common members for <see cref="MaaController"/>, <see cref="MaaTasker"/> and <see cref="MaaResource"/>.
/// </summary>
public abstract class MaaCommon : MaaDisposableHandle<nint>, IMaaCommon
{
Expand All @@ -13,28 +13,28 @@ public abstract class MaaCommon : MaaDisposableHandle<nint>, IMaaCommon
/// <summary>
/// Raises the Callback event.
/// </summary>
/// <param name="msg">The MaaStringView.</param>
/// <param name="detail">The MaaStringView.</param>
/// <param name="arg">The MaaCallbackTransparentArg.</param>
/// <param name="message">The MaaStringView.</param>
/// <param name="detailsJson">The MaaStringView.</param>
/// <param name="callbackArg">The MaaCallbackTransparentArg.</param>
/// <remarks>
/// Usually invoked by MaaFramework.
/// </remarks>
protected virtual void OnCallback(string msg, string detail, MaaCallbackTransparentArg arg)
protected virtual void OnCallback(string message, string detailsJson, nint callbackArg)
{
Callback?.Invoke(this, new MaaCallbackEventArgs(msg, detail));
Callback?.Invoke(this, new MaaCallbackEventArgs(message, detailsJson));
}

/// <summary>
/// Gets the delegate to avoid garbage collection before MaaFramework calls <see cref="OnCallback"/>.
/// </summary>
protected MaaApiCallback MaaApiCallback { get; }
protected MaaNotificationCallback MaaNotificationCallback { get; }

/// <summary>
/// Initializes <see cref="MaaApiCallback"/>.
/// Initializes <see cref="MaaNotificationCallback"/>.
/// </summary>
protected MaaCommon()
: base(invalidHandleValue: nint.Zero)
{
MaaApiCallback = OnCallback;
MaaNotificationCallback = OnCallback;
}
}
140 changes: 83 additions & 57 deletions src/MaaFramework.Binding.Native/Buffers/MaaImageBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ namespace MaaFramework.Binding.Buffers;
[System.Diagnostics.DebuggerDisplay("{Width}x{Height}x{Type}")]
public class MaaImageBuffer : MaaDisposableHandle<nint>, IMaaImageBuffer<nint>
{
/// <inheritdoc cref="MaaImageBuffer(nint)"/>
/// <remarks>
/// Wrapper of <see cref="MaaCreateImageBuffer"/>.
/// </remarks>
public MaaImageBuffer()
: base(invalidHandleValue: nint.Zero)
/// <inheritdoc/>
public bool CopyTo(IMaaImageBuffer buffer) => buffer switch
{
SetHandle(MaaCreateImageBuffer(), needReleased: true);
}
MaaImageBuffer native => MaaImageBufferSetRawData(
handle: native.Handle,
data: MaaImageBufferGetRawData(Handle),
width: MaaImageBufferWidth(Handle),
height: MaaImageBufferHeight(Handle),
type: MaaImageBufferType(Handle))
.ToBoolean(),
null => false,
_ => TrySetEncodedDataStream(buffer.EncodedDataStream),
};

/// <summary>
/// Creates a <see cref="MaaImageBuffer"/> instance.
Expand All @@ -30,61 +34,78 @@ public MaaImageBuffer(MaaImageBufferHandle handle)
SetHandle(handle, needReleased: false);
}

/// <inheritdoc cref="MaaImageBuffer(nint)"/>
/// <remarks>
/// Wrapper of <see cref="MaaImageBufferCreate"/>.
/// </remarks>
public MaaImageBuffer()
: base(invalidHandleValue: nint.Zero)
{
SetHandle(MaaImageBufferCreate(), needReleased: true);
}

/// <inheritdoc/>
/// <remarks>
/// Wrapper of <see cref="MaaDestroyImageBuffer"/>.
/// Wrapper of <see cref="MaaImageBufferDestroy"/>.
/// </remarks>
protected override void ReleaseHandle()
=> MaaDestroyImageBuffer(Handle);
=> MaaImageBufferDestroy(Handle);

/// <inheritdoc/>
/// <remarks>
/// Wrapper of <see cref="MaaIsImageEmpty"/>.
/// Wrapper of <see cref="MaaImageBufferIsEmpty"/>.
/// </remarks>
public bool IsEmpty => MaaIsImageEmpty(Handle).ToBoolean();
public bool IsEmpty => MaaImageBufferIsEmpty(Handle).ToBoolean();

/// <inheritdoc/>
/// <remarks>
/// Wrapper of <see cref="MaaClearImage"/>.
/// Wrapper of <see cref="MaaImageBufferClear"/>.
/// </remarks>
public bool Clear()
=> MaaClearImage(Handle).ToBoolean();
=> MaaImageBufferClear(Handle).ToBoolean();

/// <summary>
/// Gets the image raw data.
/// </summary>
/// <returns>The raw data of image(cv::Mat::data).</returns>
/// <remarks>
/// Wrapper of <see cref="MaaGetImageRawData"/>.
/// Wrapper of <see cref="MaaImageBufferGetRawData"/>.
/// </remarks>
public MaaImageRawData GetRawData()
=> MaaGetImageRawData(Handle);
=> MaaImageBufferGetRawData(Handle);

/// <inheritdoc/>
public ImageInfo Info => new()
{
Width = MaaGetImageWidth(Handle),
Height = MaaGetImageHeight(Handle),
Type = MaaGetImageType(Handle),
};
public ImageInfo Info => new
(
Width: MaaImageBufferWidth(Handle),
Height: MaaImageBufferHeight(Handle),
Channels: MaaImageBufferChannels(Handle),
Type: MaaImageBufferType(Handle)
);

/// <inheritdoc cref="ImageInfo.Width"/>
/// <remarks>
/// Wrapper of <see cref="MaaGetImageWidth"/>.
/// Wrapper of <see cref="MaaImageBufferWidth"/>.
/// </remarks>
public int Width => MaaGetImageWidth(Handle);
public int Width => MaaImageBufferWidth(Handle);

/// <inheritdoc cref="ImageInfo.Height"/>
/// <remarks>
/// Wrapper of <see cref="MaaGetImageHeight"/>.
/// Wrapper of <see cref="MaaImageBufferHeight"/>.
/// </remarks>
public int Height => MaaImageBufferHeight(Handle);

/// <inheritdoc cref="ImageInfo.Channels"/>
/// <remarks>
/// Wrapper of <see cref="MaaImageBufferChannels"/>.
/// </remarks>
public int Height => MaaGetImageHeight(Handle);
public int Channels => MaaImageBufferChannels(Handle);

/// <inheritdoc cref="ImageInfo.Type"/>
/// <remarks>
/// Wrapper of <see cref="MaaGetImageType"/>.
/// Wrapper of <see cref="MaaImageBufferType"/>.
/// </remarks>
public int Type => MaaGetImageType(Handle);
public int Type => MaaImageBufferType(Handle);

/// <summary>
/// Sets the image raw data.
Expand All @@ -93,65 +114,70 @@ public MaaImageRawData GetRawData()
/// <param name="width">The width of image.</param>
/// <param name="height">The height of image.</param>
/// <param name="type">The type of image.</param>
/// <returns>true if the image raw data was set successfully; otherwise, false.</returns>
/// <returns><see langword="true"/> if the image raw data was set successfully; otherwise, <see langword="false"/>.</returns>
/// <remarks>
/// Wrapper of <see cref="MaaSetImageRawData"/>.
/// Wrapper of <see cref="MaaImageBufferSetRawData"/>.
/// </remarks>
public bool SetRawData(MaaImageRawData data, int width, int height, int type)
=> MaaSetImageRawData(Handle, data, width, height, type).ToBoolean();
=> MaaImageBufferSetRawData(Handle, data, width, height, type).ToBoolean();

/// <inheritdoc/>
/// <remarks>
/// Wrapper of <see cref="MaaGetImageEncoded"/> and <see cref="MaaGetImageEncodedSize"/>.
/// Wrapper of <see cref="MaaImageBufferGetEncoded"/> and <see cref="MaaImageBufferGetEncodedSize"/>.
/// </remarks>
public MaaImageEncodedData GetEncodedData(out ulong size)
public MaaImageEncodedData GetEncodedData(out MaaSize size)
{
size = MaaGetImageEncodedSize(Handle);
return MaaGetImageEncoded(Handle);
size = MaaImageBufferGetEncodedSize(Handle);
return MaaImageBufferGetEncoded(Handle);
}

/// <inheritdoc cref="GetEncodedData"/>
public static MaaImageEncodedData Get(MaaImageBufferHandle handle, out ulong size)
public static MaaImageEncodedData Get(MaaImageBufferHandle handle, out MaaSize size)
{
size = MaaGetImageEncodedSize(handle);
return MaaGetImageEncoded(handle);
size = MaaImageBufferGetEncodedSize(handle);
return MaaImageBufferGetEncoded(handle);
}

/// <inheritdoc/>
/// <remarks>
/// Wrapper of <see cref="MaaSetImageEncoded"/>.
/// Wrapper of <see cref="MaaImageBufferSetEncoded"/>.
/// </remarks>
public bool SetEncodedData(MaaImageBufferHandle data, ulong size)
=> MaaSetImageEncoded(Handle, data, size).ToBoolean();
public bool SetEncodedData(MaaImageBufferHandle data, MaaSize size)
=> MaaImageBufferSetEncoded(Handle, data, size).ToBoolean();

/// <inheritdoc cref="SetEncodedData"/>
public static bool Set(MaaImageBufferHandle handle, MaaImageBufferHandle data, ulong size)
=> MaaSetImageEncoded(handle, data, size).ToBoolean();
public static bool Set(MaaImageBufferHandle handle, MaaImageBufferHandle data, MaaSize size)
=> MaaImageBufferSetEncoded(handle, data, size).ToBoolean();

/// <inheritdoc/>
public unsafe Stream EncodedDataStream
{
get
{
return new UnmanagedMemoryStream((byte*)GetEncodedData(out var size).ToPointer(), (long)size);
return new UnmanagedMemoryStream(
(byte*)MaaImageBufferGetEncoded(Handle).ToPointer(),
(long)MaaImageBufferGetEncodedSize(Handle));
}
set
{
ArgumentNullException.ThrowIfNull(value);
var size = (ulong)value.Length;
var data = new byte[size];
value.Position = 0;
_ = value.Read(data, 0, data.Length);
value.Close();

bool ret;
// Pin - Pin data in preparation for calling the P/Invoke.
fixed (void* __data_native = &global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller<byte, byte>.ManagedToUnmanagedIn.GetPinnableReference(data))
{
ret = SetEncodedData((nint)__data_native, size);
}
if (!ret)
if (!TrySetEncodedDataStream(value))
throw new InvalidOperationException();
}
}

private unsafe bool TrySetEncodedDataStream(Stream value)
{
var size = (MaaSize)value.Length;
var data = new byte[size];
value.Position = 0;
_ = value.Read(data, 0, data.Length);
value.Dispose();

// Pin - Pin data in preparation for calling the P/Invoke.
fixed (void* __data_native = &global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller<byte, byte>.ManagedToUnmanagedIn.GetPinnableReference(data))
{
return MaaImageBufferSetEncoded(Handle, (nint)__data_native, size).ToBoolean();
}
}
}
Loading

0 comments on commit 72c08cb

Please sign in to comment.