@@ -10,55 +10,143 @@ namespace MaaFramework.Binding;
1010/// A wrapper class providing a reference implementation for <see cref="MaaFramework.Binding.Interop.Native.MaaAgentServer"/>.
1111/// </summary>
1212[ DebuggerDisplay ( "{DebuggerDisplay,nq}" ) ]
13- public class MaaAgentServer : IMaaAgentServer
13+ public sealed class MaaAgentServer : IMaaAgentServer
1414{
15+ /// <summary>
16+ /// Gets the unique identifier used to communicate with the agent client.
17+ /// </summary>
18+ public static string CurrentId { get ; private set ; }
19+
20+ /// <summary>
21+ /// Gets the current <see cref="MaaAgentServer"/> instance.
22+ /// </summary>
23+ public static MaaAgentServer Current { get ; }
24+
25+ /// <summary>
26+ /// Creates a <see cref="MaaAgentServer"/> instance.
27+ /// </summary>
28+ private MaaAgentServer ( ) { }
29+ static MaaAgentServer ( )
30+ {
31+ NativeLibrary . Init ( isAgentServer : true ) ;
32+ CurrentId = string . Empty ;
33+ Current = new ( ) ;
34+ }
35+
1536 private readonly MaaMarshaledApiRegistry < MaaCustomActionCallback > _actions = new ( ) ;
1637 private readonly MaaMarshaledApiRegistry < MaaCustomRecognitionCallback > _recognitions = new ( ) ;
17- private string ? _debugSocketId ;
1838
1939 [ ExcludeFromCodeCoverage ( Justification = "Debugger display." ) ]
2040 [ DebuggerBrowsable ( DebuggerBrowsableState . Never ) ]
21- private string DebuggerDisplay => $ "{ GetType ( ) . Name } {{ SocketId = { _debugSocketId } , CustomActions = [{ string . Join ( ", " , _actions . Names ) } ] , CustomRecognitions = [{ string . Join ( " & " , _recognitions . Names ) } ] }}";
41+ private string DebuggerDisplay => $ "{ GetType ( ) . Name } {{ { nameof ( CurrentId ) } = { CurrentId } , CustomActions = [{ string . Join ( ", " , _actions . Names ) } ] , CustomRecognitions = [{ string . Join ( " & " , _recognitions . Names ) } ] }}";
42+
43+ IMaaAgentServer IMaaAgentServer . WithIdentifier ( string identifier ) => WithIdentifier ( identifier ) ;
44+ IMaaAgentServer IMaaAgentServer . Register < T > ( string name , T custom ) => Register ( name , custom ) ;
45+ IMaaAgentServer IMaaAgentServer . Register < T > ( T custom ) => Register ( custom ) ;
46+ IMaaAgentServer IMaaAgentServer . StartUp ( ) => StartUp ( ) ;
47+ IMaaAgentServer IMaaAgentServer . ShutDown ( ) => ShutDown ( ) ;
48+ IMaaAgentServer IMaaAgentServer . Join ( ) => Join ( ) ;
49+ IMaaAgentServer IMaaAgentServer . Detach ( ) => Detach ( ) ;
50+
51+ /// <inheritdoc cref="IMaaAgentServer.WithIdentifier"/>
52+ public MaaAgentServer WithIdentifier ( string identifier )
53+ {
54+ CurrentId = identifier ;
55+ return this ;
56+ }
2257
23- /// <inheritdoc/>
24- public bool Register < T > ( string name , T custom ) where T : IMaaCustomResource
58+ /// <inheritdoc cref="IMaaAgentServer.Register{T}(string, T)" />
59+ public MaaAgentServer Register < T > ( string name , T custom ) where T : IMaaCustomResource
2560 {
2661 custom . Name = name ;
2762 return Register ( custom ) ;
2863 }
2964
30- /// <inheritdoc/>
65+ /// <inheritdoc cref="IMaaAgentServer.Register{T}(T)" />
3166 /// <remarks>
3267 /// Wrapper of <see cref="MaaAgentServerRegisterCustomAction"/> and <see cref="MaaAgentServerRegisterCustomRecognition"/>.
3368 /// </remarks>
34- public bool Register < T > ( T custom ) where T : IMaaCustomResource => custom switch
69+ public MaaAgentServer Register < T > ( T custom ) where T : IMaaCustomResource
3570 {
36- IMaaCustomAction res
37- => MaaAgentServerRegisterCustomAction ( res . Name , res . Convert ( out var callback ) , nint . Zero )
38- && _actions . Register ( res . Name , callback ) ,
39- IMaaCustomRecognition res
40- => MaaAgentServerRegisterCustomRecognition ( res . Name , res . Convert ( out var callback ) , nint . Zero )
41- && _recognitions . Register ( res . Name , callback ) ,
42- _
43- => throw new NotImplementedException ( $ "Type '{ typeof ( T ) } ' is not implemented.") ,
44- } ;
45-
46- /// <inheritdoc/>
47- public bool StartUp ( string identifier )
71+ var ret = custom switch
72+ {
73+ IMaaCustomAction res
74+ => MaaAgentServerRegisterCustomAction ( res . Name , res . Convert ( out var callback ) , nint . Zero )
75+ && _actions . Register ( res . Name , callback ) ,
76+ IMaaCustomRecognition res
77+ => MaaAgentServerRegisterCustomRecognition ( res . Name , res . Convert ( out var callback ) , nint . Zero )
78+ && _recognitions . Register ( res . Name , callback ) ,
79+ _
80+ => throw new NotImplementedException ( $ "Type '{ typeof ( T ) } ' is not implemented.") ,
81+ } ;
82+ _ = ret . ThrowIfFalse ( ) ;
83+ return this ;
84+ }
85+
86+ /// <inheritdoc cref="IMaaAgentServer.StartUp"/>
87+ /// <remarks>
88+ /// Wrapper of <see cref="MaaAgentServerStartUp"/>.
89+ /// </remarks>
90+ public MaaAgentServer StartUp ( )
4891 {
49- _debugSocketId = identifier ;
50- return MaaAgentServerStartUp ( _debugSocketId ) ;
92+ if ( string . IsNullOrEmpty ( CurrentId ) )
93+ throw new InvalidOperationException ( "Identifier is not set. Use 'WithIdentifier' method to set it." ) ;
94+ _ = MaaAgentServerStartUp ( CurrentId ) . ThrowIfFalse ( ) ;
95+ return this ;
5196 }
5297
53- /// <inheritdoc/>
54- public void ShutDown ( )
55- => MaaAgentServerShutDown ( ) ;
98+ /// <inheritdoc cref="IMaaAgentServer.ShutDown"/>
99+ /// <remarks>
100+ /// Wrapper of <see cref="MaaAgentServerShutDown"/>.
101+ /// </remarks>
102+ public MaaAgentServer ShutDown ( )
103+ {
104+ MaaAgentServerShutDown ( ) ;
105+ return this ;
106+ }
56107
57- /// <inheritdoc/>
58- public void Join ( )
59- => MaaAgentServerJoin ( ) ;
108+ /// <inheritdoc cref="IMaaAgentServer.Join"/>
109+ /// <remarks>
110+ /// Wrapper of <see cref="MaaAgentServerJoin"/>.
111+ /// </remarks>
112+ public MaaAgentServer Join ( )
113+ {
114+ MaaAgentServerJoin ( ) ;
115+ return this ;
116+ }
117+
118+ /// <inheritdoc cref="IMaaAgentServer.Detach"/>
119+ /// <remarks>
120+ /// Wrapper of <see cref="MaaAgentServerDetach"/>.
121+ /// </remarks>
122+ public MaaAgentServer Detach ( )
123+ {
124+ MaaAgentServerDetach ( ) ;
125+ return this ;
126+ }
127+ }
60128
61- /// <inheritdoc/>
62- public void Detach ( )
63- => MaaAgentServerDetach ( ) ;
129+ /// <summary>
130+ /// A static class providing extension methods for <see cref="MaaAgentServer"/>.
131+ /// </summary>
132+ public static class MaaAgentServerExtensions
133+ {
134+ /// <returns></returns>
135+ /// <inheritdoc cref="IMaaToolkitConfig.InitOption"/>
136+ public static MaaAgentServer WithToolkitConfig_InitOption ( this MaaAgentServer server , string userPath = nameof ( Environment . CurrentDirectory ) , [ StringSyntax ( "Json" ) ] string defaultJson = "{}" )
137+ {
138+ _ = MaaToolkit . Shared . Config . InitOption ( userPath , defaultJson ) . ThrowIfFalse ( ) ;
139+ return server ;
140+ }
141+
142+ /// <summary>
143+ /// Configures the MaaAgentServer to use the specified native libraries.
144+ /// </summary>
145+ /// <param name="server">The server.</param>
146+ /// <param name="paths">The directory paths to search for native libraries.</param>
147+ public static MaaAgentServer WithNativeLibrary ( this MaaAgentServer server , params string [ ] paths )
148+ {
149+ NativeLibrary . Init ( true , paths ) ;
150+ return server ;
151+ }
64152}
0 commit comments