Skip to content

Commit f31de20

Browse files
committed
[Breaking change] chore: method ToControllerWith() instead of null check in ToController()
1 parent 5bcbcfb commit f31de20

7 files changed

Lines changed: 71 additions & 64 deletions

File tree

src/MaaFramework.Binding.Native/Extensions/AdbDeviceInfoExtensions.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,36 @@ public static class AdbDeviceInfoExtensions
1919
/// <param name="check">Checks LinkStart().Wait() status if <see cref="CheckStatusOption.ThrowIfNotSucceeded"/>; otherwise, not check.</param>
2020
/// <returns>A MaaAdbController.</returns>
2121
/// <exception cref="ArgumentNullException"/>
22-
public static MaaAdbController ToAdbController(this AdbDeviceInfo info,
22+
public static MaaAdbController ToAdbControllerWith(this AdbDeviceInfo info,
2323
string? adbPath = null,
2424
string? adbSerial = null,
2525
AdbScreencapMethods? screencapMethods = null,
2626
AdbInputMethods? inputMethods = null,
2727
string? config = null,
28-
string agentPath = "./MaaAgentBinary",
28+
string? agentPath = null,
2929
LinkOption link = LinkOption.Start,
3030
CheckStatusOption check = CheckStatusOption.ThrowIfNotSucceeded)
3131
{
3232
ArgumentNullException.ThrowIfNull(info);
3333

34-
if (adbPath is null
35-
&& adbSerial is null
36-
&& screencapMethods is null
37-
&& inputMethods is null
38-
&& config is null)
39-
{
40-
return new MaaAdbController(info, agentPath, link, check);
41-
}
42-
4334
return new MaaAdbController(
4435
new AdbDeviceInfo(
4536
info.Name,
4637
adbPath ?? info.AdbPath,
4738
adbSerial ?? info.AdbSerial,
4839
screencapMethods ?? info.ScreencapMethods,
4940
inputMethods ?? info.InputMethods,
50-
config ?? info.Config
41+
config ?? info.Config,
42+
agentPath ?? info.AgentPath
5143
),
52-
agentPath,
5344
link,
5445
check
5546
);
5647
}
48+
49+
/// <inheritdoc cref="ToAdbControllerWith(AdbDeviceInfo, string?, string?, AdbScreencapMethods?, AdbInputMethods?, string?, string?, LinkOption, CheckStatusOption)"/>
50+
public static MaaAdbController ToAdbController(this AdbDeviceInfo info,
51+
LinkOption link = LinkOption.Start,
52+
CheckStatusOption check = CheckStatusOption.ThrowIfNotSucceeded)
53+
=> new(info, link, check);
5754
}

src/MaaFramework.Binding.Native/Extensions/DesktopWindowInfoExtensions.cs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,38 @@ public static class DesktopWindowInfoExtensions
1717
/// <param name="check">Checks LinkStart().Wait() status if <see cref="CheckStatusOption.ThrowIfNotSucceeded"/>; otherwise, not check.</param>
1818
/// <returns>A MaaWin32Controller.</returns>
1919
/// <exception cref="ArgumentNullException"/>
20-
public static MaaWin32Controller ToWin32Controller(this DesktopWindowInfo info,
21-
Win32ScreencapMethod screencapMethod,
22-
Win32InputMethod mouseMethod,
23-
Win32InputMethod keyboardMethod,
20+
public static MaaWin32Controller ToWin32ControllerWith(this DesktopWindowInfo info,
21+
Win32ScreencapMethod? screencapMethod = null,
22+
Win32InputMethod? mouseMethod = null,
23+
Win32InputMethod? keyboardMethod = null,
2424
nint? hWnd = null,
2525
LinkOption link = LinkOption.Start,
2626
CheckStatusOption check = CheckStatusOption.ThrowIfNotSucceeded)
2727
{
2828
ArgumentNullException.ThrowIfNull(info);
2929

30-
return hWnd.HasValue
31-
? new MaaWin32Controller(hWnd.Value, screencapMethod, mouseMethod, keyboardMethod, link, check)
32-
: new MaaWin32Controller(info, screencapMethod, mouseMethod, keyboardMethod, link, check);
30+
var handle = hWnd ?? info.Handle;
31+
return new MaaWin32Controller(
32+
new DesktopWindowInfo(
33+
handle,
34+
handle == info.Handle ? info.Name : string.Empty,
35+
handle == info.Handle ? info.ClassName : string.Empty,
36+
screencapMethod ?? info.ScreencapMethod,
37+
mouseMethod ?? info.MouseMethod,
38+
keyboardMethod ?? info.KeyboardMethod
39+
),
40+
link,
41+
check
42+
);
43+
}
44+
45+
/// <inheritdoc cref="ToWin32ControllerWith(DesktopWindowInfo, Win32ScreencapMethod?, Win32InputMethod?, Win32InputMethod?, nint?, LinkOption, CheckStatusOption)"/>
46+
public static MaaWin32Controller ToWin32Controller(this DesktopWindowInfo info,
47+
LinkOption link = LinkOption.Start,
48+
CheckStatusOption check = CheckStatusOption.ThrowIfNotSucceeded)
49+
{
50+
ArgumentNullException.ThrowIfNull(info);
51+
52+
return new MaaWin32Controller(info, link, check);
3353
}
3454
}

