Skip to content

Commit 599ff15

Browse files
committed
feat: add agent sample
1 parent 407c40e commit 599ff15

4 files changed

Lines changed: 142 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": 1,
3+
"isRoot": true,
4+
"tools": {
5+
"dotnet-script": {
6+
"version": "1.6.0",
7+
"commands": [
8+
"dotnet-script"
9+
],
10+
"rollForward": false
11+
}
12+
}
13+
}

sample/Agent/AgentChild.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env dotnet-script
2+
#r "nuget: Maa.Framework.Binding.Native, 4.0.0-preview.25163.6"
3+
4+
using MaaFramework.Binding;
5+
using MaaFramework.Binding.Buffers;
6+
using MaaFramework.Binding.Custom;
7+
8+
var commandLineArgs = Environment.GetCommandLineArgs();
9+
if (commandLineArgs.Length < 4)
10+
{
11+
Console.WriteLine("Call AgentMain.cs instead of this file.");
12+
return 1;
13+
}
14+
15+
var socketId = commandLineArgs[^1];
16+
var userPath = commandLineArgs[^2];
17+
var dllPath = commandLineArgs[^3];
18+
19+
NativeBindingInfo.Set(isAgentServer: true, dllPath); // First step
20+
_ = new MaaToolkit(true, userPath);
21+
var agentServer = new MaaAgentServer();
22+
agentServer.Register(new MyRec());
23+
agentServer.Register(new MyAct());
24+
agentServer.StartUp(socketId);
25+
agentServer.Join();
26+
agentServer.ShutDown();
27+
28+
Console.Write("Press any key to exit:");
29+
Console.ReadKey();
30+
return 0;
31+
32+
internal sealed class MyRec : IMaaCustomRecognition
33+
{
34+
public string Name { get; set; } = nameof(MyRec);
35+
36+
public bool Analyze(in IMaaContext context, in AnalyzeArgs args, in AnalyzeResults results)
37+
{
38+
Console.WriteLine("{0} Called", Name);
39+
40+
results.Box.SetValues(0, 0, 100, 100);
41+
results.Detail.SetValue("Hello Client!");
42+
return true;
43+
}
44+
}
45+
46+
internal sealed class MyAct : IMaaCustomAction
47+
{
48+
public string Name { get; set; } = nameof(MyAct);
49+
50+
public bool Run(in IMaaContext context, in RunArgs args)
51+
{
52+
Console.WriteLine("{0} Called", Name);
53+
Console.WriteLine("recognition detail: {0}", args.RecognitionDetail);
54+
Console.WriteLine("custom action param: {0}", args.ActionParam);
55+
56+
return true;
57+
}
58+
}

sample/Agent/AgentMain.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env dotnet-script
2+
#r "nuget: Maa.Framework.Native, 4.0.0-preview.25163.6"
3+
#r "nuget: Maa.Framework.Runtime.win-x64, 4.0.0"
4+
5+
using System.Diagnostics;
6+
using MaaFramework.Binding;
7+
8+
var toolkit = new MaaToolkit(true);
9+
var resource = new MaaResource();
10+
var maa = new MaaTasker
11+
{
12+
Controller = toolkit.AdbDevice.Find().First().ToAdbController(),
13+
Resource = resource,
14+
DisposeOptions = DisposeOptions.All,
15+
Toolkit = toolkit,
16+
};
17+
18+
if (!maa.Initialized)
19+
throw new InvalidOperationException("Failed to init tasker.");
20+
21+
var agent = new MaaAgentClient
22+
{
23+
Resource = resource,
24+
DisposeOptions = DisposeOptions.All,
25+
};
26+
27+
var socketId = agent.CreateSocket(string.Empty)
28+
?? throw new InvalidOperationException("Failed to create socket.");
29+
30+
var p = Process.Start(new ProcessStartInfo(
31+
"dotnet",["script",
32+
"AgentChild.cs",
33+
NativeBindingInfo.NativeAssemblyDirectory
34+
?? throw new ArgumentNullException("Native.BindingInfo.NativeAssemblyDirectory"),
35+
Environment.CurrentDirectory,
36+
socketId]) { UseShellExecute = true });
37+
38+
if (!agent.LinkStart())
39+
throw new InvalidOperationException("Failed to connect.");
40+
41+
var ppover = """
42+
{
43+
"Entry": {"next": "Rec"},
44+
"Rec": {
45+
"recognition": "Custom",
46+
"custom_recognition": "MyRec",
47+
"action": "Custom",
48+
"custom_action": "MyAct",
49+
"custom_action_param": {
50+
"param": "Hello Server!"
51+
}
52+
}
53+
}
54+
""";
55+
Console.WriteLine(ppover);
56+
57+
var detail = maa
58+
.AppendTask("Entry", ppover)
59+
.WaitFor(MaaJobStatus.Succeeded)
60+
.QueryTaskDetail()
61+
?? throw new InvalidOperationException("Failed to pipeline.");
62+
Console.WriteLine($"pipeline detail: {detail}");
63+
Console.WriteLine($"MyRec detail: {detail.QueryRecognitionDetail(maa, 1)?.Detail}");
64+
65+
agent.LinkStop();
66+
67+
Console.Write("Press any key to exit:");
68+
Console.ReadKey();

sample/Agent/QuickStart.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
$ENV:DOTNET_SCRIPT_CACHE_LOCATION="$PSScriptRoot\.cache"
2+
dotnet tool restore
3+
dotnet script AgentMain.cs -s https://api.nuget.org/v3/index.json -s https://maaxyz.github.io/pkg/nuget/index.json

0 commit comments

Comments
 (0)