@@ -13,52 +13,71 @@ namespace MaaFramework.Binding;
1313[ DebuggerDisplay ( "{DebuggerDisplay,nq}" ) ]
1414public class MaaAgentClient : MaaDisposableHandle < MaaAgentClientHandle > , IMaaAgentClient < MaaAgentClientHandle >
1515{
16- private string ? _debugSocketId ;
16+ private bool _isConnected ;
17+ private Process ? _agentServerProcess ;
1718
1819 [ ExcludeFromCodeCoverage ( Justification = "Debugger display." ) ]
1920 [ DebuggerBrowsable ( DebuggerBrowsableState . Never ) ]
2021 private string DebuggerDisplay => IsInvalid
2122 ? $ "Invalid { GetType ( ) . Name } "
22- : $ "{ GetType ( ) . Name } {{ SocketId = { _debugSocketId } , { nameof ( DisposeOptions ) } = { DisposeOptions } }}";
23+ : $ "{ GetType ( ) . Name } {{ Id = { Id } , IsConnected = { _isConnected } }}";
2324
2425 /// <summary>
2526 /// Creates a <see cref="MaaAgentClient"/> instance.
2627 /// </summary>
28+ /// <param name="identifier">The unique identifier used to communicate with the agent server.</param>
2729 /// <remarks>
2830 /// Wrapper of <see cref="MaaAgentClientCreate"/>.
2931 /// </remarks>
30- public MaaAgentClient ( )
32+ public MaaAgentClient ( string identifier = "" )
3133 : base ( invalidHandleValue : MaaAgentClientHandle . Zero )
3234 {
3335 var handle = MaaAgentClientCreate ( ) ;
3436 SetHandle ( handle , needReleased : true ) ;
37+ Id = CreateSocket ( identifier ) . ThrowIfNull ( ) ;
38+
39+ if ( ! string . IsNullOrEmpty ( identifier ) )
40+ _ = Id . ThrowIfNotEquals ( identifier ) ;
3541 }
3642
43+ /// <param name="identifier">The unique identifier used to communicate with the agent server.</param>
3744 /// <param name="resource">The resource.</param>
38- /// <param name="disposeOptions">The dispose options.</param>
39- /// <inheritdoc cref="MaaAgentClient()"/>
45+ /// <inheritdoc cref="MaaAgentClient(string)"/>
4046 [ SetsRequiredMembers ]
41- public MaaAgentClient ( MaaResource resource , DisposeOptions disposeOptions )
42- : this ( )
47+ public MaaAgentClient ( string identifier , MaaResource resource )
48+ : this ( identifier )
4349 {
4450 Resource = resource ;
45- DisposeOptions = disposeOptions ;
4651 }
4752
53+ /// <inheritdoc cref="MaaAgentClient(string, MaaResource)"/>
54+ [ SetsRequiredMembers ]
55+ public MaaAgentClient ( MaaResource resource )
56+ : this ( "" , resource )
57+ {
58+ }
59+
60+ /// <summary>
61+ /// Creates a <see cref="MaaAgentClient"/> instance.
62+ /// </summary>
63+ /// <param name="identifier">The unique identifier used to communicate with the agent server.</param>
64+ /// <param name="resource">The resource.</param>
65+ /// <returns>The <see cref="MaaAgentClient"/> instance.</returns>
66+ public static MaaAgentClient Create ( string identifier , MaaResource resource )
67+ => new ( identifier , resource ) ;
68+
69+ /// <inheritdoc cref="Create(string, MaaResource)"/>
70+ public static MaaAgentClient Create ( MaaResource resource )
71+ => new ( resource ) ;
72+
4873 /// <inheritdoc/>
49- public required DisposeOptions DisposeOptions { get ; set ; }
74+ public string Id { get ; }
5075
5176 /// <inheritdoc/>
5277 protected override void Dispose ( bool disposing )
5378 {
54- // Cannot destroy Instance before disposing Resource.
55-
56- if ( DisposeOptions . HasFlag ( DisposeOptions . Resource ) )
57- {
58- Resource . Dispose ( ) ;
59- }
60-
6179 base . Dispose ( disposing ) ;
80+ _agentServerProcess ? . Dispose ( ) ;
6281 }
6382
6483 /// <inheritdoc/>
@@ -90,28 +109,71 @@ public required MaaResource Resource
90109 }
91110 }
92111
93- /// <inheritdoc/>
112+ /// <summary>
113+ /// Creates a socket connection with the specified identifier.
114+ /// </summary>
115+ /// <param name="identifier">The specified identifier.</param>
116+ /// <returns><see langword="true"/> if the socket was created successfully; otherwise, <see langword="false"/>.</returns>
94117 /// <remarks>
95118 /// Wrapper of <see cref="MaaAgentClientCreateSocket"/>.
96119 /// </remarks>
97- public string ? CreateSocket ( string identifier = "" )
98- => MaaStringBuffer . TryGetValue ( out _debugSocketId , handle
120+ protected string ? CreateSocket ( string identifier = "" )
121+ => MaaStringBuffer . TryGetValue ( out var socketId , handle
99122 => MaaStringBuffer . TrySetValue ( handle , identifier )
100123 && MaaAgentClientCreateSocket ( Handle , handle ) )
101- ? _debugSocketId
124+ ? socketId
102125 : null ;
103126
104127 /// <inheritdoc/>
105128 /// <remarks>
106129 /// Wrapper of <see cref="MaaAgentClientConnect"/>.
107130 /// </remarks>
108131 public bool LinkStart ( )
109- => MaaAgentClientConnect ( Handle ) ;
132+ => _isConnected = MaaAgentClientConnect ( Handle ) ;
133+
134+ /// <inheritdoc/>
135+ /// <remarks>
136+ /// Wrapper of <see cref="MaaAgentClientConnect"/>.
137+ /// </remarks>
138+ public bool LinkStart ( ProcessStartInfo info )
139+ {
140+ _agentServerProcess = Process . Start ( info ) ;
141+ return LinkStart ( ) ;
142+ }
143+
144+ /// <inheritdoc/>
145+ /// <remarks>
146+ /// Wrapper of <see cref="MaaAgentClientConnect"/>.
147+ /// </remarks>
148+ public bool LinkStart ( IMaaAgentClient . AgentServerStartupMethod method )
149+ {
150+ ArgumentException . ThrowIfNullOrEmpty ( Id ) ;
151+ ArgumentException . ThrowIfNullOrEmpty ( NativeBindingInfo . NativeAssemblyDirectory ) ;
152+
153+ _agentServerProcess = method . Invoke ( Id , NativeBindingInfo . NativeAssemblyDirectory ) ;
154+ return LinkStart ( ) ;
155+ }
110156
111157 /// <inheritdoc/>
112158 /// <remarks>
113159 /// Wrapper of <see cref="MaaAgentClientDisconnect"/>.
114160 /// </remarks>
115161 public bool LinkStop ( )
116- => MaaAgentClientDisconnect ( Handle ) ;
162+ {
163+ if ( _isConnected )
164+ {
165+ _isConnected = false ;
166+ if ( ! MaaAgentClientDisconnect ( Handle ) )
167+ {
168+ _isConnected = true ;
169+ return false ;
170+ }
171+ }
172+
173+ return true ;
174+ }
175+
176+ /// <inheritdoc/>
177+ public Process AgentServerProcess => _agentServerProcess
178+ ?? throw new InvalidOperationException ( $ "The agent server process is unavailable or not managed by { nameof ( MaaAgentClient ) } .") ;
117179}
0 commit comments