src/MaaFramework.Binding.Native/MaaController/MaaAdbController.cs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,39 @@ namespace MaaFramework.Binding;
1111
public class MaaAdbController : MaaController
1212
{
1313
private readonly AdbDeviceInfo _debugInfo;
14-
private readonly string _debugAgentPath;
1514

1615
[ExcludeFromCodeCoverage(Justification = "Debugger display.")]
1716
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
1817
private string DebuggerDisplay => IsInvalid
1918
? $"Invalid {GetType().Name}"
20-
: $"{GetType().Name} {{ {nameof(_debugInfo.Name)} = {_debugInfo.Name}, {nameof(_debugInfo.AdbSerial)} = {_debugInfo.AdbSerial}, {nameof(_debugInfo.ScreencapMethods)} = {_debugInfo.ScreencapMethods}, {nameof(_debugInfo.InputMethods)} = {_debugInfo.InputMethods}, AgentPath = {_debugAgentPath} }}";
19+
: $"{GetType().Name} {{ {nameof(_debugInfo.Name)} = {_debugInfo.Name}, {nameof(_debugInfo.AdbSerial)} = {_debugInfo.AdbSerial}, {nameof(_debugInfo.ScreencapMethods)} = {_debugInfo.ScreencapMethods}, {nameof(_debugInfo.InputMethods)} = {_debugInfo.InputMethods}, AgentPath = {_debugInfo.AgentPath} }}";
2120

2221
/// <summary>
2322
/// Creates a <see cref="MaaAdbController"/> instance.
2423
/// </summary>
25-
/// <param name="adbDevice">The adb device info.</param>
26-
/// <param name="agentPath">The path of agent directory. Default is "./MaaAgentBinary" if package "Maa.Framework" or "Maa.AgentBinary" is used.</param>
24+
/// <param name="info">The adb device info.</param>
2725
/// <param name="link">Executes <see cref="IMaaController.LinkStart"/> if <see cref="LinkOption.Start"/>; otherwise, not link.</param>
2826
/// <param name="check">Checks LinkStart().Wait() status if <see cref="CheckStatusOption.ThrowIfNotSucceeded"/>; otherwise, not check.</param>
2927
/// <remarks>
3028
/// Wrapper of <see cref="MaaAdbControllerCreate"/>.
3129
/// </remarks>
3230
/// <exception cref="ArgumentException"/>
3331
/// <exception cref="MaaJobStatusException"/>
34-
public MaaAdbController(AdbDeviceInfo adbDevice, string agentPath = "./MaaAgentBinary", LinkOption link = LinkOption.Start, CheckStatusOption check = CheckStatusOption.ThrowIfNotSucceeded)
32+
public MaaAdbController(AdbDeviceInfo info, LinkOption link = LinkOption.Start, CheckStatusOption check = CheckStatusOption.ThrowIfNotSucceeded)
3533
{
36-
ArgumentException.ThrowIfNullOrEmpty(adbDevice.AdbPath);
37-
ArgumentException.ThrowIfNullOrEmpty(adbDevice.AdbSerial);
38-
if (adbDevice.ScreencapMethods == AdbScreencapMethods.None) throw new ArgumentException($"Value cannot be {AdbScreencapMethods.None}.", "adbDevice.ScreencapMethods");
39-
if (adbDevice.InputMethods == AdbInputMethods.None) throw new ArgumentException($"Value cannot be {AdbInputMethods.None}.", "adbDevice.InputMethods");
40-
ArgumentException.ThrowIfNullOrEmpty(adbDevice.Config);
41-
ArgumentException.ThrowIfNullOrEmpty(agentPath);
34+
ArgumentException.ThrowIfNullOrEmpty(info.AdbPath);
35+
ArgumentException.ThrowIfNullOrEmpty(info.AdbSerial);
36+
ArgumentException.ThrowIfNullOrEmpty(info.Config);
37+
ArgumentException.ThrowIfNullOrEmpty(info.AgentPath);
4238

43-
var handle = MaaAdbControllerCreate(adbDevice.AdbPath, adbDevice.AdbSerial, (MaaAdbScreencapMethod)adbDevice.ScreencapMethods, (MaaAdbInputMethod)adbDevice.InputMethods, adbDevice.Config, agentPath);
39+
var handle = MaaAdbControllerCreate(info.AdbPath, info.AdbSerial, (MaaAdbScreencapMethod)info.ScreencapMethods, (MaaAdbInputMethod)info.InputMethods, info.Config, info.AgentPath);
4440
_ = MaaControllerAddSink(Handle, MaaEventCallback, nint.Zero);
4541
SetHandle(handle, needReleased: true);
4642

47-
_debugInfo = adbDevice;
48-
_debugAgentPath = agentPath;
43+
_debugInfo = info;
4944

5045
if (link == LinkOption.Start)
51-
LinkStartOnConstructed(check, adbDevice, _debugAgentPath);
46+
LinkStartOnConstructed(check, info);
5247
}
5348

5449
/// <param name="adbPath">The path of adb executable file.</param>
@@ -59,10 +54,9 @@ public MaaAdbController(AdbDeviceInfo adbDevice, string agentPath = "./MaaAgentB
5954
/// <param name="agentPath">The path of agent directory. Default is "./MaaAgentBinary" if package "Maa.Framework" or "Maa.AgentBinary" is used.</param>
6055
/// <param name="link">Executes <see cref="IMaaController.LinkStart"/> if <see cref="LinkOption.Start"/>; otherwise, not link.</param>
6156
/// <param name="check">Checks LinkStart().Wait() status if <see cref="CheckStatusOption.ThrowIfNotSucceeded"/>; otherwise, not check.</param>
62-
/// <inheritdoc cref="Binding.MaaAdbController(AdbDeviceInfo, string, LinkOption, CheckStatusOption)"/>
57+
/// <inheritdoc cref="Binding.MaaAdbController(AdbDeviceInfo, LinkOption, CheckStatusOption)"/>
6358
public MaaAdbController(string adbPath, string adbSerial, AdbScreencapMethods screencapMethods, AdbInputMethods inputMethods, [StringSyntax("Json")] string config = "{}", string agentPath = "./MaaAgentBinary", LinkOption link = LinkOption.Start, CheckStatusOption check = CheckStatusOption.ThrowIfNotSucceeded)
64-
: this(new AdbDeviceInfo(string.Empty, adbPath, adbSerial, screencapMethods, inputMethods, config),
65-
agentPath, link, check)
59+
: this(new AdbDeviceInfo(string.Empty, adbPath, adbSerial, screencapMethods, inputMethods, config, agentPath), link, check)
6660
{
6761
}
6862
}

src/MaaFramework.Binding.Native/MaaController/MaaWin32Controller.cs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,36 @@ namespace MaaFramework.Binding;
1111
public class MaaWin32Controller : MaaController
1212
{
1313
private readonly DesktopWindowInfo _debugInfo;
14-
private readonly Win32ScreencapMethod _debugScreencapMethod;
15-
private readonly Win32InputMethod _debugMouseMethod;
16-
private readonly Win32InputMethod _debugKeyboardMethod;
1714

1815
[ExcludeFromCodeCoverage(Justification = "Debugger display.")]
1916
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
2017
private string DebuggerDisplay => IsInvalid
2118
? $"Invalid {GetType().Name}"
22-
: $"{GetType().Name} {{ {nameof(_debugInfo.Name)} = {_debugInfo.Name}, {nameof(_debugInfo.ClassName)} = {_debugInfo.ClassName}, ScreencapMethod = {_debugScreencapMethod}, MouseMethod = {_debugMouseMethod}, KeyboardMethod = {_debugKeyboardMethod} }}";
19+
: $"{GetType().Name} {{ {nameof(_debugInfo.Name)} = {_debugInfo.Name}, {nameof(_debugInfo.ClassName)} = {_debugInfo.ClassName}, ScreencapMethod = {_debugInfo.ScreencapMethod}, MouseMethod = {_debugInfo.MouseMethod}, KeyboardMethod = {_debugInfo.KeyboardMethod} }}";
2320

2421
/// <summary>
2522
/// Creates a <see cref="MaaWin32Controller"/> instance.
2623
/// </summary>
27-
/// <param name="desktopWindow">The desktop window info.</param>
28-
/// <param name="screencapMethod">The screencap method.</param>
29-
/// <param name="mouseMethod">The mouse method.</param>
30-
/// <param name="keyboardMethod">The keyboard method.</param>
24+
/// <param name="info">The desktop window info.</param>
3125
/// <param name="link">Executes <see cref="IMaaController.LinkStart"/> if <see cref="LinkOption.Start"/>; otherwise, not link.</param>
3226
/// <param name="check">Checks LinkStart().Wait() status if <see cref="CheckStatusOption.ThrowIfNotSucceeded"/>; otherwise, not check.</param>
3327
/// <remarks>
3428
/// Wrapper of <see cref="MaaWin32ControllerCreate"/>.
3529
/// </remarks>
3630
/// <exception cref="ArgumentException"/>
3731
/// <exception cref="MaaJobStatusException"/>
38-
public MaaWin32Controller(DesktopWindowInfo desktopWindow, Win32ScreencapMethod screencapMethod, Win32InputMethod mouseMethod, Win32InputMethod keyboardMethod, LinkOption link = LinkOption.Start, CheckStatusOption check = CheckStatusOption.ThrowIfNotSucceeded)
32+
public MaaWin32Controller(DesktopWindowInfo info, LinkOption link = LinkOption.Start, CheckStatusOption check = CheckStatusOption.ThrowIfNotSucceeded)
3933
{
40-
if (screencapMethod == Win32ScreencapMethod.None) throw new ArgumentException($"Value cannot be {Win32ScreencapMethod.None}.", nameof(screencapMethod));
41-
if (mouseMethod == Win32InputMethod.None) throw new ArgumentException($"Value cannot be {Win32InputMethod.None}.", nameof(mouseMethod));
42-
if (keyboardMethod == Win32InputMethod.None) throw new ArgumentException($"Value cannot be {Win32InputMethod.None}.", nameof(keyboardMethod));
34+
if (info.Handle == nint.Zero) throw new ArgumentException("Value cannot be zero.", "info.Handle");
4335

44-
var handle = MaaWin32ControllerCreate(desktopWindow.Handle, (MaaWin32ScreencapMethod)screencapMethod, (MaaWin32InputMethod)mouseMethod, (MaaWin32InputMethod)keyboardMethod);
36+
var handle = MaaWin32ControllerCreate(info.Handle, (MaaWin32ScreencapMethod)info.ScreencapMethod, (MaaWin32InputMethod)info.MouseMethod, (MaaWin32InputMethod)info.KeyboardMethod);
4537
_ = MaaControllerAddSink(Handle, MaaEventCallback, nint.Zero);
4638
SetHandle(handle, needReleased: true);
4739

48-
_debugInfo = desktopWindow;
49-
_debugScreencapMethod = screencapMethod;
50-
_debugMouseMethod = mouseMethod;
51-
_debugKeyboardMethod = keyboardMethod;
40+
_debugInfo = info;
5241

5342
if (link == LinkOption.Start)
54-
LinkStartOnConstructed(check, desktopWindow, screencapMethod, mouseMethod, keyboardMethod);
43+
LinkStartOnConstructed(check, info);
5544
}
5645

5746
/// <param name="hWnd">The handle to a win32 window.</param>
@@ -60,10 +49,9 @@ public MaaWin32Controller(DesktopWindowInfo desktopWindow, Win32ScreencapMethod
6049
/// <param name="keyboardMethod">The keyboard method.</param>
6150
/// <param name="link">Executes <see cref="IMaaController.LinkStart"/> if <see cref="LinkOption.Start"/>; otherwise, not link.</param>
6251
/// <param name="check">Checks LinkStart().Wait() status if <see cref="CheckStatusOption.ThrowIfNotSucceeded"/>; otherwise, not check.</param>
63-
/// <inheritdoc cref="Binding.MaaWin32Controller(DesktopWindowInfo, Win32ScreencapMethod, Win32InputMethod, Win32InputMethod, LinkOption, CheckStatusOption)"/>
52+
/// <inheritdoc cref="Binding.MaaWin32Controller(DesktopWindowInfo, LinkOption, CheckStatusOption)"/>
6453
public MaaWin32Controller(nint hWnd, Win32ScreencapMethod screencapMethod, Win32InputMethod mouseMethod, Win32InputMethod keyboardMethod, LinkOption link = LinkOption.Start, CheckStatusOption check = CheckStatusOption.ThrowIfNotSucceeded)
65-
: this(new DesktopWindowInfo(hWnd, string.Empty, string.Empty),
66-
screencapMethod, mouseMethod, keyboardMethod, link, check)
54+
: this(new DesktopWindowInfo(hWnd, string.Empty, string.Empty, screencapMethod, mouseMethod, keyboardMethod), link, check)
6755
{
6856
}
6957
}

src/MaaFramework.Binding.UnitTests/Test_IMaaToolkit.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void Interface_AdbDevice(MaaTypes type, IMaaToolkit maaToolkit, string ar
6060

6161
using var maaController = type switch
6262
{
63-
MaaTypes.Native => devices[0].ToAdbController(
63+
MaaTypes.Native => devices[0].ToAdbControllerWith(
6464
adbPath: Common.AdbPath,
6565
screencapMethods: AdbScreencapMethods.Encode,
6666
inputMethods: AdbInputMethods.AdbShell,
@@ -102,7 +102,7 @@ private static void Test_Win32_WindowInfos(MaaTypes type, IList<DesktopWindowInf
102102

103103
using var maaController = type switch
104104
{
105-
MaaTypes.Native => windows[0].ToWin32Controller(
105+
MaaTypes.Native => windows[0].ToWin32ControllerWith(
106106
screencapMethod: Win32ScreencapMethod.GDI,
107107
mouseMethod: Win32InputMethod.SendMessage,
108108
keyboardMethod: Win32InputMethod.SendMessage,
@@ -117,7 +117,7 @@ private static void Test_Win32_WindowInfos(MaaTypes type, IList<DesktopWindowInf
117117
#if MAA_WIN32
118118
using var optionalArgumentDefaultValuesTest = type switch
119119
{
120-
MaaTypes.Native => windows[0].ToWin32Controller(
120+
MaaTypes.Native => windows[0].ToWin32ControllerWith(
121121
screencapMethod: Win32ScreencapMethod.GDI,
122122
mouseMethod: Win32InputMethod.SendMessage,
123123
keyboardMethod: Win32InputMethod.SendMessage),

src/MaaFramework.Binding/Infos/AdbDeviceInfo.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ namespace MaaFramework.Binding;
1111
/// <param name="ScreencapMethods">Gets the <see cref="AdbScreencapMethods"/> of a device.</param>
1212
/// <param name="InputMethods">Gets the <see cref="AdbInputMethods"/> of a device.</param>
1313
/// <param name="Config">Gets the config of a device.</param>
14+
/// <param name="AgentPath">The path of agent directory. Default is "./MaaAgentBinary" if package "Maa.Framework" or "Maa.AgentBinary" is used.</param>
1415
public record AdbDeviceInfo(
1516
string Name,
1617
string AdbPath,
1718
string AdbSerial,
1819
AdbScreencapMethods ScreencapMethods,
1920
AdbInputMethods InputMethods,
20-
[StringSyntax("Json")] string Config
21+
[StringSyntax("Json")] string Config,
22+
string AgentPath = "./MaaAgentBinary"
2123
);

src/MaaFramework.Binding/Infos/DesktopWindowInfo.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66
/// <param name="Handle">Gets the handle to a window.</param>
77
/// <param name="Name">Gets the window name.</param>
88
/// <param name="ClassName">Gets the window class name.</param>
9+
/// <param name="ScreencapMethod">The screencap method.</param>
10+
/// <param name="MouseMethod">The mouse method.</param>
11+
/// <param name="KeyboardMethod">The keyboard method.</param>
912
public record DesktopWindowInfo(
1013
nint Handle,
1114
string Name,
12-
string ClassName
15+
string ClassName,
16+
Win32ScreencapMethod ScreencapMethod = Win32ScreencapMethod.DXGIDesktopDup,
17+
Win32InputMethod MouseMethod = Win32InputMethod.Seize,
18+
Win32InputMethod KeyboardMethod = Win32InputMethod.Seize
1319
);

0 commit comments

Comments
 (0)