Skip to content

Commit 13b0abb

Browse files
committed
feat: add IMaaDisposable.IsStateless
1 parent 4b833ea commit 13b0abb

5 files changed

Lines changed: 27 additions & 3 deletions

File tree

src/MaaFramework.Binding.UnitTests/Test_Buffers.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,7 @@ public bool ThrowOnInvalid
11011101
get => throw new NotImplementedException();
11021102
set => throw new NotImplementedException();
11031103
}
1104+
public bool IsStateless => throw new NotImplementedException();
11041105
public bool IsEmpty => throw new NotImplementedException();
11051106
public bool TryClear() => throw new NotImplementedException();
11061107
public ImageInfo GetInfo() => throw new NotImplementedException();
@@ -1126,6 +1127,7 @@ public bool ThrowOnInvalid
11261127
get => throw new NotImplementedException();
11271128
set => throw new NotImplementedException();
11281129
}
1130+
public bool IsStateless => throw new NotImplementedException();
11291131
public bool IsEmpty => throw new NotImplementedException();
11301132
public ulong Size => throw new NotImplementedException();
11311133
public bool TryClear() => throw new NotImplementedException();
@@ -1143,6 +1145,7 @@ public bool ThrowOnInvalid
11431145
get => throw new NotImplementedException();
11441146
set => throw new NotImplementedException();
11451147
}
1148+
public bool IsStateless => throw new NotImplementedException();
11461149
public int X => throw new NotImplementedException();
11471150
public int Y => throw new NotImplementedException();
11481151
public int Width => throw new NotImplementedException();

src/MaaFramework.Binding.UnitTests/Test_Custom.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ public bool Analyze(in IMaaContext context, in AnalyzeArgs args, in AnalyzeResul
3535
Assert.AreEqual(NodeName, args.NodeName);
3636
Assert.AreEqual(RecognitionParam, args.RecognitionParam);
3737

38-
_ = Assert.ThrowsException<ArgumentException>(() => new MaaContext(IntPtr.Zero));
38+
_ = Assert.ThrowsException<ArgumentException>(() =>
39+
#if MAA_NATIVE
40+
new MaaContext(IntPtr.Zero));
41+
#endif
3942
var cloneContext = (context as ICloneable).Clone() as IMaaContext;
4043
cloneContext = cloneContext?.Clone();
4144
#if MAA_NATIVE
@@ -105,7 +108,7 @@ public bool Run(in IMaaContext context, in RunArgs args)
105108
}
106109
}
107110

108-
internal sealed class TestController(MaaController c) : IMaaCustomController, IMaaDisposable
111+
internal sealed class TestController(IMaaController c) : IMaaCustomController, IMaaDisposable
109112
{
110113
#region Test_IMaaDisposable
111114

@@ -117,6 +120,8 @@ public bool ThrowOnInvalid
117120
set => c.ThrowOnInvalid = value;
118121
}
119122

123+
public bool IsStateless => c.IsStateless;
124+
120125
public void Dispose() => c.Dispose();
121126

122127
#endregion
@@ -137,7 +142,9 @@ public bool PressKey(int keycode)
137142

138143
public bool RequestResolution(out int width, out int height)
139144
{
145+
#if MAA_NATIVE
140146
using var image = new MaaImageBuffer();
147+
#endif
141148
if (Screencap(image))
142149
{
143150
width = image.Width;

src/MaaFramework.Binding/Abstractions/IMaaDisposable.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,17 @@ public interface IMaaDisposable : IDisposable
1111
bool IsInvalid { get; }
1212

1313
/// <summary>
14-
/// Gets a value indicating whether an <see cref="ObjectDisposedException"/> is thrown when current instance is invalid but still called.
14+
/// Gets a value indicating whether an <see cref="ObjectDisposedException"/> is thrown when the current instance is invalid but still called.
1515
/// </summary>
1616
bool ThrowOnInvalid { get; set; }
17+
18+
/// <summary>
19+
/// Gets a value indicating whether the current instance is stateless.
20+
/// </summary>
21+
/// <remarks>
22+
/// The lifetime of unmanaged resources is controlled by <see cref="MaaFramework"/>.
23+
/// </remarks>
24+
bool IsStateless { get; }
1725
}
1826

1927
// 设计思路:

src/MaaFramework.Binding/Abstractions/MaaDisposableHandle.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public void SetHandleAsInvalid()
4545
/// <inheritdoc/>
4646
public bool ThrowOnInvalid { get; set; }
4747

48+
/// <inheritdoc/>
49+
public bool IsStateless => !_needReleased;
50+
4851
/// <summary>
4952
/// Creates a <see cref="MaaDisposableHandle{T}"/> instance.
5053
/// </summary>

src/MaaFramework.Binding/MaaImage.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public bool ThrowOnInvalid
2525
set => buffer.ThrowOnInvalid = value;
2626
}
2727

28+
/// <inheritdoc/>
29+
public bool IsStateless => buffer.IsStateless;
30+
2831
/// <summary>
2932
/// Caches the image data from the <see cref="IMaaImageBuffer"/>.
3033
/// </summary>

0 commit comments

Comments
 (0)